| 1 | package calls |
| 2 | |
| 3 | // The door's refusals: a verified address authorize refuses (auth's |
| 4 | // Refused), a session the signed-in routes no longer admit (the admission |
| 5 | // probe), and a gate that cannot decide (a 5xx, never a refusal). |
| 6 | |
| 7 | import ( |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/home/connect" |
| 15 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 16 | ) |
| 17 | |
| 18 | // Paul's wording (approved for Docs and Sheets, 2026-11-21/22, the same |
| 19 | // sentence with the app's name). |
| 20 | func TestTheRefusalIsPaulsWording(t *testing.T) { |
| 21 | if signinRefusal != "That email can’t sign in to Calls. Use a different email, or ask whoever runs this team to add it." { |
| 22 | t.Fatalf("refusal = %q", signinRefusal) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | // auth's Refused hook draws the door with the refusal in the app-problem |
| 27 | // slot and the email field, in a 403, on the plain door only. |
| 28 | func TestRefusedDrawsTheDoorWithTheRefusal(t *testing.T) { |
| 29 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 30 | w := httptest.NewRecorder() |
| 31 | f.app.refused(w, httptest.NewRequest(http.MethodGet, homeTestOrigin+"/auth/callback?state=x", nil), rauthIdentity("someone@else.test")) |
| 32 | card := door(t, w.Body.String()) |
| 33 | if w.Code != http.StatusForbidden || !strings.Contains(card, appProblem) || !strings.Contains(card, signinRefusal) || !strings.Contains(card, emailField) { |
| 34 | t.Fatalf("refused: %d\n%s", w.Code, card) |
| 35 | } |
| 36 | // Nobody can summon it by typing a query. |
| 37 | for _, path := range []string{"/signin?err=refused", "/signin?ways=1&err=refused"} { |
| 38 | if body := f.fresh().get(path).Body.String(); strings.Contains(body, signinRefusal) { |
| 39 | t.Errorf("GET %s draws the refusal", path) |
| 40 | } |
| 41 | } |
| 42 | }) |
| 43 | } |
| 44 | |
| 45 | // A removed Home member's live session at the door: the removal is drawn |
| 46 | // on the plain door, the session is ended on the door's own response, a |
| 47 | // remembered Oficina ID is forgotten, and the next /signin is an ordinary |
| 48 | // door, not a loop. |
| 49 | func TestARemovedMembersSessionAtTheDoor(t *testing.T) { |
| 50 | for _, path := range []string{"/signin", "/signin/local"} { |
| 51 | t.Run(path, func(t *testing.T) { |
| 52 | f := newHomeRouterFixture(t) |
| 53 | f.link(t) |
| 54 | if rec, res := f.remembered(); res != lastsignin.Valid || rec.Method != lastsignin.MethodFederated { |
| 55 | t.Fatalf("after the Oficina sign-in the jar holds %+v (%v)", rec, res) |
| 56 | } |
| 57 | session := f.sessionCookie().Value |
| 58 | f.app.cfg.allow = allowlist{} |
| 59 | rec := f.get(path) |
| 60 | card := door(t, rec.Body.String()) |
| 61 | if rec.Code != http.StatusOK || !strings.Contains(card, signinRemoved) || !strings.Contains(card, appProblem) { |
| 62 | t.Fatalf("GET %s: %d\n%s", path, rec.Code, card) |
| 63 | } |
| 64 | if !clears(rec, f.app.sessionCookie()) || f.liveSession(session) { |
| 65 | t.Fatal("the door kept the removed member's session") |
| 66 | } |
| 67 | if _, res := f.remembered(); res == lastsignin.Valid { |
| 68 | t.Fatal("the door kept the remembered Oficina ID") |
| 69 | } |
| 70 | again := f.get("/signin") |
| 71 | if again.Code != http.StatusOK || strings.Contains(again.Body.String(), signinRemoved) { |
| 72 | t.Fatalf("the next /signin: %d", again.Code) |
| 73 | } |
| 74 | }) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Other ways stays reachable for a refused session: the chooser draws no |
| 79 | // refusal and lists its ways, while the session is still ended; and with |
| 80 | // the remembered Oficina ID forgotten, the plain door offers Oficina ID |
| 81 | // afresh (under another account, say). |
| 82 | func TestARefusedSessionCanReachOtherWays(t *testing.T) { |
| 83 | f := newHomeRouterFixture(t) |
| 84 | f.link(t) |
| 85 | f.app.cfg.allow = allowlist{} |
| 86 | rec := f.get("/signin?ways=1") |
| 87 | card := door(t, rec.Body.String()) |
| 88 | 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") { |
| 89 | t.Fatalf("the chooser for a refused session: %d\n%s", rec.Code, card) |
| 90 | } |
| 91 | if f.sessionCookie() != nil { |
| 92 | t.Fatal("the chooser kept a refused session") |
| 93 | } |
| 94 | if card := door(t, f.get("/signin").Body.String()); !strings.Contains(card, oficinaButton) { |
| 95 | t.Fatalf("after the refusal the door does not offer Oficina ID:\n%s", card) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Continue with Oficina ID for a refused session ends it and starts the |
| 100 | // exchange, rather than sending it back to the page that refuses it. |
| 101 | func TestARefusedSessionAtSigninOficinaStartsTheExchange(t *testing.T) { |
| 102 | f := newHomeRouterFixture(t) |
| 103 | f.link(t) |
| 104 | f.app.cfg.allow = allowlist{} |
| 105 | rec := f.get("/signin/oficina") |
| 106 | if rec.Code != http.StatusSeeOther || !strings.HasPrefix(rec.Header().Get("Location"), "/auth/start") { |
| 107 | t.Fatalf("GET /signin/oficina: %d %q", rec.Code, rec.Header().Get("Location")) |
| 108 | } |
| 109 | if f.sessionCookie() != nil { |
| 110 | t.Fatal("the removed member's session survived") |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // A remembered email way is no loop, so a refusal keeps it. |
| 115 | func TestARefusalKeepsARememberedEmailWay(t *testing.T) { |
| 116 | f := newHomeRouterFixture(t) |
| 117 | f.link(t) |
| 118 | f.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "alice@example.test"}) |
| 119 | f.app.cfg.allow = allowlist{} |
| 120 | f.get("/signin") |
| 121 | if rec, res := f.remembered(); res != lastsignin.Valid || rec.Method != lastsignin.MethodMagicLink { |
| 122 | t.Fatalf("the refusal dropped the email way: %+v (%v)", rec, res) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Standalone, a session the signed-in routes refuse (here a Home sign-in |
| 127 | // on an instance with no Home) gets the door with the refusal and is ended. |
| 128 | func TestStandaloneRefusedSession(t *testing.T) { |
| 129 | f := newStandaloneDoor(t) |
| 130 | f.session(t, "opaque-home-person", connect.HomeMethod, time.Now()) |
| 131 | session := f.sessionCookie().Value |
| 132 | rec := f.get("/signin") |
| 133 | card := door(t, rec.Body.String()) |
| 134 | if rec.Code != http.StatusOK || !strings.Contains(card, signinRefusal) || !strings.Contains(card, emailField) { |
| 135 | t.Fatalf("standalone refused session: %d\n%s", rec.Code, card) |
| 136 | } |
| 137 | if !clears(rec, f.app.sessionCookie()) || f.liveSession(session) { |
| 138 | t.Fatal("the standalone door kept a refused session") |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // The removal is Calls' own sentence, on the plain door, in both modes. |
| 143 | func TestTheRemovalIsOnThePlainDoorOnly(t *testing.T) { |
| 144 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 145 | if card := door(t, f.get("/signin?err=removed").Body.String()); !strings.Contains(card, signinRemoved) || strings.Contains(card, "Something went wrong") { |
| 146 | t.Fatalf("?err=removed:\n%s", card) |
| 147 | } |
| 148 | if card := door(t, f.get("/signin?err=removed&ways=1").Body.String()); strings.Contains(card, signinRemoved) { |
| 149 | t.Fatalf("the chooser draws the removal:\n%s", card) |
| 150 | } |
| 151 | }) |
| 152 | } |
| 153 | |
| 154 | // A gate that cannot decide is no refusal. Identity storage that cannot be |
| 155 | // read (the persons table gone) is a 503 on a page, and at the door: no |
| 156 | // refusal, the session and the remembered Oficina ID kept, nothing set on |
| 157 | // /signin. The strict probe MintHomeSession asks refuses it, so nothing |
| 158 | // would be remembered on one. |
| 159 | func TestAnUndecidedGateIsNoRefusal(t *testing.T) { |
| 160 | f := newHomeRouterFixture(t) |
| 161 | f.link(t) |
| 162 | session := f.sessionCookie().Value |
| 163 | _, token := doorMeeting(t, f, false) |
| 164 | if _, err := f.app.db.Writer().Exec("ALTER TABLE persons RENAME TO persons_away"); err != nil { |
| 165 | t.Fatal(err) |
| 166 | } |
| 167 | t.Cleanup(func() { _, _ = f.app.db.Writer().Exec("ALTER TABLE persons_away RENAME TO persons") }) |
| 168 | if rec := f.doWith(http.MethodGet, "/", "", http.Header{"Accept": {"text/html"}}); rec.Code != http.StatusServiceUnavailable || rec.Header().Get("Cache-Control") != "no-store" { |
| 169 | t.Fatalf("a page with identity storage down = %d %q, want a no-store 503", rec.Code, rec.Header().Get("Cache-Control")) |
| 170 | } |
| 171 | // The same no-store 503 everywhere storage is met: an invitation |
| 172 | // followed signed in, and the native apps' API (a deliberate change: |
| 173 | // outside team mode this was a 404, or a 500 on the invitation). |
| 174 | if rec := f.doWith(http.MethodGet, "/j/"+token, "", http.Header{"Accept": {"text/html"}}); rec.Code != http.StatusServiceUnavailable || rec.Header().Get("Cache-Control") != "no-store" { |
| 175 | t.Fatalf("following an invitation with identity storage down = %d %q, want a no-store 503", rec.Code, rec.Header().Get("Cache-Control")) |
| 176 | } |
| 177 | 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" { |
| 178 | t.Fatalf("the native API with identity storage down = %d %q, want a no-store 503", rec.Code, rec.Header().Get("Cache-Control")) |
| 179 | } |
| 180 | if !f.liveSession(session) { |
| 181 | t.Fatal("a 503 ended the session") |
| 182 | } |
| 183 | rec := f.get("/signin") |
| 184 | if strings.Contains(rec.Body.String(), signinRefusal) || strings.Contains(rec.Body.String(), signinRemoved) { |
| 185 | t.Fatal("the door refused a session it could not judge") |
| 186 | } |
| 187 | if len(rec.Result().Cookies()) != 0 { |
| 188 | t.Fatalf("/signin set cookies on an undecided session: %v", rec.Result().Cookies()) |
| 189 | } |
| 190 | if !f.liveSession(session) { |
| 191 | t.Fatal("the door ended a session it could not judge") |
| 192 | } |
| 193 | if r, res := f.remembered(); res != lastsignin.Valid || r.Method != lastsignin.MethodFederated { |
| 194 | t.Fatal("the door forgot Oficina ID for a session it could not judge") |
| 195 | } |
| 196 | if f.app.admission(f.jarRequest(), true).admitted { |
| 197 | t.Fatal("the strict probe admitted a session it could not judge") |
| 198 | } |
| 199 | if !f.app.admission(f.jarRequest(), false).admitted { |
| 200 | t.Fatal("the door's probe refused a session it could not judge") |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // The refusal is the plain door's only: a refused session at the chooser |
| 205 | // (?ways=) or a way's own page (?way=) is drawn no refusal, standalone as |
| 206 | // with Home, and is still ended. |
| 207 | func TestARefusalIsNotDrawnOnTheChooser(t *testing.T) { |
| 208 | for _, path := range []string{"/signin?ways=1", "/signin?way=email"} { |
| 209 | f := newStandaloneDoor(t) |
| 210 | f.session(t, "opaque-home-person", connect.HomeMethod, time.Now()) |
| 211 | rec := f.get(path) |
| 212 | if rec.Code != http.StatusOK || strings.Contains(rec.Body.String(), signinRefusal) { |
| 213 | t.Fatalf("GET %s for a refused session: %d, refusal drawn", path, rec.Code) |
| 214 | } |
| 215 | if f.sessionCookie() != nil { |
| 216 | t.Fatalf("GET %s kept a refused session", path) |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |