package calls import ( "context" "errors" "net/http" "net/http/httptest" "strings" "testing" "amadan.net/oficina/calls/internal/calls/teamstate" ) // A roster member with no account here signs in through Home and gets an // addressless person: the placeholder in the email column, the label as // their name, and neither the placeholder nor an address on any page. func TestTeamHomeOnlyMemberSignsInWithNoAddress(t *testing.T) { f := newTeamFixture(t) subject := teamSubject('a') p := f.member(subject, "member") if !teamstate.Addressless(p.Email) || strings.Contains(p.Email, "@") || p.Anonymous { t.Fatalf("Home-only person %+v", p) } if p.Name != homeOnlyLabel(subject) || !strings.HasSuffix(p.Name, subject[len(subject)-4:]) { t.Fatalf("Home-only name %q", p.Name) } for _, path := range []string{"/", "/settings", "/welcome"} { body := f.page(path).Body.String() if strings.Contains(body, p.Email) || strings.Contains(body, teamstate.PlaceholderPrefix) { t.Fatalf("%s shows the placeholder:\n%s", path, body) } } if body := f.page("/").Body.String(); !strings.Contains(body, p.Name) { t.Fatalf("the account menu does not name the Home-only member:\n%s", body) } sqid, _ := f.meeting("Standup") if w := f.page("/m/" + sqid + "/join"); w.Code != http.StatusOK || strings.Contains(w.Body.String(), teamstate.PlaceholderPrefix) { t.Fatalf("holding screen = %d", w.Code) } // Signing in again finds the same person; nothing new is allocated. before := f.count("persons") f.signInHome(subject) if f.count("persons") != before || f.personFor(subject).ID != p.ID { t.Fatal("a second sign-in allocated another person") } } // Only a current Member is allocated a person: a stranger is connect's // unknown (403), an undecidable roster a 503, and neither leaves a row. func TestTeamOnlyACurrentMemberIsAllocated(t *testing.T) { f := newTeamFixture(t) f.refreshed() if w := f.signInHome(teamSubject('s')); w.Code != http.StatusForbidden { t.Fatalf("a stranger's sign-in = %d, want 403", w.Code) } f.dir.Set(teamSubject('u'), "member") f.expired() if w := f.signInHome(teamSubject('u')); w.Code != http.StatusServiceUnavailable { t.Fatalf("a member's sign-in with the roster unknown = %d, want 503", w.Code) } if n := f.count("persons"); n != 0 { t.Fatalf("%d persons allocated", n) } if n := f.count("calls_team_home_persons"); n != 0 { t.Fatalf("%d Home-only rows allocated", n) } } // A known arrival over somebody else's live session is refused and that // session survives; nothing new is allocated. func TestTeamKnownArrivalOverAnotherSessionIsRefused(t *testing.T) { f := newTeamFixture(t) f.member(teamSubject('b'), "member") bob := f.jar() f.member(teamSubject('c'), "member") f.cookies = bob before := f.count("persons") w := f.exchange(t, "/auth/start", teamSubject('c')) if w.Code < 400 { t.Fatalf("an arrival over another live session = %d, want a refusal", w.Code) } if f.count("persons") != before { t.Fatal("allocated a person") } if g := f.get("/"); g.Code != http.StatusOK { t.Fatalf("the session it arrived over did not survive: %d", g.Code) } } // Removal reaches an existing session within the lease: every page and // write signs them out with the reason, /signin does too, and signing in // again allocates nothing and is refused. Re-added, they are back. func TestTeamRemovalSignsOutAnExistingSession(t *testing.T) { f := newTeamFixture(t) subject := teamSubject('r') f.member(subject, "member") sqid, _ := f.meeting("Planning") // Four live sessions (four devices), each to be tried once: a // sign-out ends the session it was asked of. var jars []map[string]*http.Cookie for range 4 { f.signInHome(subject) jars = append(jars, f.jar()) } f.removed(subject) for i, probe := range []struct { what string do func() *httptest.ResponseRecorder }{ {"GET /", func() *httptest.ResponseRecorder { return f.get("/") }}, {"POST /meetings", func() *httptest.ResponseRecorder { return f.post("/meetings", "title=After") }}, {"the holding screen", func() *httptest.ResponseRecorder { return f.get("/m/" + sqid + "/join") }}, {"GET /signin", func() *httptest.ResponseRecorder { return f.get("/signin") }}, } { f.cookies = jars[i] signedOutRemoved(t, probe.what, probe.do()) } before := f.count("persons") if w := f.signInHome(subject); w.Code != http.StatusSeeOther { t.Fatalf("a removed member's sign-in = %d", w.Code) } if w := f.get("/"); w.Code != http.StatusSeeOther || !strings.Contains(w.Header().Get("Location"), "err=removed") { t.Fatalf("a removed member signed in again: %d %q", w.Code, w.Header().Get("Location")) } if f.count("persons") != before { t.Fatal("re-sign-in allocated a person") } f.dir.Set(subject, "member") f.refreshed() f.signInHome(subject) if w := f.get("/"); w.Code != http.StatusOK { t.Fatalf("re-added member = %d", w.Code) } } // A local account linked to an Oficina account stands on the roster, on // or off the allowlist: off the team it is signed out even though the // local door admits it, and on the team it is a member with no place on // the list. An unlinked local account is a guest: no member pages. func TestTeamLinkedAccountStandsOnTheRoster(t *testing.T) { f := newTeamFixture(t, "alice@example.test") subject := teamSubject('l') f.dir.Set(subject, "member") f.refreshed() f.local(t) // Unlinked: a guest, sent to link from a member page, which the // account menu offers. if w := f.get("/settings"); w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/account/home" { t.Fatalf("an unlinked local account on a member page = %d %q", w.Code, w.Header().Get("Location")) } if w := f.get("/"); w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/welcome" { t.Fatalf("an unlinked local account's list = %d %q", w.Code, w.Header().Get("Location")) } if body := f.page("/welcome").Body.String(); !strings.Contains(body, "/account/home") { t.Fatal("the account menu does not offer to connect the account") } if w := f.get("/account/home"); w.Code != http.StatusOK { t.Fatalf("link page = %d", w.Code) } start := homeRedirect(t, f.post("/account/home", "")) if got := homeRedirect(t, f.exchange(t, start, subject)); got != "/account/home/confirm" { t.Fatalf("link exchange landed on %s", got) } homeRedirect(t, f.post("/account/home/confirm", "")) if w := f.get("/"); w.Code != http.StatusOK { t.Fatalf("linked member's list = %d", w.Code) } // A local account the local door does not admit (a guest who signed // in through a meeting link) may not link: linking is the door's. jar := f.jar() f.localSession("guest@example.test") if w := f.get("/account/home"); w.Code != http.StatusNotFound { t.Fatalf("a guest's local account on the link page = %d", w.Code) } if w := f.get("/settings"); w.Code != http.StatusNotFound { t.Fatalf("a guest's local account on a member page = %d", w.Code) } f.cookies = jar // Off the allowlist is not off the team, signed in either way. f.app.allow.Store(&allowlist{addrs: map[string]struct{}{}}) if w := f.get("/settings"); w.Code != http.StatusOK { t.Fatalf("a linked member off the allowlist = %d", w.Code) } local := f.jar() if w := f.signInHome(subject); w.Code != http.StatusSeeOther { t.Fatalf("a linked member's Home sign-in off the allowlist = %d", w.Code) } if w := f.get("/settings"); w.Code != http.StatusOK { t.Fatalf("a linked member's Home session off the allowlist = %d", w.Code) } f.cookies = local // Off the team is off, whatever the allowlist says. f.app.allow.Store(nil) f.removed(subject) signedOutRemoved(t, "linked local account off the team", f.get("/")) } // Storage that cannot be read resolving identity is a 503, never a // sign-out: the session survives it. func TestTeamIdentityStorageFailureIsNotASignOut(t *testing.T) { f := newTeamFixture(t) f.member(teamSubject('d'), "member") if _, err := f.app.db.Writer().Exec("ALTER TABLE calls_team_home_persons RENAME TO unavailable_home_persons"); err != nil { t.Fatal(err) } w := f.get("/") retryable(t, "the list with identity storage down", w) for _, c := range w.Result().Cookies() { if c.MaxAge < 0 { t.Fatal("storage failure ended the session") } } if _, err := f.app.db.Writer().Exec("ALTER TABLE unavailable_home_persons RENAME TO calls_team_home_persons"); err != nil { t.Fatal(err) } if w := f.get("/"); w.Code != http.StatusOK { t.Fatalf("recovered = %d", w.Code) } } // The Home-only rows hold: addressless, never updated, never deleted // (foreign keys off too), disjoint from account links both ways. func TestTeamHomeOnlyRowsKeepTheirShape(t *testing.T) { f := newTeamFixture(t) p := f.member(teamSubject('h'), "member") db := f.app.db.Writer() for name, stmt := range map[string]string{ "give an address": "UPDATE persons SET email='h@example.test' WHERE id=?", "delete the person": "DELETE FROM persons WHERE id=?", "move the row": "UPDATE calls_team_home_persons SET person_id=person_id WHERE person_id=?", "delete the row": "DELETE FROM calls_team_home_persons WHERE person_id=?", "link it": "INSERT INTO home_identity_links(issuer,subject,person_id,created_at) VALUES('" + homeTestIssuer + "','other',?,0)", } { if _, err := db.Exec(stmt, p.ID); err == nil { t.Fatalf("%s: allowed", name) } } if _, err := db.Exec("PRAGMA foreign_keys=OFF"); err != nil { t.Fatal(err) } if _, err := db.Exec("DELETE FROM persons WHERE id=?", p.ID); err == nil { t.Fatal("deleted a Home-only person with foreign keys off") } db.Exec("PRAGMA foreign_keys=ON") // An address can never be a Home-only person, and a linked subject // never becomes one. if _, err := db.Exec("INSERT INTO persons(email,name,created_at,anonymous) VALUES('x@example.test','x',0,0)"); err != nil { t.Fatal(err) } var id int64 db.QueryRow("SELECT id FROM persons WHERE email='x@example.test'").Scan(&id) if _, err := db.Exec("INSERT INTO calls_team_home_persons(issuer,subject,person_id) VALUES(?,?,?)", homeTestIssuer, teamSubject('x'), id); err == nil { t.Fatal("an addressed person became Home-only") } if _, _, err := teamstate.EnsureHomePerson(context.Background(), db, homeTestIssuer, "a@b", "n"); !errors.Is(err, teamstate.ErrRefused) { t.Fatalf("an address as a subject = %v", err) } if _, _, err := teamstate.EnsureHomePerson(context.Background(), db, homeTestIssuer, teamSubject('y'), "x@y"); !errors.Is(err, teamstate.ErrRefused) { t.Fatalf("an address as a name = %v", err) } } // A removed member who follows one of the team's links is signed out like // anywhere else, not turned into a guest with a grant. func TestTeamARemovedMemberFollowingALinkIsSignedOut(t *testing.T) { f := newTeamFixture(t) f.member(teamSubject('h'), "member") _, token := f.meeting("Still running") subject := teamSubject('r') f.member(subject, "member") f.removed(subject) signedOutRemoved(t, "following a link", f.get("/j/"+token)) if n := f.count("guest_grants"); n != 0 { t.Fatalf("a removed member was given %d grants", n) } }