oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "net/http/httptest"
8 "sync/atomic"
9 "testing"
10 "time"
11
12 access "amadan.net/oficina/agent-access.git"
13 "github.com/google/uuid"
14 "gorm.io/gorm"
15)
16
17type sheetAgentFixture struct {
18 home *homeFixture
19 a *sheetAgent
20 p access.Principal
21 revoked atomic.Bool
22 checks atomic.Int64
23 revokeAt atomic.Int64
24 verified chan struct{}
25}
26type agentDeadlineRecorder struct {
27 *httptest.ResponseRecorder
28 afterWrite func()
29}
30
31func (*agentDeadlineRecorder) SetWriteDeadline(time.Time) error { return nil }
32func (w *agentDeadlineRecorder) Write(body []byte) (int, error) {
33 n, e := w.ResponseRecorder.Write(body)
34 if w.afterWrite != nil {
35 f := w.afterWrite
36 w.afterWrite = nil
37 f()
38 }
39 return n, e
40}
41func newSheetAgentFixture(t *testing.T, issuers ...string) *sheetAgentFixture {
42 t.Helper()
43 home := newHomeFixture(t)
44 home.local(t, signedInAddress)
45 home.link(t, "opaque-owner")
46 issuer := "https://connect.oficina.works"
47 if len(issuers) > 0 {
48 issuer = issuers[0]
49 }
50 a := &sheetAgent{app: home.app, issuer: issuer, identityIssuer: homeTestIssuer, team: "blue", key: bytes.Repeat([]byte{81}, 32)}
51 if e := a.app.db.Transaction(func(tx *gorm.DB) error { return a.bind(tx) }); e != nil {
52 t.Fatal(e)
53 }
54 f := &sheetAgentFixture{home: home, a: a, p: access.Principal{Subject: "opaque-owner", Client: "test-client", Grant: "grant-one", Teams: []string{"blue"}, Scopes: []string{"sheets:read", "sheets:write"}}}
55 f.install(t)
56 return f
57}
58func (f *sheetAgentFixture) install(t *testing.T) {
59 t.Helper()
60 a := f.a
61 handler, e := a.httpHandler(access.VerifyFunc(func(ctx context.Context, token string) (access.Principal, error) {
62 n := f.checks.Add(1)
63 if n == 1 && f.verified != nil {
64 close(f.verified)
65 }
66 if at := f.revokeAt.Load(); at > 0 && n >= at {
67 f.revoked.Store(true)
68 }
69 if token != "test-token" || f.revoked.Load() {
70 return access.Principal{}, access.Fail("unauthenticated", "Connection expired.")
71 }
72 return f.p, nil
73 }))
74 if e != nil {
75 t.Fatal(e)
76 }
77 a.app.agent = handler
78}
79func (f *sheetAgentFixture) call(t *testing.T, name string, input any) *httptest.ResponseRecorder {
80 t.Helper()
81 raw, e := json.Marshal(input)
82 if e != nil {
83 t.Fatal(e)
84 }
85 return f.raw(t, name, raw)
86}
87func (f *sheetAgentFixture) raw(t *testing.T, name string, raw []byte) *httptest.ResponseRecorder {
88 t.Helper()
89 req := httptest.NewRequest("POST", testOrigin+"/api/v1/operations/"+name, bytes.NewReader(raw))
90 req.Header.Set("Authorization", "Bearer test-token")
91 req.Header.Set("Content-Type", "application/json")
92 w := &agentDeadlineRecorder{ResponseRecorder: httptest.NewRecorder()}
93 f.a.app.Router().ServeHTTP(w, req)
94 return w.ResponseRecorder
95}
96func initialSheetAgentInput() sheetAgentCreate {
97 return sheetAgentCreate{Team: "blue", ClientID: uuid.NewString(), Title: "PRIVATE_BUDGET_TITLE_42", TabName: "PRIVATE_TAB_NAME_42", Cells: []sheetAgentCell{{Row: 0, Column: 0, Raw: "21"}, {Row: 1, Column: 0, Raw: "=A1*2"}, {Row: 0, Column: 1, Raw: "PRIVATE_CELL_CANARY_42 ✓"}}}
98}
99func decodeSheetCreated(t *testing.T, rec *httptest.ResponseRecorder) sheetAgentCreated {
100 t.Helper()
101 if rec.Code != 200 {
102 t.Fatalf("create: %d %s", rec.Code, rec.Body.String())
103 }
104 var out sheetAgentCreated
105 if e := json.Unmarshal(rec.Body.Bytes(), &out); e != nil {
106 t.Fatal(e)
107 }
108 if out.SheetID == "" || out.TabID == "" || out.Revision == "" {
109 t.Fatal("incomplete creation receipt")
110 }
111 return out
112}
113func (f *sheetAgentFixture) rangeInput(created sheetAgentCreated) map[string]any {
114 return map[string]any{"team": "blue", "sheet_id": created.SheetID, "tab_id": created.TabID, "row": 0, "column": 0, "height": 2, "width": 2}
115}
116