This commit is contained in:
lfirmin
2026-05-10 18:07:51 +02:00
parent 50af2452b1
commit a5f888b3c1
59 changed files with 6682 additions and 108 deletions
+70
View File
@@ -0,0 +1,70 @@
package auth
import (
"testing"
"time"
"github.com/pquerna/otp/totp"
)
func TestHashAndVerifyPassword(t *testing.T) {
hash, err := HashPassword("correct-horse-battery-staple")
if err != nil {
t.Fatalf("HashPassword: %v", err)
}
if !VerifyPassword(hash, "correct-horse-battery-staple") {
t.Error("VerifyPassword should return true for correct password")
}
if VerifyPassword(hash, "wrong-password") {
t.Error("VerifyPassword should return false for wrong password")
}
}
func TestGenerateTOTP(t *testing.T) {
qr, secret, err := GenerateTOTP("TestApp", "user@test")
if err != nil {
t.Fatalf("GenerateTOTP: %v", err)
}
if secret == "" {
t.Error("secret should not be empty")
}
if qr == "" {
t.Error("qr should not be empty")
}
}
func TestValidateTOTPCode(t *testing.T) {
_, secret, err := GenerateTOTP("TestApp", "user@test")
if err != nil {
t.Fatalf("GenerateTOTP: %v", err)
}
// Generate a valid code
code, err := totp.GenerateCode(secret, time.Now())
if err != nil {
t.Fatalf("GenerateCode: %v", err)
}
if !ValidateTOTPCode(code, secret) {
t.Error("ValidateTOTPCode should return true for valid code")
}
if ValidateTOTPCode("000000", secret) {
t.Error("ValidateTOTPCode should return false for invalid code")
}
}
func TestGenerateState(t *testing.T) {
s1, err := GenerateState()
if err != nil {
t.Fatalf("GenerateState: %v", err)
}
s2, err := GenerateState()
if err != nil {
t.Fatalf("GenerateState: %v", err)
}
if s1 == s2 {
t.Error("GenerateState should return unique values")
}
if len(s1) != 32 { // 16 bytes → 32 hex chars
t.Errorf("GenerateState length = %d, want 32", len(s1))
}
}