| 1 | package sheets |
| 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 a Home fixture, its |
| 5 | // remembered way in, and the two ways it signs in. |
| 6 | |
| 7 | import ( |
| 8 | "html" |
| 9 | "net/http" |
| 10 | "net/http/httptest" |
| 11 | "net/url" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "amadan.net/oficina/home/connect" |
| 16 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 17 | ) |
| 18 | |
| 19 | // browser is another browser on f's app: the same instance, its own jar. |
| 20 | func (f *homeFixture) browser() *homeFixture { |
| 21 | return &homeFixture{app: f.app, key: f.key, jar: map[string]*http.Cookie{}, creds: f.creds} |
| 22 | } |
| 23 | |
| 24 | // get is a GET from this browser. |
| 25 | func (f *homeFixture) get(t *testing.T, path string) *httptest.ResponseRecorder { |
| 26 | t.Helper() |
| 27 | return f.request(t, "GET", path, nil, false) |
| 28 | } |
| 29 | |
| 30 | // remember gives the browser a remembered way in, sealed by Sheets' own |
| 31 | // jar, as rastrillo would have after a sign-in. |
| 32 | func (f *homeFixture) remember(t *testing.T, rec lastsignin.Record) { |
| 33 | t.Helper() |
| 34 | w := httptest.NewRecorder() |
| 35 | f.app.auth.RememberJar().Remember(w, rec) |
| 36 | cookies := w.Result().Cookies() |
| 37 | if len(cookies) != 1 { |
| 38 | t.Fatalf("the jar refused %+v", rec) |
| 39 | } |
| 40 | f.jar[cookies[0].Name] = cookies[0] |
| 41 | } |
| 42 | |
| 43 | // remembered is the browser's remembered way in, read by Sheets' own jar. |
| 44 | func (f *homeFixture) remembered(t *testing.T) (lastsignin.Record, lastsignin.ReadResult) { |
| 45 | t.Helper() |
| 46 | r := httptest.NewRequest(http.MethodGet, testOrigin+"/", nil) |
| 47 | for _, c := range f.jar { |
| 48 | r.AddCookie(c) |
| 49 | } |
| 50 | return f.app.auth.RememberJar().Read(r) |
| 51 | } |
| 52 | |
| 53 | // emailSignin signs this browser in by the emailed link, posting form to |
| 54 | // the door, and returns where /auth/local/complete sends it. |
| 55 | func (f *homeFixture) emailSignin(t *testing.T, form url.Values) string { |
| 56 | t.Helper() |
| 57 | if w := f.request(t, "POST", "/signin", form, false); w.Code != http.StatusSeeOther { |
| 58 | t.Fatalf("POST /signin: %d", w.Code) |
| 59 | } |
| 60 | u, err := url.Parse(f.app.testMailer(t).lastLink(t)) |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | open := f.request(t, "GET", u.RequestURI(), nil, false) |
| 65 | m := confirmForm.FindStringSubmatch(open.Body.String()) |
| 66 | if open.Code != http.StatusOK || m == nil { |
| 67 | t.Fatalf("the link's confirm step: %d", open.Code) |
| 68 | } |
| 69 | verify := f.request(t, "POST", html.UnescapeString(m[1]), url.Values{"token": {html.UnescapeString(m[2])}}, false) |
| 70 | if verify.Code != http.StatusSeeOther || verify.Header().Get("Location") != connect.LocalCompletePath { |
| 71 | t.Fatalf("verify: %d %q", verify.Code, verify.Header().Get("Location")) |
| 72 | } |
| 73 | done := f.request(t, "GET", connect.LocalCompletePath, nil, false) |
| 74 | if done.Code != http.StatusSeeOther { |
| 75 | t.Fatalf("local completion: %d", done.Code) |
| 76 | } |
| 77 | return done.Header().Get("Location") |
| 78 | } |
| 79 | |
| 80 | // oficinaSignin runs Continue with Oficina ID for subject from path, and |
| 81 | // returns the exchange's last answer. |
| 82 | func (f *homeFixture) oficinaSignin(t *testing.T, path, subject string) *httptest.ResponseRecorder { |
| 83 | t.Helper() |
| 84 | start := f.request(t, "GET", path, nil, false) |
| 85 | if start.Code != http.StatusSeeOther || !strings.HasPrefix(start.Header().Get("Location"), "/auth/start?") { |
| 86 | t.Fatalf("GET %s: %d %q, want the Home exchange", path, start.Code, start.Header().Get("Location")) |
| 87 | } |
| 88 | return f.exchange(t, f.request(t, "GET", start.Header().Get("Location"), nil, false), subject) |
| 89 | } |
| 90 | |
| 91 | // redirectsTo fails unless rec is a 303 to want. |
| 92 | func redirectsTo(t *testing.T, rec *httptest.ResponseRecorder, want string) { |
| 93 | t.Helper() |
| 94 | if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != want { |
| 95 | t.Fatalf("got %d %q, want 303 %q", rec.Code, rec.Header().Get("Location"), want) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // door is the sign-in card's own content: rastrillo's section inside |
| 100 | // estilo-signin-screen, unescaped. |
| 101 | func door(t *testing.T, body string) string { |
| 102 | t.Helper() |
| 103 | start := strings.Index(body, "<div estilo-signin-screen>") |
| 104 | if start < 0 { |
| 105 | t.Fatalf("no estilo-signin-screen:\n%s", body) |
| 106 | } |
| 107 | end := strings.Index(body[start:], "</section>") |
| 108 | return html.UnescapeString(body[start : start+end]) |
| 109 | } |
| 110 | |
| 111 | // What the door's markup says, for the tests to look for. |
| 112 | var ( |
| 113 | oficinaButton = "Continue with Oficina ID" |
| 114 | otherWays = "Other ways to sign in" |
| 115 | emailField = `id="rst-signin-email" name="address"` |
| 116 | appProblem = `id="rst-signin-app-problem"` |
| 117 | ) |
| 118 | |