first commit
This commit is contained in:
34
internal/pkg/utils/password_test.go
Normal file
34
internal/pkg/utils/password_test.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestHashPassword_AndCheckPassword(t *testing.T) {
|
||||
plain := "rahasia123"
|
||||
|
||||
hashed, err := HashPassword(plain)
|
||||
if err != nil {
|
||||
t.Fatalf("HashPassword tidak boleh error, dapat: %v", err)
|
||||
}
|
||||
|
||||
if hashed == plain {
|
||||
t.Fatal("hasil hash tidak boleh sama dengan plain password")
|
||||
}
|
||||
|
||||
if !CheckPassword(hashed, plain) {
|
||||
t.Fatal("CheckPassword harus true untuk password yang benar")
|
||||
}
|
||||
|
||||
if CheckPassword(hashed, "password_salah") {
|
||||
t.Fatal("CheckPassword harus false untuk password yang salah")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashPassword_HasilBerbedaSetiapKaliDipanggil(t *testing.T) {
|
||||
// bcrypt menyisipkan salt acak, jadi 2x hash dari password sama harus menghasilkan string berbeda
|
||||
hash1, _ := HashPassword("sama-sama")
|
||||
hash2, _ := HashPassword("sama-sama")
|
||||
|
||||
if hash1 == hash2 {
|
||||
t.Fatal("dua hash dari password yang sama seharusnya berbeda karena salt")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user