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:], "
") ||
!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")
}
}