package approvals import ( "context" "encoding/json/jsontext" "errors" "strings" "testing" "github.com/google/uuid" "github.com/samcharles93/archie-core/modules/approvals/internal/store" "github.com/samcharles93/archie-core/modules/connectors" "github.com/samcharles93/archie-core/modules/governance" "github.com/samcharles93/archie-core/modules/judgement" ) // fakeConfirmer records the subject it was asked to confirm and answers with // err. type fakeConfirmer struct { err error subjects []string } func (f *fakeConfirmer) BeginConfirmation(context.Context, string) (governance.PasskeyCeremony, error) { return governance.PasskeyCeremony{}, nil } func (f *fakeConfirmer) Confirm(_ context.Context, _ string, _ []byte, subject string) error { f.subjects = append(f.subjects, subject) return f.err } // TestDashboardAnswerComesFromAPersonWhoMayGiveIt guards the dashboard answer // path: the signed-in caller is checked as a channel identity is, so the // bootstrap token or an account no person holds can't approve, and approving // an action that can't be undone also needs a passkey assertion that the // confirmer accepts for this request. func TestDashboardAnswerComesFromAPersonWhoMayGiveIt(t *testing.T) { people := fakePeople{ "account:owner": {ID: "owner", Name: "Owner", Approver: true}, "account:bystander": {ID: "bystander", Name: "Bystander"}, } signedIn := func(id string) *governance.Principal { return &governance.Principal{IdentityID: governance.IdentityID(id), Kind: governance.KindUser} } shared := governance.SharedTokenOwner() request := uuid.New() row := func(irreversible bool) store.Request { return store.Request{ID: request, Kind: KindAction, Level: judgement.ProposeAndWait.String(), Asked: `["owner"]`, Irreversible: irreversible} } assertion := &Assertion{Token: "t", Credential: jsontext.Value(`{}`)} tests := []struct { name string as *governance.Principal row store.Request approved bool assertion *Assertion confirmer *fakeConfirmer wantErr error }{ {name: "a person on the path may approve", as: signedIn("owner"), row: row(false), approved: true}, {name: "the bootstrap token is no person", as: &shared, row: row(false), approved: true, wantErr: ErrNotSignedIn}, {name: "a request with no one signed in is refused", as: nil, row: row(false), approved: true, wantErr: ErrNotSignedIn}, {name: "an account no person holds is refused", as: signedIn("stranger"), row: row(false), approved: true, wantErr: ErrUnverified}, {name: "a person who isn't an approver is refused", as: signedIn("bystander"), row: row(false), approved: true, wantErr: ErrNotApprover}, {name: "a person who isn't an approver may not deny either", as: signedIn("bystander"), row: row(false), approved: false, wantErr: ErrNotApprover}, {name: "an irreversible action needs an assertion", as: signedIn("owner"), row: row(true), approved: true, confirmer: &fakeConfirmer{}, wantErr: ErrNeedsFreshAuthentication}, {name: "an irreversible action with no confirmer can't be approved", as: signedIn("owner"), row: row(true), approved: true, assertion: assertion, wantErr: ErrNeedsFreshAuthentication}, {name: "a rejected assertion is refused", as: signedIn("owner"), row: row(true), approved: true, assertion: assertion, confirmer: &fakeConfirmer{err: governance.ErrCredentialRejected}, wantErr: ErrFreshAuthenticationFailed}, {name: "an accepted assertion approves", as: signedIn("owner"), row: row(true), approved: true, assertion: assertion, confirmer: &fakeConfirmer{}}, {name: "a denial needs no assertion", as: signedIn("owner"), row: row(true), approved: false, confirmer: &fakeConfirmer{}}, {name: "a person who isn't an approver can't approve with an assertion", as: signedIn("bystander"), row: row(true), approved: true, assertion: assertion, confirmer: &fakeConfirmer{}, wantErr: ErrNotApprover}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { m := &Module{people: people} if tt.confirmer != nil { m.UseConfirmer(tt.confirmer) } ctx := t.Context() if tt.as != nil { ctx = governance.WithPrincipal(ctx, *tt.as) } by, _, err := m.authorise(ctx, tt.row, tt.approved, tt.assertion) if !errors.Is(err, tt.wantErr) { t.Fatalf("err = %v, want %v", err, tt.wantErr) } if err == nil && !strings.Contains(by, "account:") { t.Fatalf("answered by %q, want the account's identity recorded", by) } if tt.confirmer != nil && len(tt.confirmer.subjects) == 1 && tt.confirmer.subjects[0] != confirmationSubject(request) { t.Fatalf("confirmed %q, want the assertion confirmed for this request", tt.confirmer.subjects[0]) } }) } } // TestIrreversibleActionsNeedFreshAuthentication guards the risk threshold: // anything that changes something and isn't declared undoable needs a passkey // assertion to approve, including an action that declares nothing. func TestIrreversibleActionsNeedFreshAuthentication(t *testing.T) { action := func(effect connectors.Effect, disposable bool) judgement.Action { return judgement.Action{Effect: effect, Target: connectors.Target{Disposable: disposable}} } tests := []struct { name string in judgement.Action want bool }{ {"an irreversible action", action(connectors.Effect{Reversibility: connectors.Irreversible}, false), true}, {"an action that declares no reversibility", action(connectors.Effect{}, false), true}, {"a recoverable action", action(connectors.Effect{Reversibility: connectors.Recoverable}, false), false}, {"an action undone automatically", action(connectors.Effect{Reversibility: connectors.UndoneAutomatically}, false), false}, {"a read-only action", action(connectors.Effect{ReadOnly: true}, false), false}, {"an irreversible action on a disposable target", action(connectors.Effect{Reversibility: connectors.Irreversible}, true), false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := irreversible(tt.in); got != tt.want { t.Fatalf("irreversible = %v, want %v", got, tt.want) } }) } }