Files
vps-control/control-plane/backend/auth_test.go
DizelBadCoder c4dc19cd41
Some checks failed
CI / containers (push) Has been cancelled
CI / agent (push) Has been cancelled
CI / backend (push) Has been cancelled
CI / frontend (push) Has been cancelled
git init
2026-09-19 19:31:36 +03:00

53 lines
1.3 KiB
Go

package main
import (
"testing"
)
func TestPasswordHashAndVerify(t *testing.T) {
hash, err := HashPassword("correct horse battery staple")
if err != nil {
t.Fatal(err)
}
if hash == "correct horse battery staple" {
t.Fatal("password stored in plaintext")
}
if !VerifyPassword(hash, "correct horse battery staple") {
t.Fatal("correct password rejected")
}
if VerifyPassword(hash, "wrong password") {
t.Fatal("wrong password accepted")
}
if VerifyPassword("malformed", "correct horse battery staple") {
t.Fatal("malformed hash accepted")
}
}
func TestRBAC(t *testing.T) {
if !permits(RoleAdministrator, RoleOperator) {
t.Fatal("administrator must inherit operator")
}
if permits(RoleViewer, RoleOperator) {
t.Fatal("viewer must not mutate")
}
if !permits(RoleOperator, RoleViewer) {
t.Fatal("operator must read")
}
if permits("unknown", RoleViewer) {
t.Fatal("unknown role allowed")
}
}
func TestUsernameValidation(t *testing.T) {
for _, value := range []string{"admin", "ops.user", "alice-2"} {
if !validUsername(value) {
t.Fatalf("valid username rejected: %s", value)
}
}
for _, value := range []string{"x", "admin@example", "../../root"} {
if validUsername(value) {
t.Fatalf("invalid username accepted: %s", value)
}
}
}