| new file mode 100644 |
| index 0000000..08bf757 |
| --- /dev/null |
| +++ b/internal/slides/home_menu_test.go |
| @@ -0,0 +1,314 @@ |
| +package slides |
| + |
| +import ( |
| + "html" |
| + "net/http" |
| + "net/http/httptest" |
| + "net/url" |
| + "regexp" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/home/connect" |
| + "amadan.net/oficina/home/handoff" |
| + "amadan.net/rastrillo/rastrillo" |
| + "amadan.net/rastrillo/rastrillo/assertion" |
| + assertioncrypto "amadan.net/rastrillo/rastrillo/crypto" |
| +) |
| + |
| +// The account menu's "Connect your Oficina account" follows the stored |
| +// link, not how this session signed in (Paul hit the item while already |
| +// linked, signed in with the emailed link). These run the app's real |
| +// router under the locale middleware, with one browser's cookie jar. |
| + |
| +const menuItem = `href="/account/home"` |
| +const menuLabel = "Connect your Oficina account" |
| + |
| +// homeBrowser is one browser against the Home-configured app. |
| +type homeBrowser struct { |
| + t *testing.T |
| + a *app |
| + key *assertioncrypto.Keypair |
| + h http.Handler |
| + jar map[string]*http.Cookie |
| +} |
| + |
| +func newHomeBrowser(t *testing.T) (*homeBrowser, User) { |
| + t.Helper() |
| + mux, a, u, key := homeFixtureKey(t) |
| + loc, err := rastrillo.NewLocales(Locales, DefaultLocale, rastrillo.BaseCatalog(), LocaleFS) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return &homeBrowser{t: t, a: a, key: key, h: loc.Middleware(mux), jar: map[string]*http.Cookie{}}, u |
| +} |
| + |
| +func (b *homeBrowser) set(c *http.Cookie) { b.jar[c.Name] = c } |
| + |
| +func (b *homeBrowser) do(method, path string, form url.Values, origin string) *httptest.ResponseRecorder { |
| + b.t.Helper() |
| + var body *strings.Reader |
| + if form != nil { |
| + body = strings.NewReader(form.Encode()) |
| + } else { |
| + body = strings.NewReader("") |
| + } |
| + r := httptest.NewRequest(method, homeTestOrigin+path, body) |
| + r.Header.Set("Accept", "text/html") |
| + if method == http.MethodPost { |
| + r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| + if origin == "" { |
| + origin = homeTestOrigin |
| + } |
| + } |
| + if origin != "" { |
| + r.Header.Set("Origin", origin) |
| + } |
| + for _, c := range b.jar { |
| + r.AddCookie(c) |
| + } |
| + w := httptest.NewRecorder() |
| + b.h.ServeHTTP(w, r) |
| + for _, c := range w.Result().Cookies() { |
| + if c.MaxAge < 0 { |
| + delete(b.jar, c.Name) |
| + } else { |
| + b.jar[c.Name] = c |
| + } |
| + } |
| + return w |
| +} |
| + |
| +func (b *homeBrowser) get(path string) *httptest.ResponseRecorder { |
| + return b.do(http.MethodGet, path, nil, "") |
| +} |
| + |
| +// home answers the Home request at loc, as Oficina would, with a signed |
| +// assertion for subject, and follows the callback to the finish redirect. |
| +func (b *homeBrowser) home(loc, subject string) *httptest.ResponseRecorder { |
| + b.t.Helper() |
| + target, err := url.Parse(loc) |
| + if err != nil || target.Query().Get("nonce") == "" { |
| + b.t.Fatalf("not a Home request: %q", loc) |
| + } |
| + id, _ := assertion.RandomID() |
| + now := time.Now().Unix() |
| + 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}) |
| + if err != nil { |
| + b.t.Fatal(err) |
| + } |
| + cb := b.do(http.MethodPost, handoff.CallbackPath, url.Values{"assertion": {token}}, b.a.cfg.Home.Issuer) |
| + if cb.Code != 303 { |
| + b.t.Fatal("callback", cb.Code, cb.Body.String()) |
| + } |
| + return b.get(handoff.FinishPath) |
| +} |
| + |
| +// localSignIn signs in with the emailed link through the real form, mail |
| +// and confirm door, and returns the verify POST's response. |
| +func (b *homeBrowser) localSignIn(address string) *httptest.ResponseRecorder { |
| + b.t.Helper() |
| + sent := b.do(http.MethodPost, "/signin", url.Values{"address": {address}}, "") |
| + if sent.Code != 303 { |
| + b.t.Fatal("send", sent.Code, sent.Body.String()) |
| + } |
| + b.a.FlushMail() |
| + link := regexp.MustCompile(`https://[^\s<>]+/auth/verify\?token=[A-Za-z0-9_-]+`).FindString(b.a.cfg.Mailer.(*homeTestMail).body) |
| + if link == "" { |
| + b.t.Fatal("no sign-in link mailed") |
| + } |
| + parsed, _ := url.Parse(link) |
| + open := b.get(parsed.RequestURI()) |
| + m := confirmForm.FindStringSubmatch(open.Body.String()) |
| + if open.Code != 200 || m == nil { |
| + b.t.Fatal("confirm door", open.Code, open.Body.String()) |
| + } |
| + return b.do(http.MethodPost, html.UnescapeString(m[1]), url.Values{"token": {html.UnescapeString(m[2])}}, "") |
| +} |
| + |
| +func (b *homeBrowser) linkRows() int64 { |
| + var n int64 |
| + if err := b.a.db.G.Table("home_identity_links").Count(&n).Error; err != nil { |
| + b.t.Fatal(err) |
| + } |
| + return n |
| +} |
| + |
| +// menuOffers renders the deck list and reports whether its account menu |
| +// offers the connection. |
| +func (b *homeBrowser) menuOffers() bool { |
| + b.t.Helper() |
| + w := b.get("/") |
| + if w.Code != 200 || !strings.Contains(w.Body.String(), "Sign out") { |
| + b.t.Fatalf("deck list not rendered signed in: %d\n%s", w.Code, w.Body.String()) |
| + } |
| + body := w.Body.String() |
| + return strings.Contains(body, menuItem) && strings.Contains(body, menuLabel) |
| +} |
| + |
| +func TestHomeMenuOffersConnectToUnlinkedLocalSession(t *testing.T) { |
| + b, u := newHomeBrowser(t) |
| + b.set(homeSession(t, b.a, u.Email, "magiclink")) |
| + if !b.menuOffers() { |
| + t.Fatal("an unlinked local session is not offered Connect your Oficina account") |
| + } |
| +} |
| + |
| +// The bug: linked, but signed in with the emailed link. |
| +func TestHomeMenuHidesConnectFromLinkedLocalSession(t *testing.T) { |
| + b, u := newHomeBrowser(t) |
| + homeLinked(t, b.a, u) |
| + b.set(homeSession(t, b.a, u.Email, "magiclink")) |
| + if b.menuOffers() { |
| + t.Fatal("a linked person signed in locally is still offered Connect your Oficina account") |
| + } |
| +} |
| + |
| +func TestHomeMenuHidesConnectFromHomeSession(t *testing.T) { |
| + b, u := newHomeBrowser(t) |
| + homeLinked(t, b.a, u) |
| + b.set(homeSession(t, b.a, "home-person", connect.HomeMethod)) |
| + if b.menuOffers() { |
| + t.Fatal("a Home session is offered Connect your Oficina account") |
| + } |
| +} |
| + |
| +// Right after the confirm POST, in the same session: Accounts connected, |
| +// the row stored, and the menu item gone on the next page. |
| +func TestHomeMenuConnectThenAccountsConnectedAndItemGone(t *testing.T) { |
| + b, u := newHomeBrowser(t) |
| + b.set(homeSession(t, b.a, u.Email, "magiclink")) |
| + if !b.menuOffers() { |
| + t.Fatal("precondition: the item is offered before linking") |
| + } |
| + start := b.do(http.MethodPost, "/account/home", nil, "") |
| + if start.Code != 303 { |
| + t.Fatal("start", start.Code, start.Body.String()) |
| + } |
| + auth := b.get(start.Header().Get("Location")) |
| + finish := b.home(auth.Header().Get("Location"), "menu-home-person") |
| + if finish.Code != 303 || finish.Header().Get("Location") != "/account/home/confirm" { |
| + t.Fatal("finish", finish.Code, finish.Header()) |
| + } |
| + page := b.get("/account/home/confirm") |
| + 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") { |
| + t.Fatalf("confirm page lacks its own title and copy: %d\n%s", page.Code, page.Body.String()) |
| + } |
| + post := b.do(http.MethodPost, "/account/home/confirm", nil, "") |
| + loc := post.Header().Get("Location") |
| + if post.Code != 303 || !strings.HasPrefix(loc, "/account/home/confirm?") || !strings.Contains(loc, "connected=1") { |
| + t.Fatalf("confirm POST does not lead to Accounts connected: %d %q", post.Code, loc) |
| + } |
| + if n := b.linkRows(); n != 1 { |
| + t.Fatalf("link rows after confirm = %d, want 1", n) |
| + } |
| + if person, err := b.a.home.Resolve(t.Context(), "menu-home-person"); err != nil || person.ID != u.ID { |
| + t.Fatal("stored link does not resolve to the local person", person, err) |
| + } |
| + done := b.get(loc) |
| + if done.Code != 200 || !strings.Contains(done.Body.String(), "Accounts connected") { |
| + t.Fatalf("Accounts connected not shown: %d\n%s", done.Code, done.Body.String()) |
| + } |
| + if !strings.Contains(done.Body.String(), `<a rst-btn="primary" href="/">Back to Slides</a>`) { |
| + t.Fatal("Accounts connected has no way back\n" + done.Body.String()) |
| + } |
| + // Its way back is the page the link began from (connect.PageFor), |
| + // not always the deck list. |
| + back := b.get("/account/home/confirm?connected=1&return_to=%2Fagents") |
| + if back.Code != 200 || !strings.Contains(back.Body.String(), `<a rst-btn="primary" href="/agents">Back to Slides</a>`) { |
| + t.Fatalf("Accounts connected does not lead back to where the link began: %d\n%s", back.Code, back.Body.String()) |
| + } |
| + if b.menuOffers() { |
| + t.Fatal("the menu still offers Connect your Oficina account after the link was stored") |
| + } |
| +} |
| + |
| +// An ordinary Home sign-in with an unlinked subject shows the unknown |
| +// page; a local sign-in in the same browser then resumes the link, |
| +// through a fresh Home assertion and the explicit confirm. |
| +func TestHomeUnknownAssertionResumesLinkAfterLocalSignIn(t *testing.T) { |
| + b, u := newHomeBrowser(t) |
| + front := b.get("/signin") |
| + if front.Code != 303 { |
| + t.Fatal("front door", front.Code, front.Header()) |
| + } |
| + auth := b.get(front.Header().Get("Location")) |
| + unknown := b.home(auth.Header().Get("Location"), "unlinked-home-person") |
| + body := unknown.Body.String() |
| + 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.") { |
| + t.Fatalf("not the unknown page: %d\n%s", unknown.Code, body) |
| + } |
| + if !regexp.MustCompile(`<a rst-btn="primary" href="/signin/local">`).MatchString(body) { |
| + t.Fatal("the unknown page's button does not go to /signin/local\n" + body) |
| + } |
| + if b.get("/signin/local").Code != 200 { |
| + t.Fatal("local sign-in form") |
| + } |
| + verify := b.localSignIn(u.Email) |
| + if verify.Code != 303 || verify.Header().Get("Location") != connect.LocalCompletePath { |
| + t.Fatal("local verify", verify.Code, verify.Header()) |
| + } |
| + resume := b.get(connect.LocalCompletePath) |
| + if resume.Code != 303 || resume.Header().Get("Location") != "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm" { |
| + t.Fatalf("local sign-in did not resume the link: %d %q", resume.Code, resume.Header().Get("Location")) |
| + } |
| + if b.jar["__Host-oficina-home-init"] == nil { |
| + t.Fatal("resume set no link-init cookie") |
| + } |
| + fresh := b.get(resume.Header().Get("Location")) |
| + target, _ := url.Parse(fresh.Header().Get("Location")) |
| + if fresh.Code != 303 || target == nil || target.Query().Get("max_age") != "300" { |
| + t.Fatal("resumed link did not ask Oficina for fresh authentication", fresh.Code, fresh.Header()) |
| + } |
| + finish := b.home(fresh.Header().Get("Location"), "unlinked-home-person") |
| + if finish.Code != 303 || finish.Header().Get("Location") != "/account/home/confirm" { |
| + t.Fatal("finish", finish.Code, finish.Header()) |
| + } |
| + if n := b.linkRows(); n != 0 { |
| + t.Fatal("linked before the explicit confirm") |
| + } |
| + if page := b.get("/account/home/confirm"); page.Code != 200 || !strings.Contains(page.Body.String(), "Connect these two accounts?") { |
| + t.Fatalf("confirm page: %d\n%s", page.Code, page.Body.String()) |
| + } |
| + post := b.do(http.MethodPost, "/account/home/confirm", nil, "") |
| + if post.Code != 303 || !strings.Contains(post.Header().Get("Location"), "connected=1") { |
| + t.Fatal("confirm", post.Code, post.Header()) |
| + } |
| + if person, err := b.a.home.Resolve(t.Context(), "unlinked-home-person"); err != nil || person.ID != u.ID { |
| + t.Fatal("resumed link not stored", person, err) |
| + } |
| + if b.linkRows() != 1 { |
| + t.Fatal("expected one link row") |
| + } |
| +} |
| + |
| +// Without the remembered arrival, a local sign-in lands as before. |
| +func TestHomeLocalSignInWithoutMarkerLandsAsBefore(t *testing.T) { |
| + b, u := newHomeBrowser(t) |
| + verify := b.localSignIn(u.Email) |
| + if verify.Code != 303 || verify.Header().Get("Location") != connect.LocalCompletePath { |
| + t.Fatal("local verify", verify.Code, verify.Header()) |
| + } |
| + if done := b.get(connect.LocalCompletePath); done.Code != 303 || done.Header().Get("Location") != "/" { |
| + t.Fatalf("plain local sign-in did not land on /: %d %q", done.Code, done.Header().Get("Location")) |
| + } |
| + if b.jar["__Host-oficina-home-init"] != nil { |
| + t.Fatal("a plain local sign-in began a link") |
| + } |
| +} |
| + |
| +// The connect page's two buttons share one rst-form-foot row, primary |
| +// first (estilo's homeconnect template). |
| +func TestHomeConnectPageButtonsInOneFootRow(t *testing.T) { |
| + b, u := newHomeBrowser(t) |
| + b.set(homeSession(t, b.a, u.Email, "magiclink")) |
| + w := b.get("/account/home") |
| + if w.Code != 200 { |
| + t.Fatal(w.Code) |
| + } |
| + 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>`) |
| + if !row.MatchString(w.Body.String()) { |
| + t.Fatalf("the Connect and Back buttons are not one rst-form-foot row, primary first:\n%s", w.Body.String()) |
| + } |
| +} |