package cloudcp import ( "net/http" "net/http/httptest" "strings" "testing" "github.com/rcourtman/pulse-go-rewrite/internal/cloudcp/registry" ) func TestRegisterRoutes_AccountAndTenantMethodDispatch(t *testing.T) { dir := t.TempDir() reg, err := registry.NewTenantRegistry(dir) if err != nil { t.Fatalf("NewTenantRegistry: %v", err) } t.Cleanup(func() { _ = reg.Close() }) trialStore, err := NewTrialSignupStore(dir) if err != nil { t.Fatalf("NewTrialSignupStore: %v", err) } t.Cleanup(func() { trialStore.Close() }) mux := http.NewServeMux() RegisterRoutes(mux, &Deps{ Config: &CPConfig{ DataDir: dir, AdminKey: "test-admin-key", BaseURL: "https://cloud.example.com", StripeWebhookSecret: "whsec_test", PublicCloudSignupEnabled: true, }, Registry: reg, TrialSignupStore: trialStore, Version: "test", }) tests := []struct { name string path string want int }{ // /members collection handler: GET/POST supported, others rejected. {name: "members-get-dispatches", path: "/api/accounts/acct_1/members", want: http.StatusNotFound}, {name: "members-post-dispatches", path: "/api/accounts/acct_1/members", want: http.StatusNotFound}, {name: "members-put-rejected", path: "/api/accounts/acct_1/members", want: http.StatusMethodNotAllowed}, // /members/{user_id} handler: PATCH/DELETE supported, others rejected. {name: "member-patch-dispatches", path: "/api/accounts/acct_1/members/user_1", want: http.StatusNotFound}, {name: "member-delete-dispatches", path: "/api/accounts/acct_1/members/user_1", want: http.StatusNotFound}, {name: "member-get-rejected", path: "/api/accounts/acct_1/members/user_1", want: http.StatusMethodNotAllowed}, // /tenants collection handler: GET/POST supported, others rejected. {name: "tenants-get-dispatches", path: "/api/accounts/acct_1/tenants", want: http.StatusNotFound}, {name: "tenants-post-dispatches", path: "/api/accounts/acct_1/tenants", want: http.StatusNotFound}, {name: "tenants-put-rejected", path: "/api/accounts/acct_1/tenants", want: http.StatusMethodNotAllowed}, // /tenants/{tenant_id} handler: PATCH/DELETE supported, others rejected. {name: "tenant-patch-dispatches", path: "/api/accounts/acct_1/tenants/tenant_1", want: http.StatusNotFound}, {name: "tenant-delete-dispatches", path: "/api/accounts/acct_1/tenants/tenant_1", want: http.StatusNotFound}, {name: "tenant-get-rejected", path: "/api/accounts/acct_1/tenants/tenant_1", want: http.StatusMethodNotAllowed}, } methodFor := map[string]string{ "members-get-dispatches": http.MethodGet, "members-post-dispatches": http.MethodPost, "members-put-rejected": http.MethodPut, "member-patch-dispatches": http.MethodPatch, "member-delete-dispatches": http.MethodDelete, "member-get-rejected": http.MethodGet, "tenants-get-dispatches": http.MethodGet, "tenants-post-dispatches": http.MethodPost, "tenants-put-rejected": http.MethodPut, "tenant-patch-dispatches": http.MethodPatch, "tenant-delete-dispatches": http.MethodDelete, "tenant-get-rejected": http.MethodGet, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { req := httptest.NewRequest(methodFor[tt.name], tt.path, nil) req.Header.Set("X-Admin-Key", "test-admin-key") rec := httptest.NewRecorder() mux.ServeHTTP(rec, req) if rec.Code != tt.want { t.Fatalf("%s %s status = %d, want %d (body=%q)", methodFor[tt.name], tt.path, rec.Code, tt.want, rec.Body.String()) } }) } } func TestRegisterRoutes_FaviconRouteParity(t *testing.T) { dir := t.TempDir() reg, err := registry.NewTenantRegistry(dir) if err != nil { t.Fatalf("NewTenantRegistry: %v", err) } t.Cleanup(func() { _ = reg.Close() }) trialStore, err := NewTrialSignupStore(dir) if err != nil { t.Fatalf("NewTrialSignupStore: %v", err) } t.Cleanup(func() { trialStore.Close() }) mux := http.NewServeMux() RegisterRoutes(mux, &Deps{ Config: &CPConfig{ DataDir: dir, AdminKey: "test-admin-key", BaseURL: "https://cloud.example.com", StripeWebhookSecret: "whsec_test", PublicCloudSignupEnabled: true, }, Registry: reg, TrialSignupStore: trialStore, Version: "test", }) svgReq := httptest.NewRequest(http.MethodGet, "/favicon.svg", nil) svgRec := httptest.NewRecorder() mux.ServeHTTP(svgRec, svgReq) if svgRec.Code != http.StatusOK { t.Fatalf("GET /favicon.svg status=%d, want %d", svgRec.Code, http.StatusOK) } if got := svgRec.Header().Get("Content-Type"); got != "image/svg+xml" { t.Fatalf("GET /favicon.svg content-type=%q, want %q", got, "image/svg+xml") } body := svgRec.Body.String() if !strings.Contains(body, "