| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/home/connect" |
| 14 | "amadan.net/oficina/suite" |
| 15 | "amadan.net/rastrillo/rastrillo/sessions" |
| 16 | ) |
| 17 | |
| 18 | // partialMark is the partial notice's title as a page renders it. |
| 19 | var partialMark = strings.ReplaceAll(partialTitle, "’", "’") |
| 20 | |
| 21 | func isPartial(body string) bool { |
| 22 | return strings.Contains(body, partialTitle) || strings.Contains(body, partialMark) |
| 23 | } |
| 24 | |
| 25 | // A roster member with no account here signs in with Home alone: an |
| 26 | // addressless person, the team's memory, and the chrome of a signed-in |
| 27 | // person named by their person ref. |
| 28 | func TestTeamHomeOnlyMemberSignsInAndReachesTheTeamsMemory(t *testing.T) { |
| 29 | f := newTeamFixture(t) |
| 30 | doc := f.ingest("plan", "Nebula plan", "The nebula plan is ready.") |
| 31 | f.member(teamSubject('a'), "member") |
| 32 | |
| 33 | var lookup string |
| 34 | var sealed []byte |
| 35 | var person int64 |
| 36 | if err := f.d.Writer().QueryRow("SELECT person_id FROM memoria_team_home_members WHERE subject=?", teamSubject('a')).Scan(&person); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | if err := f.d.Writer().QueryRow("SELECT address_lookup, address_sealed FROM memoria_home_local_identities WHERE id=?", person).Scan(&lookup, &sealed); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | if len(sealed) != 0 || !strings.HasPrefix(lookup, "home-only:") { |
| 43 | t.Fatalf("Home-only person row = %q, %d sealed bytes; want no address at all", lookup, len(sealed)) |
| 44 | } |
| 45 | |
| 46 | w := f.page("/?q=nebula") |
| 47 | body := w.Body.String() |
| 48 | if w.Code != 200 || !strings.Contains(body, "Nebula plan") || isPartial(body) { |
| 49 | t.Fatalf("member search = %d, want the team's document and no partial notice:\n%s", w.Code, body) |
| 50 | } |
| 51 | if want := "Oficina account …" + teamSubject('a')[len(teamSubject('a'))-4:]; homeOnlyLabel(teamSubject('a')) != want { |
| 52 | t.Fatalf("Home-only label = %q, want %q", homeOnlyLabel(teamSubject('a')), want) |
| 53 | } |
| 54 | if !strings.Contains(body, `action="/signout"`) || !strings.Contains(body, homeOnlyLabel(teamSubject('a'))) { |
| 55 | t.Fatalf("Home-only member's chrome is not signed in as %q", homeOnlyLabel(teamSubject('a'))) |
| 56 | } |
| 57 | if got := f.page("/documents/" + itoa64(doc)); got.Code != 200 { |
| 58 | t.Fatalf("document = %d", got.Code) |
| 59 | } |
| 60 | // A member searches and reads; the connectors are an owner's. |
| 61 | if got := f.page("/connectors"); got.Code != 404 { |
| 62 | t.Fatalf("a member's /connectors = %d, want 404", got.Code) |
| 63 | } |
| 64 | if strings.Contains(body, `href="/connectors"`) { |
| 65 | t.Fatal("a member is shown Sources") |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Allocation only for a current Member: a stranger is connect's |
| 70 | // "unknown", an undecidable roster is a 503, and a live session for |
| 71 | // somebody else is refused before anything is allocated. |
| 72 | func TestTeamSignInAllocatesOnlyForACurrentMember(t *testing.T) { |
| 73 | f := newTeamFixture(t, "x@example.test") |
| 74 | people := f.count("memoria_home_local_identities") |
| 75 | |
| 76 | if got := f.freshSignIn(teamSubject('s')); got.Code != http.StatusForbidden { |
| 77 | t.Fatalf("stranger = %d, want connect's 403", got.Code) |
| 78 | } |
| 79 | if f.count("memoria_home_local_identities") != people { |
| 80 | t.Fatal("a stranger was allocated a person") |
| 81 | } |
| 82 | |
| 83 | f.dir.Set(teamSubject('u'), "member") |
| 84 | f.expired() |
| 85 | if got := f.freshSignIn(teamSubject('u')); got.Code != http.StatusServiceUnavailable { |
| 86 | t.Fatalf("Unknown roster = %d, want 503", got.Code) |
| 87 | } |
| 88 | if f.count("memoria_home_local_identities") != people { |
| 89 | t.Fatal("an undecidable roster allocated a person") |
| 90 | } |
| 91 | f.renewed() |
| 92 | |
| 93 | f.cookies = map[string]*http.Cookie{} |
| 94 | f.localSession("x@example.test") |
| 95 | if got := f.signIn(teamSubject('u')); got.Code != http.StatusForbidden { |
| 96 | t.Fatalf("member arriving over another session = %d, want 403", got.Code) |
| 97 | } |
| 98 | if f.count("memoria_team_home_members") != 0 { |
| 99 | t.Fatal("an arrival over a live session allocated a person") |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // A known Home-only member arriving over somebody else's live session is |
| 104 | // connect's same-person refusal, and that session survives. |
| 105 | func TestTeamKnownArrivalOverAnotherSessionIsRefused(t *testing.T) { |
| 106 | f := newTeamFixture(t, "x@example.test") |
| 107 | f.member(teamSubject('a'), "member") |
| 108 | f.cookies = map[string]*http.Cookie{} |
| 109 | f.localSession("x@example.test") |
| 110 | name := f.app.home.sessions.CookieName() |
| 111 | before := f.cookies[name].Value |
| 112 | if got := f.signIn(teamSubject('a')); got.Code != http.StatusForbidden { |
| 113 | t.Fatalf("known arrival over another session = %d, want 403", got.Code) |
| 114 | } |
| 115 | if f.cookies[name] == nil || f.cookies[name].Value != before { |
| 116 | t.Fatal("the local session was replaced") |
| 117 | } |
| 118 | if s, ok := f.sessionNow(); !ok || s.Subject != "x@example.test" { |
| 119 | t.Fatalf("session now = %+v, want the local one", s) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Removal reaches an existing session within the lease: Memoria has no |
| 124 | // shares, so a person off the roster reaches nothing — signed out and |
| 125 | // told so, as a person taken off MEMORIA_OWNERS always has been. |
| 126 | func TestTeamRemovalEndsAnExistingSession(t *testing.T) { |
| 127 | f := newTeamFixture(t) |
| 128 | doc := f.ingest("plan", "Nebula plan", "The nebula plan is ready.") |
| 129 | f.member(teamSubject('a'), "member") |
| 130 | if got := f.page("/documents/" + itoa64(doc)); got.Code != 200 { |
| 131 | t.Fatalf("before removal = %d", got.Code) |
| 132 | } |
| 133 | f.dir.Set(teamSubject('a'), "") |
| 134 | f.refreshed() |
| 135 | w := f.page("/documents/" + itoa64(doc)) |
| 136 | if w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/signin?err=removed" { |
| 137 | t.Fatalf("after removal = %d %q, want the removed sign-out", w.Code, w.Header().Get("Location")) |
| 138 | } |
| 139 | if _, ok := f.sessionNow(); ok { |
| 140 | t.Fatal("a removed member kept their session") |
| 141 | } |
| 142 | // The sign-in door asks too (its admission probe), before connect's |
| 143 | // shortcut sends a live session on to a page that refuses it: the |
| 144 | // door itself, saying why, with the session ended at the door. |
| 145 | f.member(teamSubject('c'), "member") |
| 146 | f.dir.Set(teamSubject('c'), "") |
| 147 | f.refreshed() |
| 148 | if w := f.page("/signin"); w.Code != http.StatusOK || !strings.Contains(w.Body.String(), signinRemoved) { |
| 149 | t.Fatalf("a removed member's /signin = %d %q, want the door with the removal", w.Code, w.Header().Get("Location")) |
| 150 | } |
| 151 | if _, ok := f.sessionNow(); ok { |
| 152 | t.Fatal("a removed member kept their session at the door") |
| 153 | } |
| 154 | // NotMember is never partial: there is no list to be short. |
| 155 | f.member(teamSubject('b'), "member") |
| 156 | f.dir.Set(teamSubject('b'), "") |
| 157 | f.refreshed() |
| 158 | if w := f.page("/?q=nebula"); w.Code != http.StatusSeeOther || isPartial(w.Body.String()) { |
| 159 | t.Fatalf("a removed member's list = %d, partial %v", w.Code, isPartial(w.Body.String())) |
| 160 | } |
| 161 | // Signing in again finds the same Home-only person — identity, not |
| 162 | // standing — and the first page asks the roster and signs them out |
| 163 | // again. Nothing is allocated. |
| 164 | people := f.count("memoria_home_local_identities") |
| 165 | if got := f.freshSignIn(teamSubject('a')); got.Code != http.StatusSeeOther { |
| 166 | t.Fatalf("removed member signing in again = %d, want connect's completion", got.Code) |
| 167 | } |
| 168 | if w := f.page("/"); w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/signin?err=removed" { |
| 169 | t.Fatalf("removed member's page after signing in again = %d %q", w.Code, w.Header().Get("Location")) |
| 170 | } |
| 171 | if f.count("memoria_home_local_identities") != people { |
| 172 | t.Fatal("signing in again allocated a person") |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // The role is the roster's, and MEMORIA_OWNERS admits a local sign-in and |
| 177 | // nothing more: an owners-list address with no Oficina link has no |
| 178 | // standing and is sent to link; a roster owner manages the connectors and |
| 179 | // a roster member does not. |
| 180 | func TestTeamStandingIsTheRostersNeverTheOwnersLists(t *testing.T) { |
| 181 | f := newTeamFixture(t, "local@example.test") |
| 182 | f.ingest("plan", "Nebula plan", "The nebula plan is ready.") |
| 183 | |
| 184 | f.cookies = map[string]*http.Cookie{} |
| 185 | f.localSession("local@example.test") |
| 186 | w := f.page("/?q=nebula") |
| 187 | if w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/account/home" { |
| 188 | t.Fatalf("unlinked owners-list address = %d %q, want /account/home", w.Code, w.Header().Get("Location")) |
| 189 | } |
| 190 | if strings.Contains(w.Body.String(), "Nebula plan") { |
| 191 | t.Fatal("an unlinked local account saw the team's memory") |
| 192 | } |
| 193 | if got := f.page("/connectors"); got.Code != http.StatusSeeOther { |
| 194 | t.Fatalf("unlinked owners-list address on /connectors = %d", got.Code) |
| 195 | } |
| 196 | if got := f.form("/documents", "title=x&body=y"); got.Code != 404 { |
| 197 | t.Fatalf("unlinked owners-list address writing = %d, want 404", got.Code) |
| 198 | } |
| 199 | if _, ok := f.sessionNow(); !ok { |
| 200 | t.Fatal("an unlinked local account was signed out before it could link") |
| 201 | } |
| 202 | if got := f.page("/account/home"); got.Code != 200 { |
| 203 | t.Fatalf("the link page for an unlinked local account = %d", got.Code) |
| 204 | } |
| 205 | |
| 206 | f.member(teamSubject('o'), "owner") |
| 207 | if got := f.page("/connectors"); got.Code != 200 || !strings.Contains(f.page("/").Body.String(), `href="/connectors"`) { |
| 208 | t.Fatalf("a roster owner's /connectors = %d", got.Code) |
| 209 | } |
| 210 | f.member(teamSubject('m'), "member") |
| 211 | if got := f.page("/connectors"); got.Code != 404 { |
| 212 | t.Fatalf("a roster member's /connectors = %d, want 404", got.Code) |
| 213 | } |
| 214 | if got := f.form("/connectors/notion/disconnect", ""); got.Code != 404 { |
| 215 | t.Fatalf("a roster member's connector write = %d, want 404", got.Code) |
| 216 | } |
| 217 | // A role change reaches an existing session within the lease. |
| 218 | f.member(teamSubject('o'), "owner") |
| 219 | f.dir.Set(teamSubject('o'), "member") |
| 220 | f.refreshed() |
| 221 | if got := f.page("/connectors"); got.Code != 404 { |
| 222 | t.Fatalf("a demoted owner's /connectors = %d, want 404", got.Code) |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // A linked local account stands on the roster: its Home session and its |
| 227 | // local session ask as one person, by its Home subject, and the owners |
| 228 | // list no longer decides its Home sign-in. |
| 229 | func TestTeamLinkedAccountStandsOnTheRoster(t *testing.T) { |
| 230 | f := newTeamFixture(t, "alice@example.test") |
| 231 | custody := &fixedCustody{apps: docsRoster(), give: suite.VISIBLE} |
| 232 | f.app.store.custody = custody |
| 233 | f.ingest("plan", "Nebula plan", "The nebula plan is ready.") |
| 234 | if _, err := f.app.store.Ingest(systemWrite(context.Background()), IngestSpec{Source: "Docs", Key: "s", Title: "Shared nebula", Body: "nebula sibling content", ItemID: "doc-one", SiblingRevision: 3}); err != nil { |
| 235 | t.Fatal(err) |
| 236 | } |
| 237 | subject := teamSubject('l') |
| 238 | f.dir.Set(subject, "member") |
| 239 | f.refreshed() |
| 240 | f.local(t) |
| 241 | if got := f.linkFromMenu(t, subject); got.Code != http.StatusSeeOther { |
| 242 | t.Fatalf("link = %d %s", got.Code, got.Body.String()) |
| 243 | } |
| 244 | if w := f.page("/?q=nebula"); w.Code != 200 || !strings.Contains(w.Body.String(), "Nebula plan") { |
| 245 | t.Fatalf("linked local session = %d", w.Code) |
| 246 | } |
| 247 | if custody.asker != (suite.Subject{Kind: suite.SubjectPersonRef, Value: subject}) { |
| 248 | t.Fatalf("a linked local session asks as %+v, want its person ref", custody.asker) |
| 249 | } |
| 250 | // On MEMORIA_OWNERS and a roster member: the owners list makes no |
| 251 | // owner in team mode, in the bar or at the route. |
| 252 | if w := f.page("/"); strings.Contains(w.Body.String(), `href="/connectors"`) { |
| 253 | t.Fatal("a roster member on the owners list is shown Sources") |
| 254 | } |
| 255 | if got := f.page("/connectors"); got.Code != 404 { |
| 256 | t.Fatalf("a roster member on the owners list: /connectors = %d, want 404", got.Code) |
| 257 | } |
| 258 | // Off the roster, the LOCAL session is signed out too: a linked |
| 259 | // account is not an unlinked one, whatever its sign-in method. |
| 260 | localJar := map[string]*http.Cookie{} |
| 261 | for k, v := range f.cookies { |
| 262 | localJar[k] = v |
| 263 | } |
| 264 | f.dir.Set(subject, "") |
| 265 | f.refreshed() |
| 266 | if w := f.page("/"); w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/signin?err=removed" { |
| 267 | t.Fatalf("linked local session off the roster = %d %q, want the removed sign-out", w.Code, w.Header().Get("Location")) |
| 268 | } |
| 269 | if _, ok := f.sessionNow(); ok { |
| 270 | t.Fatal("a linked local session off the roster kept its session") |
| 271 | } |
| 272 | f.dir.Set(subject, "member") |
| 273 | f.refreshed() |
| 274 | // Off the owners list: the Home session still stands on the roster. |
| 275 | f.app.SetOwners(nil) |
| 276 | if got := f.freshSignIn(subject); got.Code != http.StatusSeeOther { |
| 277 | t.Fatalf("linked Home sign-in off the owners list = %d", got.Code) |
| 278 | } |
| 279 | if w := f.page("/?q=nebula"); w.Code != 200 || !strings.Contains(w.Body.String(), "Nebula plan") { |
| 280 | t.Fatalf("linked Home session off the owners list = %d", w.Code) |
| 281 | } |
| 282 | // Off the roster: nothing, owners list or not. |
| 283 | f.app.SetOwners([]string{"alice@example.test"}) |
| 284 | f.dir.Set(subject, "") |
| 285 | f.refreshed() |
| 286 | if w := f.page("/"); w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/signin?err=removed" { |
| 287 | t.Fatalf("linked account off the roster = %d %q", w.Code, w.Header().Get("Location")) |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // A held decision ends at the lease under a failing refresh: 59 s still |
| 292 | // a member, 61 s a retryable no-store 503 rather than a 404 — for a |
| 293 | // document that does not exist too, so there is no oracle. |
| 294 | func TestTeamStandingEndsAtTheLeaseUnderAFailingRefresh(t *testing.T) { |
| 295 | f := newTeamFixture(t) |
| 296 | doc := f.ingest("plan", "Nebula plan", "The nebula plan is ready.") |
| 297 | f.member(teamSubject('a'), "member") |
| 298 | f.dir.SetDown(true) |
| 299 | f.clock.Step(59 * time.Second) |
| 300 | if got := f.page("/documents/" + itoa64(doc)); got.Code != 200 { |
| 301 | t.Fatalf("at 59 s = %d, want 200", got.Code) |
| 302 | } |
| 303 | f.clock.Step(2 * time.Second) |
| 304 | got := f.page("/documents/" + itoa64(doc)) |
| 305 | if got.Code != http.StatusServiceUnavailable || got.Header().Get("Retry-After") == "" || got.Header().Get("Cache-Control") != "no-store" { |
| 306 | t.Fatalf("at 61 s = %d %v, want a retryable no-store 503", got.Code, got.Header()) |
| 307 | } |
| 308 | if got := f.page("/documents/999999"); got.Code != http.StatusServiceUnavailable { |
| 309 | t.Fatalf("missing document under Unknown = %d, want 503", got.Code) |
| 310 | } |
| 311 | if _, ok := f.sessionNow(); !ok { |
| 312 | t.Fatal("Unknown signed the person out") |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | // Unknown: every list is partial with the notice and holds nothing (there |
| 317 | // are no shares to fall back on); every item, the add page, the |
| 318 | // correction page and the connectors are a retryable 503; the native |
| 319 | // API's JSON list, which cannot carry the notice, is a 503 too. |
| 320 | // |
| 321 | // MUTATION-TESTED 2026-11-02 (Fable's survivors on the search-home merge, |
| 322 | // each seen red and reverted): /documents mounted under member → "Unknown |
| 323 | // /documents = 503"; /suggest under list, and under no standing → 204; |
| 324 | // Try set after withhold → "Unknown / … nothing listed". |
| 325 | func TestTeamUnknownListsArePartialAndItemsAreRetryable(t *testing.T) { |
| 326 | f := newTeamFixture(t) |
| 327 | doc := f.ingest("plan", "Nebula plan", "The nebula plan is ready.") |
| 328 | // A labelled value, so the front page has a Try chip to withhold |
| 329 | // (Fable, 2026-11-02: no team fixture could show one, and a chip set |
| 330 | // after withhold rendered beside the notice unseen). |
| 331 | f.ingest("desk", "Front desk", "## Reception\n\nOffice phone: +353 (0)1 547 9999") |
| 332 | f.member(teamSubject('a'), "owner") |
| 333 | if w := f.page("/"); !strings.Contains(w.Body.String(), "rst-home-try") { |
| 334 | t.Fatalf("a Member's front page has no Try chips, so their absence below proves nothing:\n%s", w.Body.String()) |
| 335 | } |
| 336 | f.expired() |
| 337 | |
| 338 | for _, path := range []string{"/?q=nebula", "/", "/documents", "/reports"} { |
| 339 | w := f.page(path) |
| 340 | body := w.Body.String() |
| 341 | if w.Code != 200 || !isPartial(body) || strings.Contains(body, "Nebula plan") || strings.Contains(body, "Front desk") || strings.Contains(body, "rst-home-try") { |
| 342 | t.Fatalf("Unknown %s = %d, want the partial notice and nothing listed:\n%s", path, w.Code, body) |
| 343 | } |
| 344 | if strings.Count(body, partialBody) != 1 { |
| 345 | t.Fatalf("Unknown %s carries the notice %d times", path, strings.Count(body, partialBody)) |
| 346 | } |
| 347 | } |
| 348 | frag := f.do("GET", "/search?q=nebula", "") |
| 349 | if frag.Code != 200 || !isPartial(frag.Body.String()) || strings.Contains(frag.Body.String(), "Nebula plan") { |
| 350 | t.Fatalf("Unknown search fragment = %d:\n%s", frag.Code, frag.Body.String()) |
| 351 | } |
| 352 | for _, path := range []string{"/documents/" + itoa64(doc), "/documents/" + itoa64(doc) + "/delete", "/suggest?q=nebula", "/add", "/facts/correct?q=nebula", "/answer?q=nebula", "/connectors", "/connectors/notion", "/answer/feedback/1", "/api/native/session"} { |
| 353 | got := f.page(path) |
| 354 | if got.Code != http.StatusServiceUnavailable || got.Header().Get("Retry-After") == "" || got.Header().Get("Cache-Control") != "no-store" { |
| 355 | t.Fatalf("Unknown %s = %d %v, want a retryable no-store 503", path, got.Code, got.Header()) |
| 356 | } |
| 357 | // The 503 page wears the person's chrome, and its footer does not |
| 358 | // count a corpus they cannot currently reach. |
| 359 | if b := got.Body.String(); strings.Contains(b, "passages indexed") || strings.Contains(b, "passage indexed") { |
| 360 | t.Fatalf("Unknown %s's 503 page states the passage count", path) |
| 361 | } |
| 362 | } |
| 363 | native := f.json("POST", "/api/native/search", `{"query":"nebula"}`) |
| 364 | if native.Code != http.StatusServiceUnavailable || native.Header().Get("Cache-Control") != "no-store" || strings.Contains(native.Body.String(), "Nebula") { |
| 365 | t.Fatalf("Unknown native search = %d %s, want 503", native.Code, native.Body.String()) |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | // Fable's #53 required test 3, for Memoria: under Unknown the list is |
| 370 | // empty — always, here — and says partial, never "nothing stored", "no |
| 371 | // matches" or "no corrections", and never states a passage count. |
| 372 | func TestTeamEmptyPartialListSaysPartialNotEmpty(t *testing.T) { |
| 373 | f := newTeamFixture(t) |
| 374 | f.ingest("plan", "Nebula plan", "The nebula plan is ready.") |
| 375 | f.member(teamSubject('a'), "member") |
| 376 | f.expired() |
| 377 | for path, empty := range map[string][]string{ |
| 378 | "/": {"Nothing stored yet", "0 documents"}, |
| 379 | "/documents": {"Nothing stored yet", "0 documents"}, |
| 380 | "/?q=nebula": {"No matches", "Nothing stored yet", "0 documents"}, |
| 381 | "/reports": {"No corrections yet", "No ratings yet"}, |
| 382 | } { |
| 383 | w := f.page(path) |
| 384 | body := w.Body.String() |
| 385 | if w.Code != 200 || !isPartial(body) { |
| 386 | t.Fatalf("empty Unknown %s = %d, want the partial notice:\n%s", path, w.Code, body) |
| 387 | } |
| 388 | for _, s := range empty { |
| 389 | if strings.Contains(body, s) { |
| 390 | t.Errorf("empty Unknown %s says %q", path, s) |
| 391 | } |
| 392 | } |
| 393 | if strings.Contains(body, "passage indexed") || strings.Contains(body, "passages indexed") { |
| 394 | t.Errorf("Unknown %s states the corpus's passage count", path) |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // A storage failure resolving the session's identity is a retryable 503, |
| 400 | // never a sign-out. |
| 401 | func TestTeamIdentityStorageFailureIsUnavailableNotSignOut(t *testing.T) { |
| 402 | f := newTeamFixture(t, "x@example.test") |
| 403 | if _, _, err := f.app.ensureHomeLocalIdentity(context.Background(), sessionFor("x@example.test")); err != nil { |
| 404 | t.Fatal(err) |
| 405 | } |
| 406 | f.localSession("x@example.test") |
| 407 | broken, err := sql.Open("sqlite", "file:closed?mode=memory") |
| 408 | if err == nil { |
| 409 | broken.Close() |
| 410 | } |
| 411 | real := f.app.team.db |
| 412 | f.app.team.db = broken |
| 413 | got := f.page("/") |
| 414 | f.app.team.db = real |
| 415 | if got.Code != http.StatusServiceUnavailable { |
| 416 | t.Fatalf("identity storage failure = %d, want 503", got.Code) |
| 417 | } |
| 418 | if _, ok := f.sessionNow(); !ok { |
| 419 | t.Fatal("a storage failure signed the person out") |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | // connect's HomeOnly hook: a storage failure is not "nobody". |
| 424 | func TestTeamHomeOnlyStorageFailureIsNotNobody(t *testing.T) { |
| 425 | f := newTeamFixture(t) |
| 426 | broken, err := sql.Open("sqlite", "file:closed2?mode=memory") |
| 427 | if err == nil { |
| 428 | broken.Close() |
| 429 | } |
| 430 | _, err = f.app.teamHomeOnly(broken)(context.Background(), homeTestIssuer, teamSubject('a')) |
| 431 | if err == nil || errors.Is(err, connect.ErrNoPerson) { |
| 432 | t.Fatalf("HomeOnly over broken storage = %v, want an error that is not ErrNoPerson", err) |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | // A Home-only person keeps no address, ever: the row cannot be given one, |
| 437 | // deleted, linked to, or re-pointed, and the members table cannot be |
| 438 | // rewritten. |
| 439 | func TestTeamHomeOnlyPersonCannotGainAnAddress(t *testing.T) { |
| 440 | f := newTeamFixture(t) |
| 441 | f.member(teamSubject('a'), "member") |
| 442 | var person int64 |
| 443 | if err := f.d.Writer().QueryRow("SELECT person_id FROM memoria_team_home_members WHERE subject=?", teamSubject('a')).Scan(&person); err != nil { |
| 444 | t.Fatal(err) |
| 445 | } |
| 446 | for name, stmt := range map[string]string{ |
| 447 | "address": "UPDATE memoria_home_local_identities SET address_sealed=X'01' WHERE id=?", |
| 448 | "lookup": "UPDATE memoria_home_local_identities SET address_lookup='abc' WHERE id=?", |
| 449 | "delete": "DELETE FROM memoria_home_local_identities WHERE id=?", |
| 450 | "link": "INSERT INTO home_identity_links(issuer,subject,person_id) VALUES('" + homeTestIssuer + "','pr_other',?)", |
| 451 | "re-point": "UPDATE memoria_team_home_members SET person_id=person_id WHERE person_id=?", |
| 452 | "forget": "DELETE FROM memoria_team_home_members WHERE person_id=?", |
| 453 | } { |
| 454 | if _, err := f.d.Writer().Exec(stmt, person); err == nil { |
| 455 | t.Errorf("%s: the trigger let it through", name) |
| 456 | } |
| 457 | } |
| 458 | // With foreign keys off, as an operator's sqlite3 shell has them, the |
| 459 | // triggers still hold the row. |
| 460 | w := f.d.Writer() |
| 461 | if _, err := w.Exec("PRAGMA foreign_keys=OFF"); err != nil { |
| 462 | t.Fatal(err) |
| 463 | } |
| 464 | if _, err := w.Exec("DELETE FROM memoria_home_local_identities WHERE id=?", person); err == nil { |
| 465 | t.Error("delete with foreign keys off: the trigger let it through") |
| 466 | } |
| 467 | if _, err := w.Exec("PRAGMA foreign_keys=ON"); err != nil { |
| 468 | t.Fatal(err) |
| 469 | } |
| 470 | // And a person with an address cannot be made Home-only. |
| 471 | res, err := w.Exec("INSERT INTO memoria_home_local_identities(address_lookup,address_sealed) VALUES('abc',X'01')") |
| 472 | if err != nil { |
| 473 | t.Fatal(err) |
| 474 | } |
| 475 | addressful, _ := res.LastInsertId() |
| 476 | if _, err := w.Exec("INSERT INTO memoria_team_home_members(issuer,subject,person_id) VALUES(?,?,?)", homeTestIssuer, teamSubject('n'), addressful); err == nil { |
| 477 | t.Error("an addressful person was made Home-only") |
| 478 | } |
| 479 | res, err = w.Exec("INSERT INTO memoria_home_local_identities(address_lookup,address_sealed) VALUES('home-only:sealed',X'01')") |
| 480 | if err != nil { |
| 481 | t.Fatal(err) |
| 482 | } |
| 483 | prefixed, _ := res.LastInsertId() |
| 484 | if _, err := w.Exec("INSERT INTO memoria_team_home_members(issuer,subject,person_id) VALUES(?,?,?)", homeTestIssuer, teamSubject('n'), prefixed); err == nil { |
| 485 | t.Error("a person with a sealed address was made Home-only") |
| 486 | } |
| 487 | if _, err := w.Exec("INSERT INTO memoria_team_home_members(issuer,subject,person_id) VALUES(?,?,?)", homeTestIssuer, teamSubject('n'), person+1000); err == nil { |
| 488 | t.Error("a Home-only member was given a person that does not exist") |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | // json is a same-origin JSON request, as the native client makes one. |
| 493 | func (f *teamFixture) json(method, path, body string) *httptest.ResponseRecorder { |
| 494 | r := httptest.NewRequest(method, homeTestOrigin+path, strings.NewReader(body)) |
| 495 | r.Header.Set("Content-Type", "application/json") |
| 496 | r.Header.Set("Origin", homeTestOrigin) |
| 497 | for _, c := range f.cookies { |
| 498 | r.AddCookie(c) |
| 499 | } |
| 500 | return f.send(r) |
| 501 | } |
| 502 | |
| 503 | // sessionFor is a fresh local session for address. |
| 504 | func sessionFor(address string) sessions.Session { |
| 505 | return sessions.Session{Subject: address, Method: "magiclink", AuthTime: time.Now()} |
| 506 | } |
| 507 | |