oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1package slides
2
3// The refusal on the door, and a session the signed-in routes refuse.
4
5import (
6 "database/sql"
7 "net/http"
8 "net/http/httptest"
9 "regexp"
10 "strings"
11 "testing"
12
13 "amadan.net/rastrillo/rastrillo"
14 rauth "amadan.net/rastrillo/rastrillo/auth"
15 "amadan.net/rastrillo/rastrillo/lastsignin"
16)
17
18// A session the signed-in routes refuse (an address taken off the
19// allowlist) reaching the door, with Home: the door says why in the
20// app-problem slot with the email field, the session's cookie is cleared
21// on this very response, a remembered Oficina ID is forgotten (offered
22// again it would sign the same refused person straight back here), and a
23// remembered email way is kept. Nothing loops: /signin and
24// /signin/oficina answer the door and the exchange, never the page.
25func TestARefusedSessionGetsTheRefusalWithHome(t *testing.T) {
26 b, _ := newHomeBrowser(t)
27 for _, c := range []struct {
28 name string
29 record lastsignin.Record
30 kept bool
31 }{
32 {"Oficina ID remembered", lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "gone@example.com"}, false},
33 {"email remembered", lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "gone@example.com"}, true},
34 } {
35 for _, path := range []string{"/signin", "/signin/local", "/signin?return_to=%2Fd%2F7"} {
36 t.Run(c.name+" "+path, func(t *testing.T) {
37 br := b.browser()
38 br.remember(c.record)
39 session := homeSession(t, b.a, "gone@example.com", "magiclink")
40 br.set(session)
41 rec := br.get(path)
42 if rec.Code != http.StatusOK {
43 t.Fatalf("GET %s: %d %q, want the door", path, rec.Code, rec.Header().Get("Location"))
44 }
45 card := door(t, rec.Body.String())
46 if !strings.Contains(card, appProblem) || !strings.Contains(card, refusalText) || !strings.Contains(card, emailField) {
47 t.Fatalf("the door does not say why:\n%s", card)
48 }
49 if !clears(rec, session.Name) {
50 t.Error("the refused session's cookie is not cleared on the door's own response")
51 }
52 if _, still := br.jar[session.Name]; still {
53 t.Error("the jar still holds the refused session")
54 }
55 _, res := br.remembered()
56 if kept := res == lastsignin.Valid; kept != c.kept {
57 t.Errorf("remembered way kept = %v, want %v", kept, c.kept)
58 }
59 })
60 }
61 }
62 // And Continue with Oficina ID starts the exchange rather than sending
63 // the refused session back to a page that refuses it.
64 br := b.browser()
65 br.set(homeSession(t, b.a, "gone@example.com", "magiclink"))
66 if rec := br.get("/signin/oficina"); rec.Code != http.StatusSeeOther || !strings.HasPrefix(rec.Header().Get("Location"), "/auth/start?") {
67 t.Fatalf("a refused session at /signin/oficina: %d %q, want the Home exchange", rec.Code, rec.Header().Get("Location"))
68 }
69}
70
71// Standalone too: the refusal, the cookie cleared, no loop.
72func TestARefusedSessionGetsTheRefusalStandalone(t *testing.T) {
73 b, _ := newStandaloneBrowser(t)
74 session := standaloneSession(t, b.a, "gone@example.com")
75 b.set(session)
76 rec := b.get("/signin")
77 card := door(t, rec.Body.String())
78 if rec.Code != http.StatusOK || !strings.Contains(card, refusalText) || !strings.Contains(card, emailField) {
79 t.Fatalf("GET /signin: %d\n%s", rec.Code, card)
80 }
81 if !clears(rec, session.Name) {
82 t.Error("the refused session's cookie is not cleared")
83 }
84 if again := b.get("/signin"); strings.Contains(again.Body.String(), refusalText) {
85 t.Error("a reload is not plainly signed out")
86 }
87}
88
89// The refusal is only on the plain door: the chooser and a way's own page
90// are how a refused person reaches another way in, another Oficina ID
91// account included (Fable on Docs #139, N1). The refused door's Other ways
92// link leads to a chooser with Oficina ID on it, and no refusal.
93func TestARefusedSessionCanReachOtherWays(t *testing.T) {
94 b, _ := newHomeBrowser(t)
95 br := b.browser()
96 br.set(homeSession(t, b.a, "gone@example.com", "magiclink"))
97 card := door(t, br.get("/signin").Body.String())
98 m := regexp.MustCompile(`<a rst-btn="ghost" href="([^"]+)"><span>Other ways to sign in</span>`).FindStringSubmatch(card)
99 if !strings.Contains(card, refusalText) || m == nil {
100 t.Fatalf("the refused door has no way on:\n%s", card)
101 }
102 ways := door(t, br.get(m[1]).Body.String())
103 if strings.Contains(ways, appProblem) || !strings.Contains(ways, "<span>Oficina ID</span>") {
104 t.Fatalf("GET %s:\n%s", m[1], ways)
105 }
106 for _, path := range []string{"/signin?ways=1", "/signin?way=email", "/signin?way=oficina"} {
107 br := b.browser()
108 br.set(homeSession(t, b.a, "gone@example.com", "magiclink"))
109 rec := br.get(path)
110 if card := door(t, rec.Body.String()); rec.Code != http.StatusOK || strings.Contains(card, appProblem) {
111 t.Errorf("GET %s: %d\n%s", path, rec.Code, card)
112 }
113 }
114}
115
116// auth's Refused hook: a verified address the allowlist does not hold gets
117// the door with the refusal, in a 403, and Paul's wording.
118func TestTheRefusedHookIsTheDoorWithTheRefusal(t *testing.T) {
119 for _, mk := range []func(*testing.T) (*homeBrowser, User){newHomeBrowser, newStandaloneBrowser} {
120 b, _ := mk(t)
121 loc, err := rastrillo.NewLocales(Locales, DefaultLocale, rastrillo.BaseCatalog(), LocaleFS)
122 if err != nil {
123 t.Fatal(err)
124 }
125 w := httptest.NewRecorder()
126 loc.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
127 b.a.refused(w, r, rauth.Identity{})
128 })).ServeHTTP(w, httptest.NewRequest(http.MethodGet, homeTestOrigin+"/auth/callback?state=x&code=y", nil))
129 card := door(t, w.Body.String())
130 if w.Code != http.StatusForbidden || !strings.Contains(card, refusalText) || !strings.Contains(card, emailField) || w.Header().Get("Cache-Control") != "no-store" {
131 t.Fatalf("refused: %d %q\n%s", w.Code, w.Header().Get("Cache-Control"), card)
132 }
133 if got := titleOf(t, w.Body.String()); got != "Problem signing in to Slides" {
134 t.Errorf("<title> = %q", got)
135 }
136 }
137}
138
139// A guard that cannot decide (team mode, its identity storage failing: a
140// 503) is not a refusal: /signin draws no refusal and sends the session on
141// to its page, where the 503 is; the session and a remembered Oficina ID
142// are kept; and nothing is remembered on one.
143func TestAnUndecidedGuardIsNotARefusal(t *testing.T) {
144 f := newTeamFixture(t, "x@example.com")
145 f.localUser("x@example.com")
146 f.localSession("x@example.com")
147 w := httptest.NewRecorder()
148 f.a.auth.RememberJar().Remember(w, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina})
149 for _, c := range w.Result().Cookies() {
150 f.jar[c.Name] = c
151 }
152 broken, err := sql.Open("sqlite", "file:door-closed?mode=memory")
153 if err == nil {
154 broken.Close()
155 }
156 real := f.a.team.db
157 f.a.team.db = broken
158 defer func() { f.a.team.db = real }()
159
160 if page := f.get("/"); page.Code != http.StatusServiceUnavailable {
161 t.Fatalf("the guard answers %d, want its 503", page.Code)
162 }
163 rec := f.get("/signin")
164 if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/" {
165 t.Fatalf("GET /signin under a 503: %d %q, want the page", rec.Code, rec.Header().Get("Location"))
166 }
167 if len(rec.Result().Cookies()) != 0 {
168 t.Errorf("GET /signin under a 503 set cookies: %v", rec.Result().Cookies())
169 }
170 // A state of the door draws, with no refusal.
171 if card := door(t, f.get("/signin?err=expired").Body.String()); strings.Contains(card, appProblem) {
172 t.Errorf("a refusal under a 503:\n%s", card)
173 }
174 if _, ok := f.sessionNow(); !ok {
175 t.Error("a 503 signed the person out")
176 }
177 if _, res := readRemembered(f.a, f.jar); res != lastsignin.Valid {
178 t.Error("a 503 forgot the remembered Oficina ID")
179 }
180 // Strictly, an undecided guard admits nothing, so nothing is remembered.
181 probe := httptest.NewRequest(http.MethodGet, homeTestOrigin+"/", nil)
182 for _, c := range f.jar {
183 probe.AddCookie(c)
184 }
185 if signedIn, admitted := f.a.admission(probe, true); !signedIn || admitted {
186 t.Errorf("strict admission under a 503 = %v, %v; want signed in, not admitted", signedIn, admitted)
187 }
188 if signedIn, admitted := f.a.admission(probe, false); !signedIn || !admitted {
189 t.Errorf("admission under a 503 = %v, %v; want signed in and (for the door) admitted", signedIn, admitted)
190 }
191}
192