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 "reflect"
8 "strings"
9 "testing"
10
11 access "amadan.net/oficina/agent-access.git"
12 "amadan.net/oficina/home/roster"
13 "amadan.net/oficina/home/teammode"
14 "amadan.net/oficina/sheets/internal/sheets/seal"
15 rdb "amadan.net/rastrillo/rastrillo/db"
16)
17
18func TestAgentRangeEditLimitsAndTarget(t *testing.T) {
19 f := newSheetAgentFixture(t)
20 made := decodeSheetCreated(t, f.call(t, "sheets_create", initialSheetAgentInput()))
21 other := decodeSheetCreated(t, f.call(t, "sheets_create", initialSheetAgentInput()))
22 for _, rev := range []string{"0", "01", "-1", "1.0", "9223372036854775808"} {
23 in := rangeEdit(made)
24 in.ExpectedRevision = rev
25 if r := f.call(t, "sheets_set_range", in); r.Code != 422 {
26 t.Fatal("revision", rev, r.Code)
27 }
28 }
29 for _, cells := range [][]sheetAgentCell{nil, make([]sheetAgentCell, 501), {{Row: 5000, Column: 0, Raw: "1"}}, {{Row: 0, Column: 600, Raw: "1"}}, {{Raw: strings.Repeat("a", 8193)}}, {{Raw: "=SUM(("}}, {{Raw: "1"}, {Raw: "2"}}} {
30 in := rangeEdit(made)
31 in.Cells = cells
32 if r := f.call(t, "sheets_set_range", in); r.Code != 422 {
33 t.Fatal("invalid batch", r.Code)
34 }
35 }
36 in := rangeEdit(made)
37 in.TabID = other.TabID
38 if r := f.call(t, "sheets_set_range", in); r.Code != 404 {
39 t.Fatal("foreign tab", r.Code)
40 }
41 in = rangeEdit(made)
42 in.Team = "wrong"
43 if r := f.call(t, "sheets_set_range", in); r.Code != 403 {
44 t.Fatal("wrong team", r.Code)
45 }
46 in = rangeEdit(made)
47 raw, _ := json.Marshal(in)
48 raw = bytes.Replace(raw, []byte(`"32"`), []byte(`"\ud800"`), 1)
49 if r := f.raw(t, "sheets_set_range", raw); r.Code != 422 {
50 t.Fatal("Unicode", r.Code)
51 }
52 in.Cells = []sheetAgentCell{}
53 for i := 0; i < 500; i++ {
54 in.Cells = append(in.Cells, sheetAgentCell{Row: i, Column: 0, Raw: strings.Repeat("x", 3000)})
55 }
56 if r := f.call(t, "sheets_set_range", in); r.Code != 413 {
57 t.Fatal("request cap", r.Code)
58 }
59 var n int64
60 f.a.app.db.Model(&sheetAgentEdit{}).Count(&n)
61 if n != 0 {
62 t.Fatal("invalid input wrote receipts")
63 }
64}
65func TestAgentRangeEditEmissionRequiresEditor(t *testing.T) {
66 f := newSheetAgentFixture(t)
67 made := decodeSheetCreated(t, f.call(t, "sheets_create", initialSheetAgentInput()))
68 in := rangeEdit(made)
69 checks := 0
70 handler, e := f.a.httpHandler(access.VerifyFunc(func(context.Context, string) (access.Principal, error) {
71 checks++
72 if checks == 4 {
73 if e := f.a.app.db.Model(&Grant{}).Where("sheet_id=?", made.SheetID).Update("role", SheetViewer).Error; e != nil {
74 t.Fatal(e)
75 }
76 }
77 return f.p, nil
78 }))
79 if e != nil {
80 t.Fatal(e)
81 }
82 f.a.app.agent = handler
83 if r := f.call(t, "sheets_set_range", in); r.Code != 404 {
84 t.Fatal("viewer received edit acknowledgement", r.Code, r.Body.String())
85 }
86 var n int64
87 f.a.app.db.Model(&sheetAgentEdit{}).Count(&n)
88 if n != 1 {
89 t.Fatal("test did not revoke after commit")
90 }
91}
92func TestAgentRangeEditRestart(t *testing.T) {
93 f := newSheetAgentFixture(t)
94 made := decodeSheetCreated(t, f.call(t, "sheets_create", initialSheetAgentInput()))
95 in := rangeEdit(made)
96 ack := decodeSheetCreated(t, f.call(t, "sheets_set_range", in))
97 var path string
98 if e := f.a.app.db.Raw("SELECT file FROM pragma_database_list WHERE name='main'").Scan(&path).Error; e != nil {
99 t.Fatal(e)
100 }
101 writer, e := f.a.app.db.DB()
102 if e != nil {
103 t.Fatal(e)
104 }
105 if e = writer.Close(); e != nil {
106 t.Fatal(e)
107 }
108 reopened, e := rdb.Open(path, quietLogger())
109 if e != nil {
110 t.Fatal(e)
111 }
112 defer reopened.Close()
113 sealer, e := seal.New(make([]byte, dekBytes))
114 if e != nil {
115 t.Fatal(e)
116 }
117 app := New(Config{DB: reopened.G, Sealer: sealer, Origin: testOrigin, Admit: Admits([]string{signedInAddress})})
118 cfg := f.credentials()
119 auth, e := NewAuth(reopened.Writer(), AuthEnv{Origin: testOrigin, InstanceKey: "test-instance-key", Owners: []string{signedInAddress}, Home: cfg, Mailer: f.home.app.testMailer(t)}, quietLogger(), app)
120 if e != nil {
121 t.Fatal(e)
122 }
123 defer CloseSigninMail(auth)
124 app.UseAuth(auth)
125 if e = app.ConfigureHome(cfg); e != nil {
126 t.Fatal(e)
127 }
128 if e = app.ConfigureAgentAccess(f.a.issuer, f.a.team, cfg); e != nil {
129 t.Fatal(e)
130 }
131 a := *f.a
132 a.app = app
133 fresh := &sheetAgentFixture{home: f.home, a: &a, p: f.p}
134 fresh.install(t)
135 if got := decodeSheetCreated(t, fresh.call(t, "sheets_set_range", in)); got != ack {
136 t.Fatal("reopened receipt changed", got, ack)
137 }
138}
139func testAgentRangeEditGuardLapse(t *testing.T) {
140 f, af, _ := newTeamSheetAgent(t)
141 made := decodeSheetCreated(t, af.call(t, "sheets_create", teamSheetInput()))
142 in := rangeEdit(made)
143 in.Team = teamTestTeam
144 before := editCounts(t, af)
145 old := teamGuard
146 t.Cleanup(func() { teamGuard = old })
147 teamGuard = func(ctx context.Context, authorize func(roster.State) error, write func() error) error {
148 return teammode.Guard(ctx, authorize, func() error {
149 if e := write(); e != nil {
150 return e
151 }
152 f.expired(t)
153 return nil
154 })
155 }
156 r := af.call(t, "sheets_set_range", in)
157 if r.Code != 503 || !reflect.DeepEqual(before, editCounts(t, af)) {
158 t.Fatal("last team guard omitted", r.Code, r.Body.String())
159 }
160}
161
162func TestAgentRangeEditUsesWriteScope(t *testing.T) {
163 f := newSheetAgentFixture(t)
164 made := decodeSheetCreated(t, f.call(t, "sheets_create", initialSheetAgentInput()))
165 in := rangeEdit(made)
166 f.p.Scopes = []string{"sheets:write"}
167 decodeSheetCreated(t, f.call(t, "sheets_set_range", in))
168 f.p.Scopes = []string{"sheets:read"}
169 if r := f.call(t, "sheets_set_range", in); r.Code != 403 {
170 t.Fatal("read scope edited", r.Code)
171 }
172 f.p.Scopes = []string{"sheets:write"}
173 n := 0
174 handler, e := f.a.httpHandler(access.VerifyFunc(func(context.Context, string) (access.Principal, error) {
175 n++
176 p := f.p
177 if n > 1 {
178 p.Scopes = []string{"sheets:read"}
179 }
180 return p, nil
181 }))
182 if e != nil {
183 t.Fatal(e)
184 }
185 f.a.app.agent = handler
186 if r := f.call(t, "sheets_set_range", in); r.Code != 403 {
187 t.Fatal("lost write scope replayed", r.Code)
188 }
189}
190
191func TestAgentRangeEditUnboundReceiptRefused(t *testing.T) {
192 f := newSheetAgentFixture(t)
193 made := decodeSheetCreated(t, f.call(t, "sheets_create", initialSheetAgentInput()))
194 in := rangeEdit(made)
195 decodeSheetCreated(t, f.call(t, "sheets_set_range", in))
196 // Isolate edit receipts: creation state cannot mask a missing adoption gate.
197 for _, table := range []string{"sheets_agent_receipts", "sheets_agent_attribution", "sheets_agent_state"} {
198 if e := f.a.app.db.Exec("DELETE FROM " + table).Error; e != nil {
199 t.Fatal(e)
200 }
201 }
202 if r := f.call(t, "sheets_set_range", in); r.Code != 503 {
203 t.Fatal("unbound old edit adopted", r.Code)
204 }
205}
206