| 1 | package docs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "net/http/httptest" |
| 7 | "strings" |
| 8 | "sync" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "gorm.io/gorm" |
| 13 | ) |
| 14 | |
| 15 | func grantAgentEdit(t *testing.T, f *docAgentFixture, sqid string, role Role) { |
| 16 | t.Helper() |
| 17 | other, e := f.a.store.EnsurePerson(t.Context(), "other@example.com") |
| 18 | if e != nil { |
| 19 | t.Fatal(e) |
| 20 | } |
| 21 | var doc Document |
| 22 | if e = f.a.store.db.Where("sqid=?", sqid).First(&doc).Error; e != nil { |
| 23 | t.Fatal(e) |
| 24 | } |
| 25 | if e = f.a.store.db.Model(&doc).Update("owner_id", other).Error; e != nil { |
| 26 | t.Fatal(e) |
| 27 | } |
| 28 | if e = f.a.store.db.Create(&Grant{DocID: doc.ID, Subject: subjectOf("owner@example.com"), Role: role, CreatedBy: other}).Error; e != nil { |
| 29 | t.Fatal(e) |
| 30 | } |
| 31 | } |
| 32 | func newTeamEditFixture(t *testing.T) (*docAgentFixture, *teamTestClock) { |
| 33 | t.Helper() |
| 34 | f := newDocAgentFixture(t) |
| 35 | _, clock := configureDocAgentTestTeam(t, f) |
| 36 | // Provision the test's final configuration before it owns any receipt. |
| 37 | if e := f.a.store.db.Exec("DELETE FROM docs_agent_state").Error; e != nil { |
| 38 | t.Fatal(e) |
| 39 | } |
| 40 | if e := f.a.store.db.Transaction(func(tx *gorm.DB) error { return f.a.bind(tx) }); e != nil { |
| 41 | t.Fatal(e) |
| 42 | } |
| 43 | return f, clock |
| 44 | } |
| 45 | func TestAgentUpdateIndependentGrantAndRosterAuthority(t *testing.T) { |
| 46 | for _, mode := range []string{"member", "unknown-owner", "unknown-writer", "unknown-reader", "expired-final"} { |
| 47 | t.Run(mode, func(t *testing.T) { |
| 48 | f, clock := newTeamEditFixture(t) |
| 49 | create := initialAgentInput() |
| 50 | create.Team = f.a.team |
| 51 | body := decodeAgentBody(t, f.call(t, "docs_create", create)) |
| 52 | in := editAgentInput(body) |
| 53 | in.Team = f.a.team |
| 54 | if strings.Contains(mode, "writer") { |
| 55 | grantAgentEdit(t, f, body.ID, RoleWriter) |
| 56 | } |
| 57 | if strings.Contains(mode, "reader") { |
| 58 | grantAgentEdit(t, f, body.ID, RoleReader) |
| 59 | } |
| 60 | if strings.HasPrefix(mode, "unknown") { |
| 61 | clock.step(time.Minute) |
| 62 | } |
| 63 | if mode == "expired-final" { |
| 64 | ctx, id, e := f.a.prepareMode(t.Context(), f.p, true, false) |
| 65 | if e != nil { |
| 66 | t.Fatal(e) |
| 67 | } |
| 68 | checks := 0 |
| 69 | ctx.Value(accessContextKey{}).(*requestAccess).delegationCheck = func(context.Context) error { |
| 70 | checks++ |
| 71 | if checks == 2 { |
| 72 | clock.step(time.Minute) |
| 73 | } |
| 74 | return nil |
| 75 | } |
| 76 | if result, e := f.a.update(ctx, f.p, id, in); e == nil || result != nil { |
| 77 | t.Fatal("expired writer committed", result, e) |
| 78 | } |
| 79 | agentEditCounts(t, f, 0) |
| 80 | return |
| 81 | } |
| 82 | want := 200 |
| 83 | if mode == "unknown-owner" || mode == "unknown-reader" { |
| 84 | want = 503 |
| 85 | } |
| 86 | if w := f.call(t, "docs_update", in); w.Code != want { |
| 87 | t.Fatal(mode, w.Code, w.Body.String()) |
| 88 | } |
| 89 | expected := int64(0) |
| 90 | if want == 200 { |
| 91 | expected = 1 |
| 92 | } |
| 93 | agentEditCounts(t, f, expected) |
| 94 | }) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func TestAgentUpdateResponseRequiresWritePermission(t *testing.T) { |
| 99 | f := newDocAgentFixture(t) |
| 100 | body := decodeAgentBody(t, f.call(t, "docs_create", initialAgentInput())) |
| 101 | grantAgentEdit(t, f, body.ID, RoleWriter) |
| 102 | in := editAgentInput(body) |
| 103 | f.a.notify = func(docID, seq int64) { |
| 104 | if e := f.a.store.db.Model(&Grant{}).Where("doc_id=?", docID).Update("role", RoleReader).Error; e != nil { |
| 105 | t.Fatal(e) |
| 106 | } |
| 107 | } |
| 108 | w := f.call(t, "docs_update", in) |
| 109 | if w.Code == 200 || strings.Contains(w.Body.String(), body.ID) { |
| 110 | t.Fatal("read-only response disclosed write acknowledgement", w.Code, w.Body.String()) |
| 111 | } |
| 112 | agentEditCounts(t, f, 1) |
| 113 | // The lost acknowledgement did not roll the committed edit back. |
| 114 | if w = f.call(t, "docs_get", map[string]any{"team": "blue", "id": body.ID}); w.Code != 200 { |
| 115 | t.Fatal(w.Code) |
| 116 | } |
| 117 | f.a.notify = nil |
| 118 | if e := f.a.store.db.Model(&Grant{}).Where("subject=?", subjectOf("owner@example.com")).Update("role", RoleWriter).Error; e != nil { |
| 119 | t.Fatal(e) |
| 120 | } |
| 121 | if w = f.call(t, "docs_update", in); w.Code != 200 { |
| 122 | t.Fatal(w.Code, w.Body.String()) |
| 123 | } |
| 124 | agentEditCounts(t, f, 1) |
| 125 | } |
| 126 | |
| 127 | func TestAgentUpdateHeldWriterRechecksBeforeBody(t *testing.T) { |
| 128 | f := newDocAgentFixture(t) |
| 129 | body := decodeAgentBody(t, f.call(t, "docs_create", initialAgentInput())) |
| 130 | in := editAgentInput(body) |
| 131 | var seq int |
| 132 | var name, path string |
| 133 | if e := f.app.db.Writer().QueryRow("PRAGMA database_list").Scan(&seq, &name, &path); e != nil { |
| 134 | t.Fatal(e) |
| 135 | } |
| 136 | held, e := sql.Open("sqlite", path) |
| 137 | if e != nil { |
| 138 | t.Fatal(e) |
| 139 | } |
| 140 | defer held.Close() |
| 141 | held.SetMaxOpenConns(1) |
| 142 | if _, e = f.app.db.Writer().Exec("PRAGMA busy_timeout=5000"); e != nil { |
| 143 | t.Fatal(e) |
| 144 | } |
| 145 | if e = f.a.store.db.Exec("CREATE TRIGGER edit_body_trap BEFORE INSERT ON doc_updates BEGIN SELECT RAISE(ABORT,'reserved-check omitted'); END").Error; e != nil { |
| 146 | t.Fatal(e) |
| 147 | } |
| 148 | tx, e := held.BeginTx(t.Context(), nil) |
| 149 | if e != nil { |
| 150 | t.Fatal(e) |
| 151 | } |
| 152 | defer tx.Rollback() |
| 153 | if _, e = tx.Exec("UPDATE docs_agent_state SET keycheck=keycheck WHERE id=1"); e != nil { |
| 154 | t.Fatal(e) |
| 155 | } |
| 156 | reached := make(chan struct{}) |
| 157 | var once sync.Once |
| 158 | const callback = "edit-held-writer" |
| 159 | if e = f.a.store.db.Callback().Raw().Before("gorm:raw").Register(callback, func(db *gorm.DB) { |
| 160 | if strings.HasPrefix(db.Statement.SQL.String(), "INSERT INTO docs_agent_state") { |
| 161 | once.Do(func() { close(reached) }) |
| 162 | } |
| 163 | }); e != nil { |
| 164 | t.Fatal(e) |
| 165 | } |
| 166 | defer f.a.store.db.Callback().Raw().Remove(callback) |
| 167 | result := make(chan *httptest.ResponseRecorder, 1) |
| 168 | go func() { result <- f.call(t, "docs_update", in) }() |
| 169 | select { |
| 170 | case <-reached: |
| 171 | case <-time.After(5 * time.Second): |
| 172 | t.Fatal("writer not reached") |
| 173 | } |
| 174 | select { |
| 175 | case w := <-result: |
| 176 | t.Fatal("request did not wait for writer", w.Code) |
| 177 | case <-time.After(50 * time.Millisecond): |
| 178 | } |
| 179 | f.revoked.Store(true) |
| 180 | if e = tx.Commit(); e != nil { |
| 181 | t.Fatal(e) |
| 182 | } |
| 183 | select { |
| 184 | case w := <-result: |
| 185 | if w.Code != 401 { |
| 186 | t.Fatal("revocation did not precede body trap", w.Code, w.Body.String()) |
| 187 | } |
| 188 | case <-time.After(10 * time.Second): |
| 189 | t.Fatal("request remained blocked") |
| 190 | } |
| 191 | agentEditCounts(t, f, 0) |
| 192 | } |
| 193 | |