oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1package slides
2
3import (
4 access "amadan.net/oficina/agent-access.git"
5 "amadan.net/rastrillo/rastrillo/db"
6 "bytes"
7 "context"
8 "encoding/json"
9 "gorm.io/gorm"
10 "strings"
11 "sync/atomic"
12 "testing"
13)
14
15func TestAgentDeckAtomicAuthority(t *testing.T) {
16 for _, mode := range []string{"reserved", "final", "unlink", "binding", "member", "update", "receipt"} {
17 t.Run(mode, func(t *testing.T) {
18 f := newSlideAgentFixture(t)
19 trigger := ""
20 switch mode {
21 case "reserved":
22 f.revokeAt.Store(2)
23 trigger = "CREATE TRIGGER body_trap BEFORE INSERT ON decks BEGIN SELECT RAISE(ABORT,'body attempted'); END"
24 case "final":
25 f.revokeAt.Store(3)
26 case "unlink":
27 trigger = "CREATE TRIGGER lose_link AFTER UPDATE OF deck_id ON slides_agent_receipts BEGIN DELETE FROM home_identity_links; END"
28 case "binding":
29 trigger = "CREATE TRIGGER lose_binding AFTER INSERT ON deck_updates BEGIN UPDATE home_identity_links SET person_binding='broken'; END"
30 case "member":
31 trigger = "CREATE TRIGGER lose_member AFTER UPDATE OF deck_id ON slides_agent_receipts BEGIN UPDATE users SET email='removed@example.com'; END"
32 case "update":
33 trigger = "CREATE TRIGGER fail_update BEFORE INSERT ON deck_updates BEGIN SELECT RAISE(ABORT,'write failed'); END"
34 case "receipt":
35 trigger = "CREATE TRIGGER fail_receipt BEFORE UPDATE OF deck_id ON slides_agent_receipts BEGIN SELECT RAISE(ABORT,'receipt failed'); END"
36 }
37 if trigger != "" {
38 if e := f.a.app.db.G.Exec(trigger).Error; e != nil {
39 t.Fatal(e)
40 }
41 }
42 r := f.call(t, "slides_create_deck", initialSlideAgentInput())
43 want := 503
44 if mode == "reserved" || mode == "final" {
45 want = 401
46 }
47 if mode == "unlink" || mode == "member" || mode == "binding" {
48 want = 403
49 }
50 if r.Code != want {
51 t.Fatal(mode, r.Code, r.Body.String())
52 }
53 for _, table := range []string{"decks", "deck_updates", "slides_agent_receipts", "slides_connections"} {
54 var n int
55 if e := f.a.app.db.G.Raw("SELECT COUNT(*) FROM " + table).Scan(&n).Error; e != nil || n != 0 {
56 t.Fatal("partial create", table, n, e)
57 }
58 }
59 })
60 }
61}
62func TestAgentDeckRetryAndMarker(t *testing.T) {
63 for _, mode := range []string{"retry-final", "marker-create", "marker-retry"} {
64 t.Run(mode, func(t *testing.T) {
65 f := newSlideAgentFixture(t)
66 in := initialSlideAgentInput()
67 slideCreated(t, f.call(t, "slides_create_deck", in))
68 want := 503
69 if mode == "retry-final" {
70 f.checks.Store(0)
71 f.revokeAt.Store(3)
72 want = 401
73 } else {
74 if e := f.a.app.db.G.Exec("UPDATE slides_agent_state SET keycheck='damaged'").Error; e != nil {
75 t.Fatal(e)
76 }
77 if mode == "marker-create" {
78 in.ClientID = initialSlideAgentInput().ClientID
79 }
80 }
81 if r := f.call(t, "slides_create_deck", in); r.Code != want {
82 t.Fatal(mode, r.Code)
83 }
84 var n int
85 f.a.app.db.G.Raw("SELECT COUNT(*) FROM decks").Scan(&n)
86 if n != 1 {
87 t.Fatal(n)
88 }
89 })
90 }
91}
92func TestAgentDeckPrincipalContinuity(t *testing.T) {
93 for _, field := range []string{"subject", "client", "grant", "team", "scope"} {
94 t.Run(field, func(t *testing.T) {
95 f := newSlideAgentFixture(t)
96 var checks atomic.Int64
97 e := f.a.app.mountAgent(f.a, access.VerifyFunc(func(context.Context, string) (access.Principal, error) {
98 p := f.p
99 if checks.Add(1) > 1 {
100 switch field {
101 case "subject":
102 p.Subject = "other"
103 case "client":
104 p.Client = "other"
105 case "grant":
106 p.Grant = "other"
107 case "team":
108 p.Teams = []string{"other"}
109 case "scope":
110 p.Scopes = []string{"slides:read"}
111 }
112 }
113 return p, nil
114 }))
115 if e != nil {
116 t.Fatal(e)
117 }
118 if r := f.call(t, "slides_create_deck", initialSlideAgentInput()); r.Code != 403 {
119 t.Fatal(r.Code)
120 }
121 var n int
122 f.a.app.db.G.Raw("SELECT COUNT(*) FROM decks").Scan(&n)
123 if n != 0 {
124 t.Fatal(n)
125 }
126 })
127 }
128}
129func TestAgentDeckDurableBinding(t *testing.T) {
130 f := newSlideAgentFixture(t)
131 in := initialSlideAgentInput()
132 sqid := slideCreated(t, f.call(t, "slides_create_deck", in))
133 var path string
134 if e := f.a.app.db.Writer().QueryRow("SELECT file FROM pragma_database_list WHERE name='main'").Scan(&path); e != nil {
135 t.Fatal(e)
136 }
137 f.a.app.Close()
138 f.a.app.db.Close()
139 d, e := db.Open(path, quietLogger())
140 if e != nil {
141 t.Fatal(e)
142 }
143 defer d.Close()
144 _, app, e := New(d, f.a.app.cfg)
145 if e != nil {
146 t.Fatal(e)
147 }
148 defer app.Close()
149 f.a.app = app
150 f.install(t)
151 if retry := slideCreated(t, f.call(t, "slides_create_deck", in)); retry != sqid {
152 t.Fatal("restart lost receipt")
153 }
154 for _, mode := range []string{"home", "issuer", "team", "key"} {
155 t.Run(mode, func(t *testing.T) {
156 copy := *f.a
157 switch mode {
158 case "home":
159 copy.identityIssuer = "https://other-home.test"
160 case "issuer":
161 copy.issuer = "https://other-connect.test"
162 case "team":
163 copy.team = "other"
164 case "key":
165 copy.key = bytes.Repeat([]byte{41}, 32)
166 }
167 if e := app.db.G.Transaction(copy.bind); e == nil {
168 t.Fatal("rebound authority")
169 }
170 })
171 }
172 if e := app.db.G.Exec("DELETE FROM slides_agent_state").Error; e != nil {
173 t.Fatal(e)
174 }
175 for _, table := range []string{"slides_connections", "slides_agent_receipts"} {
176 t.Run(table, func(t *testing.T) {
177 e := app.db.G.Transaction(func(tx *gorm.DB) error {
178 if e := tx.Exec("DELETE FROM " + table).Error; e != nil {
179 return e
180 }
181 if e := f.a.bind(tx); e == nil {
182 t.Fatal("adopted old state")
183 }
184 return gorm.ErrInvalidTransaction
185 })
186 if e != gorm.ErrInvalidTransaction {
187 t.Fatal(e)
188 }
189 })
190 }
191}
192func TestAgentDeckMalformedUnicode(t *testing.T) {
193 f := newSlideAgentFixture(t)
194 in := initialSlideAgentInput()
195 in.Title = "REPLACEME"
196 raw, _ := json.Marshal(in)
197 for _, bad := range []string{`\ud800`, string([]byte{255})} {
198 if r := f.raw(t, "slides_create_deck", []byte(strings.Replace(string(raw), "REPLACEME", bad, 1))); r.Code != 422 {
199 t.Fatal(r.Code)
200 }
201 }
202}
203
204// Call the transaction body directly so a later response check cannot mask
205// omission of the completed receipt's final in-transaction guard.
206func TestAgentDeckCompletedRetryWriterGuard(t *testing.T) {
207 f := newSlideAgentFixture(t)
208 in := initialSlideAgentInput()
209 slideCreated(t, f.call(t, "slides_create_deck", in))
210 var checks int
211 state := &slideAgentContext{member: f.u, slides: map[int64]bool{}, verify: func(context.Context) error {
212 checks++
213 if checks >= 2 {
214 return access.Fail("unauthenticated", "Revoked.")
215 }
216 return nil
217 }}
218 ctx := context.WithValue(t.Context(), slideAgentKey{}, state)
219 if _, e := f.a.create(ctx, f.p, f.u, in); e == nil || access.SafeFailure(e).Code != "unauthenticated" {
220 t.Fatal("completed retry omitted final writer guard", e)
221 }
222}
223