| 1 | package calls |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/base64" |
| 6 | "encoding/json" |
| 7 | "net/http" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/home/connect" |
| 14 | "amadan.net/oficina/home/handoff/seal" |
| 15 | ) |
| 16 | |
| 17 | // connectItem is the account menu's "Connect your Oficina account". |
| 18 | const connectItem = `<a estilo-menu-item href="/account/home"><span estilo-menu-label>Connect your Oficina account</span></a>` |
| 19 | |
| 20 | // localComplete signs alice in with an emailed link in this browser and |
| 21 | // returns where GET /auth/local/complete sends her, without following it. |
| 22 | func (f *homeRouterFixture) localComplete(t *testing.T) string { |
| 23 | t.Helper() |
| 24 | homeRedirect(t, f.do("POST", "/signin", "address=alice%40example.test&force=1")) |
| 25 | link := regexp.MustCompile(`https://[^\s<>\"]+/auth/verify\?[^\s<>\"]+`).FindString(f.mailBody()) |
| 26 | if link == "" { |
| 27 | t.Fatal("no captured local verification link") |
| 28 | } |
| 29 | if landing := homeRedirect(t, f.followLink(t, link)); landing != connect.LocalCompletePath { |
| 30 | t.Fatal("wrong local landing", landing) |
| 31 | } |
| 32 | return homeRedirect(t, f.do("GET", connect.LocalCompletePath, "")) |
| 33 | } |
| 34 | |
| 35 | // existingAccount gives alice the account an unknown Oficina sign-in asks |
| 36 | // her to sign in to: one earlier emailed-link sign-in (which creates her |
| 37 | // persons row on /continue), then sign out. Without the row the resume |
| 38 | // cannot bind, and connect falls back to the Connect page. |
| 39 | func (f *homeRouterFixture) existingAccount(t *testing.T) { |
| 40 | t.Helper() |
| 41 | f.local(t) |
| 42 | homeSignout(t, f.do("POST", "/signout", "")) |
| 43 | } |
| 44 | |
| 45 | // linkedRows is how many stored links alice's persons row has. |
| 46 | func (f *homeRouterFixture) linkedRows(t *testing.T) int { |
| 47 | t.Helper() |
| 48 | var n int |
| 49 | if err := f.app.db.Writer().QueryRow("SELECT count(*) FROM home_identity_links l JOIN persons p ON p.id = l.person_id WHERE p.email = 'alice@example.test'").Scan(&n); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | return n |
| 53 | } |
| 54 | |
| 55 | // confirmAndLand presses Connect accounts on the confirm page and follows |
| 56 | // the answer to the page it leads to, which must be "Accounts connected" |
| 57 | // whose one button goes back to want. |
| 58 | func (f *homeRouterFixture) confirmAndLand(t *testing.T, want string) { |
| 59 | t.Helper() |
| 60 | next := homeRedirect(t, f.do("POST", "/account/home/confirm", "")) |
| 61 | if !strings.HasPrefix(next, "/account/home/confirm?") { |
| 62 | t.Fatalf("the confirm POST leads to %q, not the connected page", next) |
| 63 | } |
| 64 | w := f.do("GET", next, "") |
| 65 | body := w.Body.String() |
| 66 | if w.Code != http.StatusOK || !strings.Contains(body, "<h1>Accounts connected</h1>") { |
| 67 | t.Fatalf("after the confirm POST: %d, want Accounts connected:\n%s", w.Code, body) |
| 68 | } |
| 69 | if button := `<a rst-btn="primary" href="` + want + `">Back to the app</a>`; !strings.Contains(body, button) { |
| 70 | t.Fatalf("Accounts connected does not lead back with %q:\n%s", button, body) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Paul's bug: a linked account signed in with the app's own emailed link |
| 75 | // was still offered "Connect your Oficina account". The offer follows the |
| 76 | // stored link, not how this session signed in. |
| 77 | func TestTheConnectItemFollowsTheStoredLinkNotTheSessionMethod(t *testing.T) { |
| 78 | f := newHomeRouterFixture(t) |
| 79 | f.local(t) |
| 80 | if body := f.do("GET", "/", "").Body.String(); !strings.Contains(body, connectItem) { |
| 81 | t.Fatalf("an unlinked local session is not offered to connect:\n%s", body) |
| 82 | } |
| 83 | f.link(t) // ends signed in through Home |
| 84 | if body := f.do("GET", "/", "").Body.String(); strings.Contains(body, "/account/home") { |
| 85 | t.Fatal("a Home session is offered to connect the account it already is") |
| 86 | } |
| 87 | if f.linkedRows(t) != 1 { |
| 88 | t.Fatal("the link flow stored no link") |
| 89 | } |
| 90 | homeSignout(t, f.do("POST", "/signout", "")) |
| 91 | f.local(t) |
| 92 | if body := f.do("GET", "/", "").Body.String(); strings.Contains(body, connectItem) { |
| 93 | t.Fatal("a linked account signed in by emailed link is still offered to connect") |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Right after Connect accounts, in the same (emailed-link) session: the |
| 98 | // answer is "Accounts connected", the link row exists, and the next page's |
| 99 | // menu no longer offers to connect. |
| 100 | func TestConfirmingTheLinkSaysSoAndRetiresTheMenuItem(t *testing.T) { |
| 101 | f := newHomeRouterFixture(t) |
| 102 | f.local(t) |
| 103 | if w := f.do("GET", "/account/home", ""); w.Code != 200 { |
| 104 | t.Fatal("account", w.Code) |
| 105 | } |
| 106 | start := homeRedirect(t, f.do("POST", "/account/home", "")) |
| 107 | if next := homeRedirect(t, f.exchange(t, start, "opaque-home-alice")); next != "/account/home/confirm" { |
| 108 | t.Fatal("confirmation", next) |
| 109 | } |
| 110 | w := f.do("GET", "/account/home/confirm", "") |
| 111 | if w.Code != 200 || !strings.Contains(w.Body.String(), "<h1>Connect these two accounts?</h1>") || !strings.Contains(w.Body.String(), "alice@example.test") { |
| 112 | t.Fatalf("confirm page: %d\n%s", w.Code, w.Body.String()) |
| 113 | } |
| 114 | f.confirmAndLand(t, "/") |
| 115 | if f.linkedRows(t) != 1 { |
| 116 | t.Fatal("Connect accounts stored no link") |
| 117 | } |
| 118 | if body := f.do("GET", "/", "").Body.String(); strings.Contains(body, connectItem) { |
| 119 | t.Fatal("the menu still offers to connect right after connecting") |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // An Oficina sign-in with no link here shows the unknown page, whose one |
| 124 | // button is the local sign-in. Signing in locally in the same browser then |
| 125 | // begins the link itself: the Home freshness start, a fresh assertion, the |
| 126 | // confirm page, and Connect accounts stores the link, landing back on the |
| 127 | // page the sign-in was for. |
| 128 | func TestAnUnknownOficinaSigninResumesAsALinkAfterLocalSignin(t *testing.T) { |
| 129 | f := newHomeRouterFixture(t) |
| 130 | f.existingAccount(t) |
| 131 | start := homeRedirect(t, f.do("GET", "/signin/oficina?return_to=%2Fsettings", "")) |
| 132 | w := f.exchange(t, start, "opaque-home-alice") |
| 133 | if w.Code != http.StatusForbidden || !strings.Contains(w.Body.String(), `<a rst-btn="primary" href="/signin/local">Sign in to your existing account</a>`) { |
| 134 | t.Fatalf("unknown assertion: %d, want the unknown page:\n%s", w.Code, w.Body.String()) |
| 135 | } |
| 136 | resume := f.localComplete(t) |
| 137 | if resume != "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm" { |
| 138 | t.Fatalf("local sign-in after an unknown assertion lands on %q, not the Home freshness start", resume) |
| 139 | } |
| 140 | if _, ok := f.cookies["__Host-oficina-home-init"]; !ok { |
| 141 | t.Fatal("no link initiation cookie") |
| 142 | } |
| 143 | if next := homeRedirect(t, f.exchange(t, resume, "opaque-home-alice")); next != "/account/home/confirm" { |
| 144 | t.Fatal("after the fresh assertion", next) |
| 145 | } |
| 146 | if w := f.do("GET", "/account/home/confirm", ""); w.Code != 200 || !strings.Contains(w.Body.String(), "<h1>Connect these two accounts?</h1>") { |
| 147 | t.Fatalf("confirm page: %d\n%s", w.Code, w.Body.String()) |
| 148 | } |
| 149 | if f.linkedRows(t) != 0 { |
| 150 | t.Fatal("linked before the confirm click") |
| 151 | } |
| 152 | f.confirmAndLand(t, "/settings") |
| 153 | if f.linkedRows(t) != 1 { |
| 154 | t.Fatal("Connect accounts stored no link") |
| 155 | } |
| 156 | homeSignout(t, f.do("POST", "/signout", "")) |
| 157 | start = homeRedirect(t, f.do("GET", "/signin/oficina", "")) |
| 158 | if w := f.exchange(t, start, "opaque-home-alice"); w.Code != 303 { |
| 159 | t.Fatalf("Oficina sign-in after linking: %d\n%s", w.Code, w.Body.String()) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // homeMarker seals connect's local-link navigation marker as connect |
| 164 | // does, for the fixture's Home DEK, issued at issued. |
| 165 | func homeMarker(t *testing.T, action string, issued time.Time) *http.Cookie { |
| 166 | t.Helper() |
| 167 | s, err := seal.New(bytes.Repeat([]byte{7}, 32), "oficina/calls") |
| 168 | if err != nil { |
| 169 | t.Fatal(err) |
| 170 | } |
| 171 | payload, _ := json.Marshal(struct { |
| 172 | Version int `json:"v"` |
| 173 | Action string `json:"action"` |
| 174 | Issued int64 `json:"issued"` |
| 175 | Expires int64 `json:"expires"` |
| 176 | }{1, action, issued.Unix(), issued.Unix() + 600}) |
| 177 | sealed, err := s.Seal("oficina/calls.local-link-navigation.v1:"+homeTestOrigin, payload) |
| 178 | if err != nil { |
| 179 | t.Fatal(err) |
| 180 | } |
| 181 | return &http.Cookie{Name: "__Host-oficina-local-link-navigation", Value: base64.RawURLEncoding.EncodeToString(sealed)} |
| 182 | } |
| 183 | |
| 184 | // An expired unknown-assertion marker (10 minutes) is ignored: the local |
| 185 | // sign-in lands on /continue as any other. The same marker, current, does |
| 186 | // resume, which shows the forged marker is the one connect writes. |
| 187 | func TestAnExpiredUnknownMarkerLandsAsBefore(t *testing.T) { |
| 188 | f := newHomeRouterFixture(t) |
| 189 | f.existingAccount(t) |
| 190 | f.cookies["__Host-oficina-local-link-navigation"] = homeMarker(t, "link-resume", time.Now().Add(-11*time.Minute)) |
| 191 | if got := f.localComplete(t); got != "/continue" { |
| 192 | t.Fatalf("expired marker: local sign-in lands on %q, want /continue", got) |
| 193 | } |
| 194 | homeSignout(t, f.do("POST", "/signout", "")) |
| 195 | f.cookies["__Host-oficina-local-link-navigation"] = homeMarker(t, "link-resume", time.Now().Add(-time.Minute)) |
| 196 | if got := f.localComplete(t); got != "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm" { |
| 197 | t.Fatalf("current marker: local sign-in lands on %q, want the Home freshness start", got) |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // The connect pages' two buttons share one form-foot row, primary first. |
| 202 | func TestTheConnectPagesButtonsShareOneRowPrimaryFirst(t *testing.T) { |
| 203 | f := newHomeRouterFixture(t) |
| 204 | f.local(t) |
| 205 | body := f.do("GET", "/account/home", "").Body.String() |
| 206 | want := `<div rst-form-foot><form method="post" action="/account/home"><button rst-btn="primary" type="submit">Connect</button></form><a rst-btn="ghost" href="/">Back</a></div>` |
| 207 | if !strings.Contains(body, want) { |
| 208 | t.Fatalf("the connect page's buttons are not one row, primary first; want %s in:\n%s", want, body) |
| 209 | } |
| 210 | } |
| 211 | |