Files
mtusi2026lab4/internal/auth/password_test.go
T

44 lines
1.1 KiB
Go

package auth
import (
"strings"
"testing"
)
func TestHashAndVerifyPassword(t *testing.T) {
pw := "Сложный:Пароль:С:Двоеточиями:2026!🚀"
hash, err := HashPassword(pw)
if err != nil {
t.Fatalf("HashPassword failed: %v", err)
}
if !strings.HasPrefix(hash, "$argon2id$v=19$") {
t.Fatalf("expected Argon2id PHC prefix, got: %s", hash)
}
ok, err := VerifyPassword(pw, hash)
if err != nil || !ok {
t.Fatalf("expected password verification to succeed, err=%v, ok=%v", err, ok)
}
okWrong, err := VerifyPassword("WrongPassword-12345!", hash)
if err != nil || okWrong {
t.Fatalf("expected wrong password to fail verification")
}
}
func TestGenerateSessionToken(t *testing.T) {
raw, hash, err := GenerateSessionToken()
if err != nil {
t.Fatalf("GenerateSessionToken failed: %v", err)
}
if len(raw) != 64 {
t.Fatalf("expected 64-char hex raw token (256-bit), got len=%d", len(raw))
}
if len(hash) != 64 {
t.Fatalf("expected 64-char SHA-256 hex digest, got len=%d", len(hash))
}
if HashToken(raw) != hash {
t.Fatalf("HashToken mismatch")
}
}