| new file mode 100644 |
| index 0000000..83d89bd |
| --- /dev/null |
| +++ b/internal/sheets/home_connect_item_test.go |
| @@ -0,0 +1,222 @@ |
| +package sheets |
| + |
| +import ( |
| + "html" |
| + "net/http" |
| + "net/http/httptest" |
| + "net/url" |
| + "strings" |
| + "testing" |
| + |
| + "amadan.net/oficina/home/connect" |
| +) |
| + |
| +// 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>` |
| + |
| +// offersConnect renders the list and reports whether its account menu |
| +// offers the connection. It fails unless the page is a signed-in one, so |
| +// a missing item can never be a missing menu. |
| +func offersConnect(t *testing.T, f *homeFixture) bool { |
| + t.Helper() |
| + page := f.request(t, "GET", "/sheets", nil, false) |
| + body := page.Body.String() |
| + if page.Code != http.StatusOK || !strings.Contains(body, "<details estilo-account") { |
| + t.Fatalf("/sheets is not a signed-in page: %d", page.Code) |
| + } |
| + return strings.Contains(body, connectItem) |
| +} |
| + |
| +// linkRows counts the stored Home links for one member. |
| +func linkRows(t *testing.T, f *homeFixture, member Member) int64 { |
| + t.Helper() |
| + var n int64 |
| + if err := f.app.db.Table("home_identity_links").Where("person_id = ?", member.ID).Count(&n).Error; err != nil { |
| + t.Fatal(err) |
| + } |
| + return n |
| +} |
| + |
| +// emailSignIn is a real local sign-in through the emailed link, in this |
| +// fixture's browser: request the link, open it, press Sign in. It returns |
| +// the response to the local completion GET, which decides the landing. |
| +func emailSignIn(t *testing.T, f *homeFixture, address string) *httptest.ResponseRecorder { |
| + t.Helper() |
| + if w := f.request(t, "POST", "/signin", url.Values{"address": {address}, "force": {"1"}}, false); w.Code != http.StatusSeeOther { |
| + t.Fatal("request link", w.Code) |
| + } |
| + u, err := url.Parse(f.app.testMailer(t).lastLink(t)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + open := f.request(t, "GET", u.RequestURI(), nil, false) |
| + m := confirmForm.FindStringSubmatch(open.Body.String()) |
| + if open.Code != http.StatusOK || m == nil { |
| + t.Fatalf("open link: %d, want the confirm door", open.Code) |
| + } |
| + verify := f.request(t, "POST", html.UnescapeString(m[1]), url.Values{"token": {html.UnescapeString(m[2])}}, false) |
| + if verify.Code != http.StatusSeeOther || verify.Header().Get("Location") != connect.LocalCompletePath { |
| + t.Fatal("local callback", verify.Code, verify.Header().Get("Location")) |
| + } |
| + return f.request(t, "GET", connect.LocalCompletePath, nil, false) |
| +} |
| + |
| +// The menu item follows the stored link, not how this session signed in. |
| +// Paul hit the bug in Docs: linked, signed in with the email link, and |
| +// still offered "Connect your Oficina account". |
| +func TestConnectItemFollowsTheStoredLink(t *testing.T) { |
| + f := newHomeFixture(t) |
| + member := f.local(t, signedInAddress) |
| + if !offersConnect(t, f) { |
| + t.Fatal("an unlinked local session is not offered the connection") |
| + } |
| + f.link(t, "linked-subject") |
| + if linkRows(t, f, member) != 1 { |
| + t.Fatal("the link flow stored no link") |
| + } |
| + if offersConnect(t, f) { |
| + t.Error("the same session is still offered the connection after linking") |
| + } |
| + // A later sign-in with the app's own email link, by the linked person. |
| + f.jar = map[string]*http.Cookie{} |
| + f.local(t, signedInAddress) |
| + if offersConnect(t, f) { |
| + t.Error("a linked person signed in by email link is offered the connection") |
| + } |
| + g := newHomeFixture(t) |
| + homeSignedIn(t, g) |
| + if offersConnect(t, g) { |
| + t.Error("a Home session is offered the connection") |
| + } |
| +} |
| + |
| +// After the confirm POST, in the same session: the answer leads to |
| +// "Accounts connected", the link row exists, and the next render's menu |
| +// no longer offers the item. The confirm page itself is Sheets' copy and |
| +// its two buttons share one form-foot row, primary first. |
| +func TestConfirmLeadsToAccountsConnected(t *testing.T) { |
| + f := newHomeFixture(t) |
| + member := f.local(t, signedInAddress) |
| + start := f.request(t, "POST", "/account/home", nil, false) |
| + if start.Code != http.StatusSeeOther { |
| + t.Fatal("link start", start.Code) |
| + } |
| + authStart := f.request(t, "GET", start.Header().Get("Location"), nil, false) |
| + if finish := f.exchange(t, authStart, "confirm-subject"); finish.Header().Get("Location") != "/account/home/confirm" { |
| + t.Fatal("finish link", finish.Code, finish.Header().Get("Location")) |
| + } |
| + confirm := f.request(t, "GET", "/account/home/confirm", nil, false) |
| + body := confirm.Body.String() |
| + if confirm.Code != http.StatusOK || !strings.Contains(body, "<h1>Connect these two accounts?</h1>") || |
| + !strings.Contains(body, "Connect it to the Sheets account below, and you can sign in to Sheets with Oficina from now on.") { |
| + t.Fatalf("confirm page: %d, want Sheets' confirm copy\n%s", confirm.Code, body) |
| + } |
| + if strings.Contains(body, "Home account") { |
| + t.Error("the confirm page names the hub") |
| + } |
| + row := strings.Index(body, "<div rst-form-foot>") |
| + if row < 0 { |
| + t.Fatal("the confirm page's buttons are not in one rst-form-foot row") |
| + } |
| + end := row + strings.Index(body[row:], "</div>") |
| + primary := strings.Index(body[row:end], `<button rst-btn="primary" type="submit">Connect accounts</button>`) |
| + ghost := strings.Index(body[row:end], `<a rst-btn="ghost" href="/">`) |
| + if primary < 0 || ghost < 0 || primary > ghost { |
| + t.Fatalf("form-foot row is not primary then ghost: %q", body[row:end]) |
| + } |
| + if linkRows(t, f, member) != 0 { |
| + t.Fatal("a link was stored before the confirm click") |
| + } |
| + posted := f.request(t, "POST", "/account/home/confirm", nil, false) |
| + if posted.Code != http.StatusSeeOther || !strings.HasPrefix(posted.Header().Get("Location"), "/account/home/confirm?connected=1") { |
| + t.Fatal("confirm POST", posted.Code, posted.Header().Get("Location")) |
| + } |
| + done := f.request(t, "GET", posted.Header().Get("Location"), nil, false) |
| + if done.Code != http.StatusOK || !strings.Contains(done.Body.String(), "<h1>Accounts connected</h1>") { |
| + t.Fatalf("after confirm: %d, want Accounts connected\n%s", done.Code, done.Body.String()) |
| + } |
| + if linkRows(t, f, member) != 1 { |
| + t.Fatal("the confirm POST stored no link") |
| + } |
| + if offersConnect(t, f) { |
| + t.Error("the menu still offers the connection right after confirming") |
| + } |
| +} |
| + |
| +// An Oficina arrival with no link shows the unknown page; a local sign-in |
| +// in the same browser then begins the link itself, through a fresh Home |
| +// assertion to the confirm page, and the confirm click stores the link. |
| +// "Accounts connected" leads back to where sign-in began (PageFor). |
| +func TestUnknownArrivalResumesTheLinkAfterLocalSignIn(t *testing.T) { |
| + f := newHomeFixture(t) |
| + signin := f.request(t, "GET", "/signin?return_to=%2Fsheets", nil, false) |
| + unknown := f.exchange(t, f.request(t, "GET", signin.Header().Get("Location"), nil, false), "unknown-subject") |
| + body := unknown.Body.String() |
| + if unknown.Code != http.StatusForbidden || !strings.Contains(body, `href="/signin/local"`) || |
| + !strings.Contains(body, "Sign in to your Sheets account once to connect it to your Oficina account.") { |
| + t.Fatalf("unknown arrival: %d, want the unknown page\n%s", unknown.Code, body) |
| + } |
| + completed := emailSignIn(t, f, signedInAddress) |
| + if completed.Code != http.StatusSeeOther || completed.Header().Get("Location") != "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm" { |
| + t.Fatal("local sign-in after unknown arrival", completed.Code, completed.Header().Get("Location")) |
| + } |
| + if _, ok := f.jar["__Host-oficina-home-init"]; !ok { |
| + t.Fatal("the local sign-in began no link") |
| + } |
| + authStart := f.request(t, "GET", completed.Header().Get("Location"), nil, false) |
| + if finish := f.exchange(t, authStart, "unknown-subject"); finish.Code != http.StatusSeeOther || finish.Header().Get("Location") != "/account/home/confirm" { |
| + t.Fatal("fresh assertion", finish.Code, finish.Header().Get("Location")) |
| + } |
| + member, ok := f.app.memberByID(1) |
| + if !ok || member.Address != signedInAddress { |
| + t.Fatal("local member missing") |
| + } |
| + if page := f.request(t, "GET", "/account/home/confirm", nil, false); page.Code != http.StatusOK || !strings.Contains(page.Body.String(), "Connect these two accounts?") { |
| + t.Fatal("confirm page", page.Code) |
| + } |
| + if linkRows(t, f, member) != 0 { |
| + t.Fatal("a link was stored before the confirm click") |
| + } |
| + posted := f.request(t, "POST", "/account/home/confirm", nil, false) |
| + if posted.Code != http.StatusSeeOther { |
| + t.Fatal("confirm POST", posted.Code) |
| + } |
| + if linkRows(t, f, member) != 1 { |
| + t.Fatal("the confirm POST stored no link") |
| + } |
| + done := f.request(t, "GET", posted.Header().Get("Location"), nil, false) |
| + if done.Code != http.StatusOK || !strings.Contains(done.Body.String(), "<h1>Accounts connected</h1>") || |
| + !strings.Contains(done.Body.String(), `<a rst-btn="primary" href="/sheets">Back to Sheets</a>`) { |
| + t.Fatalf("after confirm: %d, want Accounts connected leading back to /sheets\n%s", done.Code, done.Body.String()) |
| + } |
| + if offersConnect(t, f) { |
| + t.Error("the menu still offers the connection after the resumed link") |
| + } |
| +} |
| + |
| +// Without the remembered arrival the local sign-in lands as before. The |
| +// marker lives ten minutes in the browser (its Max-Age), so a browser |
| +// that has dropped it is the app-level expiry; the sealed in-marker |
| +// expiry is checked upstream (home/connect TestUnknownMarkerExpiresToTheMenuPath), |
| +// since this fixture cannot move connect's navigation clock. |
| +func TestUnknownArrivalForgottenLandsAsBefore(t *testing.T) { |
| + f := newHomeFixture(t) |
| + signin := f.request(t, "GET", "/signin?return_to=%2Fsheets", nil, false) |
| + unknown := f.exchange(t, f.request(t, "GET", signin.Header().Get("Location"), nil, false), "unknown-subject") |
| + if unknown.Code != http.StatusForbidden { |
| + t.Fatal("unknown arrival", unknown.Code) |
| + } |
| + const marker = "__Host-oficina-local-link-navigation" |
| + c, ok := f.jar[marker] |
| + if !ok || c.MaxAge != 600 { |
| + t.Fatal("the unknown arrival left no ten-minute marker", ok) |
| + } |
| + delete(f.jar, marker) |
| + completed := emailSignIn(t, f, signedInAddress) |
| + if completed.Code != http.StatusSeeOther || completed.Header().Get("Location") != "/sheets" { |
| + t.Fatal("local sign-in without the marker", completed.Code, completed.Header().Get("Location")) |
| + } |
| + if !offersConnect(t, f) { |
| + t.Error("the unlinked person is not offered the connection") |
| + } |
| +} |