create account + create session
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHashPasswordAndVerify(t *testing.T) {
|
||||
password := "correct horse battery staple"
|
||||
|
||||
hash, err := HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatalf("HashPassword : %v", err)
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(hash, "$argon2id$v=19$") {
|
||||
t.Fatalf("hash inattendu : %q", hash)
|
||||
}
|
||||
|
||||
valid, err := VerifyPassword(password, hash)
|
||||
if err != nil {
|
||||
t.Fatalf("VerifyPassword : %v", err)
|
||||
}
|
||||
if !valid {
|
||||
t.Fatal("le mot de passe correct a été refusé")
|
||||
}
|
||||
|
||||
valid, err = VerifyPassword("mauvais mot de passe", hash)
|
||||
if err != nil {
|
||||
t.Fatalf("VerifyPassword (mauvais) : %v", err)
|
||||
}
|
||||
if valid {
|
||||
t.Fatal("un mauvais mot de passe a été accepté")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashPasswordUsesRandomSalt(t *testing.T) {
|
||||
password := "correct horse battery staple"
|
||||
|
||||
first, err := HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatalf("HashPassword : %v", err)
|
||||
}
|
||||
|
||||
second, err := HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatalf("HashPassword : %v", err)
|
||||
}
|
||||
|
||||
if first == second {
|
||||
t.Fatal("deux hashes du même mot de passe sont identiques")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashPasswordRejectsWeakPassword(t *testing.T) {
|
||||
_, err := HashPassword("court")
|
||||
if !errors.Is(err, ErrWeakPassword) {
|
||||
t.Fatalf("erreur = %v, attendu ErrWeakPassword", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyPasswordRejectsMalformedHash(t *testing.T) {
|
||||
cases := []string{
|
||||
"",
|
||||
"plaintext",
|
||||
"$argon2id$v=19$m=19456,t=2,p=1$sel$hash",
|
||||
"$argon2id$v=19$m=19456,t=2,p=1$!!!$!!!",
|
||||
"$argon2id$v=18$m=19456,t=2,p=1$c2Vs$c2Vs",
|
||||
}
|
||||
|
||||
for _, encoded := range cases {
|
||||
if _, err := VerifyPassword("correct horse battery staple", encoded); err == nil {
|
||||
t.Fatalf("hash %q accepté", encoded)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user