| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "amadan.net/oficina/equipo/internal/equipo" |
| 7 | "amadan.net/oficina/equipo/internal/wire" |
| 8 | ) |
| 9 | |
| 10 | // researchDispatched is b's task whose Research was dispatched to b's |
| 11 | // runner (the grant's approver is a's person), with its dispatch. |
| 12 | func researchDispatched(t *testing.T, a, b *toolRig, key string) (string, *wire.DispatchCommand) { |
| 13 | t.Helper() |
| 14 | id := b.namedTask(key) |
| 15 | if res := b.research(key+"-r", id, "https://maps.example.org/place/shop"); res.IsError { |
| 16 | t.Fatalf("request %+v", res) |
| 17 | } |
| 18 | card := b.sourcesCard(wire.CardOpen) |
| 19 | if _, err := b.client.Answer(b.ctx, card.ID, wire.ChoiceAllow, card.Revision); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | b.begin(key+"-start", id) |
| 23 | d := b.nextDispatch() |
| 24 | if d.Specialist != wire.SpecialistResearch || d.TaskID != id { |
| 25 | t.Fatalf("dispatch %+v", d) |
| 26 | } |
| 27 | return id, d |
| 28 | } |
| 29 | |
| 30 | // TestResearchGrantCheckedAtStart (Astra security finding 3): the start |
| 31 | // authorization rechecks the grant the step was dispatched under; with |
| 32 | // its approver no longer an owner, the start is refused, the step ends |
| 33 | // research_no_grant and Engineering goes. |
| 34 | func TestResearchGrantCheckedAtStart(t *testing.T) { |
| 35 | a := newToolRigAt(t, wire.ProtocolCurrent) |
| 36 | a.researchOn() |
| 37 | a.inventoryOn() |
| 38 | b := personToolRigAt(t, a, "second@example.com", equipo.RoleOwner, wire.ProtocolCurrent) |
| 39 | b.inventoryOn() |
| 40 | id, d := researchDispatched(t, a, b, "gs") |
| 41 | var revision int64 |
| 42 | if err := a.inst.DB.Writer().QueryRow("SELECT revision FROM members WHERE address = ?", Owner).Scan(&revision); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | if err := a.inst.Store.SetMember(a.ctx, equipo.Actor{Address: "second@example.com"}, Owner, equipo.RoleMember, true, revision); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | if refused := b.lease(b.runner, "gs-start", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt}); refused == "" { |
| 49 | t.Fatal("started after the approver stopped being an owner") |
| 50 | } |
| 51 | if s := b.newest(id, wire.SpecialistResearch); s.State != wire.StepCancelled || s.Reason != wire.ReasonResearchNoGrant { |
| 52 | t.Fatalf("Research after a refused start %+v", s) |
| 53 | } |
| 54 | if e := b.nextDispatch(); e.Specialist != wire.SpecialistEngineering || e.TaskID != id { |
| 55 | t.Fatalf("Engineering not released %+v", e) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // TestResearchRevokeCancelsStartedSteps (Astra security finding 3): |
| 60 | // revocation cancels every unfinished Research step. One authorized and |
| 61 | // deferred on its runner (started, not running) ends cancelled at once |
| 62 | // and its runner is asked to cancel it; one running gets the ordinary |
| 63 | // cancel, and its start is refused from then on. |
| 64 | func TestResearchRevokeCancelsStartedSteps(t *testing.T) { |
| 65 | a := newToolRigAt(t, wire.ProtocolCurrent) |
| 66 | a.researchOn() |
| 67 | a.inventoryOn() |
| 68 | b := personToolRigAt(t, a, "second@example.com", equipo.RoleOwner, wire.ProtocolCurrent) |
| 69 | b.inventoryOn() |
| 70 | deferred, d1 := researchDispatched(t, a, b, "rd") |
| 71 | if refused := b.lease(b.runner, "rd-start", wire.LeaseCheck{StepID: d1.StepID, Attempt: d1.Attempt}); refused != "" { |
| 72 | t.Fatalf("start refused %q", refused) |
| 73 | } |
| 74 | running, d2 := researchDispatched(t, a, b, "rr") |
| 75 | if refused := b.lease(b.runner, "rr-start", wire.LeaseCheck{StepID: d2.StepID, Attempt: d2.Attempt}); refused != "" { |
| 76 | t.Fatalf("start refused %q", refused) |
| 77 | } |
| 78 | b.step("rr-run", wire.StepReport{TaskID: d2.TaskID, StepID: d2.StepID, Attempt: d2.Attempt, State: wire.StepRunning}) |
| 79 | list, err := a.client.Specialists(a.ctx) |
| 80 | if err != nil || len(list.Items) != 1 { |
| 81 | t.Fatalf("%+v %v", list, err) |
| 82 | } |
| 83 | if _, err := a.client.RevokeSpecialist(a.ctx, list.Items[0].ID, list.Items[0].Revision); err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | if s := b.newest(deferred, wire.SpecialistResearch); s.State != wire.StepCancelled || s.Reason != wire.ReasonResearchNoGrant || !s.Terminated { |
| 87 | t.Fatalf("deferred Research after revoke %+v", s) |
| 88 | } |
| 89 | for _, d := range []*wire.DispatchCommand{d1, d2} { |
| 90 | if n := b.commandsLike(wire.FrameCancelStep, `"step_id":"`+d.StepID+`"`); n != 1 { |
| 91 | t.Fatalf("%d cancels for %s", n, d.StepID) |
| 92 | } |
| 93 | } |
| 94 | if s := b.newest(running, wire.SpecialistResearch); s.State != wire.StepRunning { |
| 95 | t.Fatalf("running Research after revoke %+v", s) |
| 96 | } |
| 97 | if refused := b.lease(b.runner, "rr-again", wire.LeaseCheck{StepID: d2.StepID, Attempt: d2.Attempt}); refused == "" { |
| 98 | t.Fatal("a running step's start authorized after revoke") |
| 99 | } |
| 100 | if s := b.newest(running, wire.SpecialistResearch); s.State != wire.StepCancelled || s.Reason != wire.ReasonResearchNoGrant { |
| 101 | t.Fatalf("running Research after a refused lease %+v", s) |
| 102 | } |
| 103 | } |
| 104 | |