| 1 | package fichastest |
| 2 | |
| 3 | // Helpers for the smart door's tests (signin_door_*_test.go): a browser's |
| 4 | // cookie jar over httptest, a sign-in walked the way a person walks it, |
| 5 | // and a second app over the same database with a different owners list, |
| 6 | // which is how a session or a link for an address taken off FICHAS_OWNERS |
| 7 | // is made from the outside. |
| 8 | |
| 9 | import ( |
| 10 | "io" |
| 11 | "log/slog" |
| 12 | "net/http" |
| 13 | "net/http/httptest" |
| 14 | "net/url" |
| 15 | "path/filepath" |
| 16 | "regexp" |
| 17 | "strings" |
| 18 | "testing" |
| 19 | |
| 20 | fichas "amadan.net/oficina/fichas/internal/fichas" |
| 21 | "amadan.net/rastrillo/rastrillo/db" |
| 22 | ) |
| 23 | |
| 24 | // TestOther is a second address, an owner of the wider instance only. |
| 25 | const TestOther = "bo@example.com" |
| 26 | |
| 27 | // browser is a cookie jar: what a browser would send back. |
| 28 | type browser struct { |
| 29 | cookies map[string]*http.Cookie |
| 30 | } |
| 31 | |
| 32 | func newBrowser() *browser { return &browser{cookies: map[string]*http.Cookie{}} } |
| 33 | |
| 34 | // keep applies a response's Set-Cookie headers as a browser would. |
| 35 | func (b *browser) keep(rec *httptest.ResponseRecorder) { |
| 36 | for _, c := range rec.Result().Cookies() { |
| 37 | if c.MaxAge < 0 || c.Value == "" { |
| 38 | delete(b.cookies, c.Name) |
| 39 | continue |
| 40 | } |
| 41 | b.cookies[c.Name] = c |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func (b *browser) has(name string) bool { _, ok := b.cookies[name]; return ok } |
| 46 | |
| 47 | func (b *browser) send(req *http.Request) { |
| 48 | for _, c := range b.cookies { |
| 49 | req.AddCookie(&http.Cookie{Name: c.Name, Value: c.Value}) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func (b *browser) get(t *testing.T, h http.Handler, target string) *httptest.ResponseRecorder { |
| 54 | t.Helper() |
| 55 | req := httptest.NewRequest(http.MethodGet, target, nil) |
| 56 | b.send(req) |
| 57 | rec := httptest.NewRecorder() |
| 58 | h.ServeHTTP(rec, req) |
| 59 | b.keep(rec) |
| 60 | return rec |
| 61 | } |
| 62 | |
| 63 | func (b *browser) post(t *testing.T, h http.Handler, target string, form url.Values) *httptest.ResponseRecorder { |
| 64 | t.Helper() |
| 65 | return b.postRaw(t, h, target, form.Encode()) |
| 66 | } |
| 67 | |
| 68 | // postRaw posts body as it is, so a test can send one that does not parse. |
| 69 | func (b *browser) postRaw(t *testing.T, h http.Handler, target, body string) *httptest.ResponseRecorder { |
| 70 | t.Helper() |
| 71 | req := httptest.NewRequest(http.MethodPost, target, strings.NewReader(body)) |
| 72 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 73 | req.Header.Set("Origin", testOrigin) |
| 74 | b.send(req) |
| 75 | rec := httptest.NewRecorder() |
| 76 | h.ServeHTTP(rec, req) |
| 77 | b.keep(rec) |
| 78 | return rec |
| 79 | } |
| 80 | |
| 81 | // crossSitePost is a form post from another site, carrying b's cookies. |
| 82 | func crossSitePost(t *testing.T, h http.Handler, b *browser, target string) *httptest.ResponseRecorder { |
| 83 | t.Helper() |
| 84 | req := httptest.NewRequest(http.MethodPost, target, strings.NewReader("")) |
| 85 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 86 | req.Header.Set("Origin", "https://evil.example") |
| 87 | req.Header.Set("Sec-Fetch-Site", "cross-site") |
| 88 | b.send(req) |
| 89 | rec := httptest.NewRecorder() |
| 90 | h.ServeHTTP(rec, req) |
| 91 | b.keep(rec) |
| 92 | return rec |
| 93 | } |
| 94 | |
| 95 | // getWith is a GET carrying cookies. |
| 96 | func getWith(t *testing.T, h http.Handler, target string, cookies []*http.Cookie) *httptest.ResponseRecorder { |
| 97 | t.Helper() |
| 98 | req := httptest.NewRequest(http.MethodGet, target, nil) |
| 99 | for _, c := range cookies { |
| 100 | req.AddCookie(c) |
| 101 | } |
| 102 | rec := httptest.NewRecorder() |
| 103 | h.ServeHTTP(rec, req) |
| 104 | return rec |
| 105 | } |
| 106 | |
| 107 | // cookie names, as auth names them on testOrigin (http, so no __Host-). |
| 108 | const ( |
| 109 | sessionCookie = "rastrillo_session" |
| 110 | returnCookie = "rastrillo_return_to" |
| 111 | rememberName = "rastrillo_last_signin" |
| 112 | ) |
| 113 | |
| 114 | // sharedApps is two apps over one database, as main.go wires them, with |
| 115 | // the same keys: wide admits TestOwner and TestOther, narrow TestOwner |
| 116 | // only. A link sent by wide and opened on narrow, or a session minted by |
| 117 | // wide and presented to narrow, is an address taken off the owners list. |
| 118 | func sharedApps(t *testing.T) (wide, narrow http.Handler, mailer *capturingMailer, d *db.DB) { |
| 119 | t.Helper() |
| 120 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 121 | d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) |
| 122 | if err != nil { |
| 123 | t.Fatalf("db.Open: %v", err) |
| 124 | } |
| 125 | t.Cleanup(func() { d.Close() }) |
| 126 | mailer = &capturingMailer{} |
| 127 | build := func(owners ...string) http.Handler { |
| 128 | cfg := testConfig(t) |
| 129 | cfg.Owners = owners |
| 130 | mux, app, err := fichas.App(d, cfg, logger) |
| 131 | if err != nil { |
| 132 | t.Fatalf("App: %v", err) |
| 133 | } |
| 134 | au, err := fichas.NewAuth(d.Writer(), fichas.AuthEnv{Origin: cfg.Origin, InstanceKey: cfg.InstanceKey, Mailer: mailer}, logger, app) |
| 135 | if err != nil { |
| 136 | t.Fatalf("NewAuth: %v", err) |
| 137 | } |
| 138 | app.UseAuth(au) |
| 139 | return mux |
| 140 | } |
| 141 | return build(TestOwner, TestOther), build(TestOwner), mailer, d |
| 142 | } |
| 143 | |
| 144 | // linkFor asks h for a link to address (force=1: no classifier, no dial) |
| 145 | // and returns the emailed token. |
| 146 | func linkFor(t *testing.T, h http.Handler, b *browser, m *capturingMailer, address string, extra url.Values) string { |
| 147 | t.Helper() |
| 148 | form := url.Values{"address": {address}, "force": {"1"}} |
| 149 | for k, v := range extra { |
| 150 | form[k] = v |
| 151 | } |
| 152 | rec := b.post(t, h, "/auth/begin", form) |
| 153 | if rec.Code != http.StatusSeeOther || !strings.HasPrefix(rec.Header().Get("Location"), "/signin?sent=1&attempt=") { |
| 154 | t.Fatalf("POST /auth/begin: %d %q", rec.Code, rec.Header().Get("Location")) |
| 155 | } |
| 156 | mail := m.last(t) |
| 157 | if !strings.HasPrefix(mail, address+"\n") { |
| 158 | t.Fatalf("the last mail went elsewhere:\n%s", mail) |
| 159 | } |
| 160 | match := linkToken.FindStringSubmatch(mail) |
| 161 | if match == nil { |
| 162 | t.Fatalf("no link in:\n%s", mail) |
| 163 | } |
| 164 | tok, err := url.QueryUnescape(match[1]) |
| 165 | if err != nil { |
| 166 | t.Fatal(err) |
| 167 | } |
| 168 | return tok |
| 169 | } |
| 170 | |
| 171 | // signIn walks the email path in b on h, link to landing, and returns the |
| 172 | // landing's answer. |
| 173 | func signIn(t *testing.T, h http.Handler, b *browser, m *capturingMailer, address string, extra url.Values) *httptest.ResponseRecorder { |
| 174 | t.Helper() |
| 175 | tok := linkFor(t, h, b, m, address, extra) |
| 176 | return b.post(t, h, "/auth/verify", url.Values{"token": {tok}}) |
| 177 | } |
| 178 | |
| 179 | var titleRE = regexp.MustCompile(`<title>([^<]*)</title>`) |
| 180 | |
| 181 | func titleOf(body string) string { |
| 182 | if m := titleRE.FindStringSubmatch(body); m != nil { |
| 183 | return m[1] |
| 184 | } |
| 185 | return "" |
| 186 | } |
| 187 | |
| 188 | // refusalText is the refusal, as html/template writes it. |
| 189 | const refusalText = "That email can’t sign in to Fichas. Use a different email, or ask whoever runs this team to add it." |
| 190 | |