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
3// The smart door's routes and every state it answers.
4
5import (
6 "html"
7 "net/http"
8 "strings"
9 "testing"
10
11 "amadan.net/oficina/estilo.git/chrome"
12 "amadan.net/rastrillo/rastrillo/lastsignin"
13)
14
15func TestSmartDoorStatesWithHome(t *testing.T) {
16 f := newHomeFixture(t)
17 glyph := string(chrome.OficinaGlyph)
18 oficina := &lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.com"}
19 cases := []struct {
20 name, path string
21 record *lastsignin.Record
22 want, not []string
23 title string
24 }{
25 {name: "first visit", path: "/signin",
26 want: []string{oficinaButton, `href="/signin/oficina"`, glyph, otherWays, `href="/signin?way=oficina&ways=1"`},
27 not: []string{emailField, "Continue as", "Use a different email"},
28 title: "Sign in to Sheets"},
29 {name: "first visit from a page", path: "/signin?return_to=%2Fsheets%2F7",
30 want: []string{`href="/signin/oficina?return_to=%2Fsheets%2F7"`, `href="/signin?return_to=%2Fsheets%2F7&way=oficina&ways=1"`}},
31 {name: "local: email is the one way", path: "/signin/local",
32 want: []string{emailField, ">Continue</button>"},
33 not: []string{"Oficina ID", "/signin/oficina", otherWays}},
34 {name: "step-up: local ways only", path: "/signin?reauth=1",
35 want: []string{emailField, "Sign in again to continue."},
36 not: []string{"Oficina ID", "/signin/oficina"}},
37 {name: "step-up remembered Oficina ID", path: "/signin?reauth=1", record: oficina,
38 want: []string{"Continue as <bdi>alice@example.com</bdi>", "Sign in again to continue."},
39 not: []string{"Oficina ID", "/signin/oficina"}},
40 {name: "chooser", path: "/signin?ways=1",
41 want: []string{`<h1 id="rst-signin-heading">Other ways to sign in</h1>`, `href="/signin?way=email"`, ">Email</span>", ">Back</span>"},
42 not: []string{oficinaButton, emailField},
43 title: "Other ways to sign in to Sheets"},
44 {name: "email chosen", path: "/signin?way=email",
45 want: []string{emailField, otherWays},
46 not: []string{oficinaButton}},
47 {name: "Oficina ID remembered", path: "/signin", record: oficina,
48 want: []string{oficinaButton, otherWays},
49 // Never captioned with an address.
50 not: []string{"alice@example.com", emailField}},
51 {name: "Oficina ID remembered: the chooser", path: "/signin?ways=1&way=oficina", record: oficina,
52 want: []string{">Email <bdi>alice@example.com</bdi></span>", "Use a different email", ">Back</span>"},
53 not: []string{oficinaButton}},
54 {name: "Oficina ID remembered for a Home-only member", path: "/signin?ways=1&way=oficina",
55 record: &lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina},
56 want: []string{">Email</span>"},
57 not: []string{"<bdi>"}},
58 {name: "email link remembered", path: "/signin",
59 record: &lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "bob@example.com"},
60 want: []string{"Continue as <bdi>bob@example.com</bdi>", `name="address" value="bob@example.com"`, otherWays},
61 not: []string{oficinaButton}},
62 {name: "email link remembered: the chooser", path: "/signin?ways=1&way=email",
63 record: &lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "bob@example.com"},
64 want: []string{glyph + "<span>Oficina ID</span>", `href="/signin?way=oficina"`, "Use a different email"},
65 not: []string{">Email</span>"}},
66 {name: "Keymail remembered", path: "/signin",
67 record: &lastsignin.Record{Method: lastsignin.MethodKeymail, Address: "bob@example.com"},
68 // rastrillo v0.45.0: the door never names Keymail.
69 want: []string{"Continue as <bdi>bob@example.com</bdi>", `name="expect" value="keymail"`, otherWays}},
70 {name: "problem", path: "/signin?err=rate", record: oficina,
71 want: []string{"Too many tries. Try again in a few minutes.", emailField},
72 not: []string{oficinaButton},
73 title: "Problem signing in to Sheets"},
74 {name: "a failed Keymail keeps force", path: "/signin?err=keymail&force=1",
75 want: []string{"We couldn't confirm it's you.", `name="force" value="1"`, "Send me a link instead"}},
76 {name: "sent", path: "/signin?sent=1",
77 want: []string{"Check your email", "We sent a link to your inbox.", "Use a different email"},
78 not: []string{oficinaButton, emailField},
79 title: "Check your email to sign in to Sheets"},
80 {name: "an unknown way is the remembered one", path: "/signin?way=nonsense",
81 want: []string{oficinaButton}},
82 }
83 for _, c := range cases {
84 t.Run(c.name, func(t *testing.T) {
85 b := f.browser()
86 if c.record != nil {
87 b.remember(t, *c.record)
88 }
89 rec := b.request(t, "GET", c.path, nil, false)
90 if rec.Code != http.StatusOK {
91 t.Fatalf("GET %s: %d %q", c.path, rec.Code, rec.Header().Get("Location"))
92 }
93 if rec.Header().Get("Cache-Control") != "no-store" {
94 t.Error("the door is cacheable")
95 }
96 card := door(t, rec.Body.String())
97 for _, want := range c.want {
98 if !strings.Contains(card, want) {
99 t.Errorf("lacks %q:\n%s", want, card)
100 }
101 }
102 for _, not := range c.not {
103 if strings.Contains(card, not) {
104 t.Errorf("has %q:\n%s", not, card)
105 }
106 }
107 if c.title != "" {
108 if got := html.UnescapeString(titleOf(t, rec.Body.String())); got != c.title {
109 t.Errorf("<title> = %q, want %q", got, c.title)
110 }
111 }
112 })
113 }
114}
115
116// Standalone Sheets has only email: no Oficina ID anywhere, and no chooser
117// link with nothing on it.
118func TestSmartDoorStandaloneHasOnlyEmail(t *testing.T) {
119 app, _ := appWithRecorder(t)
120 for _, path := range []string{"/signin", "/signin?way=oficina"} {
121 rec := page(t, app, nil, path)
122 card := door(t, rec.Body.String())
123 if rec.Code != http.StatusOK || !strings.Contains(card, emailField) || strings.Contains(card, "Oficina") || strings.Contains(card, otherWays) {
124 t.Errorf("GET %s: %d\n%s", path, rec.Code, card)
125 }
126 }
127 // Nothing links the chooser here; typed in, it has no Oficina ID row.
128 if card := door(t, page(t, app, nil, "/signin?ways=1").Body.String()); strings.Contains(card, "Oficina") || !strings.Contains(card, ">Back</span>") {
129 t.Errorf("standalone chooser:\n%s", card)
130 }
131 for _, path := range []string{"/signin/oficina", "/signin/local"} {
132 if rec := page(t, app, nil, path); rec.Code != http.StatusNotFound {
133 t.Errorf("standalone %s: %d, want 404", path, rec.Code)
134 }
135 }
136}
137
138// Continue with Oficina ID starts the Home exchange, keeping the page; a
139// step-up never reaches Home; a browser already signed in and admitted
140// skips both doors, unless the query names a state of the door.
141func TestSmartDoorRoutesWithHome(t *testing.T) {
142 f := newHomeFixture(t)
143 b := f.browser()
144 redirectsTo(t, b.request(t, "GET", "/signin/oficina?return_to=%2Fsheets%2F7", nil, false), "/auth/start?return_to=%2Fsheets%2F7")
145 redirectsTo(t, b.request(t, "GET", "/signin/oficina?reauth=1&return_to=%2Fsheets%2F7", nil, false), "/signin/local?reauth=1&return_to=%2Fsheets%2F7")
146
147 paul := f.browser()
148 paul.local(t, signedInAddress)
149 redirectsTo(t, paul.request(t, "GET", "/signin?return_to=%2Fsheets%2F7", nil, false), "/sheets/7")
150 redirectsTo(t, paul.request(t, "GET", "/signin/oficina", nil, false), "/")
151 for _, path := range []string{"/signin?reauth=1", "/signin?sent=1", "/signin?err=rate", "/signin/local"} {
152 if rec := paul.request(t, "GET", path, nil, false); rec.Code != http.StatusOK {
153 t.Errorf("signed in, GET %s: %d %q, want the door", path, rec.Code, rec.Header().Get("Location"))
154 }
155 }
156}
157