package handlers import ( "net/http" "net/http/httptest" "path/filepath" "runtime" "testing" "git.nixc.us/a250/ss-atlas/internal/config" ) func TestContains(t *testing.T) { tests := []struct { s string sub string want bool }{ {"customers,admins", "customers", true}, {"admins,customers", "customers", true}, {"customers", "customers", true}, {"admins,users", "customers", false}, {"", "customers", false}, {"cus", "customers", false}, {"customersx", "customers", true}, } for _, tt := range tests { if got := contains(tt.s, tt.sub); got != tt.want { t.Errorf("contains(%q, %q) = %v, want %v", tt.s, tt.sub, got, tt.want) } } } func TestSearchString(t *testing.T) { tests := []struct { s string sub string want bool }{ {"customers,admins", "customers", true}, {"admins", "admins", true}, {"a", "a", true}, {"abc", "b", true}, {"abc", "d", false}, {"", "a", false}, } for _, tt := range tests { if got := searchString(tt.s, tt.sub); got != tt.want { t.Errorf("searchString(%q, %q) = %v, want %v", tt.s, tt.sub, got, tt.want) } } } func TestSanitizeUsername(t *testing.T) { tests := []struct { email string want string }{ {"user@example.com", "user"}, {"User.Name@domain.com", "user-name"}, {"user_name@domain.com", "user_name"}, {"user123@domain.com", "user123"}, {"UPPER@domain.com", "upper"}, {"a.b.c@x.com", "a-b-c"}, {"spécial@x.com", "sp-cial"}, } for _, tt := range tests { if got := sanitizeUsername(tt.email); got != tt.want { t.Errorf("sanitizeUsername(%q) = %q, want %q", tt.email, got, tt.want) } } } func TestHealthRoute(t *testing.T) { _, filename, _, _ := runtime.Caller(0) rootDir := filepath.Join(filepath.Dir(filename), "..", "..") templatePath := filepath.Join(rootDir, "templates") cfg := &config.Config{ Port: "8080", TemplatePath: templatePath, } router := NewRouter(cfg, nil, nil, nil) req := httptest.NewRequest(http.MethodGet, "/health", nil) rec := httptest.NewRecorder() router.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Errorf("GET /health status = %d, want 200", rec.Code) } if body := rec.Body.String(); body != "ok" { t.Errorf("GET /health body = %q, want ok", body) } }