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
219 lines
8.4 KiB
Go
219 lines
8.4 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.breakpilot.com/platform/tenant-registry/internal/authn"
|
|
"git.breakpilot.com/platform/tenant-registry/internal/store"
|
|
)
|
|
|
|
func TestPruefwerkPlanFor(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name string
|
|
skus interface{}
|
|
want string
|
|
}{
|
|
{"none", nil, "standard"},
|
|
{"empty", []string{}, "standard"},
|
|
{"scan only", []string{"CRA-CSA-CLD-SCAN"}, "standard"},
|
|
{"werkbank add-on only", []string{"CRA-WBK-CLD-ENT"}, "standard"},
|
|
{"standard", []string{"CRA-CSA-CLD-STD"}, "standard"},
|
|
{"business", []string{"CRA-CSA-CLD-BUS"}, "business"},
|
|
{"enterprise", []string{"CRA-CSA-CLD-ENT"}, "enterprise"},
|
|
{"business with add-on and scan", []string{"CRA-CSA-CLD-SCAN", "CRA-WBK-CLD-ENT", "CRA-CSA-CLD-BUS"}, "business"},
|
|
{"all three", []string{"CRA-CSA-CLD-BUS", "CRA-CSA-CLD-ENT", "CRA-CSA-CLD-STD"}, "enterprise"},
|
|
{"standard and business", []string{"CRA-CSA-CLD-STD", "CRA-CSA-CLD-BUS"}, "business"},
|
|
{"from JSONB", []interface{}{"CRA-CSA-CLD-STD", "CRA-CSA-CLD-ENT"}, "enterprise"},
|
|
{"JSONB with junk", []interface{}{42, "CRA-CSA-CLD-BUS", nil}, "business"},
|
|
{"not a list", "CRA-CSA-CLD-ENT", "standard"},
|
|
{"other product's SKU", []string{"DEV-FWK-CLD-ENT"}, "standard"},
|
|
} {
|
|
if got := pruefwerkPlanFor(c.skus).Name; got != c.want {
|
|
t.Errorf("%s: %s, want %s", c.name, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPruefwerkLimits(t *testing.T) {
|
|
seven := 7
|
|
for _, c := range []struct {
|
|
skus []string
|
|
cap *int
|
|
targets, pruefungen, cap2 interface{}
|
|
}{
|
|
{nil, nil, 5, 100, nil},
|
|
{[]string{"CRA-CSA-CLD-STD"}, &seven, 5, 100, 7},
|
|
{[]string{"CRA-CSA-CLD-BUS"}, &seven, 25, 500, 7},
|
|
{[]string{"CRA-CSA-CLD-BUS"}, nil, 25, 500, nil},
|
|
{[]string{"CRA-CSA-CLD-ENT"}, &seven, nil, nil, nil},
|
|
} {
|
|
cfg := map[string]interface{}{"source": "erp", "runners": 2}
|
|
if c.skus != nil {
|
|
cfg["skus"] = c.skus
|
|
}
|
|
got := pruefwerkLimits(cfg, c.cap)
|
|
if len(cfg) != 2+boolInt(c.skus != nil) {
|
|
t.Fatalf("%v: stored config changed: %+v", c.skus, cfg)
|
|
}
|
|
if got["source"] != "erp" || got["runners"] != 2 {
|
|
t.Errorf("%v: config keys lost: %+v", c.skus, got)
|
|
}
|
|
raw, _ := json.Marshal(got)
|
|
for key, want := range map[string]interface{}{limitIncludedTargets: c.targets, limitMonthlyPruefungen: c.pruefungen, limitPrueftagCap: c.cap2} {
|
|
v, ok := got[key]
|
|
if !ok || v != want {
|
|
t.Errorf("%v %s = %v (present %v), want %v", c.skus, key, v, ok, want)
|
|
}
|
|
if want == nil && !strings.Contains(string(raw), `"`+key+`":null`) {
|
|
t.Errorf("%v %s not null in JSON: %s", c.skus, key, raw)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func boolInt(b bool) int {
|
|
if b {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func TestPruefwerkSettings_roles(t *testing.T) {
|
|
tenantID := "00000000-0000-0000-0000-000000000001"
|
|
path := "/v1/tenants/" + tenantID + "/products/pruefwerk/settings"
|
|
for _, c := range []struct {
|
|
name string
|
|
roles []string
|
|
want int
|
|
}{
|
|
{"it admin", []string{"IT_ADMIN"}, http.StatusOK},
|
|
{"owner", []string{"OWNER"}, http.StatusOK},
|
|
{"finance", []string{"USER", "FINANCE"}, http.StatusOK},
|
|
{"finance lower case", []string{"finance"}, http.StatusOK},
|
|
{"cxo", []string{"CXO"}, http.StatusForbidden},
|
|
{"legal", []string{"LEGAL"}, http.StatusForbidden},
|
|
{"plain user", []string{"USER"}, http.StatusForbidden},
|
|
{"auditor", []string{"AUDITOR"}, http.StatusForbidden},
|
|
{"quality", []string{"QUALITY"}, http.StatusForbidden},
|
|
{"no roles", nil, http.StatusForbidden},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
p := &authn.Principal{Subject: "u-1", ClientID: "portal", TenantID: tenantID, Email: "u1@acme.test", OrgRoles: c.roles}
|
|
s, h, tenant := scopedServer(t, p)
|
|
for _, req := range []*http.Request{
|
|
httptest.NewRequest(http.MethodGet, path, nil),
|
|
httptest.NewRequest(http.MethodPut, path, strings.NewReader(`{"prueftag_cap":20}`)),
|
|
} {
|
|
rr := httptest.NewRecorder()
|
|
h.ServeHTTP(rr, req)
|
|
if rr.Code != c.want {
|
|
t.Fatalf("%s: %d, want %d: %s", req.Method, rr.Code, c.want, rr.Body.String())
|
|
}
|
|
if c.want == http.StatusForbidden && !strings.Contains(rr.Body.String(), "FORBIDDEN_ROLE") {
|
|
t.Fatalf("%s error code: %s", req.Method, rr.Body.String())
|
|
}
|
|
}
|
|
evs, _, _ := s.Store.ListAudit(context.Background(), store.AuditFilter{TenantID: tenant.ID, Action: "product_settings.changed"})
|
|
if c.want == http.StatusForbidden {
|
|
if len(evs) != 0 {
|
|
t.Fatalf("refused change audited: %+v", evs)
|
|
}
|
|
if _, err := s.Store.GetTenantProductSettings(context.Background(), tenant.ID, pruefwerkProduct); err == nil {
|
|
t.Fatal("refused change stored")
|
|
}
|
|
return
|
|
}
|
|
if len(evs) != 1 {
|
|
t.Fatalf("audit: %+v", evs)
|
|
}
|
|
ev := evs[0]
|
|
if ev.ActorID != "u-1" || ev.ActorName != "u1@acme.test" || ev.ActorType != "user" || ev.TargetID != pruefwerkProduct ||
|
|
ev.Metadata["old"] != nil || toFloat(ev.Metadata["new"]) != 20 || ev.Metadata["setting"] != limitPrueftagCap {
|
|
t.Fatalf("audit event: %+v", ev)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Another tenant's 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"}}
|
|
_, h, _ := scopedServer(t, other)
|
|
rr := httptest.NewRecorder()
|
|
h.ServeHTTP(rr, httptest.NewRequest(http.MethodPut, path, strings.NewReader(`{"prueftag_cap":1}`)))
|
|
if rr.Code != http.StatusNotFound {
|
|
t.Fatalf("cross-tenant: %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestPruefwerkSettings_clients(t *testing.T) {
|
|
const rules = "portal-svc:tenants,users,users.act,admin;pruefwerk-svc:entitlements,tenants.read"
|
|
tenantID := "00000000-0000-0000-0000-000000000001"
|
|
path := "/v1/tenants/" + tenantID + "/products/pruefwerk/settings"
|
|
|
|
// A product client reads (tenants.read) but may not write.
|
|
pw := &authn.Principal{Subject: "svc-pw", ClientID: "pruefwerk-svc"}
|
|
_, h, _ := scopedServer(t, pw, rules)
|
|
rr := httptest.NewRecorder()
|
|
h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, path, nil))
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("product client read: %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
rr = httptest.NewRecorder()
|
|
h.ServeHTTP(rr, httptest.NewRequest(http.MethodPut, path, strings.NewReader(`{"prueftag_cap":1}`)))
|
|
if rr.Code != http.StatusForbidden || !strings.Contains(rr.Body.String(), "FORBIDDEN_CLIENT") {
|
|
t.Fatalf("product client write: %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
|
|
// Staff through the portal's admin client: audited as the staff member;
|
|
// the portal for a workspace user: audited as that user.
|
|
portal := &authn.Principal{Subject: "svc-portal", ClientID: "portal-svc"}
|
|
s, h, tenant := scopedServer(t, portal, rules)
|
|
send := func(body string, hdr ...string) {
|
|
t.Helper()
|
|
req := httptest.NewRequest(http.MethodPut, path, strings.NewReader(body))
|
|
for i := 0; i+1 < len(hdr); i += 2 {
|
|
req.Header.Set(hdr[i], hdr[i+1])
|
|
}
|
|
rr := httptest.NewRecorder()
|
|
h.ServeHTTP(rr, req)
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("portal put: %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
}
|
|
send(`{"prueftag_cap":3}`, "X-Staff-Subject", "staff-1", "X-Staff-Email", "ops@breakpilot.com")
|
|
send(`{"prueftag_cap":4}`, "X-Portal-User-Subject", "u-fin", "X-Portal-User-Email", "fin@acme.test")
|
|
send(`{"prueftag_cap":4}`, "X-Portal-User-Subject", "u-fin", "X-Portal-User-Email", "fin@acme.test") // unchanged: no event
|
|
evs, _, _ := s.Store.ListAudit(context.Background(), store.AuditFilter{TenantID: tenant.ID, Action: "product_settings.changed"})
|
|
if len(evs) != 2 {
|
|
t.Fatalf("audit events: %+v", evs)
|
|
}
|
|
byActor := map[string]store.AuditEvent{}
|
|
for _, ev := range evs {
|
|
byActor[ev.ActorType] = ev
|
|
}
|
|
if st := byActor["staff"]; st.ActorID != "staff-1" || st.ActorName != "ops@breakpilot.com" || st.Metadata["old"] != nil || toFloat(st.Metadata["new"]) != 3 {
|
|
t.Fatalf("staff event: %+v", st)
|
|
}
|
|
if u := byActor["user"]; u.ActorID != "u-fin" || u.ActorName != "fin@acme.test" || toFloat(u.Metadata["old"]) != 3 || toFloat(u.Metadata["new"]) != 4 {
|
|
t.Fatalf("portal user event: %+v", u)
|
|
}
|
|
row, err := s.Store.GetTenantProductSettings(context.Background(), tenant.ID, pruefwerkProduct)
|
|
if err != nil || row.UpdatedBy != "fin@acme.test" {
|
|
t.Fatalf("stored: %+v %v", row, err)
|
|
}
|
|
}
|
|
|
|
func TestRouteGroup_pruefwerkSettings(t *testing.T) {
|
|
path := "/v1/tenants/x/products/pruefwerk/settings"
|
|
if g := routeGroup(http.MethodGet, path); g != "tenants.read" {
|
|
t.Errorf("GET group %q", g)
|
|
}
|
|
if g := routeGroup(http.MethodPut, path); g != "tenants.write" {
|
|
t.Errorf("PUT group %q", g)
|
|
}
|
|
}
|