| new file mode 100644 |
| index 0000000..93d68fd |
| --- /dev/null |
| +++ b/internal/calls/home_connect_item_test.go |
| @@ -0,0 +1,210 @@ |
| +package calls |
| + |
| +import ( |
| + "bytes" |
| + "encoding/base64" |
| + "encoding/json" |
| + "net/http" |
| + "regexp" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/home/connect" |
| + "amadan.net/oficina/home/handoff/seal" |
| +) |
| + |
| +// connectItem is the account menu's "Connect your Oficina account". |
| +const connectItem = `<a estilo-menu-item href="/account/home"><span estilo-menu-label>Connect your Oficina account</span></a>` |
| + |
| +// localComplete signs alice in with an emailed link in this browser and |
| +// returns where GET /auth/local/complete sends her, without following it. |
| +func (f *homeRouterFixture) localComplete(t *testing.T) string { |
| + t.Helper() |
| + homeRedirect(t, f.do("POST", "/signin", "address=alice%40example.test&force=1")) |
| + link := regexp.MustCompile(`https://[^\s<>\"]+/auth/verify\?[^\s<>\"]+`).FindString(f.mailBody()) |
| + if link == "" { |
| + t.Fatal("no captured local verification link") |
| + } |
| + if landing := homeRedirect(t, f.followLink(t, link)); landing != connect.LocalCompletePath { |
| + t.Fatal("wrong local landing", landing) |
| + } |
| + return homeRedirect(t, f.do("GET", connect.LocalCompletePath, "")) |
| +} |
| + |
| +// existingAccount gives alice the account an unknown Oficina sign-in asks |
| +// her to sign in to: one earlier emailed-link sign-in (which creates her |
| +// persons row on /continue), then sign out. Without the row the resume |
| +// cannot bind, and connect falls back to the Connect page. |
| +func (f *homeRouterFixture) existingAccount(t *testing.T) { |
| + t.Helper() |
| + f.local(t) |
| + homeSignout(t, f.do("POST", "/signout", "")) |
| +} |
| + |
| +// linkedRows is how many stored links alice's persons row has. |
| +func (f *homeRouterFixture) linkedRows(t *testing.T) int { |
| + t.Helper() |
| + var n int |
| + 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 { |
| + t.Fatal(err) |
| + } |
| + return n |
| +} |
| + |
| +// confirmAndLand presses Connect accounts on the confirm page and follows |
| +// the answer to the page it leads to, which must be "Accounts connected" |
| +// whose one button goes back to want. |
| +func (f *homeRouterFixture) confirmAndLand(t *testing.T, want string) { |
| + t.Helper() |
| + next := homeRedirect(t, f.do("POST", "/account/home/confirm", "")) |
| + if !strings.HasPrefix(next, "/account/home/confirm?") { |
| + t.Fatalf("the confirm POST leads to %q, not the connected page", next) |
| + } |
| + w := f.do("GET", next, "") |
| + body := w.Body.String() |
| + if w.Code != http.StatusOK || !strings.Contains(body, "<h1>Accounts connected</h1>") { |
| + t.Fatalf("after the confirm POST: %d, want Accounts connected:\n%s", w.Code, body) |
| + } |
| + if button := `<a rst-btn="primary" href="` + want + `">Back to the app</a>`; !strings.Contains(body, button) { |
| + t.Fatalf("Accounts connected does not lead back with %q:\n%s", button, body) |
| + } |
| +} |
| + |
| +// Paul's bug: a linked account signed in with the app's own emailed link |
| +// was still offered "Connect your Oficina account". The offer follows the |
| +// stored link, not how this session signed in. |
| +func TestTheConnectItemFollowsTheStoredLinkNotTheSessionMethod(t *testing.T) { |
| + f := newHomeRouterFixture(t) |
| + f.local(t) |
| + if body := f.do("GET", "/", "").Body.String(); !strings.Contains(body, connectItem) { |
| + t.Fatalf("an unlinked local session is not offered to connect:\n%s", body) |
| + } |
| + f.link(t) // ends signed in through Home |
| + if body := f.do("GET", "/", "").Body.String(); strings.Contains(body, "/account/home") { |
| + t.Fatal("a Home session is offered to connect the account it already is") |
| + } |
| + if f.linkedRows(t) != 1 { |
| + t.Fatal("the link flow stored no link") |
| + } |
| + homeSignout(t, f.do("POST", "/signout", "")) |
| + f.local(t) |
| + if body := f.do("GET", "/", "").Body.String(); strings.Contains(body, connectItem) { |
| + t.Fatal("a linked account signed in by emailed link is still offered to connect") |
| + } |
| +} |
| + |
| +// Right after Connect accounts, in the same (emailed-link) session: the |
| +// answer is "Accounts connected", the link row exists, and the next page's |
| +// menu no longer offers to connect. |
| +func TestConfirmingTheLinkSaysSoAndRetiresTheMenuItem(t *testing.T) { |
| + f := newHomeRouterFixture(t) |
| + f.local(t) |
| + if w := f.do("GET", "/account/home", ""); w.Code != 200 { |
| + t.Fatal("account", w.Code) |
| + } |
| + start := homeRedirect(t, f.do("POST", "/account/home", "")) |
| + if next := homeRedirect(t, f.exchange(t, start, "opaque-home-alice")); next != "/account/home/confirm" { |
| + t.Fatal("confirmation", next) |
| + } |
| + w := f.do("GET", "/account/home/confirm", "") |
| + if w.Code != 200 || !strings.Contains(w.Body.String(), "<h1>Connect these two accounts?</h1>") || !strings.Contains(w.Body.String(), "alice@example.test") { |
| + t.Fatalf("confirm page: %d\n%s", w.Code, w.Body.String()) |
| + } |
| + f.confirmAndLand(t, "/") |
| + if f.linkedRows(t) != 1 { |
| + t.Fatal("Connect accounts stored no link") |
| + } |
| + if body := f.do("GET", "/", "").Body.String(); strings.Contains(body, connectItem) { |
| + t.Fatal("the menu still offers to connect right after connecting") |
| + } |
| +} |
| + |
| +// An Oficina sign-in with no link here shows the unknown page, whose one |
| +// button is the local sign-in. Signing in locally in the same browser then |
| +// begins the link itself: the Home freshness start, a fresh assertion, the |
| +// confirm page, and Connect accounts stores the link, landing back on the |
| +// page the sign-in was for. |
| +func TestAnUnknownOficinaSigninResumesAsALinkAfterLocalSignin(t *testing.T) { |
| + f := newHomeRouterFixture(t) |
| + f.existingAccount(t) |
| + start := homeRedirect(t, f.do("GET", "/signin?return_to=%2Fsettings", "")) |
| + w := f.exchange(t, start, "opaque-home-alice") |
| + if w.Code != http.StatusForbidden || !strings.Contains(w.Body.String(), `<a rst-btn="primary" href="/signin/local">Sign in to your existing account</a>`) { |
| + t.Fatalf("unknown assertion: %d, want the unknown page:\n%s", w.Code, w.Body.String()) |
| + } |
| + resume := f.localComplete(t) |
| + if resume != "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm" { |
| + t.Fatalf("local sign-in after an unknown assertion lands on %q, not the Home freshness start", resume) |
| + } |
| + if _, ok := f.cookies["__Host-oficina-home-init"]; !ok { |
| + t.Fatal("no link initiation cookie") |
| + } |
| + if next := homeRedirect(t, f.exchange(t, resume, "opaque-home-alice")); next != "/account/home/confirm" { |
| + t.Fatal("after the fresh assertion", next) |
| + } |
| + if w := f.do("GET", "/account/home/confirm", ""); w.Code != 200 || !strings.Contains(w.Body.String(), "<h1>Connect these two accounts?</h1>") { |
| + t.Fatalf("confirm page: %d\n%s", w.Code, w.Body.String()) |
| + } |
| + if f.linkedRows(t) != 0 { |
| + t.Fatal("linked before the confirm click") |
| + } |
| + f.confirmAndLand(t, "/settings") |
| + if f.linkedRows(t) != 1 { |
| + t.Fatal("Connect accounts stored no link") |
| + } |
| + homeSignout(t, f.do("POST", "/signout", "")) |
| + start = homeRedirect(t, f.do("GET", "/signin", "")) |
| + if w := f.exchange(t, start, "opaque-home-alice"); w.Code != 303 { |
| + t.Fatalf("Oficina sign-in after linking: %d\n%s", w.Code, w.Body.String()) |
| + } |
| +} |
| + |
| +// homeMarker seals connect's local-link navigation marker as connect |
| +// does, for the fixture's Home DEK, issued at issued. |
| +func homeMarker(t *testing.T, action string, issued time.Time) *http.Cookie { |
| + t.Helper() |
| + s, err := seal.New(bytes.Repeat([]byte{7}, 32), "oficina/calls") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + payload, _ := json.Marshal(struct { |
| + Version int `json:"v"` |
| + Action string `json:"action"` |
| + Issued int64 `json:"issued"` |
| + Expires int64 `json:"expires"` |
| + }{1, action, issued.Unix(), issued.Unix() + 600}) |
| + sealed, err := s.Seal("oficina/calls.local-link-navigation.v1:"+homeTestOrigin, payload) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return &http.Cookie{Name: "__Host-oficina-local-link-navigation", Value: base64.RawURLEncoding.EncodeToString(sealed)} |
| +} |
| + |
| +// An expired unknown-assertion marker (10 minutes) is ignored: the local |
| +// sign-in lands on /continue as any other. The same marker, current, does |
| +// resume, which shows the forged marker is the one connect writes. |
| +func TestAnExpiredUnknownMarkerLandsAsBefore(t *testing.T) { |
| + f := newHomeRouterFixture(t) |
| + f.existingAccount(t) |
| + f.cookies["__Host-oficina-local-link-navigation"] = homeMarker(t, "link-resume", time.Now().Add(-11*time.Minute)) |
| + if got := f.localComplete(t); got != "/continue" { |
| + t.Fatalf("expired marker: local sign-in lands on %q, want /continue", got) |
| + } |
| + homeSignout(t, f.do("POST", "/signout", "")) |
| + f.cookies["__Host-oficina-local-link-navigation"] = homeMarker(t, "link-resume", time.Now().Add(-time.Minute)) |
| + if got := f.localComplete(t); got != "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm" { |
| + t.Fatalf("current marker: local sign-in lands on %q, want the Home freshness start", got) |
| + } |
| +} |
| + |
| +// The connect pages' two buttons share one form-foot row, primary first. |
| +func TestTheConnectPagesButtonsShareOneRowPrimaryFirst(t *testing.T) { |
| + f := newHomeRouterFixture(t) |
| + f.local(t) |
| + body := f.do("GET", "/account/home", "").Body.String() |
| + 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>` |
| + if !strings.Contains(body, want) { |
| + t.Fatalf("the connect page's buttons are not one row, primary first; want %s in:\n%s", want, body) |
| + } |
| +} |