Files
tenant-registry/internal/server/services_scope_test.go
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

166 lines
7.0 KiB
Go

package server
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.breakpilot.com/platform/tenant-registry/internal/authn"
"git.breakpilot.com/platform/tenant-registry/internal/store"
)
func seedService(t *testing.T, s *Server, tenantID string) *store.ManagedService {
t.Helper()
ms, err := s.Store.CreateManagedService(context.Background(), store.ManagedService{
TenantID: tenantID, Kind: "git", Name: "Git", URL: "https://git.k-00001.example", SKU: "INF-GIT-CLD-STD",
InstanceID: "gitea-k-00001", Notes: "staff only",
})
if err != nil {
t.Fatal(err)
}
return ms
}
func TestManagedServices_tenantRoles(t *testing.T) {
tenantID := "00000000-0000-0000-0000-000000000001"
cases := []struct {
name string
roles []string
want int
}{
{"it admin", []string{"IT_ADMIN"}, http.StatusOK},
{"owner", []string{"OWNER"}, http.StatusOK},
{"cxo", []string{"USER", "CXO"}, http.StatusOK},
{"cxo lower case", []string{"cxo"}, http.StatusOK},
{"plain user", []string{"USER"}, http.StatusForbidden},
{"finance", []string{"FINANCE"}, http.StatusForbidden},
{"auditor reads (step 4)", []string{"AUDITOR"}, http.StatusOK},
{"psirt", []string{"PSIRT"}, http.StatusForbidden},
{"external", []string{"EXTERNAL"}, http.StatusForbidden},
{"unknown bundle", []string{"NOBODY"}, http.StatusForbidden},
{"legal", []string{"LEGAL"}, http.StatusForbidden},
{"no roles", nil, http.StatusForbidden},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
p := &authn.Principal{Subject: "u-1", ClientID: "portal", TenantID: tenantID, OrgRoles: c.roles}
s, h, tenant := scopedServer(t, p)
seedService(t, s, tenant.ID)
rr := httptest.NewRecorder()
h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/v1/tenants/"+tenant.ID+"/managed-services", nil))
if rr.Code != c.want {
t.Fatalf("status %d, want %d: %s", rr.Code, c.want, rr.Body.String())
}
body := rr.Body.String()
if c.want == http.StatusForbidden {
if !strings.Contains(body, "FORBIDDEN_ROLE") {
t.Fatalf("error code: %s", body)
}
return
}
if !strings.Contains(body, "https://git.k-00001.example") || strings.Contains(body, "instance_id") || strings.Contains(body, "staff only") {
t.Fatalf("tenant view: %s", body)
}
})
}
// Another tenant's user, even an IT_ADMIN: 404, never a hint it exists.
other := &authn.Principal{Subject: "u-x", ClientID: "portal", TenantID: "11111111-1111-1111-1111-111111111111", OrgRoles: []string{"IT_ADMIN"}}
s, h, tenant := scopedServer(t, other)
seedService(t, s, tenant.ID)
rr := httptest.NewRecorder()
h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/v1/tenants/"+tenant.ID+"/managed-services", nil))
if rr.Code != http.StatusNotFound {
t.Fatalf("cross-tenant: %d %s", rr.Code, rr.Body.String())
}
}
func TestManagedServices_clients(t *testing.T) {
const rules = "portal-svc:tenants,admin;pruefwerk-svc:tenants.read"
// The portal reads the tenant view with its service token.
portal := &authn.Principal{Subject: "svc", ClientID: "portal-svc"}
s, h, tenant := scopedServer(t, portal, rules)
ms := seedService(t, s, tenant.ID)
rr := httptest.NewRecorder()
h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/v1/tenants/"+tenant.ID+"/managed-services", nil))
if rr.Code != http.StatusOK || strings.Contains(rr.Body.String(), "instance_id") {
t.Fatalf("portal tenant read: %d %s", rr.Code, rr.Body.String())
}
// A client without the admin group may neither read nor write the admin
// routes; staff write access is decided by the portal (SUPPORT_ENGINEER
// reads only), the registry only sees the admin client.
ro := &authn.Principal{Subject: "svc2", ClientID: "pruefwerk-svc"}
_, h2, _ := scopedServer(t, ro, rules)
base := "/v1/admin/tenants/" + tenant.ID + "/managed-services"
for _, c := range []struct{ method, path, body string }{
{http.MethodGet, base, ""},
{http.MethodPost, base, `{"kind":"git","name":"x","url":"https://x.k-00001.example","sku":"INF-GIT-CLD-STD"}`},
{http.MethodPatch, base + "/" + ms.ID, `{"status":"halted"}`},
{http.MethodDelete, base + "/" + ms.ID, ""},
} {
rr := httptest.NewRecorder()
h2.ServeHTTP(rr, httptest.NewRequest(c.method, c.path, strings.NewReader(c.body)))
if rr.Code != http.StatusForbidden || !strings.Contains(rr.Body.String(), "FORBIDDEN_CLIENT") {
t.Fatalf("%s %s by a non-admin client: %d %s", c.method, c.path, rr.Code, rr.Body.String())
}
}
// ...but it may read the tenant view (tenants.read).
rr = httptest.NewRecorder()
h2.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/v1/tenants/"+tenant.ID+"/managed-services", nil))
if rr.Code != http.StatusOK {
t.Fatalf("read client tenant view: %d %s", rr.Code, rr.Body.String())
}
}
func TestManagedServices_staffIsAuditActor(t *testing.T) {
portal := &authn.Principal{Subject: "svc", ClientID: "portal-svc"}
s, h, tenant := scopedServer(t, portal, "portal-svc:tenants,admin")
base := "/v1/admin/tenants/" + tenant.ID + "/managed-services"
send := func(method, path, body string) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, path, strings.NewReader(body))
r.Header.Set("X-Staff-Subject", "staff-1")
r.Header.Set("X-Staff-Email", "ops@breakpilot.com")
rr := httptest.NewRecorder()
h.ServeHTTP(rr, r)
return rr
}
rr := send(http.MethodPost, base, `{"kind":"git","name":"Git","url":"https://git.k-00001.example","sku":"INF-GIT-OPR-BUS","instance_id":"gitea-k-00001"}`)
if rr.Code != http.StatusCreated || !strings.Contains(rr.Body.String(), `"instance_id":"gitea-k-00001"`) || !strings.Contains(rr.Body.String(), `"status":"provisioning"`) {
t.Fatalf("create: %d %s", rr.Code, rr.Body.String())
}
items, _ := s.Store.ListManagedServices(context.Background(), tenant.ID)
if len(items) != 1 {
t.Fatalf("stored: %+v", items)
}
id := items[0].ID
if rr := send(http.MethodPatch, base+"/"+id, `{"status":"restricted"}`); rr.Code != http.StatusOK {
t.Fatalf("patch: %d %s", rr.Code, rr.Body.String())
}
if rr := send(http.MethodDelete, base+"/"+id, ""); rr.Code != http.StatusNoContent {
t.Fatalf("delete: %d %s", rr.Code, rr.Body.String())
}
for _, action := range []string{"managed_service.created", "managed_service.updated", "managed_service.deleted"} {
evs, _, _ := s.Store.ListAudit(context.Background(), store.AuditFilter{TenantID: tenant.ID, Action: action})
if len(evs) != 1 || evs[0].ActorType != "staff" || evs[0].ActorID != "staff-1" || evs[0].ActorName != "ops@breakpilot.com" || evs[0].TargetID != id {
t.Fatalf("%s actor: %+v", action, evs)
}
if action == "managed_service.updated" && (evs[0].Metadata["old_status"] != "provisioning" || evs[0].Metadata["new_status"] != "restricted") {
t.Fatalf("status move: %+v", evs[0].Metadata)
}
}
}
func TestRouteGroup_managedServices(t *testing.T) {
if g := routeGroup(http.MethodGet, "/v1/tenants/x/managed-services"); g != "tenants.read" {
t.Errorf("tenant group %q", g)
}
for _, m := range []string{http.MethodGet, http.MethodPost, http.MethodPatch, http.MethodDelete} {
if g := routeGroup(m, "/v1/admin/tenants/x/managed-services/y"); g != "admin" {
t.Errorf("%s admin group %q", m, g)
}
}
}