| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | "html" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "net/url" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/home/connect" |
| 14 | "amadan.net/oficina/home/handoff" |
| 15 | "amadan.net/rastrillo/rastrillo" |
| 16 | "amadan.net/rastrillo/rastrillo/assertion" |
| 17 | assertioncrypto "amadan.net/rastrillo/rastrillo/crypto" |
| 18 | ) |
| 19 | |
| 20 | // The account menu's "Connect your Oficina account" follows the stored |
| 21 | // link, not how this session signed in (Paul hit the item while already |
| 22 | // linked, signed in with the emailed link). These run the app's real |
| 23 | // router under the locale middleware, with one browser's cookie jar. |
| 24 | |
| 25 | const menuItem = `href="/account/home"` |
| 26 | const menuLabel = "Connect your Oficina account" |
| 27 | |
| 28 | // homeBrowser is one browser against the Home-configured app. |
| 29 | type homeBrowser struct { |
| 30 | t *testing.T |
| 31 | a *app |
| 32 | key *assertioncrypto.Keypair |
| 33 | h http.Handler |
| 34 | jar map[string]*http.Cookie |
| 35 | } |
| 36 | |
| 37 | func newHomeBrowser(t *testing.T) (*homeBrowser, User) { |
| 38 | t.Helper() |
| 39 | mux, a, u, key := homeFixtureKey(t) |
| 40 | loc, err := rastrillo.NewLocales(Locales, DefaultLocale, rastrillo.BaseCatalog(), LocaleFS) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | return &homeBrowser{t: t, a: a, key: key, h: loc.Middleware(mux), jar: map[string]*http.Cookie{}}, u |
| 45 | } |
| 46 | |
| 47 | func (b *homeBrowser) set(c *http.Cookie) { b.jar[c.Name] = c } |
| 48 | |
| 49 | func (b *homeBrowser) do(method, path string, form url.Values, origin string) *httptest.ResponseRecorder { |
| 50 | b.t.Helper() |
| 51 | var body *strings.Reader |
| 52 | if form != nil { |
| 53 | body = strings.NewReader(form.Encode()) |
| 54 | } else { |
| 55 | body = strings.NewReader("") |
| 56 | } |
| 57 | r := httptest.NewRequest(method, homeTestOrigin+path, body) |
| 58 | r.Header.Set("Accept", "text/html") |
| 59 | if method == http.MethodPost { |
| 60 | r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 61 | if origin == "" { |
| 62 | origin = homeTestOrigin |
| 63 | } |
| 64 | } |
| 65 | if origin != "" { |
| 66 | r.Header.Set("Origin", origin) |
| 67 | } |
| 68 | for _, c := range b.jar { |
| 69 | r.AddCookie(c) |
| 70 | } |
| 71 | w := httptest.NewRecorder() |
| 72 | b.h.ServeHTTP(w, r) |
| 73 | for _, c := range w.Result().Cookies() { |
| 74 | if c.MaxAge < 0 { |
| 75 | delete(b.jar, c.Name) |
| 76 | } else { |
| 77 | b.jar[c.Name] = c |
| 78 | } |
| 79 | } |
| 80 | return w |
| 81 | } |
| 82 | |
| 83 | func (b *homeBrowser) get(path string) *httptest.ResponseRecorder { |
| 84 | return b.do(http.MethodGet, path, nil, "") |
| 85 | } |
| 86 | |
| 87 | // home answers the Home request at loc, as Oficina would, with a signed |
| 88 | // assertion for subject, and follows the callback to the finish redirect. |
| 89 | func (b *homeBrowser) home(loc, subject string) *httptest.ResponseRecorder { |
| 90 | b.t.Helper() |
| 91 | target, err := url.Parse(loc) |
| 92 | if err != nil || target.Query().Get("nonce") == "" { |
| 93 | b.t.Fatalf("not a Home request: %q", loc) |
| 94 | } |
| 95 | id, _ := assertion.RandomID() |
| 96 | now := time.Now().Unix() |
| 97 | token, err := assertion.Sign(b.key, assertion.Claims{Version: 1, Issuer: b.a.cfg.Home.Issuer, Audience: homeTestOrigin, Subject: subject, KeyID: "test", ID: id, Nonce: target.Query().Get("nonce"), IssuedAt: now, ExpiresAt: now + 90, AuthTime: now}) |
| 98 | if err != nil { |
| 99 | b.t.Fatal(err) |
| 100 | } |
| 101 | cb := b.do(http.MethodPost, handoff.CallbackPath, url.Values{"assertion": {token}}, b.a.cfg.Home.Issuer) |
| 102 | if cb.Code != 303 { |
| 103 | b.t.Fatal("callback", cb.Code, cb.Body.String()) |
| 104 | } |
| 105 | return b.get(handoff.FinishPath) |
| 106 | } |
| 107 | |
| 108 | // localSignIn signs in with the emailed link through the real form, mail |
| 109 | // and confirm door, and returns the verify POST's response. |
| 110 | func (b *homeBrowser) localSignIn(address string) *httptest.ResponseRecorder { |
| 111 | b.t.Helper() |
| 112 | return b.localSignInReturning(address, "") |
| 113 | } |
| 114 | |
| 115 | // localSignInReturning is localSignIn from a door that carried return_to. |
| 116 | func (b *homeBrowser) localSignInReturning(address, returnTo string) *httptest.ResponseRecorder { |
| 117 | b.t.Helper() |
| 118 | form := url.Values{"address": {address}} |
| 119 | if returnTo != "" { |
| 120 | form.Set("return_to", returnTo) |
| 121 | } |
| 122 | sent := b.do(http.MethodPost, "/signin", form, "") |
| 123 | if sent.Code != 303 { |
| 124 | b.t.Fatal("send", sent.Code, sent.Body.String()) |
| 125 | } |
| 126 | b.a.FlushMail() |
| 127 | link := regexp.MustCompile(`https://[^\s<>]+/auth/verify\?token=[A-Za-z0-9_-]+`).FindString(b.a.cfg.Mailer.(*homeTestMail).body) |
| 128 | if link == "" { |
| 129 | b.t.Fatal("no sign-in link mailed") |
| 130 | } |
| 131 | parsed, _ := url.Parse(link) |
| 132 | open := b.get(parsed.RequestURI()) |
| 133 | m := confirmForm.FindStringSubmatch(open.Body.String()) |
| 134 | if open.Code != 200 || m == nil { |
| 135 | b.t.Fatal("confirm door", open.Code, open.Body.String()) |
| 136 | } |
| 137 | return b.do(http.MethodPost, html.UnescapeString(m[1]), url.Values{"token": {html.UnescapeString(m[2])}}, "") |
| 138 | } |
| 139 | |
| 140 | func (b *homeBrowser) linkRows() int64 { |
| 141 | var n int64 |
| 142 | if err := b.a.db.G.Table("home_identity_links").Count(&n).Error; err != nil { |
| 143 | b.t.Fatal(err) |
| 144 | } |
| 145 | return n |
| 146 | } |
| 147 | |
| 148 | // menuOffers renders the deck list and reports whether its account menu |
| 149 | // offers the connection. |
| 150 | func (b *homeBrowser) menuOffers() bool { |
| 151 | b.t.Helper() |
| 152 | w := b.get("/") |
| 153 | if w.Code != 200 || !strings.Contains(w.Body.String(), "Sign out") { |
| 154 | b.t.Fatalf("deck list not rendered signed in: %d\n%s", w.Code, w.Body.String()) |
| 155 | } |
| 156 | body := w.Body.String() |
| 157 | return strings.Contains(body, menuItem) && strings.Contains(body, menuLabel) |
| 158 | } |
| 159 | |
| 160 | func TestHomeMenuOffersConnectToUnlinkedLocalSession(t *testing.T) { |
| 161 | b, u := newHomeBrowser(t) |
| 162 | b.set(homeSession(t, b.a, u.Email, "magiclink")) |
| 163 | if !b.menuOffers() { |
| 164 | t.Fatal("an unlinked local session is not offered Connect your Oficina account") |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // The bug: linked, but signed in with the emailed link. |
| 169 | func TestHomeMenuHidesConnectFromLinkedLocalSession(t *testing.T) { |
| 170 | b, u := newHomeBrowser(t) |
| 171 | homeLinked(t, b.a, u) |
| 172 | b.set(homeSession(t, b.a, u.Email, "magiclink")) |
| 173 | if b.menuOffers() { |
| 174 | t.Fatal("a linked person signed in locally is still offered Connect your Oficina account") |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | func TestHomeMenuHidesConnectFromHomeSession(t *testing.T) { |
| 179 | b, u := newHomeBrowser(t) |
| 180 | homeLinked(t, b.a, u) |
| 181 | b.set(homeSession(t, b.a, "home-person", connect.HomeMethod)) |
| 182 | if b.menuOffers() { |
| 183 | t.Fatal("a Home session is offered Connect your Oficina account") |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // Right after the confirm POST, in the same session: Accounts connected, |
| 188 | // the row stored, and the menu item gone on the next page. |
| 189 | func TestHomeMenuConnectThenAccountsConnectedAndItemGone(t *testing.T) { |
| 190 | b, u := newHomeBrowser(t) |
| 191 | b.set(homeSession(t, b.a, u.Email, "magiclink")) |
| 192 | if !b.menuOffers() { |
| 193 | t.Fatal("precondition: the item is offered before linking") |
| 194 | } |
| 195 | start := b.do(http.MethodPost, "/account/home", nil, "") |
| 196 | if start.Code != 303 { |
| 197 | t.Fatal("start", start.Code, start.Body.String()) |
| 198 | } |
| 199 | auth := b.get(start.Header().Get("Location")) |
| 200 | finish := b.home(auth.Header().Get("Location"), "menu-home-person") |
| 201 | if finish.Code != 303 || finish.Header().Get("Location") != "/account/home/confirm" { |
| 202 | t.Fatal("finish", finish.Code, finish.Header()) |
| 203 | } |
| 204 | page := b.get("/account/home/confirm") |
| 205 | if page.Code != 200 || !strings.Contains(page.Body.String(), "Connect these two accounts?") || !strings.Contains(page.Body.String(), "Connect it to the Slides account below") { |
| 206 | t.Fatalf("confirm page lacks its own title and copy: %d\n%s", page.Code, page.Body.String()) |
| 207 | } |
| 208 | post := b.do(http.MethodPost, "/account/home/confirm", nil, "") |
| 209 | loc := post.Header().Get("Location") |
| 210 | if post.Code != 303 || !strings.HasPrefix(loc, "/account/home/confirm?") || !strings.Contains(loc, "connected=1") { |
| 211 | t.Fatalf("confirm POST does not lead to Accounts connected: %d %q", post.Code, loc) |
| 212 | } |
| 213 | if n := b.linkRows(); n != 1 { |
| 214 | t.Fatalf("link rows after confirm = %d, want 1", n) |
| 215 | } |
| 216 | if person, err := b.a.home.Resolve(t.Context(), "menu-home-person"); err != nil || person.ID != u.ID { |
| 217 | t.Fatal("stored link does not resolve to the local person", person, err) |
| 218 | } |
| 219 | done := b.get(loc) |
| 220 | if done.Code != 200 || !strings.Contains(done.Body.String(), "Accounts connected") { |
| 221 | t.Fatalf("Accounts connected not shown: %d\n%s", done.Code, done.Body.String()) |
| 222 | } |
| 223 | if !strings.Contains(done.Body.String(), `<a rst-btn="primary" href="/">Back to Slides</a>`) { |
| 224 | t.Fatal("Accounts connected has no way back\n" + done.Body.String()) |
| 225 | } |
| 226 | // Its way back is the page the link began from (connect.PageFor), |
| 227 | // not always the deck list. |
| 228 | back := b.get("/account/home/confirm?connected=1&return_to=%2Fagents") |
| 229 | if back.Code != 200 || !strings.Contains(back.Body.String(), `<a rst-btn="primary" href="/agents">Back to Slides</a>`) { |
| 230 | t.Fatalf("Accounts connected does not lead back to where the link began: %d\n%s", back.Code, back.Body.String()) |
| 231 | } |
| 232 | if b.menuOffers() { |
| 233 | t.Fatal("the menu still offers Connect your Oficina account after the link was stored") |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // An ordinary Home sign-in with an unlinked subject shows the unknown |
| 238 | // page; a local sign-in in the same browser then resumes the link, |
| 239 | // through a fresh Home assertion and the explicit confirm. |
| 240 | func TestHomeUnknownAssertionResumesLinkAfterLocalSignIn(t *testing.T) { |
| 241 | b, u := newHomeBrowser(t) |
| 242 | b.unknownThenResume(t, u.Email, u.ID) |
| 243 | } |
| 244 | |
| 245 | // TestHomeUnknownAssertionResumesForAFirstLocalSignIn is the same flow for |
| 246 | // an admitted address with no users row yet (Astra's review of #23): the |
| 247 | // first local sign-in must create the row before connect resumes, or the |
| 248 | // resume falls to /account/home and that page answers 403. |
| 249 | func TestHomeUnknownAssertionResumesForAFirstLocalSignIn(t *testing.T) { |
| 250 | b, u := newHomeBrowser(t) |
| 251 | if err := b.a.db.G.Delete(&User{}, u.ID).Error; err != nil { |
| 252 | t.Fatal(err) |
| 253 | } |
| 254 | b.unknownThenResume(t, u.Email, 0) |
| 255 | var created User |
| 256 | if err := b.a.db.G.Where("email = ?", u.Email).Take(&created).Error; err != nil || created.ID == u.ID { |
| 257 | t.Fatal("first local sign-in did not create its own users row", created, err) |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // unknownThenResume runs an unknown Oficina arrival, a local sign-in as |
| 262 | // email, the resumed fresh exchange and the explicit confirm, and checks |
| 263 | // the link is stored only after the confirm, against the person signed in |
| 264 | // (id, or any row with that address when id is 0). |
| 265 | func (b *homeBrowser) unknownThenResume(t *testing.T, email string, id int64) { |
| 266 | t.Helper() |
| 267 | // /signin is the door, offering Continue with Oficina ID, which is |
| 268 | // /signin/oficina: that starts the Home exchange. |
| 269 | front := b.get("/signin") |
| 270 | if front.Code != 200 || !strings.Contains(front.Body.String(), `href="/signin/oficina"`) { |
| 271 | t.Fatal("front door", front.Code, front.Header()) |
| 272 | } |
| 273 | start := b.get("/signin/oficina") |
| 274 | if start.Code != 303 || !strings.HasPrefix(start.Header().Get("Location"), handoff.StartPath+"?") { |
| 275 | t.Fatal("Continue with Oficina ID", start.Code, start.Header()) |
| 276 | } |
| 277 | auth := b.get(start.Header().Get("Location")) |
| 278 | unknown := b.home(auth.Header().Get("Location"), "unlinked-home-person") |
| 279 | body := unknown.Body.String() |
| 280 | if unknown.Code != 403 || !strings.Contains(body, "Oficina account not connected") || !strings.Contains(body, "Sign in to your Slides account once to connect it to your Oficina account.") { |
| 281 | t.Fatalf("not the unknown page: %d\n%s", unknown.Code, body) |
| 282 | } |
| 283 | if !regexp.MustCompile(`<a rst-btn="primary" href="/signin/local">`).MatchString(body) { |
| 284 | t.Fatal("the unknown page's button does not go to /signin/local\n" + body) |
| 285 | } |
| 286 | if b.get("/signin/local").Code != 200 { |
| 287 | t.Fatal("local sign-in form") |
| 288 | } |
| 289 | verify := b.localSignIn(email) |
| 290 | if verify.Code != 303 || verify.Header().Get("Location") != connect.LocalCompletePath { |
| 291 | t.Fatal("local verify", verify.Code, verify.Header()) |
| 292 | } |
| 293 | resume := b.get(connect.LocalCompletePath) |
| 294 | if resume.Code != 303 || resume.Header().Get("Location") != "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm" { |
| 295 | t.Fatalf("local sign-in did not resume the link: %d %q", resume.Code, resume.Header().Get("Location")) |
| 296 | } |
| 297 | if b.jar["__Host-oficina-home-init"] == nil { |
| 298 | t.Fatal("resume set no link-init cookie") |
| 299 | } |
| 300 | fresh := b.get(resume.Header().Get("Location")) |
| 301 | target, _ := url.Parse(fresh.Header().Get("Location")) |
| 302 | if fresh.Code != 303 || target == nil || target.Query().Get("max_age") != "300" { |
| 303 | t.Fatal("resumed link did not ask Oficina for fresh authentication", fresh.Code, fresh.Header()) |
| 304 | } |
| 305 | finish := b.home(fresh.Header().Get("Location"), "unlinked-home-person") |
| 306 | if finish.Code != 303 || finish.Header().Get("Location") != "/account/home/confirm" { |
| 307 | t.Fatal("finish", finish.Code, finish.Header()) |
| 308 | } |
| 309 | if n := b.linkRows(); n != 0 { |
| 310 | t.Fatal("linked before the explicit confirm") |
| 311 | } |
| 312 | if page := b.get("/account/home/confirm"); page.Code != 200 || !strings.Contains(page.Body.String(), "Connect these two accounts?") { |
| 313 | t.Fatalf("confirm page: %d\n%s", page.Code, page.Body.String()) |
| 314 | } |
| 315 | post := b.do(http.MethodPost, "/account/home/confirm", nil, "") |
| 316 | if post.Code != 303 || !strings.Contains(post.Header().Get("Location"), "connected=1") { |
| 317 | t.Fatal("confirm", post.Code, post.Header()) |
| 318 | } |
| 319 | if person, err := b.a.home.Resolve(t.Context(), "unlinked-home-person"); err != nil || person.Label != email || (id != 0 && person.ID != id) { |
| 320 | t.Fatal("resumed link not stored", person, err) |
| 321 | } |
| 322 | if b.linkRows() != 1 { |
| 323 | t.Fatal("expected one link row") |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | // Without the remembered arrival, a local sign-in lands as before. |
| 328 | func TestHomeLocalSignInWithoutMarkerLandsAsBefore(t *testing.T) { |
| 329 | b, u := newHomeBrowser(t) |
| 330 | verify := b.localSignIn(u.Email) |
| 331 | if verify.Code != 303 || verify.Header().Get("Location") != connect.LocalCompletePath { |
| 332 | t.Fatal("local verify", verify.Code, verify.Header()) |
| 333 | } |
| 334 | if done := b.get(connect.LocalCompletePath); done.Code != 303 || done.Header().Get("Location") != "/" { |
| 335 | t.Fatalf("plain local sign-in did not land on /: %d %q", done.Code, done.Header().Get("Location")) |
| 336 | } |
| 337 | if b.jar["__Host-oficina-home-init"] != nil { |
| 338 | t.Fatal("a plain local sign-in began a link") |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | // The connect page's two buttons share one rst-form-foot row, primary |
| 343 | // first (estilo's homeconnect template). |
| 344 | func TestHomeConnectPageButtonsInOneFootRow(t *testing.T) { |
| 345 | b, u := newHomeBrowser(t) |
| 346 | b.set(homeSession(t, b.a, u.Email, "magiclink")) |
| 347 | w := b.get("/account/home") |
| 348 | if w.Code != 200 { |
| 349 | t.Fatal(w.Code) |
| 350 | } |
| 351 | row := regexp.MustCompile(`(?s)<div rst-form-foot>\s*<form method="post" action="/account/home"><button rst-btn="primary" type="submit">Connect</button></form>\s*<a rst-btn="ghost" href="/">Back</a>\s*</div>`) |
| 352 | if !row.MatchString(w.Body.String()) { |
| 353 | t.Fatalf("the Connect and Back buttons are not one rst-form-foot row, primary first:\n%s", w.Body.String()) |
| 354 | } |
| 355 | } |
| 356 | |