Files
archie/modules/approvals/dashboard_test.go
T

126 lines
6.0 KiB
Go

package approvals
import (
"context"
"encoding/json/jsontext"
"errors"
"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
}
// TestDashboardAnswerComesFromAnAccountWhoseRoleMayApprove guards the
// dashboard answer path: the signed-in caller is judged by role as a channel
// account is, so the bootstrap token or a member without approve can't answer,
// and approving an action that can't be undone also needs a passkey assertion
// that the confirmer accepts for this request.
func TestDashboardAnswerComesFromAnAccountWhoseRoleMayApprove(t *testing.T) {
signedIn := func(id string) *governance.Principal {
p, _ := newAccounts().PrincipalFor(context.Background(), governance.IdentityID(id))
p.Kind = governance.KindUser
return &p
}
shared := governance.SharedTokenOwner()
bot := &governance.Principal{IdentityID: "admin", Kind: governance.KindBot}
request := uuid.New()
row := func(irreversible bool) store.Request {
return store.Request{ID: request, Kind: KindAction, Level: judgement.ProposeAndWait.String(), 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: "an admin may approve", as: signedIn("admin"), row: row(false), approved: true},
{name: "an owner 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 bot is no person", as: bot, 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: "a developer is refused", as: signedIn("developer"), row: row(false), approved: true, wantErr: ErrNotApprover},
{name: "a viewer is refused", as: signedIn("viewer"), row: row(false), approved: true, wantErr: ErrNotApprover},
{name: "a developer may not deny either", as: signedIn("developer"), row: row(false), approved: false, wantErr: ErrNotApprover},
{name: "an irreversible action needs an assertion", as: signedIn("admin"), row: row(true), approved: true, confirmer: &fakeConfirmer{}, wantErr: ErrNeedsFreshAuthentication},
{name: "an irreversible action with no confirmer can't be approved", as: signedIn("admin"), row: row(true), approved: true, assertion: assertion, wantErr: ErrNeedsFreshAuthentication},
{name: "a rejected assertion is refused", as: signedIn("admin"), row: row(true), approved: true, assertion: assertion, confirmer: &fakeConfirmer{err: governance.ErrCredentialRejected}, wantErr: ErrFreshAuthenticationFailed},
{name: "an accepted assertion approves", as: signedIn("admin"), row: row(true), approved: true, assertion: assertion, confirmer: &fakeConfirmer{}},
{name: "a denial needs no assertion", as: signedIn("admin"), row: row(true), approved: false, confirmer: &fakeConfirmer{}},
{name: "a developer can't approve with an assertion", as: signedIn("developer"), 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{accounts: newAccounts(), authz: shipped()}
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 && by != string(tt.as.IdentityID) {
t.Fatalf("answered by %q, want the account's name 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)
}
})
}
}