| 1 | package calls |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/calls/internal/calls/teamstate" |
| 12 | ) |
| 13 | |
| 14 | // A roster member with no account here signs in through Home and gets an |
| 15 | // addressless person: the placeholder in the email column, the label as |
| 16 | // their name, and neither the placeholder nor an address on any page. |
| 17 | func TestTeamHomeOnlyMemberSignsInWithNoAddress(t *testing.T) { |
| 18 | f := newTeamFixture(t) |
| 19 | subject := teamSubject('a') |
| 20 | p := f.member(subject, "member") |
| 21 | if !teamstate.Addressless(p.Email) || strings.Contains(p.Email, "@") || p.Anonymous { |
| 22 | t.Fatalf("Home-only person %+v", p) |
| 23 | } |
| 24 | if p.Name != homeOnlyLabel(subject) || !strings.HasSuffix(p.Name, subject[len(subject)-4:]) { |
| 25 | t.Fatalf("Home-only name %q", p.Name) |
| 26 | } |
| 27 | for _, path := range []string{"/", "/settings", "/welcome"} { |
| 28 | body := f.page(path).Body.String() |
| 29 | if strings.Contains(body, p.Email) || strings.Contains(body, teamstate.PlaceholderPrefix) { |
| 30 | t.Fatalf("%s shows the placeholder:\n%s", path, body) |
| 31 | } |
| 32 | } |
| 33 | if body := f.page("/").Body.String(); !strings.Contains(body, p.Name) { |
| 34 | t.Fatalf("the account menu does not name the Home-only member:\n%s", body) |
| 35 | } |
| 36 | sqid, _ := f.meeting("Standup") |
| 37 | if w := f.page("/m/" + sqid + "/join"); w.Code != http.StatusOK || strings.Contains(w.Body.String(), teamstate.PlaceholderPrefix) { |
| 38 | t.Fatalf("holding screen = %d", w.Code) |
| 39 | } |
| 40 | // Signing in again finds the same person; nothing new is allocated. |
| 41 | before := f.count("persons") |
| 42 | f.signInHome(subject) |
| 43 | if f.count("persons") != before || f.personFor(subject).ID != p.ID { |
| 44 | t.Fatal("a second sign-in allocated another person") |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Only a current Member is allocated a person: a stranger is connect's |
| 49 | // unknown (403), an undecidable roster a 503, and neither leaves a row. |
| 50 | func TestTeamOnlyACurrentMemberIsAllocated(t *testing.T) { |
| 51 | f := newTeamFixture(t) |
| 52 | f.refreshed() |
| 53 | if w := f.signInHome(teamSubject('s')); w.Code != http.StatusForbidden { |
| 54 | t.Fatalf("a stranger's sign-in = %d, want 403", w.Code) |
| 55 | } |
| 56 | f.dir.Set(teamSubject('u'), "member") |
| 57 | f.expired() |
| 58 | if w := f.signInHome(teamSubject('u')); w.Code != http.StatusServiceUnavailable { |
| 59 | t.Fatalf("a member's sign-in with the roster unknown = %d, want 503", w.Code) |
| 60 | } |
| 61 | if n := f.count("persons"); n != 0 { |
| 62 | t.Fatalf("%d persons allocated", n) |
| 63 | } |
| 64 | if n := f.count("calls_team_home_persons"); n != 0 { |
| 65 | t.Fatalf("%d Home-only rows allocated", n) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // A known arrival over somebody else's live session is refused and that |
| 70 | // session survives; nothing new is allocated. |
| 71 | func TestTeamKnownArrivalOverAnotherSessionIsRefused(t *testing.T) { |
| 72 | f := newTeamFixture(t) |
| 73 | f.member(teamSubject('b'), "member") |
| 74 | bob := f.jar() |
| 75 | f.member(teamSubject('c'), "member") |
| 76 | f.cookies = bob |
| 77 | before := f.count("persons") |
| 78 | w := f.exchange(t, "/auth/start", teamSubject('c')) |
| 79 | if w.Code < 400 { |
| 80 | t.Fatalf("an arrival over another live session = %d, want a refusal", w.Code) |
| 81 | } |
| 82 | if f.count("persons") != before { |
| 83 | t.Fatal("allocated a person") |
| 84 | } |
| 85 | if g := f.get("/"); g.Code != http.StatusOK { |
| 86 | t.Fatalf("the session it arrived over did not survive: %d", g.Code) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Removal reaches an existing session within the lease: every page and |
| 91 | // write signs them out with the reason, /signin does too, and signing in |
| 92 | // again allocates nothing and is refused. Re-added, they are back. |
| 93 | func TestTeamRemovalSignsOutAnExistingSession(t *testing.T) { |
| 94 | f := newTeamFixture(t) |
| 95 | subject := teamSubject('r') |
| 96 | f.member(subject, "member") |
| 97 | sqid, _ := f.meeting("Planning") |
| 98 | // Four live sessions (four devices), each to be tried once: a |
| 99 | // sign-out ends the session it was asked of. |
| 100 | var jars []map[string]*http.Cookie |
| 101 | for range 4 { |
| 102 | f.signInHome(subject) |
| 103 | jars = append(jars, f.jar()) |
| 104 | } |
| 105 | f.removed(subject) |
| 106 | for i, probe := range []struct { |
| 107 | what string |
| 108 | do func() *httptest.ResponseRecorder |
| 109 | }{ |
| 110 | {"GET /", func() *httptest.ResponseRecorder { return f.get("/") }}, |
| 111 | {"POST /meetings", func() *httptest.ResponseRecorder { return f.post("/meetings", "title=After") }}, |
| 112 | {"the holding screen", func() *httptest.ResponseRecorder { return f.get("/m/" + sqid + "/join") }}, |
| 113 | {"GET /signin", func() *httptest.ResponseRecorder { return f.get("/signin") }}, |
| 114 | } { |
| 115 | f.cookies = jars[i] |
| 116 | signedOutRemoved(t, probe.what, probe.do()) |
| 117 | } |
| 118 | before := f.count("persons") |
| 119 | if w := f.signInHome(subject); w.Code != http.StatusSeeOther { |
| 120 | t.Fatalf("a removed member's sign-in = %d", w.Code) |
| 121 | } |
| 122 | if w := f.get("/"); w.Code != http.StatusSeeOther || !strings.Contains(w.Header().Get("Location"), "err=removed") { |
| 123 | t.Fatalf("a removed member signed in again: %d %q", w.Code, w.Header().Get("Location")) |
| 124 | } |
| 125 | if f.count("persons") != before { |
| 126 | t.Fatal("re-sign-in allocated a person") |
| 127 | } |
| 128 | f.dir.Set(subject, "member") |
| 129 | f.refreshed() |
| 130 | f.signInHome(subject) |
| 131 | if w := f.get("/"); w.Code != http.StatusOK { |
| 132 | t.Fatalf("re-added member = %d", w.Code) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // A local account linked to an Oficina account stands on the roster, on |
| 137 | // or off the allowlist: off the team it is signed out even though the |
| 138 | // local door admits it, and on the team it is a member with no place on |
| 139 | // the list. An unlinked local account is a guest: no member pages. |
| 140 | func TestTeamLinkedAccountStandsOnTheRoster(t *testing.T) { |
| 141 | f := newTeamFixture(t, "alice@example.test") |
| 142 | subject := teamSubject('l') |
| 143 | f.dir.Set(subject, "member") |
| 144 | f.refreshed() |
| 145 | f.local(t) |
| 146 | // Unlinked: a guest, sent to link from a member page, which the |
| 147 | // account menu offers. |
| 148 | if w := f.get("/settings"); w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/account/home" { |
| 149 | t.Fatalf("an unlinked local account on a member page = %d %q", w.Code, w.Header().Get("Location")) |
| 150 | } |
| 151 | if w := f.get("/"); w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/welcome" { |
| 152 | t.Fatalf("an unlinked local account's list = %d %q", w.Code, w.Header().Get("Location")) |
| 153 | } |
| 154 | if body := f.page("/welcome").Body.String(); !strings.Contains(body, "/account/home") { |
| 155 | t.Fatal("the account menu does not offer to connect the account") |
| 156 | } |
| 157 | if w := f.get("/account/home"); w.Code != http.StatusOK { |
| 158 | t.Fatalf("link page = %d", w.Code) |
| 159 | } |
| 160 | start := homeRedirect(t, f.post("/account/home", "")) |
| 161 | if got := homeRedirect(t, f.exchange(t, start, subject)); got != "/account/home/confirm" { |
| 162 | t.Fatalf("link exchange landed on %s", got) |
| 163 | } |
| 164 | homeRedirect(t, f.post("/account/home/confirm", "")) |
| 165 | if w := f.get("/"); w.Code != http.StatusOK { |
| 166 | t.Fatalf("linked member's list = %d", w.Code) |
| 167 | } |
| 168 | // A local account the local door does not admit (a guest who signed |
| 169 | // in through a meeting link) may not link: linking is the door's. |
| 170 | jar := f.jar() |
| 171 | f.localSession("guest@example.test") |
| 172 | if w := f.get("/account/home"); w.Code != http.StatusNotFound { |
| 173 | t.Fatalf("a guest's local account on the link page = %d", w.Code) |
| 174 | } |
| 175 | if w := f.get("/settings"); w.Code != http.StatusNotFound { |
| 176 | t.Fatalf("a guest's local account on a member page = %d", w.Code) |
| 177 | } |
| 178 | f.cookies = jar |
| 179 | // Off the allowlist is not off the team, signed in either way. |
| 180 | f.app.allow.Store(&allowlist{addrs: map[string]struct{}{}}) |
| 181 | if w := f.get("/settings"); w.Code != http.StatusOK { |
| 182 | t.Fatalf("a linked member off the allowlist = %d", w.Code) |
| 183 | } |
| 184 | local := f.jar() |
| 185 | if w := f.signInHome(subject); w.Code != http.StatusSeeOther { |
| 186 | t.Fatalf("a linked member's Home sign-in off the allowlist = %d", w.Code) |
| 187 | } |
| 188 | if w := f.get("/settings"); w.Code != http.StatusOK { |
| 189 | t.Fatalf("a linked member's Home session off the allowlist = %d", w.Code) |
| 190 | } |
| 191 | f.cookies = local |
| 192 | // Off the team is off, whatever the allowlist says. |
| 193 | f.app.allow.Store(nil) |
| 194 | f.removed(subject) |
| 195 | signedOutRemoved(t, "linked local account off the team", f.get("/")) |
| 196 | } |
| 197 | |
| 198 | // Storage that cannot be read resolving identity is a 503, never a |
| 199 | // sign-out: the session survives it. |
| 200 | func TestTeamIdentityStorageFailureIsNotASignOut(t *testing.T) { |
| 201 | f := newTeamFixture(t) |
| 202 | f.member(teamSubject('d'), "member") |
| 203 | if _, err := f.app.db.Writer().Exec("ALTER TABLE calls_team_home_persons RENAME TO unavailable_home_persons"); err != nil { |
| 204 | t.Fatal(err) |
| 205 | } |
| 206 | w := f.get("/") |
| 207 | retryable(t, "the list with identity storage down", w) |
| 208 | for _, c := range w.Result().Cookies() { |
| 209 | if c.MaxAge < 0 { |
| 210 | t.Fatal("storage failure ended the session") |
| 211 | } |
| 212 | } |
| 213 | if _, err := f.app.db.Writer().Exec("ALTER TABLE unavailable_home_persons RENAME TO calls_team_home_persons"); err != nil { |
| 214 | t.Fatal(err) |
| 215 | } |
| 216 | if w := f.get("/"); w.Code != http.StatusOK { |
| 217 | t.Fatalf("recovered = %d", w.Code) |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // The Home-only rows hold: addressless, never updated, never deleted |
| 222 | // (foreign keys off too), disjoint from account links both ways. |
| 223 | func TestTeamHomeOnlyRowsKeepTheirShape(t *testing.T) { |
| 224 | f := newTeamFixture(t) |
| 225 | p := f.member(teamSubject('h'), "member") |
| 226 | db := f.app.db.Writer() |
| 227 | for name, stmt := range map[string]string{ |
| 228 | "give an address": "UPDATE persons SET email='h@example.test' WHERE id=?", |
| 229 | "delete the person": "DELETE FROM persons WHERE id=?", |
| 230 | "move the row": "UPDATE calls_team_home_persons SET person_id=person_id WHERE person_id=?", |
| 231 | "delete the row": "DELETE FROM calls_team_home_persons WHERE person_id=?", |
| 232 | "link it": "INSERT INTO home_identity_links(issuer,subject,person_id,created_at) VALUES('" + homeTestIssuer + "','other',?,0)", |
| 233 | } { |
| 234 | if _, err := db.Exec(stmt, p.ID); err == nil { |
| 235 | t.Fatalf("%s: allowed", name) |
| 236 | } |
| 237 | } |
| 238 | if _, err := db.Exec("PRAGMA foreign_keys=OFF"); err != nil { |
| 239 | t.Fatal(err) |
| 240 | } |
| 241 | if _, err := db.Exec("DELETE FROM persons WHERE id=?", p.ID); err == nil { |
| 242 | t.Fatal("deleted a Home-only person with foreign keys off") |
| 243 | } |
| 244 | db.Exec("PRAGMA foreign_keys=ON") |
| 245 | // An address can never be a Home-only person, and a linked subject |
| 246 | // never becomes one. |
| 247 | if _, err := db.Exec("INSERT INTO persons(email,name,created_at,anonymous) VALUES('x@example.test','x',0,0)"); err != nil { |
| 248 | t.Fatal(err) |
| 249 | } |
| 250 | var id int64 |
| 251 | db.QueryRow("SELECT id FROM persons WHERE email='x@example.test'").Scan(&id) |
| 252 | if _, err := db.Exec("INSERT INTO calls_team_home_persons(issuer,subject,person_id) VALUES(?,?,?)", homeTestIssuer, teamSubject('x'), id); err == nil { |
| 253 | t.Fatal("an addressed person became Home-only") |
| 254 | } |
| 255 | if _, _, err := teamstate.EnsureHomePerson(context.Background(), db, homeTestIssuer, "a@b", "n"); !errors.Is(err, teamstate.ErrRefused) { |
| 256 | t.Fatalf("an address as a subject = %v", err) |
| 257 | } |
| 258 | if _, _, err := teamstate.EnsureHomePerson(context.Background(), db, homeTestIssuer, teamSubject('y'), "x@y"); !errors.Is(err, teamstate.ErrRefused) { |
| 259 | t.Fatalf("an address as a name = %v", err) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // A removed member who follows one of the team's links is signed out like |
| 264 | // anywhere else, not turned into a guest with a grant. |
| 265 | func TestTeamARemovedMemberFollowingALinkIsSignedOut(t *testing.T) { |
| 266 | f := newTeamFixture(t) |
| 267 | f.member(teamSubject('h'), "member") |
| 268 | _, token := f.meeting("Still running") |
| 269 | subject := teamSubject('r') |
| 270 | f.member(subject, "member") |
| 271 | f.removed(subject) |
| 272 | signedOutRemoved(t, "following a link", f.get("/j/"+token)) |
| 273 | if n := f.count("guest_grants"); n != 0 { |
| 274 | t.Fatalf("a removed member was given %d grants", n) |
| 275 | } |
| 276 | } |
| 277 | |