package connectors import ( "slices" "testing" "github.com/google/uuid" "github.com/samcharles93/archie-core/modules/connectors/internal/store" ) // TestInstallationResolutionRespectsScope guards the installation boundary: a // name resolves to the person's installation, then their organisation's, then // the instance's; an override hides only what the scope that set it inherits; // and an installation ID is usable only by a scope that may see it. func TestInstallationResolutionRespectsScope(t *testing.T) { instance := store.Installed{ID: uuid.New(), Scope: store.ScopeInstance, Name: "pager", Enabled: true} instanceOnly := store.Installed{ID: uuid.New(), Scope: store.ScopeInstance, Name: "ledger", Enabled: true} orgOwn := store.Installed{ID: uuid.New(), Scope: store.ScopeOrg, OrgID: "own", Name: "pager", Enabled: true} orgOther := store.Installed{ID: uuid.New(), Scope: store.ScopeOrg, OrgID: "other", Name: "pager", Enabled: true} personOwn := store.Installed{ID: uuid.New(), Scope: store.ScopePerson, PersonID: "me", Name: "pager", Enabled: true} personOff := store.Installed{ID: uuid.New(), Scope: store.ScopePerson, PersonID: "me", Name: "off", Enabled: false} ix := newIndex( []store.Installed{instance, instanceOnly, orgOwn, orgOther, personOwn, personOff}, []store.Override{ {InstallationID: instance.ID, Scope: store.ScopeOrg, OrgID: "own"}, {InstallationID: instanceOnly.ID, Scope: store.ScopeOrg, OrgID: "own"}, {InstallationID: orgOwn.ID, Scope: store.ScopePerson, PersonID: "me"}, }, ) me := Scope{OrgID: "own", PersonID: "me"} org := Scope{OrgID: "own"} other := Scope{OrgID: "other", PersonID: "them"} third := Scope{OrgID: "third", PersonID: "someone"} tests := []struct { name string scope Scope ref string want uuid.UUID wantOK bool visible int }{ {name: "the person's own installation wins over the organisation's and the instance's", scope: me, ref: "pager", want: personOwn.ID, wantOK: true, visible: 1}, {name: "an organisation sees only its own installation once it has switched the instance's off", scope: org, ref: "pager", want: orgOwn.ID, wantOK: true, visible: 1}, {name: "another organisation never reaches the first organisation's installation", scope: other, ref: "pager", want: orgOther.ID, wantOK: true, visible: 2}, {name: "a scope with no installation of its own inherits the instance's", scope: third, ref: "pager", want: instance.ID, wantOK: true, visible: 2}, {name: "an organisation's override hides an instance installation it would inherit", scope: org, ref: "ledger", wantOK: false, visible: 1}, {name: "a scope without the override still reaches the instance installation", scope: third, ref: "ledger", want: instanceOnly.ID, wantOK: true, visible: 2}, {name: "an explicit installation ID wins for a scope that may use it", scope: third, ref: instance.ID.String(), want: instance.ID, wantOK: true}, {name: "an explicit ID does not reach another organisation's installation", scope: other, ref: orgOwn.ID.String(), wantOK: false}, {name: "an explicit ID does not reach an installation the scope switched off", scope: me, ref: orgOwn.ID.String(), wantOK: false}, {name: "a disabled installation never resolves", scope: me, ref: "off", wantOK: false, visible: 1}, {name: "an unknown name resolves to nothing", scope: me, ref: "missing", wantOK: false, visible: 1}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, ok := ix.resolve(tt.scope, tt.ref) if ok != tt.wantOK { t.Fatalf("resolve(%q) ok = %v, want %v", tt.ref, ok, tt.wantOK) } if ok && got.id != tt.want.String() { t.Errorf("resolve(%q) = %s, want %s", tt.ref, got.id, tt.want) } if tt.visible != 0 { if got := ix.visible(tt.scope); len(got) != tt.visible { t.Errorf("visible() has %d names, want %d", len(got), tt.visible) } } }) } } // TestOverridesNameTheScopeThatSetThem guards the restore action: a caller is // told which of its own scopes switched an inherited installation off, so it // restores that one rather than a scope that never hid it. func TestOverridesNameTheScopeThatSetThem(t *testing.T) { instance := store.Installed{ID: uuid.New(), Scope: store.ScopeInstance, Name: "pager", Enabled: true} orgOwn := store.Installed{ID: uuid.New(), Scope: store.ScopeOrg, OrgID: "own", Name: "pager", Enabled: true} ix := newIndex( []store.Installed{instance, orgOwn}, []store.Override{ {InstallationID: instance.ID, Scope: store.ScopeOrg, OrgID: "own"}, {InstallationID: instance.ID, Scope: store.ScopePerson, PersonID: "me"}, }, ) me := Scope{OrgID: "own", PersonID: "me"} tests := []struct { name string ref string want []string }{ {name: "an instance installation hidden by the organisation and the person names both, person first", ref: instance.ID.String(), want: []string{store.ScopePerson, store.ScopeOrg}}, {name: "an installation with no override names none", ref: orgOwn.ID.String(), want: nil}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { mem, ok := ix.members[tt.ref] if !ok { t.Fatalf("no installation %s", tt.ref) } got := ix.overriddenBy(me, mem) if !slices.Equal(got, tt.want) { t.Errorf("overriddenBy() = %v, want %v", got, tt.want) } }) } } // TestInheritedGuardStillJudges checks that an unavailable enforcing guard // still applies despite a saved override and a same-named personal guard. func TestInheritedGuardStillJudges(t *testing.T) { instance := store.Installed{ID: uuid.New(), Scope: store.ScopeInstance, Name: "hours", Contract: store.ContractGuard, Enabled: true} personal := store.Installed{ID: uuid.New(), Scope: store.ScopePerson, PersonID: "me", Name: "hours", Contract: store.ContractGuard, Enabled: true} m := &Module{ idx: newIndex([]store.Installed{instance, personal}, []store.Override{ {InstallationID: instance.ID, Scope: store.ScopePerson, PersonID: "me"}, }), guards: map[string]*Guard{personal.ID.String(): {name: "hours", advisory: true}}, } ctx := WithScope(t.Context(), Scope{PersonID: "me"}) guards := m.Guards(ctx) for _, g := range guards { if !g.Advisory() { if _, err := g.Judge(ctx, GuardRequest{}); err == nil { t.Fatal("an unavailable guard returned a successful verdict") } return } } t.Fatalf("the inherited guard disappeared from %d verdicts", len(guards)) } // TestCredentialResolutionRespectsScope guards the credential boundary: a name // resolves to the person's credential, then their organisation's, then the // instance's, and never to another organisation's or person's. func TestCredentialResolutionRespectsScope(t *testing.T) { instance := store.Credential{Scope: store.ScopeInstance, Name: "token", Kind: "bearer"} orgOwn := store.Credential{Scope: store.ScopeOrg, OrgID: "own", Name: "token", Kind: "bearer"} orgOnly := store.Credential{Scope: store.ScopeOrg, OrgID: "own", Name: "own-only", Kind: "bearer"} orgOther := store.Credential{Scope: store.ScopeOrg, OrgID: "other", Name: "token", Kind: "bearer"} personOwn := store.Credential{Scope: store.ScopePerson, PersonID: "me", Name: "token", Kind: "bearer"} creds := []store.Credential{instance, orgOwn, orgOnly, orgOther, personOwn} tests := []struct { name string scope Scope ref string want store.Credential wantOK bool }{ {name: "the person's own credential wins", scope: Scope{OrgID: "own", PersonID: "me"}, ref: "token", want: personOwn, wantOK: true}, {name: "an organisation reaches its own credential", scope: Scope{OrgID: "own"}, ref: "token", want: orgOwn, wantOK: true}, {name: "another organisation reaches only its own", scope: Scope{OrgID: "other", PersonID: "them"}, ref: "token", want: orgOther, wantOK: true}, {name: "a scope with no credential of its own inherits the instance's", scope: Scope{OrgID: "third", PersonID: "someone"}, ref: "token", want: instance, wantOK: true}, {name: "another organisation never reaches the first organisation's credential", scope: Scope{OrgID: "other", PersonID: "them"}, ref: "own-only", wantOK: false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, ok, err := pickCredential(creds, tt.scope, tt.ref) if err != nil { t.Fatal(err) } if ok != tt.wantOK { t.Fatalf("pickCredential(%q) ok = %v, want %v", tt.ref, ok, tt.wantOK) } if ok && got.Owner() != tt.want.Owner() { t.Errorf("pickCredential(%q) owner = %+v, want %+v", tt.ref, got.Owner(), tt.want.Owner()) } }) } } // TestCredentialUseStaysAtItsScope guards which installations a stored // credential may restart: the instance's reach every installation, and an // organisation's or a person's reach only their own. func TestCredentialUseStaysAtItsScope(t *testing.T) { instance := store.Owner{Scope: store.ScopeInstance} org := store.Owner{Scope: store.ScopeOrg, OrgID: "own"} person := store.Owner{Scope: store.ScopePerson, PersonID: "me"} tests := []struct { name string installation, cred store.Owner want bool }{ {name: "an instance credential reaches an instance installation", installation: instance, cred: instance, want: true}, {name: "an instance credential reaches an organisation installation", installation: org, cred: instance, want: true}, {name: "an instance credential reaches a person installation", installation: person, cred: instance, want: true}, {name: "an organisation credential reaches its own installation", installation: org, cred: org, want: true}, {name: "an organisation credential does not reach another organisation", installation: store.Owner{Scope: store.ScopeOrg, OrgID: "other"}, cred: org, want: false}, {name: "a person credential reaches that person's installation", installation: person, cred: person, want: true}, {name: "a person credential does not reach another person", installation: store.Owner{Scope: store.ScopePerson, PersonID: "them"}, cred: person, want: false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := uses(tt.installation, tt.cred); got != tt.want { t.Errorf("uses(%+v, %+v) = %v, want %v", tt.installation, tt.cred, got, tt.want) } }) } }