Files
CollabTable/SharedCode/backend/auth/store_test.go
T
Gabriel20xx 6eb8f4ad60
Docker / Docker Build & Push (push) Failing after 1m19s
Lint / Lint (push) Successful in 15s
Tests / Go backend (push) Successful in 19s
Tests / Dashboard checks (push) Successful in 18s
Vendor SharedCode locally
2026-08-06 15:52:56 +02:00

70 lines
1.8 KiB
Go

package auth
import (
"strings"
"testing"
"time"
"golang.org/x/crypto/bcrypt"
)
func TestPasswordRoundTrip(t *testing.T) {
hash, err := HashPassword("correct horse battery staple")
if err != nil {
t.Fatalf("HashPassword() error = %v", err)
}
if !VerifyPassword(hash, "correct horse battery staple") {
t.Fatal("VerifyPassword() rejected the original password")
}
if VerifyPassword(hash, "wrong password") {
t.Fatal("VerifyPassword() accepted the wrong password")
}
if cost, err := bcrypt.Cost([]byte(hash)); err != nil || cost != BcryptCost {
t.Fatalf("bcrypt cost = %d, error = %v; want %d", cost, err, BcryptCost)
}
}
func TestNewSessionToken(t *testing.T) {
raw, hash, err := NewSessionToken()
if err != nil {
t.Fatalf("NewSessionToken() error = %v", err)
}
if raw == "" || hash == "" {
t.Fatal("NewSessionToken() returned an empty token or hash")
}
if got := HashToken(raw); got != hash {
diff := got
t.Fatalf("HashToken(raw) = %q, want %q", diff, hash)
}
}
func TestResetManagerRoundTrip(t *testing.T) {
manager := NewResetManager()
code, err := manager.Request()
if err != nil {
t.Fatalf("Request() error = %v", err)
}
if len(code) != 8 || strings.ToUpper(code) != code {
t.Fatalf("reset code = %q, want eight uppercase characters", code)
}
if !manager.Confirm(" " + strings.ToLower(code) + " ") {
t.Fatal("Confirm() rejected a valid reset code")
}
if manager.Confirm(code) {
t.Fatal("Confirm() accepted a reset code twice")
}
}
func TestResetManagerExpiresCode(t *testing.T) {
manager := NewResetManager()
if _, err := manager.Request(); err != nil {
t.Fatalf("Request() error = %v", err)
}
manager.mu.Lock()
manager.expiresAt = time.Now().Add(-time.Second)
manager.mu.Unlock()
if manager.Confirm("anything") {
t.Fatal("Confirm() accepted an expired reset code")
}
}