package calls // The door's refusals: a verified address authorize refuses (auth's // Refused), a session the signed-in routes no longer admit (the admission // probe), and a gate that cannot decide (a 5xx, never a refusal). import ( "net/http" "net/http/httptest" "strings" "testing" "time" "amadan.net/oficina/home/connect" "amadan.net/rastrillo/rastrillo/lastsignin" ) // Paul's wording (approved for Docs and Sheets, 2026-11-21/22, the same // sentence with the app's name). func TestTheRefusalIsPaulsWording(t *testing.T) { if signinRefusal != "That email can’t sign in to Calls. Use a different email, or ask whoever runs this team to add it." { t.Fatalf("refusal = %q", signinRefusal) } } // auth's Refused hook draws the door with the refusal in the app-problem // slot and the email field, in a 403, on the plain door only. func TestRefusedDrawsTheDoorWithTheRefusal(t *testing.T) { doorModes(t, func(t *testing.T, f *homeRouterFixture) { w := httptest.NewRecorder() f.app.refused(w, httptest.NewRequest(http.MethodGet, homeTestOrigin+"/auth/callback?state=x", nil), rauthIdentity("someone@else.test")) card := door(t, w.Body.String()) if w.Code != http.StatusForbidden || !strings.Contains(card, appProblem) || !strings.Contains(card, signinRefusal) || !strings.Contains(card, emailField) { t.Fatalf("refused: %d\n%s", w.Code, card) } // Nobody can summon it by typing a query. for _, path := range []string{"/signin?err=refused", "/signin?ways=1&err=refused"} { if body := f.fresh().get(path).Body.String(); strings.Contains(body, signinRefusal) { t.Errorf("GET %s draws the refusal", path) } } }) } // A removed Home member's live session at the door: the removal is drawn // on the plain door, the session is ended on the door's own response, a // remembered Oficina ID is forgotten, and the next /signin is an ordinary // door, not a loop. func TestARemovedMembersSessionAtTheDoor(t *testing.T) { for _, path := range []string{"/signin", "/signin/local"} { t.Run(path, func(t *testing.T) { f := newHomeRouterFixture(t) f.link(t) if rec, res := f.remembered(); res != lastsignin.Valid || rec.Method != lastsignin.MethodFederated { t.Fatalf("after the Oficina sign-in the jar holds %+v (%v)", rec, res) } session := f.sessionCookie().Value f.app.cfg.allow = allowlist{} rec := f.get(path) card := door(t, rec.Body.String()) if rec.Code != http.StatusOK || !strings.Contains(card, signinRemoved) || !strings.Contains(card, appProblem) { t.Fatalf("GET %s: %d\n%s", path, rec.Code, card) } if !clears(rec, f.app.sessionCookie()) || f.liveSession(session) { t.Fatal("the door kept the removed member's session") } if _, res := f.remembered(); res == lastsignin.Valid { t.Fatal("the door kept the remembered Oficina ID") } again := f.get("/signin") if again.Code != http.StatusOK || strings.Contains(again.Body.String(), signinRemoved) { t.Fatalf("the next /signin: %d", again.Code) } }) } } // Other ways stays reachable for a refused session: the chooser draws no // refusal and lists its ways, while the session is still ended; and with // the remembered Oficina ID forgotten, the plain door offers Oficina ID // afresh (under another account, say). func TestARefusedSessionCanReachOtherWays(t *testing.T) { f := newHomeRouterFixture(t) f.link(t) f.app.cfg.allow = allowlist{} rec := f.get("/signin?ways=1") card := door(t, rec.Body.String()) if rec.Code != http.StatusOK || strings.Contains(card, signinRemoved) || strings.Contains(card, signinRefusal) || !strings.Contains(card, "Email alice@example.test") || !strings.Contains(card, "Use a different email") { t.Fatalf("the chooser for a refused session: %d\n%s", rec.Code, card) } if f.sessionCookie() != nil { t.Fatal("the chooser kept a refused session") } if card := door(t, f.get("/signin").Body.String()); !strings.Contains(card, oficinaButton) { t.Fatalf("after the refusal the door does not offer Oficina ID:\n%s", card) } } // Continue with Oficina ID for a refused session ends it and starts the // exchange, rather than sending it back to the page that refuses it. func TestARefusedSessionAtSigninOficinaStartsTheExchange(t *testing.T) { f := newHomeRouterFixture(t) f.link(t) f.app.cfg.allow = allowlist{} rec := f.get("/signin/oficina") if rec.Code != http.StatusSeeOther || !strings.HasPrefix(rec.Header().Get("Location"), "/auth/start") { t.Fatalf("GET /signin/oficina: %d %q", rec.Code, rec.Header().Get("Location")) } if f.sessionCookie() != nil { t.Fatal("the removed member's session survived") } } // A remembered email way is no loop, so a refusal keeps it. func TestARefusalKeepsARememberedEmailWay(t *testing.T) { f := newHomeRouterFixture(t) f.link(t) f.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "alice@example.test"}) f.app.cfg.allow = allowlist{} f.get("/signin") if rec, res := f.remembered(); res != lastsignin.Valid || rec.Method != lastsignin.MethodMagicLink { t.Fatalf("the refusal dropped the email way: %+v (%v)", rec, res) } } // Standalone, a session the signed-in routes refuse (here a Home sign-in // on an instance with no Home) gets the door with the refusal and is ended. func TestStandaloneRefusedSession(t *testing.T) { f := newStandaloneDoor(t) f.session(t, "opaque-home-person", connect.HomeMethod, time.Now()) session := f.sessionCookie().Value rec := f.get("/signin") card := door(t, rec.Body.String()) if rec.Code != http.StatusOK || !strings.Contains(card, signinRefusal) || !strings.Contains(card, emailField) { t.Fatalf("standalone refused session: %d\n%s", rec.Code, card) } if !clears(rec, f.app.sessionCookie()) || f.liveSession(session) { t.Fatal("the standalone door kept a refused session") } } // The removal is Calls' own sentence, on the plain door, in both modes. func TestTheRemovalIsOnThePlainDoorOnly(t *testing.T) { doorModes(t, func(t *testing.T, f *homeRouterFixture) { if card := door(t, f.get("/signin?err=removed").Body.String()); !strings.Contains(card, signinRemoved) || strings.Contains(card, "Something went wrong") { t.Fatalf("?err=removed:\n%s", card) } if card := door(t, f.get("/signin?err=removed&ways=1").Body.String()); strings.Contains(card, signinRemoved) { t.Fatalf("the chooser draws the removal:\n%s", card) } }) } // A gate that cannot decide is no refusal. Identity storage that cannot be // read (the persons table gone) is a 503 on a page, and at the door: no // refusal, the session and the remembered Oficina ID kept, nothing set on // /signin. The strict probe MintHomeSession asks refuses it, so nothing // would be remembered on one. func TestAnUndecidedGateIsNoRefusal(t *testing.T) { f := newHomeRouterFixture(t) f.link(t) session := f.sessionCookie().Value _, token := doorMeeting(t, f, false) if _, err := f.app.db.Writer().Exec("ALTER TABLE persons RENAME TO persons_away"); err != nil { t.Fatal(err) } t.Cleanup(func() { _, _ = f.app.db.Writer().Exec("ALTER TABLE persons_away RENAME TO persons") }) if rec := f.doWith(http.MethodGet, "/", "", http.Header{"Accept": {"text/html"}}); rec.Code != http.StatusServiceUnavailable || rec.Header().Get("Cache-Control") != "no-store" { t.Fatalf("a page with identity storage down = %d %q, want a no-store 503", rec.Code, rec.Header().Get("Cache-Control")) } // The same no-store 503 everywhere storage is met: an invitation // followed signed in, and the native apps' API (a deliberate change: // outside team mode this was a 404, or a 500 on the invitation). if rec := f.doWith(http.MethodGet, "/j/"+token, "", http.Header{"Accept": {"text/html"}}); rec.Code != http.StatusServiceUnavailable || rec.Header().Get("Cache-Control") != "no-store" { t.Fatalf("following an invitation with identity storage down = %d %q, want a no-store 503", rec.Code, rec.Header().Get("Cache-Control")) } if rec := f.doWith(http.MethodPost, "/native/meetings/start", `{}`, http.Header{"Content-Type": {"application/json"}}); rec.Code != http.StatusServiceUnavailable || rec.Header().Get("Cache-Control") != "no-store" { t.Fatalf("the native API with identity storage down = %d %q, want a no-store 503", rec.Code, rec.Header().Get("Cache-Control")) } if !f.liveSession(session) { t.Fatal("a 503 ended the session") } rec := f.get("/signin") if strings.Contains(rec.Body.String(), signinRefusal) || strings.Contains(rec.Body.String(), signinRemoved) { t.Fatal("the door refused a session it could not judge") } if len(rec.Result().Cookies()) != 0 { t.Fatalf("/signin set cookies on an undecided session: %v", rec.Result().Cookies()) } if !f.liveSession(session) { t.Fatal("the door ended a session it could not judge") } if r, res := f.remembered(); res != lastsignin.Valid || r.Method != lastsignin.MethodFederated { t.Fatal("the door forgot Oficina ID for a session it could not judge") } if f.app.admission(f.jarRequest(), true).admitted { t.Fatal("the strict probe admitted a session it could not judge") } if !f.app.admission(f.jarRequest(), false).admitted { t.Fatal("the door's probe refused a session it could not judge") } } // The refusal is the plain door's only: a refused session at the chooser // (?ways=) or a way's own page (?way=) is drawn no refusal, standalone as // with Home, and is still ended. func TestARefusalIsNotDrawnOnTheChooser(t *testing.T) { for _, path := range []string{"/signin?ways=1", "/signin?way=email"} { f := newStandaloneDoor(t) f.session(t, "opaque-home-person", connect.HomeMethod, time.Now()) rec := f.get(path) if rec.Code != http.StatusOK || strings.Contains(rec.Body.String(), signinRefusal) { t.Fatalf("GET %s for a refused session: %d, refusal drawn", path, rec.Code) } if f.sessionCookie() != nil { t.Fatalf("GET %s kept a refused session", path) } } }