package server import ( "net/http" "net/http/httptest" "strings" "testing" "git.breakpilot.com/platform/tenant-registry/internal/authn" ) func TestModuleUsers_tenantRoles(t *testing.T) { tenantID := "00000000-0000-0000-0000-000000000001" for _, c := range []struct { roles []string want int }{ {[]string{"IT_ADMIN"}, http.StatusOK}, {[]string{"OWNER"}, http.StatusOK}, {[]string{"CXO"}, http.StatusOK}, {[]string{"FINANCE"}, http.StatusOK}, {[]string{"AUDITOR"}, http.StatusOK}, {[]string{"USER"}, http.StatusForbidden}, {[]string{"LEGAL"}, http.StatusForbidden}, {[]string{"EXTERNAL"}, http.StatusForbidden}, {nil, http.StatusForbidden}, } { p := &authn.Principal{Subject: "u-1", ClientID: "portal", TenantID: tenantID, OrgRoles: c.roles} _, h, tenant := scopedServer(t, p) rr := httptest.NewRecorder() h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/v1/tenants/"+tenant.ID+"/module-users", nil)) if rr.Code != c.want || (c.want == http.StatusForbidden && !strings.Contains(rr.Body.String(), "FORBIDDEN_ROLE")) { t.Fatalf("%v: %d %s", c.roles, rr.Code, rr.Body.String()) } } // Another tenant's user: 404. other := &authn.Principal{Subject: "u-x", ClientID: "portal", TenantID: "11111111-1111-1111-1111-111111111111", OrgRoles: []string{"IT_ADMIN"}} _, h, tenant := scopedServer(t, other) rr := httptest.NewRecorder() h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/v1/tenants/"+tenant.ID+"/module-users", nil)) if rr.Code != http.StatusNotFound { t.Fatalf("cross-tenant: %d %s", rr.Code, rr.Body.String()) } } func TestModuleUsers_clients(t *testing.T) { // Today's portal rule (orca-platform): reads need no change; the count // on demand needs the module-users group. const rules = "portal-svc:tenants,catalog,api-keys,audit,users,entitlements,usage.read,licences,admin,authz;ops-svc:module-users" portal := &authn.Principal{Subject: "svc", ClientID: "portal-svc"} _, h, tenant := scopedServer(t, portal, rules) for _, c := range []struct { method, path string want int }{ {http.MethodGet, "/v1/tenants/" + tenant.ID + "/module-users", http.StatusOK}, {http.MethodGet, "/v1/admin/tenants/" + tenant.ID + "/module-users?month=2026-11", http.StatusOK}, {http.MethodPost, "/v1/internal/module-users/count", http.StatusForbidden}, } { rr := httptest.NewRecorder() h.ServeHTTP(rr, httptest.NewRequest(c.method, c.path, nil)) if rr.Code != c.want { t.Fatalf("portal %s %s: %d %s", c.method, c.path, rr.Code, rr.Body.String()) } } ops := &authn.Principal{Subject: "svc", ClientID: "ops-svc"} _, h, _ = scopedServer(t, ops, rules) rr := httptest.NewRecorder() h.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, "/v1/internal/module-users/count", nil)) if rr.Code != http.StatusOK { t.Fatalf("ops count: %d %s", rr.Code, rr.Body.String()) } } func TestRouteGroup_moduleUsers(t *testing.T) { for _, c := range []struct{ method, path, want string }{ {http.MethodGet, "/v1/tenants/x/module-users", "tenants.read"}, {http.MethodGet, "/v1/admin/tenants/x/module-users", "admin"}, {http.MethodPut, "/v1/admin/tenants/x/managed-services/y/admin-token", "admin"}, {http.MethodPost, "/v1/internal/module-users/count", "module-users"}, } { if g := routeGroup(c.method, c.path); g != c.want { t.Errorf("%s %s: group %q, want %q", c.method, c.path, g, c.want) } } if got := subRoute("/v1/tenants/x/module-users"); got != "GET /v1/tenants/{id}/module-users" { t.Errorf("route name %q", got) } }