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 "amadan.net/oficina/sheets/internal/sheets/seal"
5 "bytes"
6 "net/http/httptest"
7 "testing"
8
9 rdb "amadan.net/rastrillo/rastrillo/db"
10 "gorm.io/gorm"
11)
12
13func TestAgentSheetReceiptRestartAndAttribution(t *testing.T) {
14 f := newSheetAgentFixture(t)
15 input := initialSheetAgentInput()
16 created := decodeSheetCreated(t, f.call(t, "sheets_create", input))
17 var attribution sheetAgentAttribution
18 if e := f.a.app.db.First(&attribution, "sheet_id=?", created.SheetID).Error; e != nil {
19 t.Fatal(e)
20 }
21 plain, e := f.a.app.sealer.Open("agent.client/"+created.SheetID, attribution.ClientSealed)
22 if e != nil || string(plain) != f.p.Client || bytes.Contains(attribution.ClientSealed, []byte(f.p.Client)) {
23 t.Fatal("client attribution is not sealed and recoverable", e)
24 }
25 member, e := f.a.member(t.Context(), f.p, false)
26 if e != nil || member.ID != attribution.MemberID {
27 t.Fatal("operator attribution", e)
28 }
29 var files []struct{ Name, File string }
30 if e = f.a.app.db.Raw("PRAGMA database_list").Scan(&files).Error; e != nil {
31 t.Fatal(e)
32 }
33 path := ""
34 for _, file := range files {
35 if file.Name == "main" {
36 path = file.File
37 }
38 }
39 d, e := f.a.app.db.DB()
40 if e != nil {
41 t.Fatal(e)
42 }
43 if e = d.Close(); e != nil {
44 t.Fatal(e)
45 }
46 reopened, e := rdb.Open(path, quietLogger())
47 if e != nil {
48 t.Fatal(e)
49 }
50 defer reopened.Close()
51 sealer, e := seal.New(make([]byte, dekBytes))
52 if e != nil {
53 t.Fatal(e)
54 }
55 app := New(Config{DB: reopened.G, Sealer: sealer, Origin: testOrigin, Admit: Admits([]string{signedInAddress})})
56 cfg := f.credentials()
57 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)
58 if e != nil {
59 t.Fatal(e)
60 }
61 defer CloseSigninMail(auth)
62 app.UseAuth(auth)
63 if e = app.ConfigureHome(cfg); e != nil {
64 t.Fatal(e)
65 }
66 if e = app.ConfigureAgentAccess(f.a.issuer, f.a.team, cfg); e != nil {
67 t.Fatal(e)
68 }
69 a := *f.a
70 a.app = app
71 restarted := &sheetAgentFixture{home: f.home, a: &a, p: f.p}
72 restarted.install(t)
73 if got := decodeSheetCreated(t, restarted.call(t, "sheets_create", input)); got != created {
74 t.Fatal("restart replay changed receipt", got, created)
75 }
76
77 if e = reopened.G.Transaction(func(tx *gorm.DB) error { return a.bind(tx) }); e != nil {
78 t.Fatal("unchanged restart refused", e)
79 }
80 coordinate := a.digest("coordinate", []string{a.identityIssuer, a.issuer, f.p.Subject, f.p.Client, a.team, input.ClientID})
81 var receipt sheetAgentReceipt
82 if e = reopened.G.First(&receipt, "coordinate=?", coordinate).Error; e != nil || receipt.SheetID != attribution.SheetID {
83 t.Fatal("receipt did not survive restart", e)
84 }
85 a.issuer = "https://other-connect.example"
86 if e = reopened.G.Transaction(func(tx *gorm.DB) error { return a.bind(tx) }); e == nil {
87 t.Fatal("restart changed authority")
88 }
89}
90func TestAgentSheetReservedWriterRevocationAndCookieRefusal(t *testing.T) {
91 f := newSheetAgentFixture(t)
92 f.revokeAt.Store(2)
93 if rec := f.call(t, "sheets_create", initialSheetAgentInput()); rec.Code != 401 {
94 t.Fatal("reserved writer used revoked delegation", rec.Code)
95 }
96 assertAgentSheetEmpty(t, f)
97 req := httptest.NewRequest("GET", testOrigin+"/api/v1/operations", nil)
98 for _, cookie := range f.home.jar {
99 req.AddCookie(cookie)
100 }
101 rec := httptest.NewRecorder()
102 f.a.app.Router().ServeHTTP(rec, req)
103 if rec.Code != 401 {
104 t.Fatal("browser cookie authenticated agent API", rec.Code)
105 }
106}
107func TestAgentSheetAuthorityStorageFailure(t *testing.T) {
108 f := newSheetAgentFixture(t)
109 created := decodeSheetCreated(t, f.call(t, "sheets_create", initialSheetAgentInput()))
110 if e := f.a.app.db.Exec("ALTER TABLE grants RENAME TO unavailable_grants").Error; e != nil {
111 t.Fatal(e)
112 }
113 for name, args := range map[string]any{"sheets_list": map[string]any{"team": "blue"}, "sheets_get_range": f.rangeInput(created)} {
114 if rec := f.call(t, name, args); rec.Code != 503 {
115 t.Fatal("authority failure hidden", name, rec.Code)
116 }
117 }
118}
119