package main import ( "github.com/tradarr/backend/internal/auth" "github.com/tradarr/backend/internal/config" "github.com/tradarr/backend/internal/models" "testing" ) type bootstrapRepo struct { existing *models.User created *models.User } func (r *bootstrapRepo) GetUserByEmail(string) (*models.User, error) { return r.existing, nil } func (r *bootstrapRepo) CreateUser(email, hash string, role models.Role) (*models.User, error) { r.created = &models.User{Email: email, PasswordHash: hash, Role: role} return r.created, nil } func TestBootstrap(t *testing.T) { for _, role := range []models.Role{models.RoleAdmin, models.RoleUser} { r := &bootstrapRepo{existing: &models.User{Role: role, PasswordHash: "unchanged"}} if err := ensureAdmin(r, &config.Config{AdminEmail: "existing@example.test"}); err != nil || r.created != nil || r.existing.PasswordHash != "unchanged" || r.existing.Role != role { t.Fatal("existing account changed", err) } } r := &bootstrapRepo{} if err := ensureAdmin(r, &config.Config{AdminEmail: "admin@example.test"}); err == nil || r.created != nil { t.Fatal("empty bootstrap password accepted") } if err := ensureAdmin(r, &config.Config{AdminEmail: "admin@example.test", AdminPassword: "secret123"}); err != nil { t.Fatal(err) } if r.created.Role != models.RoleAdmin || !auth.CheckPassword(r.created.PasswordHash, "secret123") { t.Fatal("invalid bootstrap account") } }