oficina / calendar Public

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

Plain git — no account needed to clone.

Download

Download this file

1package calendar
2
3// The smart door's test helpers (Home spec 2026-11-19-smart-door.md, step
4// 6; home/connect MIGRATING.md): a standalone browser beside
5// homeRouterFixture's Home one, a browser's remembered way in, its return
6// memory, a session minted straight into its jar, and an email sign-in
7// that posts whatever form the test gives it.
8
9import (
10 "html"
11 "io"
12 "log/slog"
13 "net/http"
14 "net/http/httptest"
15 "net/url"
16 "path/filepath"
17 "regexp"
18 "strings"
19 "testing"
20 "time"
21
22 "amadan.net/oficina/home/connect"
23 "amadan.net/rastrillo/rastrillo/lastsignin"
24 "amadan.net/rastrillo/rastrillo/sessions"
25)
26
27// newStandaloneDoor is homeRouterFixture's browser on an instance without
28// Home: email is the only way in, and alice@example.test the one address
29// admitted. The origin is https, so the door's __Host- cookies behave as in
30// production.
31func newStandaloneDoor(t *testing.T) *homeRouterFixture {
32 t.Helper()
33 a, err := New(Config{DBPath: filepath.Join(t.TempDir(), "calendar.db"), Env: "development", Origin: homeTestOrigin,
34 AllowedAddresses: "alice@example.test", Logger: slog.New(slog.NewTextHandler(io.Discard, nil))})
35 if err != nil {
36 t.Fatal(err)
37 }
38 t.Cleanup(func() { a.Close() })
39 return &homeRouterFixture{app: a, cookies: map[string]*http.Cookie{}}
40}
41
42// fresh is another browser on f's app: the same instance, an empty jar.
43func (f *homeRouterFixture) fresh() *homeRouterFixture {
44 return &homeRouterFixture{app: f.app, key: f.key, cookies: map[string]*http.Cookie{}, behind: f.behind}
45}
46
47// get is do("GET", path, "").
48func (f *homeRouterFixture) get(path string) *httptest.ResponseRecorder {
49 return f.do(http.MethodGet, path, "")
50}
51
52// remember gives the browser a remembered way in, sealed by Calendar's
53// own jar, as rastrillo would have after a sign-in.
54func (f *homeRouterFixture) remember(t *testing.T, rec lastsignin.Record) {
55 t.Helper()
56 w := httptest.NewRecorder()
57 f.app.auth.RememberJar().Remember(w, rec)
58 cookies := w.Result().Cookies()
59 if len(cookies) != 1 {
60 t.Fatalf("the jar refused %+v", rec)
61 }
62 f.cookies[cookies[0].Name] = cookies[0]
63}
64
65// remembered is the browser's remembered way in, read by Calendar's jar.
66func (f *homeRouterFixture) remembered() (lastsignin.Record, lastsignin.ReadResult) {
67 return f.app.auth.RememberJar().Read(f.jarRequest())
68}
69
70// returnPath is the page the browser's return memory holds ("/" for none).
71func (f *homeRouterFixture) returnPath() string {
72 return f.app.returns.path(f.jarRequest())
73}
74
75func (f *homeRouterFixture) jarRequest() *http.Request {
76 r := httptest.NewRequest(http.MethodGet, homeTestOrigin+"/", nil)
77 for _, c := range f.cookies {
78 r.AddCookie(c)
79 }
80 return r
81}
82
83// session puts a session for subject, signed in by method, in the jar, as
84// a finished sign-in would, minted at authTime.
85func (f *homeRouterFixture) session(t *testing.T, subject, method string, authTime time.Time) {
86 t.Helper()
87 w := httptest.NewRecorder()
88 if err := f.app.sessions.SignIn(w, httptest.NewRequest(http.MethodGet, homeTestOrigin+"/", nil), sessions.Session{Subject: subject, Method: method, AuthTime: authTime}); err != nil {
89 t.Fatal(err)
90 }
91 for _, c := range w.Result().Cookies() {
92 f.cookies[c.Name] = c
93 }
94}
95
96// sessionCookie is the session cookie in the jar, or nil.
97func (f *homeRouterFixture) sessionCookie() *http.Cookie {
98 return f.cookies[f.app.sessionCookie()]
99}
100
101// emailSignin signs the browser in by the emailed link, posting form to
102// the door, and returns where the landing sends it: /auth/local/complete's
103// answer with Home, the verify's own answer standalone.
104func (f *homeRouterFixture) emailSignin(t *testing.T, form url.Values) string {
105 t.Helper()
106 before := f.app.mailRecorder.last()
107 if w := f.do(http.MethodPost, "/signin", form.Encode()); w.Code != http.StatusSeeOther {
108 t.Fatalf("POST /signin: %d", w.Code)
109 }
110 f.app.signinMail.wait()
111 body := f.app.mailRecorder.last()
112 link := regexp.MustCompile(`https://[^\s<>"]+/auth/verify\?[^\s<>"]+`).FindString(body)
113 if link == "" || body == before {
114 t.Fatal("no sign-in link mailed")
115 }
116 u, _ := url.Parse(link)
117 open := f.get(u.RequestURI())
118 action, token, ok := ConfirmFormForTest(open.Body.String())
119 if open.Code != http.StatusOK || !ok {
120 t.Fatalf("the link's confirm step: %d\n%s", open.Code, open.Body.String())
121 }
122 verify := f.do(http.MethodPost, action, url.Values{"token": {token}}.Encode())
123 landing := verify.Header().Get("Location")
124 if verify.Code != http.StatusSeeOther {
125 t.Fatalf("verify: %d %q", verify.Code, landing)
126 }
127 if f.app.home == nil {
128 return landing
129 }
130 if landing != connect.LocalCompletePath {
131 t.Fatalf("verify with Home landed on %q", landing)
132 }
133 return homeRedirect(t, f.get(landing))
134}
135
136// oficinaSignin runs Continue with Oficina ID for subject from path, and
137// returns the exchange's last answer.
138func (f *homeRouterFixture) oficinaSignin(t *testing.T, path, subject string) *httptest.ResponseRecorder {
139 t.Helper()
140 start := f.get(path)
141 if start.Code != http.StatusSeeOther || !strings.HasPrefix(start.Header().Get("Location"), "/auth/start") {
142 t.Fatalf("GET %s: %d %q, want the Home exchange", path, start.Code, start.Header().Get("Location"))
143 }
144 return f.exchange(t, start.Header().Get("Location"), subject)
145}
146
147// linkAlice links alice@example.test to subject through the real link flow,
148// leaving her signed in locally.
149func (f *homeRouterFixture) linkAlice(t *testing.T, subject string) {
150 t.Helper()
151 f.local(t)
152 if w := f.get("/day/2026-09-10"); w.Code != http.StatusOK {
153 t.Fatalf("alice's first page: %d", w.Code)
154 }
155 start := homeRedirect(t, f.do(http.MethodPost, "/account/home", ""))
156 if got := homeRedirect(t, f.exchange(t, start, subject)); got != "/account/home/confirm" {
157 t.Fatalf("link exchange landed on %s", got)
158 }
159 homeRedirect(t, f.do(http.MethodPost, "/account/home/confirm", ""))
160}
161
162// redirectsTo fails unless rec is a 303 to want.
163func redirectsTo(t *testing.T, rec *httptest.ResponseRecorder, want string) {
164 t.Helper()
165 if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != want {
166 t.Fatalf("got %d %q, want 303 %q", rec.Code, rec.Header().Get("Location"), want)
167 }
168}
169
170// door is the sign-in card's own content: rastrillo's section inside
171// estilo-signin-screen, unescaped.
172func door(t *testing.T, body string) string {
173 t.Helper()
174 start := strings.Index(body, "<div estilo-signin-screen>")
175 if start < 0 {
176 t.Fatalf("no estilo-signin-screen:\n%s", body)
177 }
178 end := strings.Index(body[start:], "</section>")
179 return html.UnescapeString(body[start : start+end])
180}
181
182// titleOf is the page's <title>, unescaped.
183func titleOf(t *testing.T, body string) string {
184 t.Helper()
185 m := regexp.MustCompile(`<title>([^<]*)</title>`).FindStringSubmatch(body)
186 if m == nil {
187 t.Fatalf("no <title>:\n%s", body)
188 }
189 return html.UnescapeString(m[1])
190}
191
192// clears reports whether rec clears the named cookie.
193func clears(rec *httptest.ResponseRecorder, name string) bool {
194 for _, c := range rec.Result().Cookies() {
195 if c.Name == name && c.MaxAge < 0 {
196 return true
197 }
198 }
199 return false
200}
201
202// What the door's markup says, for the tests to look for.
203const (
204 oficinaButton = "Continue with Oficina ID"
205 otherWays = "Other ways to sign in"
206 emailField = `id="rst-signin-email" name="address"`
207 appProblem = `id="rst-signin-app-problem"`
208)
209