oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "strings"
8 "testing"
9
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13func accessRig(t *testing.T, words string) (*meteredRig, turnRecord) {
14 t.Helper()
15 r := placementRig(t)
16 turn := turnRecord{ID: "tr_access", Person: hkOwner, Kind: wire.TurnPerson, State: wire.TurnRunning}
17 for _, row := range []any{&turn, &messageRecord{ID: "m_access", Person: hkOwner, TurnID: turn.ID, Role: wire.RolePerson, Text: words, Origin: originComposer}} {
18 if err := r.s.db.G.Create(row).Error; err != nil {
19 t.Fatal(err)
20 }
21 }
22 return r, turn
23}
24func proposeAccess(t *testing.T, r *meteredRig, turn turnRecord, target string) cardRecord {
25 t.Helper()
26 if err := r.s.tx(context.Background(), func(tx *txn) error {
27 got, err := tx.requestAccess(turn, wire.AccessRequest{Target: target, Reason: "I need to fix the build."})
28 if err == nil && got.Code != "asked" {
29 t.Fatalf("proposal: %+v", got)
30 }
31 return err
32 }); err != nil {
33 t.Fatal(err)
34 }
35 var c cardRecord
36 if err := r.s.db.G.Where("turn_id = ? AND kind = ?", turn.ID, wire.CardAccess).First(&c).Error; err != nil {
37 t.Fatal(err)
38 }
39 return c
40}
41func TestAccessConfirmationAndNarrowTarget(t *testing.T) {
42 r, turn := accessRig(t, "Fix the build in carlos/platform")
43 c := proposeAccess(t, r, turn, "carlos")
44 var a wire.AccessRequest
45 if err := json.Unmarshal([]byte(c.Access), &a); err != nil {
46 t.Fatal(err)
47 }
48 if a.Target != "carlos/platform" || a.Role != "agent" || a.State != "confirmation" {
49 t.Fatalf("unsafe proposal: %+v", a)
50 }
51 var n int64
52 r.s.db.G.Model(&commandRecord{}).Where("kind = ?", wire.FrameRequestAccess).Count(&n)
53 if n != 0 {
54 t.Fatal("model filed without confirmation")
55 }
56 r.removed = true
57 if _, _, err := r.s.Answer(context.Background(), hkOwner, c.ID, wire.AnswerCard{Choice: "confirm", Revision: c.Revision}); !errors.Is(err, ErrForbidden) {
58 t.Fatal("removed member", err)
59 }
60 r.removed = false
61 if _, _, err := r.s.Answer(context.Background(), hkMember, c.ID, wire.AnswerCard{Choice: "confirm", Revision: c.Revision}); !errors.Is(err, ErrNotFound) {
62 t.Fatal("another person", err)
63 }
64 if _, _, err := r.s.Answer(context.Background(), hkOwner, c.ID, wire.AnswerCard{Choice: "confirm", Revision: c.Revision}); err != nil {
65 t.Fatal(err)
66 }
67 var commands []commandRecord
68 r.s.db.G.Where("kind = ?", wire.FrameRequestAccess).Find(&commands)
69 if len(commands) != 1 {
70 t.Fatalf("commands: %+v", commands)
71 }
72 var cmd wire.AccessCommand
73 if err := json.Unmarshal([]byte(commands[0].Body), &cmd); err != nil {
74 t.Fatal(err)
75 }
76 if cmd.Target != "carlos/platform" || cmd.CardID != c.ID || !cmd.Confirmed {
77 t.Fatalf("command: %+v", cmd)
78 }
79 if _, _, err := r.s.Answer(context.Background(), hkOwner, c.ID, wire.AnswerCard{Choice: "confirm", Revision: c.Revision}); !errors.Is(err, ErrCardConflict) {
80 t.Fatal("double confirm", err)
81 }
82}
83func TestAccessNamespaceRequiresExplicitRequest(t *testing.T) {
84 for _, tc := range []struct {
85 words string
86 allowed bool
87 }{{"Fix the build", false}, {"Do not give access to the whole carlos namespace", false}, {"Fix a build in namespace carlos", false}, {"Give access to the whole carlos namespace", true}} {
88 t.Run(tc.words, func(t *testing.T) {
89 r, turn := accessRig(t, tc.words)
90 if err := r.s.tx(context.Background(), func(tx *txn) error {
91 got, err := tx.requestAccess(turn, wire.AccessRequest{Target: "carlos", Reason: "I need access."})
92 if (got.Code == "asked") != tc.allowed {
93 t.Fatalf("namespace: %+v", got)
94 }
95 return err
96 }); err != nil {
97 t.Fatal(err)
98 }
99 })
100 }
101}
102func TestAccessFinalRequiresNewConfirmation(t *testing.T) {
103 for _, state := range []string{"declined", "expired", "limited", "failed"} {
104 t.Run(state, func(t *testing.T) {
105 r, turn := accessRig(t, "Fix carlos/platform")
106 c := proposeAccess(t, r, turn, "carlos/platform")
107 if _, _, err := r.s.Answer(context.Background(), hkOwner, c.ID, wire.AnswerCard{Choice: "confirm", Revision: c.Revision}); err != nil {
108 t.Fatal(err)
109 }
110 if err := r.s.tx(context.Background(), func(tx *txn) error {
111 return tx.accessReport(Lease{Runner: RunnerPrincipal{RunnerID: "RUNNER1", Person: hkOwner}, Protocol: wire.ProtocolCurrent}, &wire.AccessReport{CardID: c.ID, Person: hkOwner, RequestID: "ar_1", State: state})
112 }); err != nil {
113 t.Fatal(err)
114 }
115 r.s.db.G.First(&c, "id = ?", c.ID)
116 if c.State != wire.CardResolved {
117 t.Fatalf("not final: %+v", c)
118 }
119 if _, _, err := r.s.Answer(context.Background(), hkOwner, c.ID, wire.AnswerCard{Choice: "confirm", Revision: c.Revision}); !errors.Is(err, ErrCardConflict) {
120 t.Fatal("final reused", err)
121 }
122 if err := r.s.tx(context.Background(), func(tx *txn) error {
123 got, err := tx.requestAccess(turn, wire.AccessRequest{Target: "carlos/platform", Reason: "Try again."})
124 if got.Code == "asked" {
125 t.Fatal("model retried")
126 }
127 return err
128 }); err != nil {
129 t.Fatal(err)
130 }
131 turn.ID = "tr_again"
132 if err := r.s.db.G.Create(&turn).Error; err != nil {
133 t.Fatal(err)
134 }
135 if err := r.s.db.G.Create(&messageRecord{ID: "m_again", Person: hkOwner, TurnID: turn.ID, Role: wire.RolePerson, Text: "Try carlos/platform again", Origin: originComposer, IdempotencyKey: "again"}).Error; err != nil {
136 t.Fatal(err)
137 }
138 next := proposeAccess(t, r, turn, "carlos/platform")
139 var a wire.AccessRequest
140 json.Unmarshal([]byte(next.Access), &a)
141 if next.ID == c.ID || a.State != "confirmation" {
142 t.Fatal("retry lacks new confirmation")
143 }
144 })
145 }
146}
147
148func TestAccessApprovalWaitsForCatalogAndContinuesOnce(t *testing.T) {
149 r, turn := accessRig(t, "Fix the build in carlos/platform")
150 r.s.authority.Copy = func(key string) string {
151 if key == "access.confirm" {
152 return "Ask Amadan for access to %s?"
153 }
154 if key == "access.continue" {
155 return "Access to %s was approved. Continue the original request: %s."
156 }
157 return key
158 }
159 c := proposeAccess(t, r, turn, "carlos/platform")
160 lease := Lease{Runner: RunnerPrincipal{RunnerID: "RUNNER1", Person: hkOwner}, Protocol: wire.ProtocolCurrent}
161 report := wire.AccessReport{CardID: c.ID, Person: hkOwner, RequestID: "ar_one", State: "approved", ApproveURL: "https://amadan.net/settings/agents/requests/ar_one"}
162 if err := r.s.tx(context.Background(), func(tx *txn) error { return tx.accessReport(lease, &report) }); !errors.Is(err, ErrForbidden) {
163 t.Fatal("unconfirmed report accepted", err)
164 }
165 if _, _, err := r.s.Answer(context.Background(), hkOwner, c.ID, wire.AnswerCard{Choice: "confirm", Revision: c.Revision}); err != nil {
166 t.Fatal(err)
167 }
168 if err := r.s.db.G.Model(&turnRecord{}).Where("id = ?", turn.ID).Update("state", wire.TurnReplied).Error; err != nil {
169 t.Fatal(err)
170 }
171 if err := r.s.tx(context.Background(), func(tx *txn) error { return tx.accessReport(lease, &report) }); err != nil {
172 t.Fatal(err)
173 }
174 var n int64
175 r.s.db.G.Model(&messageRecord{}).Where("idempotency_key = ?", "access-approved:"+c.ID).Count(&n)
176 if n != 0 {
177 t.Fatal("continued before catalog refresh")
178 }
179 h := wire.Hello{Repos: []string{"amadan.net/carlos/platform"}, Catalog: []wire.RepoInfo{{Repo: "amadan.net/carlos/platform"}}}
180 for i := 0; i < 2; i++ {
181 if err := r.s.tx(context.Background(), func(tx *txn) error { return tx.resolveAccess(hkOwner, h) }); err != nil {
182 t.Fatal(err)
183 }
184 }
185 var messages []messageRecord
186 r.s.db.G.Where("idempotency_key = ?", "access-approved:"+c.ID).Find(&messages)
187 if len(messages) != 1 || !strings.Contains(messages[0].Text, "Fix the build in carlos/platform") || messages[0].Origin != originGenerated {
188 t.Fatalf("continuation: %+v", messages)
189 }
190 r.s.db.G.First(&c, "id = ?", c.ID)
191 if c.State != wire.CardResolved {
192 t.Fatal("approval unresolved")
193 }
194}
195