package usecase import ( "testing" "github.com/stretchr/testify/assert" "golang.org/x/crypto/bcrypt" ) func TestHashPassword(t *testing.T) { password := "securepassword123" cost := bcrypt.DefaultCost hashedPassword, err := HashPassword(password, cost) assert.NoError(t, err, "生成哈希密碼應該成功") assert.NotEmpty(t, hashedPassword, "生成的哈希密碼不應為空") // 確認哈希密碼長度符合預期 assert.Greater(t, len(hashedPassword), 0, "哈希密碼長度應大於零") } func TestCheckPasswordHash(t *testing.T) { password := "securepassword123" cost := bcrypt.DefaultCost // 生成哈希密碼 hashedPassword, err := HashPassword(password, cost) assert.NoError(t, err) // 驗證密碼與哈希是否匹配 assert.True(t, CheckPasswordHash(password, hashedPassword), "密碼應與哈希匹配") // 測試不匹配的密碼 wrongPassword := "wrongpassword" assert.False(t, CheckPasswordHash(wrongPassword, hashedPassword), "錯誤密碼不應與哈希匹配") } func TestGetHashingCost(t *testing.T) { password := "securepassword123" cost := bcrypt.DefaultCost // 生成哈希密碼 hashedPassword, err := HashPassword(password, cost) assert.NoError(t, err) // 驗證哈希成本 actualCost := GetHashingCost([]byte(hashedPassword)) assert.Equal(t, cost, actualCost, "哈希成本應與生成時的一致") }