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 smart door's test helpers (Home spec 2026-11-19-smart-door.md, step
4// 6; home/connect MIGRATING.md): another browser on the same app, its
5// remembered way in, and the two ways it signs in. They run the app's
6// real router under the locale middleware, as homeBrowser does.
7
8import (
9 "html"
10 "net/http"
11 "net/http/httptest"
12 "net/url"
13 "regexp"
14 "strings"
15 "testing"
16
17 "amadan.net/oficina/home/connect"
18 "amadan.net/rastrillo/rastrillo"
19 "amadan.net/rastrillo/rastrillo/lastsignin"
20)
21
22// newStandaloneBrowser is newHomeBrowser without Home: email is the only
23// way in, and owner@example.com the one address admitted.
24func newStandaloneBrowser(t *testing.T) (*homeBrowser, User) {
25 t.Helper()
26 d := testDB(t)
27 mux, a, err := New(d, Config{KeymailServers: testKeymailServers, Mailer: &homeTestMail{}, Env: EnvDevelopment, Origin: homeTestOrigin, Logger: quietLogger(), AllowedAddresses: []string{"owner@example.com"}})
28 if err != nil {
29 t.Fatal(err)
30 }
31 t.Cleanup(func() { a.Close() })
32 u := User{ID: 72, Email: "owner@example.com", Name: "Existing owner", Colour: "#112233"}
33 if err := d.G.Create(&u).Error; err != nil {
34 t.Fatal(err)
35 }
36 loc, err := rastrillo.NewLocales(Locales, DefaultLocale, rastrillo.BaseCatalog(), LocaleFS)
37 if err != nil {
38 t.Fatal(err)
39 }
40 return &homeBrowser{t: t, a: a, h: loc.Middleware(mux), jar: map[string]*http.Cookie{}}, u
41}
42
43// browser is another browser on b's app: the same instance, its own jar.
44func (b *homeBrowser) browser() *homeBrowser {
45 return &homeBrowser{t: b.t, a: b.a, key: b.key, h: b.h, jar: map[string]*http.Cookie{}}
46}
47
48// remember gives the browser a remembered way in, sealed by Slides' own
49// jar, as rastrillo would have after a sign-in.
50func (b *homeBrowser) remember(rec lastsignin.Record) {
51 b.t.Helper()
52 w := httptest.NewRecorder()
53 b.a.auth.RememberJar().Remember(w, rec)
54 cookies := w.Result().Cookies()
55 if len(cookies) != 1 {
56 b.t.Fatalf("the jar refused %+v", rec)
57 }
58 b.set(cookies[0])
59}
60
61// remembered is the browser's remembered way in, read by Slides' own jar.
62func (b *homeBrowser) remembered() (lastsignin.Record, lastsignin.ReadResult) {
63 return readRemembered(b.a, b.jar)
64}
65
66func readRemembered(a *app, jar map[string]*http.Cookie) (lastsignin.Record, lastsignin.ReadResult) {
67 r := httptest.NewRequest(http.MethodGet, homeTestOrigin+"/", nil)
68 for _, c := range jar {
69 r.AddCookie(c)
70 }
71 return a.auth.RememberJar().Read(r)
72}
73
74// returnPath is the page the browser's return memory holds, or "".
75func (b *homeBrowser) returnPath() string {
76 r := httptest.NewRequest(http.MethodGet, homeTestOrigin+"/", nil)
77 for _, c := range b.jar {
78 r.AddCookie(c)
79 }
80 path, _ := b.a.rememberedReturn(r)
81 return path
82}
83
84// emailSignin signs this browser in by the emailed link, posting form to
85// the door, and returns where the landing (/auth/local/complete with
86// Home, /auth/landed standalone) sends it.
87func (b *homeBrowser) emailSignin(form url.Values) string {
88 b.t.Helper()
89 b.a.cfg.Mailer.(*homeTestMail).body = ""
90 if w := b.do(http.MethodPost, signinPath, form, ""); w.Code != http.StatusSeeOther {
91 b.t.Fatalf("POST /signin: %d", w.Code)
92 }
93 b.a.FlushMail()
94 link := regexp.MustCompile(`https://[^\s<>]+/auth/verify\?token=[A-Za-z0-9_-]+`).FindString(b.a.cfg.Mailer.(*homeTestMail).body)
95 if link == "" {
96 b.t.Fatal("no sign-in link mailed")
97 }
98 u, _ := url.Parse(link)
99 open := b.get(u.RequestURI())
100 m := confirmForm.FindStringSubmatch(open.Body.String())
101 if open.Code != http.StatusOK || m == nil {
102 b.t.Fatalf("the link's confirm step: %d", open.Code)
103 }
104 verify := b.do(http.MethodPost, html.UnescapeString(m[1]), url.Values{"token": {html.UnescapeString(m[2])}}, "")
105 landing := verify.Header().Get("Location")
106 if verify.Code != http.StatusSeeOther || (landing != connect.LocalCompletePath && landing != landedPath) {
107 b.t.Fatalf("verify: %d %q", verify.Code, landing)
108 }
109 done := b.get(landing)
110 if done.Code != http.StatusSeeOther {
111 b.t.Fatalf("landing: %d", done.Code)
112 }
113 return done.Header().Get("Location")
114}
115
116// oficinaSignin runs Continue with Oficina ID for subject from path, and
117// returns the exchange's last answer.
118func (b *homeBrowser) oficinaSignin(path, subject string) *httptest.ResponseRecorder {
119 b.t.Helper()
120 start := b.get(path)
121 if start.Code != http.StatusSeeOther || !strings.HasPrefix(start.Header().Get("Location"), "/auth/start?") {
122 b.t.Fatalf("GET %s: %d %q, want the Home exchange", path, start.Code, start.Header().Get("Location"))
123 }
124 auth := b.get(start.Header().Get("Location"))
125 return b.home(auth.Header().Get("Location"), subject)
126}
127
128// redirectsTo fails unless rec is a 303 to want.
129func redirectsTo(t *testing.T, rec *httptest.ResponseRecorder, want string) {
130 t.Helper()
131 if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != want {
132 t.Fatalf("got %d %q, want 303 %q", rec.Code, rec.Header().Get("Location"), want)
133 }
134}
135
136// door is the sign-in card's own content: rastrillo's section inside
137// estilo-signin-screen, unescaped.
138func door(t *testing.T, body string) string {
139 t.Helper()
140 start := strings.Index(body, "<div estilo-signin-screen>")
141 if start < 0 {
142 t.Fatalf("no estilo-signin-screen:\n%s", body)
143 }
144 end := strings.Index(body[start:], "</section>")
145 return html.UnescapeString(body[start : start+end])
146}
147
148// titleOf is the page's <title>, unescaped.
149func titleOf(t *testing.T, body string) string {
150 t.Helper()
151 m := regexp.MustCompile(`<title>([^<]*)</title>`).FindStringSubmatch(body)
152 if m == nil {
153 t.Fatalf("no <title>:\n%s", body)
154 }
155 return html.UnescapeString(m[1])
156}
157
158// clears reports whether rec clears the named cookie.
159func clears(rec *httptest.ResponseRecorder, name string) bool {
160 for _, c := range rec.Result().Cookies() {
161 if c.Name == name && c.MaxAge < 0 {
162 return true
163 }
164 }
165 return false
166}
167
168// What the door's markup says, for the tests to look for.
169const (
170 oficinaButton = "Continue with Oficina ID"
171 otherWays = "Other ways to sign in"
172 emailField = `id="rst-signin-email" name="address"`
173 appProblem = `id="rst-signin-app-problem"`
174 // The refusal, as Paul approved it (Docs' and Sheets' sentence, with
175 // Slides' name).
176 refusalText = "That email can’t sign in to Slides. Use a different email, or ask whoever runs this team to add it."
177 removedText = "Your access has been removed, so you’ve been signed out. Ask whoever runs this team if that’s wrong."
178)
179
180// returnCookie is the return memory's cookie on the https test origin.
181const returnCookie = "__Host-slides_return"
182