| 1 | package slides |
| 2 | |
| 3 | // The smart door's routes and every state it answers. |
| 4 | |
| 5 | import ( |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/estilo.git/chrome" |
| 13 | "amadan.net/oficina/home/connect" |
| 14 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 15 | "amadan.net/rastrillo/rastrillo/sessions" |
| 16 | ) |
| 17 | |
| 18 | func TestSmartDoorStatesWithHome(t *testing.T) { |
| 19 | b, _ := newHomeBrowser(t) |
| 20 | glyph := string(chrome.OficinaGlyph) |
| 21 | oficina := &lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.com"} |
| 22 | cases := []struct { |
| 23 | name, path string |
| 24 | record *lastsignin.Record |
| 25 | want, not []string |
| 26 | title string |
| 27 | }{ |
| 28 | {name: "first visit", path: "/signin", |
| 29 | want: []string{oficinaButton, `href="/signin/oficina"`, glyph, otherWays, `href="/signin?way=oficina&ways=1"`}, |
| 30 | not: []string{emailField, "Continue as", "Use a different email"}, |
| 31 | title: "Sign in to Slides"}, |
| 32 | {name: "first visit from a page", path: "/signin?return_to=%2Fd%2F7", |
| 33 | want: []string{`href="/signin/oficina?return_to=%2Fd%2F7"`, `href="/signin?return_to=%2Fd%2F7&way=oficina&ways=1"`}}, |
| 34 | {name: "local: email is the one way", path: "/signin/local", |
| 35 | want: []string{emailField, ">Continue</button>"}, |
| 36 | not: []string{"Oficina ID", "/signin/oficina", otherWays}}, |
| 37 | {name: "step-up: local ways only", path: "/signin?reauth=1", |
| 38 | want: []string{emailField, "Sign in again to continue."}, |
| 39 | not: []string{"Oficina ID", "/signin/oficina"}}, |
| 40 | {name: "step-up remembered Oficina ID", path: "/signin?reauth=1", record: oficina, |
| 41 | want: []string{"Continue as <bdi>alice@example.com</bdi>", "Sign in again to continue."}, |
| 42 | not: []string{"Oficina ID", "/signin/oficina"}}, |
| 43 | {name: "chooser", path: "/signin?ways=1", |
| 44 | want: []string{`<h1 id="rst-signin-heading">Other ways to sign in</h1>`, `href="/signin?way=email"`, ">Email</span>", ">Back</span>"}, |
| 45 | not: []string{oficinaButton, emailField}, |
| 46 | title: "Other ways to sign in to Slides"}, |
| 47 | {name: "email chosen", path: "/signin?way=email", |
| 48 | want: []string{emailField, otherWays}, |
| 49 | not: []string{oficinaButton}}, |
| 50 | {name: "Oficina ID remembered", path: "/signin", record: oficina, |
| 51 | want: []string{oficinaButton, otherWays}, |
| 52 | // Never captioned with an address. |
| 53 | not: []string{"alice@example.com", emailField}}, |
| 54 | {name: "Oficina ID remembered: the chooser", path: "/signin?ways=1&way=oficina", record: oficina, |
| 55 | want: []string{">Email <bdi>alice@example.com</bdi></span>", "Use a different email", ">Back</span>"}, |
| 56 | not: []string{oficinaButton}}, |
| 57 | {name: "Oficina ID remembered for a Home-only member", path: "/signin?ways=1&way=oficina", |
| 58 | record: &lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina}, |
| 59 | want: []string{">Email</span>"}, |
| 60 | not: []string{"<bdi>"}}, |
| 61 | {name: "email link remembered", path: "/signin", |
| 62 | record: &lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "bob@example.com"}, |
| 63 | want: []string{"Continue as <bdi>bob@example.com</bdi>", `name="address" value="bob@example.com"`, otherWays}, |
| 64 | not: []string{oficinaButton}}, |
| 65 | {name: "email link remembered: the chooser", path: "/signin?ways=1&way=email", |
| 66 | record: &lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "bob@example.com"}, |
| 67 | want: []string{glyph + "<span>Oficina ID</span>", `href="/signin?way=oficina"`, "Use a different email"}, |
| 68 | not: []string{">Email</span>"}}, |
| 69 | {name: "Keymail remembered", path: "/signin", |
| 70 | record: &lastsignin.Record{Method: lastsignin.MethodKeymail, Address: "bob@example.com"}, |
| 71 | // rastrillo's own words since v0.45.0, which never name Keymail. |
| 72 | want: []string{"Continue as <bdi>bob@example.com</bdi>", `name="expect" value="keymail"`, otherWays}}, |
| 73 | {name: "problem", path: "/signin?err=expired", record: oficina, |
| 74 | want: []string{"That link has expired or was already used.", emailField}, |
| 75 | not: []string{oficinaButton}, |
| 76 | title: "Problem signing in to Slides"}, |
| 77 | {name: "a failed Keymail keeps force", path: "/signin?err=keymail&force=1", |
| 78 | want: []string{"We couldn't confirm it's you.", `name="force" value="1"`, "Send me a link instead"}}, |
| 79 | {name: "removed", path: "/signin?err=removed", record: oficina, |
| 80 | want: []string{appProblem, removedText, emailField}, |
| 81 | not: []string{oficinaButton, "Something went wrong"}, |
| 82 | title: "Problem signing in to Slides"}, |
| 83 | {name: "removed: the chooser says nothing of it", path: "/signin?err=removed&ways=1", |
| 84 | not: []string{appProblem, removedText}}, |
| 85 | {name: "sent", path: "/signin?sent=1", |
| 86 | want: []string{"Check your email", "We sent a link to your inbox.", "Use a different email"}, |
| 87 | not: []string{oficinaButton, emailField}, |
| 88 | title: "Check your email to sign in to Slides"}, |
| 89 | {name: "an unknown way is the remembered one", path: "/signin?way=nonsense", |
| 90 | want: []string{oficinaButton}}, |
| 91 | {name: "a typed refusal is no refusal", path: "/signin?err=refused", |
| 92 | not: []string{appProblem, refusalText}}, |
| 93 | } |
| 94 | for _, c := range cases { |
| 95 | t.Run(c.name, func(t *testing.T) { |
| 96 | br := b.browser() |
| 97 | if c.record != nil { |
| 98 | br.remember(*c.record) |
| 99 | } |
| 100 | rec := br.get(c.path) |
| 101 | if rec.Code != http.StatusOK { |
| 102 | t.Fatalf("GET %s: %d %q", c.path, rec.Code, rec.Header().Get("Location")) |
| 103 | } |
| 104 | if rec.Header().Get("Cache-Control") != "no-store" { |
| 105 | t.Error("the door is cacheable") |
| 106 | } |
| 107 | card := door(t, rec.Body.String()) |
| 108 | for _, want := range c.want { |
| 109 | if !strings.Contains(card, want) { |
| 110 | t.Errorf("lacks %q:\n%s", want, card) |
| 111 | } |
| 112 | } |
| 113 | for _, not := range c.not { |
| 114 | if strings.Contains(card, not) { |
| 115 | t.Errorf("has %q:\n%s", not, card) |
| 116 | } |
| 117 | } |
| 118 | if c.title != "" { |
| 119 | if got := titleOf(t, rec.Body.String()); got != c.title { |
| 120 | t.Errorf("<title> = %q, want %q", got, c.title) |
| 121 | } |
| 122 | } |
| 123 | if strings.Contains(rec.Body.String(), "slides.signin.") || strings.Contains(rec.Body.String(), "rastrillo.ui.") { |
| 124 | t.Error("a catalog key leaked onto the door") |
| 125 | } |
| 126 | }) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Standalone Slides has only email: no Oficina ID anywhere, no chooser |
| 131 | // link with nothing on it, and no /signin/oficina or /signin/local. |
| 132 | func TestSmartDoorStandaloneHasOnlyEmail(t *testing.T) { |
| 133 | b, _ := newStandaloneBrowser(t) |
| 134 | for _, path := range []string{"/signin", "/signin?way=oficina"} { |
| 135 | rec := b.get(path) |
| 136 | card := door(t, rec.Body.String()) |
| 137 | if rec.Code != http.StatusOK || !strings.Contains(card, emailField) || strings.Contains(card, "Oficina") || strings.Contains(card, otherWays) { |
| 138 | t.Errorf("GET %s: %d\n%s", path, rec.Code, card) |
| 139 | } |
| 140 | } |
| 141 | if got := titleOf(t, b.get("/signin").Body.String()); got != "Sign in to Slides" { |
| 142 | t.Errorf("<title> = %q", got) |
| 143 | } |
| 144 | // Nothing links the chooser here; typed in, it has no Oficina ID row. |
| 145 | if card := door(t, b.get("/signin?ways=1").Body.String()); strings.Contains(card, "Oficina") || !strings.Contains(card, ">Back</span>") { |
| 146 | t.Errorf("standalone chooser:\n%s", card) |
| 147 | } |
| 148 | for _, path := range []string{connect.SigninOficinaPath, connect.SigninLocalPath, connect.ChoosePath} { |
| 149 | if rec := b.get(path); rec.Code != http.StatusNotFound { |
| 150 | t.Errorf("standalone %s: %d, want 404", path, rec.Code) |
| 151 | } |
| 152 | } |
| 153 | // A remembered email way is offered back as a one-tap, with Use a |
| 154 | // different email in place of the Other ways link. |
| 155 | r := b.browser() |
| 156 | r.remember(lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "owner@example.com"}) |
| 157 | card := door(t, r.get("/signin").Body.String()) |
| 158 | if !strings.Contains(card, "Continue as <bdi>owner@example.com</bdi>") || !strings.Contains(card, "Use a different email") || strings.Contains(card, otherWays) { |
| 159 | t.Errorf("standalone remembered door:\n%s", card) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // Continue with Oficina ID starts the Home exchange, keeping the page; a |
| 164 | // step-up never reaches Home; a browser already signed in and admitted |
| 165 | // skips both doors, unless the query names a state of the door, and |
| 166 | // /signin/local has no shortcut at all. |
| 167 | func TestSmartDoorRoutesWithHome(t *testing.T) { |
| 168 | b, u := newHomeBrowser(t) |
| 169 | redirectsTo(t, b.get("/signin/oficina?return_to=%2Fd%2F7"), "/auth/start?return_to=%2Fd%2F7") |
| 170 | redirectsTo(t, b.get("/signin/oficina?reauth=1&return_to=%2Fd%2F7"), "/signin/local?reauth=1&return_to=%2Fd%2F7") |
| 171 | |
| 172 | paul := b.browser() |
| 173 | paul.set(homeSession(t, b.a, u.Email, "magiclink")) |
| 174 | in := paul.get("/signin?return_to=%2Fd%2F7") |
| 175 | redirectsTo(t, in, "/d/7") |
| 176 | if len(in.Result().Cookies()) != 0 { |
| 177 | t.Errorf("an admitted /signin set cookies: %v", in.Result().Cookies()) |
| 178 | } |
| 179 | redirectsTo(t, paul.get("/signin/oficina"), "/") |
| 180 | for _, path := range []string{"/signin?reauth=1", "/signin?sent=1", "/signin?err=expired", "/signin/local"} { |
| 181 | if rec := paul.get(path); rec.Code != http.StatusOK { |
| 182 | t.Errorf("signed in, GET %s: %d %q, want the door", path, rec.Code, rec.Header().Get("Location")) |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // Standalone, an admitted session skips the door to its page; the door's |
| 188 | // own states still draw. |
| 189 | func TestSmartDoorRoutesStandalone(t *testing.T) { |
| 190 | b, u := newStandaloneBrowser(t) |
| 191 | b.set(standaloneSession(t, b.a, u.Email)) |
| 192 | redirectsTo(t, b.get("/signin?return_to=%2Fd%2F7"), "/d/7") |
| 193 | redirectsTo(t, b.get("/signin"), "/") |
| 194 | for _, path := range []string{"/signin?sent=1", "/signin?err=expired"} { |
| 195 | if rec := b.get(path); rec.Code != http.StatusOK { |
| 196 | t.Errorf("signed in, GET %s: %d, want the door", path, rec.Code) |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // standaloneSession mints a magic-link session for address in a's own |
| 202 | // store, as a verified link would. |
| 203 | func standaloneSession(t *testing.T, a *app, address string) *http.Cookie { |
| 204 | t.Helper() |
| 205 | w := httptest.NewRecorder() |
| 206 | if err := a.sessions.SignIn(w, httptest.NewRequest(http.MethodGet, homeTestOrigin+"/", nil), sessions.Session{Subject: address, Method: "magiclink", AuthTime: time.Now()}); err != nil { |
| 207 | t.Fatal(err) |
| 208 | } |
| 209 | for _, c := range w.Result().Cookies() { |
| 210 | if c.Name == a.sessions.CookieName() { |
| 211 | return c |
| 212 | } |
| 213 | } |
| 214 | t.Fatal("no session cookie") |
| 215 | return nil |
| 216 | } |
| 217 | |