package chat import ( "context" "encoding/json" "encoding/json/jsontext" "errors" "log/slog" "strings" "testing" "time" "github.com/google/uuid" "github.com/samcharles93/archie-core/internal/platform/hub" "github.com/samcharles93/archie-core/modules/approvals" "github.com/samcharles93/archie-core/modules/chat/internal/store" "github.com/samcharles93/archie-core/modules/connectors" "github.com/samcharles93/archie-core/modules/deliveries" "github.com/samcharles93/archie-core/modules/governance" "github.com/samcharles93/archie-core/modules/judgement" ) var ( bindTurn = uuid.MustParse("11111111-1111-1111-1111-111111111111") bindSession = uuid.MustParse("22222222-2222-2222-2222-222222222222") bindAction = uuid.MustParse("33333333-3333-3333-3333-333333333333") bindRequest = uuid.MustParse("44444444-4444-4444-4444-444444444444") bindExpiry = time.Date(2100, 1, 1, 0, 0, 0, 0, time.UTC) ) func baseBinding() Binding { return Binding{ ID: bindAction, Org: "default", Session: bindSession, Turn: bindTurn, Owner: alice, Request: bindRequest, Connector: "mail", Action: "send", Input: connectors.Canonical(jsontext.Value(`{"to":"a@example.com","body":"hi"}`)), Target: connectors.Target{Kind: "mailbox", ID: "inbox", Environment: "prod"}, Effect: connectors.Effect{Reversibility: connectors.Irreversible, Summary: "sends mail"}, Approvers: []string{alice}, Expires: bindExpiry, } } // TestBindingDigestCoversTheWholeAction guards the time-of-check to // time-of-use gap: an approval is bound to everything that decides what runs // and for whom, so a change to any part gives a different digest, a respelling // of the same arguments does not, and a binding with a part missing has no // digest at all. func TestBindingDigestCoversTheWholeAction(t *testing.T) { base, err := baseBinding().Digest() if err != nil { t.Fatal(err) } other := uuid.MustParse("99999999-9999-9999-9999-999999999999") changes := []struct { name string change func(*Binding) }{ {"the action's id", func(b *Binding) { b.ID = other }}, {"the organisation", func(b *Binding) { b.Org = "other" }}, {"the conversation", func(b *Binding) { b.Session = other }}, {"the turn", func(b *Binding) { b.Turn = other }}, {"the owner", func(b *Binding) { b.Owner = bob }}, {"the approval request", func(b *Binding) { b.Request = other }}, {"the connector", func(b *Binding) { b.Connector = "chat" }}, {"the action", func(b *Binding) { b.Action = "forward" }}, {"the arguments", func(b *Binding) { b.Input = jsontext.Value(`{"to":"b@example.com","body":"hi"}`) }}, {"the target's id", func(b *Binding) { b.Target.ID = "outbox" }}, {"a target removed", func(b *Binding) { b.Target = connectors.Target{} }}, {"the target's environment", func(b *Binding) { b.Target.Environment = "dev" }}, {"the effect", func(b *Binding) { b.Effect.Reversibility = connectors.Recoverable }}, {"the approvers", func(b *Binding) { b.Approvers = []string{bob} }}, {"the expiry", func(b *Binding) { b.Expires = bindExpiry.Add(time.Hour) }}, } for _, tt := range changes { t.Run("a change to "+tt.name+" changes the digest", func(t *testing.T) { b := baseBinding() tt.change(&b) got, err := b.Digest() if err != nil { t.Fatal(err) } if got == base { t.Fatalf("digest is unchanged by a change to %s", tt.name) } }) } t.Run("respelled arguments keep the digest", func(t *testing.T) { b := baseBinding() b.Input = connectors.Canonical(jsontext.Value(`{ "body": "hi", "to": "a@example.com" }`)) got, err := b.Digest() if err != nil || got != base { t.Fatalf("digest = %q, %v; want %q", got, err, base) } }) missing := []struct { name string remove func(*Binding) }{ {"id", func(b *Binding) { b.ID = uuid.Nil }}, {"organisation", func(b *Binding) { b.Org = "" }}, {"conversation", func(b *Binding) { b.Session = uuid.Nil }}, {"turn", func(b *Binding) { b.Turn = uuid.Nil }}, {"owner", func(b *Binding) { b.Owner = "" }}, {"approval request", func(b *Binding) { b.Request = uuid.Nil }}, {"connector", func(b *Binding) { b.Connector = "" }}, {"action", func(b *Binding) { b.Action = "" }}, {"arguments", func(b *Binding) { b.Input = nil }}, {"approvers", func(b *Binding) { b.Approvers = nil }}, {"expiry", func(b *Binding) { b.Expires = time.Time{} }}, } for _, tt := range missing { t.Run("a missing "+tt.name+" has no digest", func(t *testing.T) { b := baseBinding() tt.remove(&b) if got, err := b.Digest(); !errors.Is(err, errUnbound) || got != "" { t.Fatalf("digest = %q, %v; want errUnbound", got, err) } }) } } // approvalFixture is one approved chat action waiting to run, with the judge, // approver and store fakes that record what happens to it. type approvalFixture struct { m *Module d Deps action store.Action judge *runJudge approver *runApprover store *runStore } type runJudge struct { Judge fresh judgement.Decision assessed int executed []string } func (j *runJudge) Assess(context.Context, judgement.Request) (judgement.Decision, error) { j.assessed++ return j.fresh, nil } func (j *runJudge) Execute(_ context.Context, _ judgement.Decision, _ jsontext.Value, _ string) (string, error) { j.executed = append(j.executed, "execute") return "ran", nil } func (j *runJudge) ExecuteApproved(_ context.Context, _ judgement.Decision, _ judgement.Approval, _ jsontext.Value, _ string) (string, error) { j.executed = append(j.executed, "execute approved") return "ran", nil } type runApprover struct { Approver discarded []string accepted int } func (a *runApprover) Discard(_ context.Context, _ uuid.UUID, because string) error { a.discarded = append(a.discarded, because) return nil } func (a *runApprover) Accept(context.Context, uuid.UUID) error { a.accepted++; return nil } type runSettings struct { Settings path []governance.PathStep err error } func (s runSettings) EscalationPath(context.Context, string, bool) ([]governance.PathStep, error) { return s.path, s.err } type runNotifier struct{} func (runNotifier) Notify(context.Context, deliveries.Notification) error { return nil } // runStore moves an action only from the state it is in, as the SQL does. type runStore struct { *fakeStore state string moves []string } func (s *runStore) Move(_ context.Context, _ uuid.UUID, from, to, _ string) (bool, error) { if s.state != from { return false, nil } s.state = to s.moves = append(s.moves, to) return true, nil } func heldDecision() judgement.Decision { input := jsontext.Value(`{"to":"a@example.com","body":"hi"}`) return judgement.Decision{ ID: uuid.New(), Key: "k", Level: judgement.ProposeAndWait, Action: judgement.Action{ Connector: "mail", Name: "send", Input: input, Digest: connectors.Digest("send", input), Target: connectors.Target{Kind: "mailbox", ID: "inbox", Environment: "prod"}, Effect: connectors.Effect{Reversibility: connectors.Irreversible, Summary: "sends mail"}, }, } } func newApprovalFixture(t *testing.T) *approvalFixture { t.Helper() held := heldDecision() binding := baseBinding() digest, err := binding.Digest() if err != nil { t.Fatal(err) } rawBinding, _ := json.Marshal(binding) rawDecision, _ := json.Marshal(held) rawAnswer, _ := json.Marshal(approvals.Answer{RequestID: bindRequest, Approved: true, By: "alice", At: bindExpiry.Add(-time.Hour)}) f := &approvalFixture{ action: store.Action{ ID: bindAction, TurnID: bindTurn, Owner: alice, Key: "approval:k", Decision: rawDecision, RequestID: bindRequest, State: actionApproved, Answer: rawAnswer, Binding: rawBinding, BindingDigest: digest, }, judge: &runJudge{fresh: held}, approver: &runApprover{}, } f.store = &runStore{fakeStore: &fakeStore{}, state: actionApproved} f.m = &Module{orgID: "default", store: f.store, log: slog.New(slog.DiscardHandler), hub: hub.New()} f.d = Deps{ People: fakePeople{alice: person(alice, governance.RoleAdmin)}, Access: governance.NewEngine(governance.ShippedOrgPolicies(governance.DefaultOrgID)), Settings: runSettings{path: []governance.PathStep{{Account: alice}}}, Judge: f.judge, Approver: f.approver, Notifier: runNotifier{}, } return f } // TestRunApprovedRunsOnlyWhatWasApprovedAndOnlyOnce guards the dispatch-time // recheck: an approval is discarded, and nothing runs, when the owner has lost // access, when the target, effect, arguments or approvers changed, when the // binding is missing, altered or expired, or when the answer belongs to // another request. An action that still matches runs exactly once. func TestRunApprovedRunsOnlyWhatWasApprovedAndOnlyOnce(t *testing.T) { tests := []struct { name string change func(*approvalFixture) wantRuns int wantDiscard string }{ {name: "an action that still matches runs", wantRuns: 1}, { name: "the owner's access revoked while pending", change: func(f *approvalFixture) { f.d.People = fakePeople{alice: person(alice, governance.RoleViewer)} }, wantDiscard: "you may not run connector mail", }, { name: "the target changed while pending", change: func(f *approvalFixture) { f.judge.fresh.Action.Target = connectors.Target{Kind: "mailbox", ID: "outbox", Environment: "prod"} }, wantDiscard: "its target changed", }, { name: "a target appeared where there was none", change: func(f *approvalFixture) { b := baseBinding() b.Target = connectors.Target{} f.action.Binding, _ = json.Marshal(b) f.action.BindingDigest, _ = b.Digest() }, wantDiscard: "its target changed", }, { name: "the target changed and would now run unattended", change: func(f *approvalFixture) { f.judge.fresh.Action.Target.ID = "outbox" f.judge.fresh.Level = judgement.ActQuietly }, wantDiscard: "its target changed", }, { name: "the effect changed while pending", change: func(f *approvalFixture) { f.judge.fresh.Action.Effect.Summary = "deletes the mailbox" }, wantDiscard: "its effect changed", }, { name: "the approvers changed while pending", change: func(f *approvalFixture) { f.d.Settings = runSettings{path: []governance.PathStep{{Account: bob}}} }, wantDiscard: "the people who may approve it changed", }, { name: "the action is now refused", change: func(f *approvalFixture) { f.judge.fresh.Level, f.judge.fresh.Reason = judgement.Never, "blackout" }, wantDiscard: "it is now refused", }, { name: "judgement is now stricter", change: func(f *approvalFixture) { f.judge.fresh.Level = judgement.Escalate }, wantDiscard: "judgement is stricter", }, { name: "the stored binding was altered", change: func(f *approvalFixture) { f.action.BindingDigest = strings.Repeat("0", 64) }, wantDiscard: "binding is incomplete or was altered", }, { name: "an action held before bindings existed", change: func(f *approvalFixture) { f.action.Binding, f.action.BindingDigest = nil, "" }, wantDiscard: "carries no binding", }, { name: "the answer belongs to another request", change: func(f *approvalFixture) { f.action.Answer, _ = json.Marshal(approvals.Answer{RequestID: uuid.New(), Approved: true}) }, wantDiscard: "another request", }, { name: "the binding expired", change: func(f *approvalFixture) { b := baseBinding() b.Expires = time.Now().Add(-time.Minute) f.action.Binding, _ = json.Marshal(b) f.action.BindingDigest, _ = b.Digest() }, wantDiscard: "it expired", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { f := newApprovalFixture(t) if tt.change != nil { tt.change(f) } if err := f.m.runApproved(t.Context(), f.d, f.action); err != nil { t.Fatal(err) } if got := len(f.judge.executed); got != tt.wantRuns { t.Fatalf("ran %d times (%v), want %d", got, f.judge.executed, tt.wantRuns) } switch { case tt.wantDiscard == "" && len(f.approver.discarded) != 0: t.Fatalf("discarded %v, want the approval kept", f.approver.discarded) case tt.wantDiscard != "" && (len(f.approver.discarded) != 1 || !strings.Contains(f.approver.discarded[0], tt.wantDiscard)): t.Fatalf("discarded %v, want one containing %q", f.approver.discarded, tt.wantDiscard) } }) } t.Run("a second pass over the same action runs nothing", func(t *testing.T) { f := newApprovalFixture(t) for range 2 { if err := f.m.runApproved(t.Context(), f.d, f.action); err != nil { t.Fatal(err) } } if len(f.judge.executed) != 1 { t.Fatalf("ran %d times, want once", len(f.judge.executed)) } }) t.Run("who may approve can't be read", func(t *testing.T) { f := newApprovalFixture(t) f.d.Settings = runSettings{err: errors.New("settings unavailable")} if err := f.m.runApproved(t.Context(), f.d, f.action); err != nil { t.Fatal(err) } if len(f.judge.executed) != 0 || len(f.approver.discarded) != 0 { t.Fatalf("ran %v and discarded %v, want nothing to run and the approval kept", f.judge.executed, f.approver.discarded) } }) }