Files
archie/internal/domain/storepkg/authority_test.go
T
sam ed35b826ad feat(store): record accepted package authority against the pin
An archie package declares its authority; nothing in the tree recorded the
operator's acceptance of it. This lands the acceptance-record slice of
docs/prds/store.md ("Authority"):

- storepkg.Authority gains Covers (the upper-bound predicate: equal or
  narrower applies, wider does not) and Validate (forge-permission
  vocabulary, no blank grants).
- Installed carries AcceptedAuthority (nil until accepted); install leaves it
  unset, so an unaccepted package has no authority.
- AcceptPackageAuthority on the storepkg Manager records the acceptance
  against the pinned digest and refuses grants the package does not declare
  (storepkg.ErrAuthorityNotDeclared), carried over the wire.
- staterpc gains the admin-only AcceptPackageAuthority RPC;
  InstalledPackage carries accepted_authority.
2026-09-30 07:40:06 +10:00

101 lines
3.2 KiB
Go

package storepkg
import "testing"
func TestAuthorityCovers(t *testing.T) {
tests := []struct {
name string
have Authority
want Authority
covers bool
}{
{
name: "equal authority covers itself",
have: Authority{ForgePermissions: []string{"read", "comment"}, Tools: []string{"shell"}},
want: Authority{ForgePermissions: []string{"comment", "read"}, Tools: []string{"shell"}},
covers: true,
},
{
name: "wider accepted authority covers the narrower request",
have: Authority{ForgePermissions: []string{"read", "comment", "review"}},
want: Authority{ForgePermissions: []string{"read"}},
covers: true,
},
{
name: "an update asking for a forge permission the record lacks is wider",
have: Authority{ForgePermissions: []string{"read"}},
want: Authority{ForgePermissions: []string{"read", "push"}},
covers: false,
},
{
name: "grants are never interchangeable across fields",
have: Authority{Tools: []string{"registry"}},
want: Authority{CredentialServices: []string{"registry"}},
covers: false,
},
{
name: "coverage is checked along every field",
have: Authority{
CredentialServices: []string{"github"},
EgressHosts: []string{"api.github.com"},
ForgePermissions: []string{"read"},
Triggers: []string{"repo/archie-core:pull_request"},
Tools: []string{"shell"},
},
want: Authority{
CredentialServices: []string{"github"},
EgressHosts: []string{"internal.invalid"},
},
covers: false,
},
{
name: "nothing accepted covers nothing requested",
have: Authority{},
want: Authority{},
covers: true,
},
{
name: "an empty accepted record covers any request at all",
have: Authority{},
want: Authority{Tools: []string{"shell"}},
covers: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.have.Covers(tt.want); got != tt.covers {
t.Fatalf("Covers = %v, want %v", got, tt.covers)
}
})
}
}
func TestAuthorityValidate(t *testing.T) {
tests := []struct {
name string
authority Authority
wantErr string
}{
{name: "valid authority", authority: Authority{ForgePermissions: []string{"read"}, Tools: []string{"shell"}}},
{name: "invalid forge permission", authority: Authority{ForgePermissions: []string{"admin"}}, wantErr: `invalid forge permission "admin"`},
{name: "blank credential service", authority: Authority{CredentialServices: []string{" "}}, wantErr: "credential service 0: name is required"},
{name: "blank egress host", authority: Authority{EgressHosts: []string{""}}, wantErr: "egress host 0: name is required"},
{name: "blank trigger", authority: Authority{Triggers: []string{""}}, wantErr: "trigger 0: name is required"},
{name: "blank tool", authority: Authority{Tools: []string{""}}, wantErr: "tool 0: name is required"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.authority.Validate()
if tt.wantErr == "" {
if err != nil {
t.Fatalf("Validate = %v, want nil", err)
}
return
}
if err == nil || err.Error() != tt.wantErr {
t.Fatalf("Validate = %v, want %q", err, tt.wantErr)
}
})
}
}