| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "image" |
| 8 | "image/png" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/equipo" |
| 13 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 14 | "amadan.net/oficina/equipo/internal/taskplan" |
| 15 | ) |
| 16 | |
| 17 | func TestRenderedChangeApprovalAuthority(t *testing.T) { |
| 18 | for _, guard := range []string{"valid", "requester", "team_owner", "member", "revision", "ownership", "removed", "audit", "project_revision"} { |
| 19 | t.Run(guard, func(t *testing.T) { |
| 20 | f := newFixture(t) |
| 21 | decider := equipo.Actor{Address: "copy@example.com"} |
| 22 | must(t, f.Store.SetMember(ctx, owner, decider.Address, equipo.RoleMember, true, 0)) |
| 23 | p := proposed(t, f) |
| 24 | x, task, scope := f.planCore(t, p) |
| 25 | |
| 26 | must(t, f.DB.G.Create(&equipo.DecisionOwner{ProjectID: p.ProjectID, Decision: "copy", Address: decider.Address}).Error) |
| 27 | job, err := x.Core.GetJob(ctx, scope) |
| 28 | must(t, err) |
| 29 | job.Evidence.Predicate = team.PredicateRenderedChange |
| 30 | job.Evidence.RequireBugFreeze = false |
| 31 | job.Evidence.RequiredDecisions = []string{"copy"} |
| 32 | encode := func(v any) []byte { b, e := json.Marshal(v); must(t, e); return b } |
| 33 | f.exec(t, "UPDATE jobs SET evidence=? WHERE id=?", string(encode(job.Evidence)), job.ID) |
| 34 | f.exec(t, "UPDATE tasks SET kind=? WHERE id=?", taskplan.RenderedChange, task.ID) |
| 35 | h, err := x.Core.Claim(ctx, scope, "reproduce", "runner", "workspace") |
| 36 | must(t, err) |
| 37 | f.exec(t, "UPDATE attempts SET state='succeeded',containment='contained' WHERE id=?", h.Attempt) |
| 38 | cand := team.GitCandidate{Repository: job.Target.Repository, BaseCommit: job.Target.BaseRevision, Commit: strings.Repeat("d", 40), Tree: strings.Repeat("e", 40)} |
| 39 | raw, hash, err := team.CanonicalJSON(encode(cand)) |
| 40 | must(t, err) |
| 41 | diff := encode([]taskplan.RenderDiffFile{{Path: "copy.txt", Before: []byte("old words"), After: []byte("new words")}}) |
| 42 | var pixels bytes.Buffer |
| 43 | must(t, png.Encode(&pixels, image.NewRGBA(image.Rect(0, 0, 1, 1)))) |
| 44 | render := team.RenderResult{Repository: cand.Repository, BaseCommit: cand.BaseCommit, CandidateHash: hash, DiffHash: team.ContentDigest(diff), RenderHash: team.ContentDigest(pixels.Bytes()), RecipeHash: strings.Repeat("b", 64), EnvironmentHash: strings.Repeat("c", 64)} |
| 45 | for _, blob := range [][]byte{diff, pixels.Bytes()} { |
| 46 | f.exec(t, "INSERT INTO artifact_blobs(team_id,job_id,digest,bytes) VALUES(?,?,?,?)", scope.Account, scope.Job, team.ContentDigest(blob), blob) |
| 47 | } |
| 48 | |
| 49 | for role, body := range map[team.Role][]byte{team.RoleCandidate: raw, team.RoleRender: encode(render)} { |
| 50 | id := int64(1) |
| 51 | if role == team.RoleRender { |
| 52 | id = 2 |
| 53 | } |
| 54 | f.exec(t, "INSERT INTO artifacts(id,job_id,step_id,attempt_id,fence,role,hash,media_type,size,provenance,payload,created_at) VALUES(?,?,1,?,?,?,?,?,?,?,?,'2026-11-03T00:00:00Z')", id, job.ID, h.Attempt, h.Fence, role, team.ContentDigest(body), team.GitCandidateMediaType, len(body), `{"producer":"fixture","provider":"fixture","context":"fixture"}`, body) |
| 55 | } |
| 56 | if guard == "valid" { |
| 57 | w := f.get(t, owner, "/tasks/"+task.ID+"/rendered-change") |
| 58 | if w.Code != 200 || !strings.Contains(w.Body.String(), "old words") || !strings.Contains(w.Body.String(), "new words") || !strings.Contains(w.Body.String(), render.RenderHash) { |
| 59 | t.Fatal("shared pending approval missing diff/render", w.Code, w.Body.String()) |
| 60 | } |
| 61 | w = f.get(t, owner, "/tasks/"+task.ID+"/rendered-change/"+render.RenderHash+".png") |
| 62 | if w.Code != 200 || !bytes.Equal(w.Body.Bytes(), pixels.Bytes()) { |
| 63 | t.Fatal("attested render unavailable", w.Code) |
| 64 | } |
| 65 | } |
| 66 | actor := decider |
| 67 | if guard == "requester" || guard == "team_owner" { |
| 68 | actor = owner |
| 69 | } |
| 70 | if guard == "member" { |
| 71 | actor = peer |
| 72 | f.exec(t, "UPDATE members SET role='member' WHERE address=?", peer.Address) |
| 73 | } |
| 74 | mutation := equipo.Mutation{RequestID: "approval", Revision: task.Revision} |
| 75 | if guard == "revision" { |
| 76 | mutation.Revision++ |
| 77 | } |
| 78 | before := f.count(t, "SELECT count(*) FROM audit_events") |
| 79 | if guard == "audit" { |
| 80 | f.exec(t, "CREATE TRIGGER deny_render_audit BEFORE INSERT ON audit_events BEGIN SELECT RAISE(ABORT,'audit fixture'); END") |
| 81 | } |
| 82 | err = f.Store.ApproveRenderedChange(ctx, actor, task.ID, hash, render.DiffHash, render.RenderHash, "copy", mutation) |
| 83 | if guard == "requester" || guard == "team_owner" || guard == "member" || guard == "revision" || guard == "audit" { |
| 84 | if err == nil { |
| 85 | t.Fatal("unauthorized/stale approval accepted", guard) |
| 86 | } |
| 87 | j, e := x.Core.GetJob(ctx, scope) |
| 88 | must(t, e) |
| 89 | if j.Evidence.RenderedApprovalHash != "" { |
| 90 | t.Fatal("failed operation retained approval") |
| 91 | } |
| 92 | return |
| 93 | } |
| 94 | must(t, err) |
| 95 | must(t, f.Store.ApproveRenderedChange(ctx, actor, task.ID, hash, render.DiffHash, render.RenderHash, "copy", mutation)) |
| 96 | if f.count(t, "SELECT count(*) FROM audit_events") != before+1 { |
| 97 | t.Fatal("approval retry duplicated audit") |
| 98 | } |
| 99 | j, err := x.Core.GetJob(ctx, scope) |
| 100 | must(t, err) |
| 101 | if len(j.Evidence.RenderedApprovals) != 1 || j.Evidence.RenderedApprovals[0].Person != decider.Address || j.Evidence.RenderedApprovals[0].DecisionRevision != 1 { |
| 102 | t.Fatal("approval identity not derived", j.Evidence) |
| 103 | } |
| 104 | switch guard { |
| 105 | case "ownership": |
| 106 | f.exec(t, "UPDATE decision_owners SET address=? WHERE project_id=? AND decision='copy'", owner.Address, p.ProjectID) |
| 107 | case "project_revision": |
| 108 | f.exec(t, "UPDATE projects SET revision=revision+1 WHERE id=?", p.ProjectID) |
| 109 | if err = x.Core.RecordRenderedApproval(ctx, scope, j.Revision, j.Evidence.RenderedApprovals[0]); err == nil { |
| 110 | t.Fatal("stale decision revision accepted") |
| 111 | } |
| 112 | return |
| 113 | case "removed": |
| 114 | f.exec(t, "UPDATE members SET active=0 WHERE address=?", decider.Address) |
| 115 | default: |
| 116 | return |
| 117 | } |
| 118 | if err = f.Store.ApproveRenderedChange(ctx, actor, task.ID, hash, render.DiffHash, render.RenderHash, "copy", mutation); err == nil { |
| 119 | t.Fatal("replay bypassed current authority") |
| 120 | } |
| 121 | }) |
| 122 | } |
| 123 | } |
| 124 | func TestReadWorkAuthority(t *testing.T) { |
| 125 | f := newFixture(t) |
| 126 | task := f.createTask(t) |
| 127 | f.exec(t, "UPDATE members SET role='member' WHERE address=?", peer.Address) |
| 128 | var p equipo.Project |
| 129 | must(t, f.DB.G.First(&p, task.ProjectID).Error) |
| 130 | req := team.AdmissionRequest{Account: team.AccountID(f.TeamID), Project: team.ProjectID(p.ID), Grant: equipo.GrantRef(p.ID, peer.Address), Operation: team.OpProviderCall, Target: team.TargetBinding{Kind: team.TargetProjectRead, Repository: p.Repository, BaseRevision: strings.Repeat("a", 40)}} |
| 131 | a := equipo.NewAuthority(f.Store, nil) |
| 132 | _, err := a.Introspect(ctx, req) |
| 133 | must(t, err) |
| 134 | req.Operation = team.OpForgePublish |
| 135 | if _, err = a.Introspect(ctx, req); !errors.Is(err, team.ErrGrantRevoked) { |
| 136 | t.Fatal("read grant authorized publication", err) |
| 137 | } |
| 138 | req.Operation = team.OpProviderCall |
| 139 | req.Target.Kind = team.TargetRepository |
| 140 | req.Target.Ref = "equipo/task" |
| 141 | req.Target.DefaultRef = "main" |
| 142 | if _, err = a.Introspect(ctx, req); !errors.Is(err, team.ErrGrantRevoked) { |
| 143 | t.Fatal("read member gained write authority", err) |
| 144 | } |
| 145 | } |
| 146 | |