Files
Sharang ParnerkarandClaude Opus 5.5 dc44eb22bb
ci / shared (pull_request) Successful in 12s
ci / test (pull_request) Successful in 6m6s
ci / image (pull_request) Skipped
feat(authz): the new bundles are assignable (RBAC step 4)
PSIRT, DEVELOPMENT, QUALITY, PRIVACY, PURCHASING, AUDITOR and EXTERNAL
can be given to a member (catalogue 2026-11-11.1). Invitations and role
changes accept exactly the catalogue's assignable bundles.

The registry's own checks read the catalogue instead of role lists:
notification recipients, managed services and Prüfwerk settings. For
today's six bundles nothing changes (pinned against the 2026-11-10
tables); AUDITOR now reads managed services, and the new bundles get
the mails their capabilities name. Individual capabilities still do not
count in the registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014aRnCGqELxoix5TfpX8Ejf
2026-11-11 21:29:33 +01:00

165 lines
6.7 KiB
Go

package authz
import (
"slices"
"testing"
"git.breakpilot.com/platform/tenant-registry/internal/roles"
)
// The catalogue holds together: unique codes, bundles and positions name
// what exists, positions name functions of the role dataset.
func TestTheCatalogueHoldsTogether(t *testing.T) {
seen := map[string]bool{}
for _, c := range Capabilities {
if seen[c.Code] || c.Label == "" || (c.Product != Portal && c.Product != Werkmeister && c.Product != Registry) {
t.Errorf("capability %q: duplicate, unlabelled or of no product", c.Code)
}
seen[c.Code] = true
}
bundles := map[string]bool{}
for _, b := range Bundles {
if bundles[b.Code] || b.Label == "" {
t.Errorf("bundle %q: duplicate or unlabelled", b.Code)
}
bundles[b.Code] = true
dup := map[string]bool{}
for _, c := range b.Capabilities {
if !seen[c] {
t.Errorf("bundle %s grants unknown capability %q", b.Code, c)
}
if dup[c] {
t.Errorf("bundle %s grants %q twice", b.Code, c)
}
dup[c] = true
}
}
positions := map[string]bool{}
for _, p := range Positions {
if positions[p.Code] || (p.Variant != VariantSmall && p.Variant != VariantLarge) || !bundles[p.Bundle] {
t.Errorf("position %q: duplicate, of no variant or of unknown bundle %q", p.Code, p.Bundle)
}
positions[p.Code] = true
for _, f := range p.Functions {
if _, ok := roles.FunctionByKey(f); !ok {
t.Errorf("position %s proposes unknown function %q", p.Code, f)
}
}
}
// All thirteen are assignable since step 4, in catalogue order.
all := []string{"IT_ADMIN", "OWNER", "CXO", "FINANCE", "LEGAL", "USER", "PSIRT", "DEVELOPMENT", "QUALITY", "PRIVACY", "PURCHASING", "AUDITOR", "EXTERNAL"}
if got := Assignable(); !slices.Equal(got, all) {
t.Errorf("assignable %v", got)
}
for _, code := range all {
if !IsAssignable(code) {
t.Errorf("%s is not assignable", code)
}
}
if IsAssignable("NOBODY") || IsAssignable("") {
t.Error("an unknown bundle is assignable")
}
}
// Today's rights of the portal and Werkmeister on 2026-11-10, copied from
// their code: portal src/lib/session.ts (surfaceRoles, productWriteRoles,
// infraWriteRoles), lib/company-profile/registry.ts (PROFILE_EDITORS),
// lib/document-store.ts (STORE_UPLOADERS), lib/projects.ts
// (PROJECT_EDITORS), lib/chunks.ts (CHUNK_ROLES), app/[slug]/projects/
// [pid]/documents/[doc] (reread: IT_ADMIN); Werkmeister internal/rbac
// (orgRoles). Step 2 moves those services onto the catalogue; until then
// this pins that the bundles say what they do.
var today = map[string][]string{
"portal:dashboard.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL", "USER"},
"portal:products.view": {"IT_ADMIN", "CXO", "LEGAL", "USER"},
"portal:projects.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL", "USER"},
"portal:profile.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL"},
"portal:roles.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL", "USER"},
"portal:security.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL", "USER"},
"portal:settings.view": {"IT_ADMIN"},
"portal:users.view": {"IT_ADMIN"},
"portal:api-keys.view": {"IT_ADMIN"},
"portal:integrations.view": {"IT_ADMIN"},
"portal:billing.view": {"IT_ADMIN", "CXO", "FINANCE"},
"portal:audit.view": {"IT_ADMIN", "CXO", "LEGAL"},
"portal:support.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL", "USER"},
"portal:catalog.view": {"IT_ADMIN", "CXO"},
"portal:documents.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL"},
"portal:store.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL"},
"portal:contracts.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL"},
"portal:infra.view": {"IT_ADMIN", "CXO"},
"portal:studio.view": {"IT_ADMIN", "CXO", "LEGAL", "USER"},
"portal:products.write": {"IT_ADMIN", "USER"},
"portal:infra.write": {"IT_ADMIN"},
"portal:profile.edit": {"IT_ADMIN", "CXO", "LEGAL"},
"portal:store.upload": {"IT_ADMIN", "CXO", "LEGAL"},
"portal:projects.edit": {"IT_ADMIN", "CXO", "LEGAL"},
"portal:documents.chunks": {"IT_ADMIN", "OWNER"},
"portal:documents.reread": {"IT_ADMIN"},
"werkmeister:process.view": {"IT_ADMIN", "OWNER", "USER", "CXO", "LEGAL"},
"werkmeister:run.start": {"IT_ADMIN", "OWNER", "USER"},
"werkmeister:task.work": {"IT_ADMIN", "OWNER", "USER"},
"werkmeister:process.edit": {"IT_ADMIN", "OWNER"},
"werkmeister:process.publish": {"IT_ADMIN", "OWNER"},
"werkmeister:connection.manage": {"IT_ADMIN", "OWNER"},
"werkmeister:documents.read": {"IT_ADMIN", "OWNER"},
}
// The one deliberate difference: the portal does not know OWNER today and
// will show it like IT_ADMIN (decided 2026-11-10), so OWNER's portal rights
// are IT_ADMIN's, not today's.
func TestTodaysBundlesReproduceThePortalAndWerkmeister(t *testing.T) {
for _, c := range Capabilities {
if c.Product == Registry {
continue // pinned against the registry's own code (internal/server)
}
want, ok := today[c.Code]
if !ok {
t.Errorf("%s: not pinned", c.Code)
continue
}
for _, b := range []string{"IT_ADMIN", "OWNER", "CXO", "FINANCE", "LEGAL", "USER"} {
has := slices.Contains(want, b)
if b == "OWNER" && c.Product == Portal {
has = slices.Contains(today[c.Code], "IT_ADMIN")
}
if Grants(b, c.Code) != has {
t.Errorf("%s %s: catalogue %v, today %v", b, c.Code, Grants(b, c.Code), has)
}
}
}
}
// Rights are additive and fail closed.
func TestEffectiveRights(t *testing.T) {
if Grants("SOMEONE", "portal:dashboard.view") {
t.Fatal("an unknown bundle grants something")
}
e := Effective("USER", []string{"werkmeister:process.publish", "werkmeister:everything"})
if !e["werkmeister:process.publish"] || !e["werkmeister:task.work"] || e["werkmeister:everything"] || e["portal:users.view"] {
t.Fatalf("bundle plus known extras only: %v", e)
}
if len(Effective("", nil)) != 0 {
t.Fatal("no bundle, no rights")
}
// New bundles never reach what only the administration may do.
for _, b := range []string{"PSIRT", "DEVELOPMENT", "QUALITY", "PRIVACY", "PURCHASING", "AUDITOR", "EXTERNAL"} {
for _, c := range Capabilities {
if c.Admin && Grants(b, c.Code) {
t.Errorf("%s grants the administration capability %s", b, c.Code)
}
}
}
// AUDITOR changes nothing; EXTERNAL sees no Werkmeister case until step 5.
for _, c := range []string{"portal:products.write", "portal:profile.edit", "portal:store.upload", "portal:projects.edit", "werkmeister:task.work", "werkmeister:run.start"} {
if Grants("AUDITOR", c) {
t.Errorf("AUDITOR may %s", c)
}
}
for _, c := range Capabilities {
if c.Product == Werkmeister && Grants("EXTERNAL", c.Code) {
t.Errorf("EXTERNAL may %s", c.Code)
}
}
}