package proposals import ( "database/sql" "encoding/json/jsontext" "errors" "testing" "time" "github.com/google/uuid" "github.com/samcharles93/archie-core/modules/connectors" "github.com/samcharles93/archie-core/modules/judgement" "github.com/samcharles93/archie-core/modules/proposals/internal/store" ) // TestBindApprovesExactlyTheJudgedActions guards the security seam that // approving a proposal approves only the actions that were judged and shown: an // action changed after it was judged, one whose judgement is missing or // refuses it, or a proposal nobody approved must never yield an approval. func TestBindApprovesExactlyTheJudgedActions(t *testing.T) { input := jsontext.Value(`{"path":"/tmp/runner"}`) digest := connectors.Digest("clear", input) decision := func(level judgement.Level, digest string) judgement.Decision { return judgement.Decision{ID: uuid.New(), Level: level, Action: judgement.Action{Name: "clear", Digest: digest, Input: input}} } approved := store.Proposal{ State: StateApproved, AnsweredByID: sql.NullString{String: "person-1", Valid: true}, AnsweredAt: sql.NullTime{Time: time.Date(2026, 11, 24, 0, 0, 0, 0, time.UTC), Valid: true}, } action := func(d judgement.Decision, in jsontext.Value, digest string) store.Action { return store.Action{Seq: 0, Action: "clear", Input: in, Digest: digest, DecisionID: d.ID} } good := decision(judgement.ProposeAndWait, digest) other := decision(judgement.ProposeAndWait, digest) refused := decision(judgement.Never, digest) tests := []struct { name string proposal store.Proposal actions []store.Action decisions []judgement.Decision wantErr bool }{ {"an action as judged is approved", approved, []store.Action{action(good, input, digest)}, []judgement.Decision{good}, false}, { "an input changed after judging is not approved", approved, []store.Action{action(good, jsontext.Value(`{"path":"/"}`), digest)}, []judgement.Decision{good}, true, }, { "a digest that is not the judged one is not approved", approved, []store.Action{action(good, input, "forged")}, []judgement.Decision{good}, true, }, { "an action judged under another decision is not approved", approved, []store.Action{action(other, input, digest)}, []judgement.Decision{good}, true, }, { "an action judgement refuses is not approved", approved, []store.Action{action(refused, input, digest)}, []judgement.Decision{refused}, true, }, { "a proposal nobody approved gives no approval", store.Proposal{State: StatePending}, []store.Action{action(good, input, digest)}, []judgement.Decision{good}, true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := bind(tt.proposal, tt.actions, tt.decisions) if tt.wantErr { if !errors.Is(err, ErrNotBound) { t.Fatalf("err = %v, want ErrNotBound", err) } return } if err != nil { t.Fatal(err) } if len(got) != 1 || got[0].Approval.DecisionID != good.ID || got[0].Approval.Digest != digest || got[0].Approval.ByID != "person-1" { t.Fatalf("approval = %+v, want one covering decision %s with digest %s by person-1", got, good.ID, digest) } if !got[0].Approval.Covers(got[0].Decision) { t.Fatal("the approval does not cover its own decision") } }) } }