package calls // What only Calls has, and what the door must leave as it was: guests who // join a call by name, the join cookie's email-only sign-in, and the native // apps' own sessions. import ( "context" "net/http" "net/http/httptest" "net/url" "regexp" "strings" "testing" "time" "amadan.net/oficina/home/connect" "amadan.net/rastrillo/rastrillo/lastsignin" "amadan.net/rastrillo/rastrillo/sessions" ) // doorMeeting makes a meeting on f's instance and returns its sqid and its // invitation's token, needing sign-in or not. func doorMeeting(t *testing.T, f *homeRouterFixture, requireSignin bool) (string, string) { t.Helper() m, token, err := f.app.store.createMeeting(context.Background(), "Planning", nil, "person:1") if err != nil { t.Fatal(err) } if requireSignin { if _, err := f.app.db.Writer().Exec("UPDATE meetings SET require_signin = 1 WHERE id = ?", m.ID); err != nil { t.Fatal(err) } } return m.Sqid, token } // doorMeetingModes runs fn with Home, without, and in team mode, each with // a way to make a meeting (by a member, then from an empty jar) and // return its sqid and invitation token. func doorMeetingModes(t *testing.T, fn func(t *testing.T, f *homeRouterFixture, meeting func(requireSignin bool) (string, string))) { doorModes(t, func(t *testing.T, f *homeRouterFixture) { fn(t, f, func(requireSignin bool) (string, string) { return doorMeeting(t, f, requireSignin) }) }) t.Run("team", func(t *testing.T) { tf := newTeamFixture(t) fn(t, tf.homeRouterFixture, func(requireSignin bool) (string, string) { tf.member(teamSubject('h'), "member") sqid, token := tf.meeting("Planning") if requireSignin { if _, err := tf.app.db.Writer().Exec("UPDATE meetings SET require_signin = 1 WHERE sqid = ?", sqid); err != nil { t.Fatal(err) } } tf.cookies = map[string]*http.Cookie{} return sqid, token }) }) } // Paul's ruling: Calls guests join by name by default, Oficina ID only on // explicit choice. The guest's way in is the invitation, untouched by the // door: the name form, the guest session, the holding screen. And a guest // who then opens the door is drawn it as anybody signed out is: never sent // to a page that 404s for a guest, never refused, never signed out. func TestAGuestJoiningByNameIsUnaffected(t *testing.T) { doorMeetingModes(t, func(t *testing.T, f *homeRouterFixture, meeting func(bool) (string, string)) { sqid, token := meeting(false) invite := f.get("/j/" + token) if invite.Code != http.StatusOK || !strings.Contains(invite.Body.String(), `name="name"`) { t.Fatalf("the invitation = %d, want the name form", invite.Code) } join := f.do(http.MethodPost, "/j/"+token+"/guest", url.Values{"name": {"Grace"}}.Encode()) redirectsTo(t, join, "/m/"+sqid+"/join") guest := f.sessionCookie() if guest == nil { t.Fatal("no guest session") } if w := f.get("/m/" + sqid + "/join"); w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "Planning") { t.Fatalf("the holding screen for a guest = %d", w.Code) } // The door, for a guest. rec := f.get("/signin") card := door(t, rec.Body.String()) if rec.Code != http.StatusOK || strings.Contains(card, signinRefusal) || strings.Contains(card, signinRemoved) { t.Fatalf("the door for a guest: %d\n%s", rec.Code, card) } if clears(rec, f.app.sessionCookie()) || !f.liveSession(guest.Value) { t.Fatal("the door ended a guest's session") } if w := f.get("/m/" + sqid + "/join"); w.Code != http.StatusOK { t.Fatalf("after the door, the guest's holding screen = %d", w.Code) } if ad := f.app.admission(f.jarRequest(), true); ad.signedIn { t.Fatal("the door's probe judges a guest's session") } }) } // An outsider through the join cookie (an invitation to a meeting that // needs an emailed sign-in) signs in by email, as ever, and lands in the // meeting, the pending join taken at /continue. The door opens on the // email field: Oficina ID is not its default there. func TestAnEmailSigninThroughTheJoinCookie(t *testing.T) { doorMeetingModes(t, func(t *testing.T, f *homeRouterFixture, meeting func(bool) (string, string)) { sqid, token := meeting(true) redirectsTo(t, f.get("/j/"+token), "/signin") rec := f.get("/signin") card := door(t, rec.Body.String()) if rec.Code != http.StatusOK || strings.Contains(card, oficinaButton) || !strings.Contains(card, emailField) { t.Fatalf("the door with a join cookie: %d\n%s", rec.Code, card) } if got := f.emailSignin(t, url.Values{"address": {"outsider@example.test"}}); got != "/m/"+sqid+"/join" { t.Fatalf("the outsider landed on %q, want the meeting", got) } if w := f.get("/m/" + sqid + "/join"); w.Code != http.StatusOK { t.Fatalf("the outsider's holding screen = %d", w.Code) } }) } // Paul's ruling (2026-11-22, #46): while an invitation waits, the plain // door is the email way, whatever the browser used last, and Oficina ID is // a row under Other ways. A step-up still never offers it. With Home, in // both modes. func TestTheJoinCookieDoorIsEmailFirstWithOficinaIDUnderOtherWays(t *testing.T) { homeMeetingModes(t, func(t *testing.T, f *homeRouterFixture, meeting func(bool) (string, string)) { _, token := meeting(true) f.get("/j/" + token) card := door(t, f.get("/signin").Body.String()) if strings.Contains(card, oficinaButton) || !strings.Contains(card, emailField) || !strings.Contains(card, otherWays) { t.Fatalf("the join cookie's plain door:\n%s", card) } chooser := door(t, f.get(waysHref(t, card)).Body.String()) if !strings.Contains(chooser, "Oficina ID") || !strings.Contains(chooser, `href="/signin?way=oficina"`) { t.Fatalf("the join cookie's chooser has no Oficina ID row:\n%s", chooser) } if way := door(t, f.get("/signin?way=oficina").Body.String()); !strings.Contains(way, oficinaButton) { t.Fatalf("the Oficina ID row's page:\n%s", way) } for _, step := range []string{"/signin?reauth=1", "/signin?reauth=1&ways=1&way=email"} { if card := door(t, f.get(step).Body.String()); strings.Contains(card, "Oficina ID") { t.Fatalf("a step-up with a join cookie offers Oficina ID (%s):\n%s", step, card) } } // Remembered Oficina ID does not make it the default either: the // email way, with the remembered address. f.remember(t, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.test"}) card = door(t, f.get("/signin").Body.String()) if strings.Contains(card, oficinaButton) || !strings.Contains(card, "Continue as alice@example.test") { t.Fatalf("the join cookie's door with Oficina ID remembered:\n%s", card) } }) } // Without the join cookie, the same browser's door opens on Oficina ID // again (with Home): the cookie, not the browser, decides. func TestTheJoinCookieAloneMakesEmailTheDefault(t *testing.T) { f := newHomeRouterFixture(t) _, token := doorMeeting(t, f, true) f.get("/j/" + token) if card := door(t, f.get("/signin").Body.String()); strings.Contains(card, oficinaButton) { t.Fatal("the join cookie's door opens on Oficina ID") } delete(f.cookies, joinCookieName) if card := door(t, f.get("/signin").Body.String()); !strings.Contains(card, oficinaButton) { t.Fatalf("without the join cookie the door does not open on Oficina ID:\n%s", card) } } // An Oficina ID sign-in chosen under Other ways, with an invitation // waiting, lands on the invitation, as an email sign-in does through // /continue: the exchange lands on /continue, never on a page the request // named, and /continue follows the cookie's own link. func TestAnOficinaSigninFromTheJoinCookieLandsOnTheInvitation(t *testing.T) { t.Run("home", func(t *testing.T) { f := newHomeRouterFixture(t) f.link(t) homeSignout(t, f.do(http.MethodPost, "/signout", "")) sqid, token := doorMeeting(t, f, true) redirectsTo(t, f.get("/j/"+token), "/signin") // A page kept by an earlier attempt, which the invitation spends. w := httptest.NewRecorder() f.app.rememberReturn(w, "/settings") f.cookies[f.app.returnCookieName()] = w.Result().Cookies()[0] start := homeRedirect(t, f.get("/signin/oficina?return_to=%2Fsettings")) if !strings.Contains(start, "return_to=%2Fcontinue") { t.Fatalf("the exchange starts for %q, want /continue", start) } landing := homeRedirect(t, f.exchange(t, start, "opaque-home-alice")) if landing != "/continue" { t.Fatalf("the exchange landed on %q", landing) } cont := f.get(landing) redirectsTo(t, cont, "/m/"+sqid+"/join") if f.cookies[joinCookieName] != nil { t.Fatal("the join cookie outlived the sign-in") } if !clears(cont, f.app.returnCookieName()) || f.returnPath() != "" { t.Fatal("/continue followed the invitation and kept the return page") } // A second /continue lands on the list, not the stale page. redirectsTo(t, f.get("/continue"), "/") }) t.Run("team", func(t *testing.T) { tf := newTeamFixture(t) tf.member(teamSubject('h'), "member") sqid, token := tf.meeting("Planning") if _, err := tf.app.db.Writer().Exec("UPDATE meetings SET require_signin = 1 WHERE sqid = ?", sqid); err != nil { t.Fatal(err) } joiner := teamSubject('j') tf.dir.Set(joiner, "member") tf.refreshed() tf.cookies = map[string]*http.Cookie{} redirectsTo(t, tf.get("/j/"+token), "/signin") start := homeRedirect(t, tf.get("/signin/oficina")) fin := tf.exchange(t, start, joiner) if fin.Code == http.StatusSeeOther && fin.Header().Get("Location") == connect.ChoosePath { fin = tf.choose("new") } landing := homeRedirect(t, fin) if landing != "/continue" { t.Fatalf("the exchange landed on %q", landing) } redirectsTo(t, tf.get(landing), "/m/"+sqid+"/join") if w := tf.get("/m/" + sqid + "/join"); w.Code != http.StatusOK { t.Fatalf("the member's holding screen = %d", w.Code) } }) } // homeMeetingModes is doorMeetingModes with Home only: with Home and in // team mode. func homeMeetingModes(t *testing.T, fn func(t *testing.T, f *homeRouterFixture, meeting func(bool) (string, string))) { doorMeetingModes(t, func(t *testing.T, f *homeRouterFixture, meeting func(bool) (string, string)) { if f.app.home == nil { t.Skip("no Oficina ID without Home") } fn(t, f, meeting) }) } // waysHref is the door's "Other ways to sign in" href, unescaped. func waysHref(t *testing.T, card string) string { t.Helper() m := regexp.MustCompile(`href="(/signin\?[^"]*ways=1[^"]*)"`).FindStringSubmatch(card) if m == nil { t.Fatalf("no Other ways link:\n%s", card) } return m[1] } // A native app's session (native_auth.go: a session of its own, a copy of // the browser's) is never refused or ended by the web door. Presented at // the door it is a signed-in session, sent on; and the door refusing the // browser's own session ends that one alone. func TestTheDoorNeverEndsANativeSession(t *testing.T) { f := newHomeRouterFixture(t) f.link(t) browser := f.sessionCookie().Value s, ok := f.app.nativeAuth.sessions.From(f.jarRequest()) if !ok { t.Fatal("no session") } native, err := f.app.nativeAuth.sessions.Mint(sessions.Session{Subject: s.Subject, Method: s.Method, AuthTime: s.AuthTime, At: time.Now()}) if err != nil { t.Fatal(err) } app := f.fresh() app.cookies[f.app.sessionCookie()] = &http.Cookie{Name: f.app.sessionCookie(), Value: native} redirectsTo(t, app.get("/signin"), "/") if !f.liveSession(native) { t.Fatal("the door ended the native session it was shown") } // Removed: the browser's session is refused at the door and ended; the // native one is not the door's to end (its own next request is // requirePerson's, as every request is). f.app.cfg.allow = allowlist{} if rec := f.get("/signin"); !strings.Contains(rec.Body.String(), signinRemoved) { t.Fatal("no removal at the door") } if f.liveSession(browser) { t.Fatal("the browser's refused session survived") } if !f.liveSession(native) { t.Fatal("the web door ended the native app's session") } }