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 "amadan.net/oficina/equipo/internal/wire"
5 "context"
6 "encoding/json"
7 "errors"
8 "gorm.io/gorm"
9 "strings"
10 "testing"
11)
12
13func TestAccessReportURLAndSecondaryGuards(t *testing.T) {
14 for _, bad := range []string{"https://amadan.net/other/ar_one", "https://amadan.net/settings/agents/requests/other", "https://amadan.net/settings/agents/requests/ar_one?x=1", "https://user@amadan.net/settings/agents/requests/ar_one", "https://amadan.net/settings/agents/requests/ar_one\\bad"} {
15 t.Run(bad, func(t *testing.T) {
16 r, turn := accessRig(t, "Fix carlos/platform")
17 c := proposeAccess(t, r, turn, "carlos/platform")
18 r.s.Answer(context.Background(), hkOwner, c.ID, wire.AnswerCard{Choice: "confirm", Revision: c.Revision})
19 err := r.s.tx(context.Background(), func(tx *txn) error {
20 return tx.accessReport(Lease{Runner: RunnerPrincipal{RunnerID: "RUNNER1", Person: hkOwner}, Protocol: wire.ProtocolAccess}, &wire.AccessReport{CardID: c.ID, Person: hkOwner, RequestID: "ar_one", State: "pending", ApproveURL: bad})
21 })
22 if !errors.Is(err, ErrInvalid) {
23 t.Fatal("hostile link accepted", err)
24 }
25 })
26 }
27 r, turn := accessRig(t, "Fix carlos/platform")
28 c := proposeAccess(t, r, turn, "carlos/platform")
29 r.s.Answer(context.Background(), hkOwner, c.ID, wire.AnswerCard{Choice: "confirm", Revision: c.Revision})
30 err := r.s.tx(context.Background(), func(tx *txn) error {
31 return tx.accessReport(Lease{Runner: RunnerPrincipal{RunnerID: "SECONDARY", Person: hkOwner}, Protocol: wire.ProtocolAccess}, &wire.AccessReport{CardID: c.ID, Person: hkOwner, RequestID: "ar_one", State: "approved", ApproveURL: "https://amadan.net/settings/agents/requests/ar_one"})
32 })
33 if !errors.Is(err, ErrForbidden) {
34 t.Fatal("secondary report accepted", err)
35 }
36}
37func TestAccessNamespaceUsesBroadCopy(t *testing.T) {
38 r, turn := accessRig(t, "Give access to the whole carlos namespace")
39 c := proposeAccess(t, r, turn, "carlos")
40 if c.Question == "access.confirm%!(EXTRA string=carlos)" || c.Question == "access.confirm" {
41 t.Fatal("namespace uses repository confirmation", c.Question)
42 }
43 var a wire.AccessRequest
44 json.Unmarshal([]byte(c.Access), &a)
45 if a.Target != "carlos" {
46 t.Fatal(a)
47 }
48 if c.Question != "access.confirm_namespace%!(EXTRA string=carlos)" {
49 t.Fatal("missing broad copy key", c.Question)
50 }
51}
52func TestAccessSaveDoesNotRewriteOtherCards(t *testing.T) {
53 r := newMeteredRig(t)
54 c := cardRecord{ID: "other", Person: hkOwner, Kind: wire.CardQuestion, Name: "original", Question: "original", Options: "[]"}
55 r.s.db.G.Create(&c)
56 c.Name = "changed"
57 c.Question = "changed"
58 if err := r.s.tx(context.Background(), func(tx *txn) error { return tx.saveCard(&c) }); err != nil {
59 t.Fatal(err)
60 }
61 r.s.db.G.First(&c, "id = ?", c.ID)
62 if c.Name != "original" || c.Question != "original" {
63 t.Fatal("unrelated card text rewritten")
64 }
65}
66
67func TestAccessDemotedPrimarySettlesCardBeforePost(t *testing.T) {
68 r, turn := accessRig(t, "Fix carlos/platform")
69 c := proposeAccess(t, r, turn, "carlos/platform")
70 if _, _, err := r.s.Answer(context.Background(), hkOwner, c.ID, wire.AnswerCard{Choice: "confirm", Revision: c.Revision}); err != nil {
71 t.Fatal(err)
72 }
73 r.s.authority.PrimaryRunner = func(*gorm.DB, string) (string, error) { return "NEWPRIMARY", nil }
74 lease := Lease{Runner: RunnerPrincipal{RunnerID: "RUNNER1", Person: hkOwner}, Protocol: wire.ProtocolAccess}
75 if err := r.s.tx(context.Background(), func(tx *txn) error {
76 no, err := tx.authorizeAccess(lease, c.ID)
77 if err == nil && no == "" {
78 t.Fatal("demoted primary authorized")
79 }
80 return err
81 }); err != nil {
82 t.Fatal(err)
83 }
84 r.s.db.G.First(&c, "id = ?", c.ID)
85 var a wire.AccessRequest
86 json.Unmarshal([]byte(c.Access), &a)
87 if c.State != wire.CardResolved || a.State != "authority" {
88 t.Fatal("demoted request still waits", c.State, a.State)
89 }
90}
91
92func TestAccessWorkerLeaseSettlesCard(t *testing.T) {
93 r, turn := accessRig(t, "Fix carlos/platform")
94 c := proposeAccess(t, r, turn, "carlos/platform")
95 if _, _, err := r.s.Answer(context.Background(), hkOwner, c.ID, wire.AnswerCard{Choice: "confirm", Revision: c.Revision}); err != nil {
96 t.Fatal(err)
97 }
98 r.s.authority.RunnerRole = func(*gorm.DB, string) (string, error) { return wire.RoleWorker, nil }
99 if err := r.s.tx(context.Background(), func(tx *txn) error {
100 no, err := tx.authorizeAccess(Lease{Runner: RunnerPrincipal{RunnerID: "RUNNER1", Person: hkOwner}, Protocol: wire.ProtocolAccess}, c.ID)
101 if err == nil && no == "" {
102 t.Fatal("worker lease authorized")
103 }
104 return err
105 }); err != nil {
106 t.Fatal(err)
107 }
108 r.s.db.G.First(&c, "id = ?", c.ID)
109 if c.State != wire.CardResolved {
110 t.Fatal("worker card not settled")
111 }
112}
113
114func TestAccessFindHintHonorsNegotiatedProtocol(t *testing.T) {
115 for _, proto := range []int{25, 26} {
116 r := placementRig(t)
117 r.s.db.G.Model(&runnerSessionRecord{}).Where("runner_id = ?", "RUNNER1").Update("protocol", proto)
118 // The person named it: carlos is a namespace Equipo knows nothing of.
119 r.s.db.G.Create(&messageRecord{ID: "m_find", Person: hkOwner, TurnID: "find", Role: wire.RolePerson, Text: "Fix the build in carlos/platform", Origin: originComposer})
120 if err := r.s.tx(context.Background(), func(tx *txn) error {
121 got, err := tx.find(turnRecord{ID: "find", Person: hkOwner}, "carlos/platform")
122 if strings.Contains(got.Text, "request_access") != (proto >= 26) {
123 t.Fatal("unsupported tool prompted", proto, got.Text)
124 }
125 return err
126 }); err != nil {
127 t.Fatal(err)
128 }
129 }
130}
131
132func TestAccessLateApprovalResumesAfterExpiry(t *testing.T) {
133 r, turn := accessRig(t, "Fix carlos/platform")
134 c := proposeAccess(t, r, turn, "carlos/platform")
135 if _, _, err := r.s.Answer(context.Background(), hkOwner, c.ID, wire.AnswerCard{Choice: "confirm", Revision: c.Revision}); err != nil {
136 t.Fatal(err)
137 }
138 r.s.db.G.Model(&turnRecord{}).Where("id = ?", turn.ID).Update("state", wire.TurnReplied)
139 lease := Lease{Runner: RunnerPrincipal{RunnerID: "RUNNER1", Person: hkOwner}, Protocol: wire.ProtocolAccess}
140 for _, state := range []string{"expired", "pending", "approved", "approved"} {
141 report := wire.AccessReport{CardID: c.ID, Person: hkOwner, RequestID: "ar_one", State: state, ApproveURL: "https://amadan.net/settings/agents/requests/ar_one"}
142 if err := r.s.tx(context.Background(), func(tx *txn) error { return tx.accessReport(lease, &report) }); err != nil {
143 t.Fatal(err)
144 }
145 if state == "pending" {
146 var saved cardRecord
147 r.s.db.G.First(&saved, "id = ?", c.ID)
148 var access wire.AccessRequest
149 json.Unmarshal([]byte(saved.Access), &access)
150 if access.State != "pending" || saved.State != wire.CardOpen {
151 t.Fatal("server pending did not correct local expiry")
152 }
153 }
154 }
155 h := wire.Hello{Catalog: []wire.RepoInfo{{Repo: "amadan.net/carlos/platform"}}}
156 for i := 0; i < 2; i++ {
157 if err := r.s.tx(context.Background(), func(tx *txn) error { return tx.resolveAccess(hkOwner, h) }); err != nil {
158 t.Fatal(err)
159 }
160 }
161 var n int64
162 r.s.db.G.Model(&messageRecord{}).Where("idempotency_key = ?", "access-approved:"+c.ID).Count(&n)
163 if n != 1 {
164 t.Fatal("late approval did not resume exactly once", n)
165 }
166}
167