package server import ( "net/http/httptest" "slices" "strings" "testing" "git.breakpilot.com/platform/tenant-registry/internal/authn" "git.breakpilot.com/platform/tenant-registry/internal/authz" ) // The registry's role checks as they stood on 2026-11-10, before they read // the catalogue. Today's six bundles must keep granting exactly this, so // moving the checks onto capabilities (step 4) changed nobody's rights. var registryChecks20261110 = map[string]map[string]bool{ "registry:members.manage": {"IT_ADMIN": true, "OWNER": true}, "registry:api-keys.manage": {"IT_ADMIN": true, "OWNER": true}, "registry:pruefwerk.settings": {"IT_ADMIN": true, "OWNER": true, "FINANCE": true}, "registry:services.read": {"IT_ADMIN": true, "OWNER": true, "CXO": true}, "registry:notify.documents": {"IT_ADMIN": true, "OWNER": true, "CXO": true, "FINANCE": true, "LEGAL": true}, "registry:notify.findings": {"IT_ADMIN": true, "OWNER": true, "CXO": true, "LEGAL": true}, "registry:intake.report": {"IT_ADMIN": true, "OWNER": true, "CXO": true, "FINANCE": true, "LEGAL": true, "USER": true}, } func TestTodaysBundlesKeepTheRegistrysRoleChecks(t *testing.T) { for _, role := range []string{"IT_ADMIN", "OWNER", "CXO", "FINANCE", "LEGAL", "USER"} { for capability, holders := range registryChecks20261110 { if got := authz.AnyGrants([]string{role}, capability); got != holders[role] { t.Errorf("%s %s: catalogue %v, registry on 2026-11-10 %v", role, capability, got, holders[role]) } } // The last-admin guard and the tenant-admin check of a user token // still agree with the catalogue. if isAdminRole([]string{role}) != registryChecks20261110["registry:members.manage"][role] { t.Errorf("%s: isAdminRole disagrees", role) } p := &authn.Principal{Subject: "u", TenantID: "t", OrgRoles: []string{role}} if p.IsTenantAdmin() != authz.AnyGrants(p.OrgRoles, "registry:members.manage") { t.Errorf("%s: IsTenantAdmin disagrees with the catalogue", role) } } for _, c := range authz.Capabilities { if _, ok := registryChecks20261110[c.Code]; c.Product == authz.Registry && !ok { t.Errorf("%s: not pinned against a registry check", c.Code) } } } // The new bundles (ยง3a): what the registry now gives them, and that none of // them administers the workspace. func TestTheNewBundlesInTheRegistry(t *testing.T) { want := map[string][]string{ "PSIRT": {"registry:notify.findings", "registry:intake.report"}, "DEVELOPMENT": {"registry:notify.findings", "registry:intake.report"}, "QUALITY": {"registry:notify.documents", "registry:intake.report"}, "PRIVACY": {"registry:notify.documents", "registry:intake.report"}, "PURCHASING": {"registry:notify.documents", "registry:intake.report"}, "AUDITOR": {"registry:services.read", "registry:notify.documents", "registry:intake.report"}, "EXTERNAL": {"registry:intake.report"}, } for role, caps := range want { if !authz.IsAssignable(role) { t.Errorf("%s is not assignable", role) } for capability := range registryChecks20261110 { if got := authz.AnyGrants([]string{role}, capability); got != slices.Contains(caps, capability) { t.Errorf("%s %s: %v", role, capability, got) } } if isAdminRole([]string{role}) { t.Errorf("%s administers the workspace", role) } } if authz.AnyGrants([]string{"psirt"}, "registry:notify.findings") != true || authz.AnyGrants([]string{"NOBODY"}, "registry:intake.report") { t.Error("claims are matched case-insensitively and unknown bundles grant nothing") } } // GET /v1/authz/catalogue serves the catalogue as it is in code. func TestTheCatalogueIsServed(t *testing.T) { rec := httptest.NewRecorder() (&Server{}).getAuthzCatalogue(rec, nil) if rec.Code != 200 || !strings.Contains(rec.Body.String(), `"version":"`+authz.Version+`"`) || !strings.Contains(rec.Body.String(), `"code":"PSIRT"`) { t.Fatalf("%d %s", rec.Code, rec.Body.String()) } }