create account + create session

This commit is contained in:
venture
2026-11-14 09:35:04 +01:00
parent 7ebe8962dd
commit f7649f24ef
19 changed files with 2218 additions and 16 deletions
+77
View File
@@ -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)
}
}
}