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 = `Connect your Oficina account` // 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, "
Connect these two accounts?") || !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, "
") if row < 0 { t.Fatal("the confirm page's buttons are not in one rst-form-foot row") } end := row + strings.Index(body[row:], "
") primary := strings.Index(body[row:end], ``) ghost := strings.Index(body[row:end], ``) 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(), "

Accounts connected

") { 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/oficina?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(), "

Accounts connected

") || !strings.Contains(done.Body.String(), `
Back to Sheets`) { 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/oficina?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") } }