Files
tenant-registry/internal/roles/dataset_test.go
T
sharangandClaude Opus 5.5 4dd1179ca4
ci / shared (push) Successful in 12s
ci / test (push) Successful in 24m35s
ci / image (push) Successful in 41s
fix: the CXO sign-in role makes the first role dataset entry (#94)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014aRnCGqELxoix5TfpX8Ejf
2026-10-26 22:48:21 +00:00

359 lines
15 KiB
Go

package roles
import (
"encoding/json"
"net/http"
"strings"
"testing"
"time"
)
var now = time.Date(2026, 11, 10, 9, 0, 0, 0, time.UTC)
const (
ceo = "u-ceo"
owner = "u-docowner"
staff = "u-staff"
)
func member(sub string) Holder { return Holder{Kind: HolderMember, Sub: sub} }
// base: management and the owner of R1-D01 are recorded.
func base(t *testing.T) *Dataset {
t.Helper()
ds, err := Prepare(nil, &Dataset{Assignments: []Assignment{{ID: "a-ceo", Function: Management, Holder: member(ceo)}}},
nil, Writer{Sub: ceo, OrgRoles: []string{BootstrapRole}}, now)
if err != nil {
t.Fatalf("bootstrap: %v", err)
}
next := *ds
next.Assignments = append(next.Assignments, Assignment{ID: "a-own", Function: DocumentOwner, Holder: member(owner), Scope: &Scope{Document: "r1_d01"}})
ds, err = Prepare(ds, &next, nil, Writer{Sub: ceo}, now)
if err != nil {
t.Fatalf("document owner: %v", err)
}
return ds
}
func withDeputy(ds *Dataset, d Deputy) *Dataset {
next := *ds
next.Deputies = append(append([]Deputy(nil), ds.Deputies...), d)
return &next
}
func mgmtDeputy() Deputy {
return Deputy{ID: "d-1", Function: Management, Holder: member(staff), DecisionTypes: []string{"r1.release_d01"},
Basis: Basis{Text: "Vertretungsregelung der Geschäftsführung"}}
}
func code(err *Error) string {
if err == nil {
return ""
}
return err.Code
}
func TestCatalogue_unionOfA1AndA4(t *testing.T) {
seen := map[string]bool{}
labels := map[string]bool{}
rows := map[int]bool{}
for _, f := range Functions {
if seen[f.Code] || labels[f.Label] {
t.Errorf("twice: %s / %s", f.Code, f.Label)
}
seen[f.Code], labels[f.Label] = true, true
if f.D02Row > 0 {
rows[f.D02Row] = true
}
if !strings.HasPrefix(f.Code, "r1.") {
t.Errorf("%s: R1 keys are namespaced r1.*", f.Code)
}
}
a1 := []string{"Geschäftsleitung", "Produkt-Cybersecurity-Verantwortlicher", "Technischer Ansprechpartner", "Vorfallkontakt",
"Regulatorisch Meldeberechtigter", "Produktsicherheitsverantwortlicher", "Dokumentenverantwortlicher", "Freigabestelle",
"Änderungsinitiierung", "Produktverantwortung", "Änderungskoordination", "Fachprüfung", "Freigabe von Produktänderungen",
"Dokumentenlenkung", "Maßnahmenausführer", "Prüffunktion", "Abschlussverantwortlicher", "Fachlich verantwortliche Stelle"}
a4 := []string{"Geschäftsleitung", "Produkt-Cybersecurity-Verantwortlicher", "Technischer Produktverantwortlicher",
"Externer Cybersecurity-Dienstleister", "Produktsicherheitsverantwortlicher",
"Konformitätsverantwortlicher / autorisierte Herstellerfunktion", "Regulatorisch Meldeberechtigter"}
for _, l := range append(a1, a4...) {
if !labels[l] {
t.Errorf("missing %q", l)
}
}
if len(a1) != 18 || len(a4) != 7 || len(Functions) != 21 {
t.Errorf("union of 18 and 7 with 4 shared is 21, got %d", len(Functions))
}
for i := 1; i <= 7; i++ {
if !rows[i] {
t.Errorf("R1-D02 §18 row %d unmapped", i)
}
}
for _, d := range DecisionTypes {
if d.Default != "" && d.Default != Management {
t.Errorf("%s: only the releases carry an owner default", d.Code)
}
if _, ok := FunctionByKey(d.Default); d.Default != "" && !ok {
t.Errorf("%s: unknown default", d.Code)
}
}
}
func TestPrepare_whoMayWrite(t *testing.T) {
ds := base(t)
next := *ds
next.Assignments = append(next.Assignments, Assignment{ID: "a-x", Function: "r1.vorfallkontakt", Holder: member(staff)})
if _, err := Prepare(ds, &next, nil, Writer{Sub: staff}, now); code(err) != "not_permitted" {
t.Fatalf("a person without function: %v", err)
}
if _, err := Prepare(ds, &next, nil, Writer{Sub: staff, OrgRoles: []string{BootstrapRole}}, now); code(err) != "not_permitted" {
t.Fatalf("CXO once management is recorded: %v", err)
}
if _, err := Prepare(ds, &next, nil, Writer{Sub: owner}, now); err != nil {
t.Fatalf("document owner: %v", err)
}
if _, err := Prepare(ds, &next, nil, Writer{Sub: ceo}, now); err != nil {
t.Fatalf("management: %v", err)
}
}
func TestPrepare_bootstrapOnlyRecordsManagement(t *testing.T) {
w := Writer{Sub: "u-signup", OrgRoles: []string{BootstrapRole}}
other := &Dataset{Assignments: []Assignment{{ID: "a-1", Function: "r1.vorfallkontakt", Holder: member(staff)}}}
if _, err := Prepare(nil, other, nil, w, now); code(err) != "bootstrap_only" {
t.Fatalf("bootstrap with another function: %v", err)
}
mgmt := &Dataset{Assignments: []Assignment{{ID: "a-1", Function: Management, Holder: member(ceo)}}}
if _, err := Prepare(nil, mgmt, []string{"a-1"}, w, now); code(err) != "bootstrap_only" {
t.Fatalf("no authorizing in the bootstrap: %v", err)
}
for _, r := range []string{"IT_ADMIN", "OWNER", "LEGAL", "USER"} {
if _, err := Prepare(nil, mgmt, nil, Writer{Sub: "u-signup", OrgRoles: []string{r}}, now); code(err) != "not_permitted" {
t.Fatalf("bootstrap by %s: %v", r, err)
}
}
ds, err := Prepare(nil, mgmt, nil, w, now)
if err != nil || ds.Assignments[0].RecordedBy != "u-signup" {
t.Fatalf("bootstrap: %+v %v", ds, err)
}
}
func TestPrepare_stampsComeFromTheWriter(t *testing.T) {
ds := base(t)
forged := mgmtDeputy()
forged.AuthorizedBy, forged.RecordedBy = ceo, ceo
got, err := Prepare(ds, withDeputy(ds, forged), nil, Writer{Sub: owner}, now)
if err != nil {
t.Fatal(err)
}
d := got.Deputies[0]
if d.RecordedBy != owner || d.AuthorizedBy != "" {
t.Fatalf("stamps from the body: %+v", d)
}
if st := got.State(d, now); st.State != StateRecorded || st.Reasons[0] != "authorization_missing" {
t.Fatalf("B7: a document owner's management deputy without authorization: %+v", st)
}
}
func TestPrepare_managementAuthorizes(t *testing.T) {
ds := base(t)
byOwner, err := Prepare(ds, withDeputy(ds, mgmtDeputy()), nil, Writer{Sub: owner}, now)
if err != nil {
t.Fatal(err)
}
if _, err := Prepare(byOwner, byOwner, []string{"d-1"}, Writer{Sub: owner}, now); code(err) != "not_authorizer" {
t.Fatalf("document owner authorizing: %v", err)
}
auth, err := Prepare(byOwner, byOwner, []string{"d-1"}, Writer{Sub: ceo}, now)
if err != nil || auth.Deputies[0].AuthorizedBy != ceo {
t.Fatalf("management authorizes: %+v %v", auth.Deputies, err)
}
if st := auth.State(auth.Deputies[0], now); st.State != StateEffective {
t.Fatalf("authorized with basis: %+v", st)
}
// Unchanged on the next save, by anyone.
again, err := Prepare(auth, auth, nil, Writer{Sub: owner}, now.Add(time.Hour))
if err != nil || again.Deputies[0].AuthorizedBy != ceo || !again.Deputies[0].RecordedAt.Equal(*auth.Deputies[0].RecordedAt) {
t.Fatalf("stamps kept: %+v %v", again.Deputies[0], err)
}
// Changing the scope of the deputyship drops the authorization.
changed := *auth
changed.Deputies = []Deputy{auth.Deputies[0]}
changed.Deputies[0].DecisionTypes = []string{"r1.release_d01", "r1.release_d02"}
got, err := Prepare(auth, &changed, nil, Writer{Sub: owner}, now)
if err != nil || got.Deputies[0].AuthorizedBy != "" {
t.Fatalf("changed deputyship keeps its authorization: %+v %v", got.Deputies[0], err)
}
}
func TestPrepare_managementRecordingIsTheAuthorization(t *testing.T) {
ds := base(t)
got, err := Prepare(ds, withDeputy(ds, mgmtDeputy()), nil, Writer{Sub: ceo}, now)
if err != nil || got.Deputies[0].AuthorizedBy != ceo {
t.Fatalf("B7 wording: management's own record authorizes: %+v %v", got.Deputies, err)
}
self := mgmtDeputy()
self.Holder = member(ceo)
if _, err := Prepare(ds, withDeputy(ds, self), []string{"d-1"}, Writer{Sub: ceo}, now); code(err) != "self_authorization" {
t.Fatalf("self authorization: %v", err)
}
got, err = Prepare(ds, withDeputy(ds, self), nil, Writer{Sub: ceo}, now)
if err != nil || got.Deputies[0].AuthorizedBy != "" {
t.Fatalf("no automatic self authorization: %+v %v", got.Deputies, err)
}
}
func TestState_basisAndValidity(t *testing.T) {
ds := base(t)
d := Deputy{ID: "d-2", Function: "r1.vorfallkontakt", Holder: member(staff), DecisionTypes: []string{"r1.escalation"}}
if st := ds.State(d, now); st.State != StateRecorded || st.Reasons[0] != "basis_missing" {
t.Fatalf("without basis: %+v", st)
}
d.Basis.Text = "Bestellung vom 1.10."
if st := ds.State(d, now); st.State != StateEffective {
t.Fatalf("with basis: %+v", st)
}
d.ValidFrom = "2026-12-01"
if st := ds.State(d, now); st.State != StateRecorded || st.Reasons[0] != "not_yet_valid" {
t.Fatalf("before valid_from: %+v", st)
}
d.ValidFrom, d.ValidUntil = "2026-01-01", "2026-11-09"
if st := ds.State(d, now); st.State != StateRecorded || st.Reasons[0] != "expired" {
t.Fatalf("after valid_until: %+v", st)
}
// The last day still counts, in Berlin: 23:30 UTC on 9 Nov is 10 Nov there.
if st := ds.State(d, time.Date(2026, 11, 9, 22, 30, 0, 0, time.UTC)); st.State != StateEffective {
t.Fatalf("on the last day: %+v", st)
}
if st := ds.State(d, time.Date(2026, 11, 9, 23, 30, 0, 0, time.UTC)); st.State != StateRecorded {
t.Fatalf("after the last day in Berlin: %+v", st)
}
}
func TestValidate_B8(t *testing.T) {
ds := base(t)
next := *ds
next.Authority = []Authority{{DecisionType: ReleaseD02Update, Function: "r1.freigabestelle"}}
if _, err := Prepare(ds, &next, nil, Writer{Sub: ceo}, now); code(err) != "d02_reference_required" {
t.Fatalf("B8 without reference: %v", err)
}
next.Authority[0].D02Reference = "R1-D02 §16 Abs. 2"
got, err := Prepare(ds, &next, nil, Writer{Sub: ceo}, now)
if err != nil {
t.Fatal(err)
}
if f, src := got.DecidingFunction(ReleaseD02Update); f != "r1.freigabestelle" || src != "company" {
t.Fatalf("company's release body: %s %s", f, src)
}
if f, src := ds.DecidingFunction(ReleaseD02Update); f != Management || src != "default" {
t.Fatalf("default release body: %s %s", f, src)
}
next.Authority = []Authority{{DecisionType: "r1.escalation", Function: "r1.vorfallkontakt", D02Reference: "x"}}
if _, err := Prepare(ds, &next, nil, Writer{Sub: ceo}, now); code(err) != "invalid_role_dataset" {
t.Fatalf("d02_reference elsewhere: %v", err)
}
}
func TestValidate_catalogueRules(t *testing.T) {
cases := map[string]Assignment{
"unknown function": {ID: "a", Function: "r1.nope", Holder: member(staff)},
"scope on company": {ID: "a", Function: "r1.vorfallkontakt", Holder: member(staff), Scope: &Scope{Document: "r1_d01"}},
"document without": {ID: "a", Function: DocumentOwner, Holder: member(staff)},
"unknown document": {ID: "a", Function: DocumentOwner, Holder: member(staff), Scope: &Scope{Document: "r9"}},
"external management": {ID: "a", Function: Management, Holder: Holder{Kind: HolderExternal, Name: "X GmbH"}},
"member as provider": {ID: "a", Function: "r1.externer_cybersecurity_dienstleister", Holder: member(staff)},
"not applicable here": {ID: "a", Function: Management, Holder: Holder{Kind: HolderNotApplicable}},
"any member assigned": {ID: "a", Function: "r1.aenderungsinitiierung", Holder: member(staff)},
"member with a name": {ID: "a", Function: Management, Holder: Holder{Kind: HolderMember, Sub: staff, Name: "X"}},
"bad id": {ID: "a b", Function: Management, Holder: member(staff)},
"product without scope": {ID: "a", Function: "r1.produktverantwortung", Holder: member(staff)},
}
for name, a := range cases {
if err := Validate(&Dataset{Assignments: []Assignment{a}}); code(err) != "invalid_role_dataset" || err.Status != http.StatusBadRequest || err.Path == "" {
t.Errorf("%s: %v", name, err)
}
}
two := &Dataset{Assignments: []Assignment{
{ID: "a", Function: DocumentOwner, Holder: member(staff), Scope: &Scope{Document: "r1_d01"}},
{ID: "b", Function: DocumentOwner, Holder: member(ceo), Scope: &Scope{Document: "r1_d01"}},
}}
if err := Validate(two); code(err) != "invalid_role_dataset" {
t.Errorf("two document owners: %v", err)
}
two.Assignments[1].Scope = &Scope{Document: "r1_d02"}
if err := Validate(two); err != nil {
t.Errorf("one owner per document: %v", err)
}
ok := &Dataset{Assignments: []Assignment{
{ID: "a", Function: "r1.produktsicherheit", Holder: Holder{Kind: HolderNotApplicable, ModuleDecisionRef: "D-R1-01 vom 2.11."}},
{ID: "b", Function: "r1.externer_cybersecurity_dienstleister", Holder: Holder{Kind: HolderExternal, Name: "Dienstleister", Organisation: "X GmbH", Contact: "soc@x.test"}},
}}
if err := Validate(ok); err != nil {
t.Errorf("valid: %v", err)
}
}
func TestDecode_refusesUnknownKeys(t *testing.T) {
if _, err := Decode([]byte(`{"assignments":[],"owner":"me"}`)); code(err) != "invalid_role_dataset" {
t.Fatalf("unknown top key: %v", err)
}
if _, err := Decode([]byte(`{"assignments":[{"id":"a","function":"r1.geschaeftsleitung","holder":{"kind":"member","sub":"u","role":"x"},"scope":null}]}`)); code(err) != "invalid_role_dataset" {
t.Fatalf("unknown nested key: %v", err)
}
if _, err := Decode([]byte(`{}`)); err != nil {
t.Fatalf("empty: %v", err)
}
}
func TestResolve(t *testing.T) {
ds := base(t)
r, err := ds.Resolve("r1.escalation", Scope{}, now)
if err != nil || r.Status != StatusOpen || r.Function != "" {
t.Fatalf("not set: %+v %v", r, err)
}
r, err = ds.Resolve("r1.release_d01", Scope{}, now)
if err != nil || r.Status != StatusFilled || r.Source != "default" || len(r.Holders) != 1 || r.Holders[0].Sub != ceo {
t.Fatalf("default management: %+v %v", r, err)
}
byOwner, _ := Prepare(ds, withDeputy(ds, mgmtDeputy()), nil, Writer{Sub: owner}, now)
if r, _ := byOwner.Resolve("r1.release_d01", Scope{}, now); len(r.Deputies) != 0 {
t.Fatalf("a recorded (not effective) deputy is listed: %+v", r.Deputies)
}
auth, _ := Prepare(byOwner, byOwner, []string{"d-1"}, Writer{Sub: ceo}, now)
if r, _ := auth.Resolve("r1.release_d01", Scope{}, now); len(r.Deputies) != 1 || r.Deputies[0].State != StateEffective {
t.Fatalf("effective deputy: %+v", r.Deputies)
}
if r, _ := auth.Resolve("r1.release_d02", Scope{}, now); len(r.Deputies) != 0 {
t.Fatalf("deputy outside its decision types: %+v", r.Deputies)
}
next := *ds
next.Authority = []Authority{{DecisionType: "r1.new_version_needed", Function: DocumentOwner}}
set, _ := Prepare(ds, &next, nil, Writer{Sub: ceo}, now)
if _, err := set.Resolve("r1.new_version_needed", Scope{}, now); code(err) != "scope_required" {
t.Fatalf("document-scoped without document: %v", err)
}
r, err = set.Resolve("r1.new_version_needed", Scope{Document: "r1_d01"}, now)
if err != nil || r.Status != StatusFilled || r.Holders[0].Sub != owner {
t.Fatalf("owner of R1-D01: %+v %v", r, err)
}
if r, _ := set.Resolve("r1.new_version_needed", Scope{Document: "r1_d02"}, now); r.Status != StatusOpen {
t.Fatalf("R1-D02 has no owner: %+v", r)
}
if _, err := ds.Resolve("r1.nope", Scope{}, now); code(err) != "unknown_decision_type" {
t.Fatalf("unknown type: %v", err)
}
}
func TestChanges_namesIDsOnly(t *testing.T) {
ds := base(t)
got, _ := Prepare(ds, withDeputy(ds, mgmtDeputy()), nil, Writer{Sub: ceo}, now)
ch := Changes(ds, got)
b, _ := json.Marshal(ch)
if string(b) != `{"deputies":["d-1"]}` {
t.Fatalf("changes: %s", b)
}
if strings.Contains(string(b), staff) || strings.Contains(string(b), "Vertretung") {
t.Fatal("no personal data or texts in the changes")
}
}