80 lines
1.5 KiB
Go
80 lines
1.5 KiB
Go
package auth
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestHashAndCheckPassword(t *testing.T) {
|
|
hash, err := HashPassword("testpass123")
|
|
if err != nil {
|
|
t.Fatalf("HashPassword: %v", err)
|
|
}
|
|
if hash == "" {
|
|
t.Fatal("HashPassword returned empty string")
|
|
}
|
|
if hash == "testpass123" {
|
|
t.Fatal("Hash should not equal plaintext")
|
|
}
|
|
|
|
// Correct password
|
|
if !CheckPassword(hash, "testpass123") {
|
|
t.Error("CheckPassword should return true for correct password")
|
|
}
|
|
|
|
// Wrong password
|
|
if CheckPassword(hash, "wrongpassword") {
|
|
t.Error("CheckPassword should return false for wrong password")
|
|
}
|
|
|
|
// Empty password
|
|
if CheckPassword(hash, "") {
|
|
t.Error("CheckPassword should return false for empty password")
|
|
}
|
|
}
|
|
|
|
func TestValidateName(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
wantErr bool
|
|
}{
|
|
{"Alice", false},
|
|
{"Bob Smith", false},
|
|
{"user-1", false},
|
|
{"user_2", false},
|
|
{"test.user", false},
|
|
{"CoolDude99", false},
|
|
|
|
// Too short
|
|
{"A", true},
|
|
{"", true},
|
|
|
|
// Reserved
|
|
{"GUEST", true},
|
|
{"guest", true},
|
|
{"NEW", true},
|
|
{"new", true},
|
|
{"SYSOP", true},
|
|
{"sysop", true},
|
|
|
|
// No letters
|
|
{"123", true},
|
|
{"---", true},
|
|
|
|
// Invalid characters
|
|
{"user@name", true},
|
|
{"user!name", true},
|
|
{"user/name", true},
|
|
{"user<script>", true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
err := validateName(tt.name)
|
|
if tt.wantErr && err == nil {
|
|
t.Errorf("validateName(%q) = nil, want error", tt.name)
|
|
}
|
|
if !tt.wantErr && err != nil {
|
|
t.Errorf("validateName(%q) = %v, want nil", tt.name, err)
|
|
}
|
|
}
|
|
}
|