oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "net/url"
7 "strings"
8 "testing"
9)
10
11// TestEmailedLinkOpensTheConfirmDoor walks the magic-link sign-in end to
12// end: begin, the mail, GET of the emailed link (the confirm door, which
13// spends nothing), the door's POST, and a session that opens the list.
14//
15// GET /auth/verify used to sign in on sight, so a mail-security gateway
16// that fetches every link in an inbound message (Safe Links, Proofpoint)
17// spent it before the person clicked, and they were told it had expired.
18// Rastrillo now confirms on GET and redeems on POST; this pins that the
19// confirm step is Sheets' own door with one Sign in button, not
20// rastrillo's plain English fallback, and that the POST is mounted. With
21// the smart door it is rastrillo's sign-in screen at its confirm step,
22// drawn in estilo's door.
23func TestEmailedLinkOpensTheConfirmDoor(t *testing.T) {
24 app, mail := appWithRecorder(t)
25 h := app.Router()
26
27 if w := postSignin(t, app, signedInAddress); w.Code != http.StatusSeeOther || !sentAnswer(w.Header().Get("Location")) {
28 t.Fatalf("POST /signin = %d %q, want 303 /signin?sent=1&attempt=…", w.Code, w.Header().Get("Location"))
29 }
30 link := mail.lastLink(t)
31 u, err := url.Parse(link)
32 if err != nil {
33 t.Fatal(err)
34 }
35
36 // Opening it, twice, as a scanner and then the person would.
37 for _, who := range []string{"scanner", "person"} {
38 rec := httptest.NewRecorder()
39 h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, u.RequestURI(), nil))
40 if rec.Code != http.StatusOK || len(rec.Result().Cookies()) != 0 {
41 t.Fatalf("%s's GET = %d with %d cookies, want 200 and nothing minted", who, rec.Code, len(rec.Result().Cookies()))
42 }
43 body := rec.Body.String()
44 for _, want := range []string{"estilo-door", "<div estilo-signin-screen>", "rst-signin-form", `alt=""`, "<span>Sheets</span>", "<title>Sign in to Sheets</title>", `name="token" value="` + u.Query().Get("token") + `"`} {
45 if !strings.Contains(body, want) {
46 t.Errorf("%s: confirm door lacks %q:\n%s", who, want, body)
47 }
48 }
49 if n := strings.Count(body, "<button"); n != 1 || !strings.Contains(body, `type="submit" autofocus>Sign in</button>`) {
50 t.Errorf("%s: confirm door has %d buttons, want exactly one Sign in:\n%s", who, n, body)
51 }
52 if strings.Contains(body, `name="address"`) {
53 t.Errorf("%s: confirm door asks for an address again", who)
54 }
55 if strings.Contains(body, "<style") {
56 t.Errorf("%s: rastrillo's fallback confirm page, not the door", who)
57 }
58 // auth sets these before RenderConfirm; render must not undo them.
59 for header, want := range map[string]string{
60 "Cache-Control": "no-store",
61 "Referrer-Policy": "strict-origin",
62 "X-Frame-Options": "DENY",
63 } {
64 if got := rec.Header().Get(header); got != want {
65 t.Errorf("%s: %s = %q, want %q", who, header, got, want)
66 }
67 }
68 }
69
70 // A post without a browser's same-origin evidence is refused and
71 // spends nothing.
72 action, token := openLink(t, h, link)
73 cross := httptest.NewRequest(http.MethodPost, action, strings.NewReader(url.Values{"token": {token}}.Encode()))
74 cross.Header.Set("Content-Type", "application/x-www-form-urlencoded")
75 cross.Header.Set("Sec-Fetch-Site", "cross-site")
76 if rec := do(t, app, cross); rec.Code != http.StatusForbidden || len(rec.Result().Cookies()) != 0 {
77 t.Fatalf("cross-site POST = %d with %d cookies, want 403 and nothing minted", rec.Code, len(rec.Result().Cookies()))
78 }
79
80 // Sign in: the POST spends the link and mints the session.
81 signed := postLink(t, h, action, token)
82 if signed.Code != http.StatusSeeOther || signed.Header().Get("Location") != "/sheets" {
83 t.Fatalf("POST %s = %d %q, want 303 /sheets", action, signed.Code, signed.Header().Get("Location"))
84 }
85 cookies := signed.Result().Cookies()
86 list := page(t, app, cookies, "/sheets")
87 if list.Code != http.StatusOK || !strings.Contains(list.Body.String(), signedInAddress) {
88 t.Fatalf("GET /sheets with the new session = %d, or it does not name %s", list.Code, signedInAddress)
89 }
90
91 // Spent: the door still opens (it looks nothing up), but its button
92 // now says the link expired.
93 action, token = openLink(t, h, link)
94 again := postLink(t, h, action, token)
95 if len(again.Result().Cookies()) != 0 || again.Header().Get("Location") != "/signin?err=expired" {
96 t.Fatalf("second POST = %d %q, want /signin?err=expired and no session", again.Code, again.Header().Get("Location"))
97 }
98}
99
100// TestALinkRefusedAtRedemptionIsTheDoor: an address admitted when its
101// link was mailed but not when it is redeemed — a guest whose only grant
102// was revoked in between — gets the sign-in door with the refusal on it,
103// in auth's 403 (auth.Config.Refused, App.refused), and no session.
104func TestALinkRefusedAtRedemptionIsTheDoor(t *testing.T) {
105 app, cookies := signIn(t)
106 s, _ := newSheet(t, app, cookies, "Budget")
107 guest, err := app.grant(nil, s.ID, "ada@elsewhere.example", SheetViewer, signedInMember(t, app))
108 if err != nil {
109 t.Fatal(err)
110 }
111 mail := app.testMailer(t)
112 postSignin(t, app, "ada@elsewhere.example")
113 link := mail.lastLink(t)
114 if err := app.revoke(nil, s.ID, guest.ID); err != nil {
115 t.Fatal(err)
116 }
117
118 rec := followLink(t, app.Router(), link)
119 if rec.Code != http.StatusForbidden || rec.Header().Get("Location") != "" {
120 t.Fatalf("refused redemption = %d %q, want 403 and the door itself", rec.Code, rec.Header().Get("Location"))
121 }
122 for _, c := range rec.Result().Cookies() {
123 if strings.Contains(c.Name, "session") && c.MaxAge >= 0 && c.Value != "" {
124 t.Fatalf("a refused sign-in set a session cookie %q", c.Name)
125 }
126 }
127 body := rec.Body.String()
128 for _, want := range []string{"estilo-door", `id="rst-signin-app-problem"`, `name="address"`, `action="/signin"`, signinRefusal} {
129 if !strings.Contains(body, want) {
130 t.Errorf("refusal door lacks %q:\n%s", want, body)
131 }
132 }
133}
134