package webtest import ( "net/url" "regexp" "strings" "testing" ) func TestSettingsShowsBaseAndAddress(t *testing.T) { a := newApp(t) a.signedInOwner() page := a.get("/settings") if page.Code != 200 { t.Fatalf("settings: %d", page.Code) } for _, want := range []string{"Acme", ownerAddress, "Sign out"} { if !strings.Contains(page.Body, want) { t.Errorf("settings lacks %q:\n%s", want, page.Body) } } } func TestSettingsSignOut(t *testing.T) { a := newApp(t) a.signedInOwner() if r := a.post("/auth/signout", nil); r.Code != 303 || !strings.HasPrefix(r.Location(), "/signin") { t.Fatalf("signout: %d %q", r.Code, r.Location()) } if r := a.get("/"); r.Code != 303 || !strings.HasPrefix(r.Location(), "/signin") { t.Fatalf("still signed in: %d %q", r.Code, r.Location()) } } // Finding 7 (R30): Base's sign-out says it is Base's alone. func TestSettingsSignOutSaysBaseOnly(t *testing.T) { a := newApp(t) a.signedInOwner() body := a.get("/settings").Body for _, want := range []string{">Sign out of Base", "This signs you out of Base only. Apps you've opened stay signed in until you sign out of each one."} { if !contains(body, want) { t.Errorf("settings lacks %q:\n%s", want, body) } } } // Finding 8: every inner page's "base" wordmark is a link to Home. func TestInnerPagesLinkHome(t *testing.T) { a := newApp(t) a.signedInOwner() a.setInstall("scratchpad", "installed", "") for _, p := range []string{"/people", "/store", "/settings", "/store/scratchpad/uninstall"} { if body := a.get(p).Body; !strings.Contains(body, `
base`) { t.Errorf("%s has no link to Home:\n%s", p, body) } } } var appearanceRE = regexp.MustCompile(`(?s)
(.*?)
`) // pressed is the Appearance choice Settings shows as current. func pressed(t *testing.T, body string) string { t.Helper() m := appearanceRE.FindStringSubmatch(body) if m == nil { t.Fatalf("no Appearance choice:\n%s", body) } on := regexp.MustCompile(``).FindAllString(m[1], -1)) != 3 { t.Fatalf("want System, Light and Dark with one pressed:\n%s", m[1]) } return on[0][1] } // Appearance follows the device until the member picks Light or Dark in // Settings (a plain form): then every page Base draws for them holds it, as // , on every device. System goes back to the device's. func TestSettingsAppearance(t *testing.T) { a := newApp(t) a.signedInOwner() page := a.get("/settings").Body if got := pressed(t, page); got != "system" || strings.Contains(page, "data-theme") { t.Fatalf("fresh member: %s pressed, data-theme drawn %v", got, strings.Contains(page, "data-theme")) } for _, theme := range []string{"light", "dark", "system"} { if r := a.post("/settings/appearance", url.Values{"theme": {theme}}); r.Code != 303 || r.Location() != "/settings" { t.Fatalf("%s: %d %q", theme, r.Code, r.Location()) } want := `` if theme == "system" { want = `` } page := a.get("/settings").Body if got := pressed(t, page); got != theme { t.Errorf("after %s: %s pressed", theme, got) } for _, path := range []string{"/settings", "/", "/people", "/store"} { if body := a.get(path).Body; !strings.Contains(body, want) { t.Errorf("after %s, %s lacks %s", theme, path, want) } } } other := a.newBrowser() other.signedInOwner() a.post("/settings/appearance", url.Values{"theme": {"dark"}}) if body := other.get("/").Body; !strings.Contains(body, `data-theme="dark"`) { t.Error("the member's other browser doesn't hold Dark") } for _, bad := range []url.Values{{"theme": {"purple"}}, {}} { if r := a.post("/settings/appearance", bad); r.Code != 400 { t.Errorf("%v: %d, want 400", bad, r.Code) } } // Signed out there is no member: the sign-in page follows the device. a.post("/auth/signout", nil) if body := a.get("/signin").Body; strings.Contains(body, "data-theme") { t.Error("the sign-in page forces a theme") } } // Settings says which Equipo person the member is connected as, once Base // has read it from Equipo, and keeps up when it changes. func TestSettingsShowsEquipoPerson(t *testing.T) { a, f := newAskApp(t) f.SetAddress("ana@equipo.example") a.connectEquipo(f) a.get("/ask") if s := a.get("/settings").Body; !strings.Contains(s, "ConnectionConnected as ana@equipo.example") { t.Fatalf("Settings:\n%s", s) } f.SetAddress("bea@equipo.example") a.get("/ask/state") if s := a.get("/settings").Body; !strings.Contains(s, "ConnectionConnected as bea@equipo.example") { t.Fatalf("Settings after a change:\n%s", s) } // Connecting again may be someone else: until Base reads who, it says // only that it is connected. a.approveEquipo(f) if s := a.get("/settings").Body; !strings.Contains(s, "ConnectionConnected") { t.Fatalf("Settings after reconnecting:\n%s", s) } } // Every inner page reached from Home has a close control, an X at the top // right: a plain link Home, so it works without scripts, named with the // chat's approved "Close". func TestInnerPagesCloseToHome(t *testing.T) { const closeLink = `
` a := newApp(t) a.signedInOwner() a.setInstall("scratchpad", "installed", "") pages := map[string]string{} for _, p := range []string{"/people", "/store", "/settings", "/store/scratchpad/uninstall"} { pages[p] = a.get(p).Body } ask, _ := newAskApp(t) pages["/ask"] = ask.get("/ask").Body for p, body := range pages { crumb := regexp.MustCompile(`(?s)
.*?
`).FindString(body) if !strings.HasSuffix(crumb, closeLink) { t.Errorf("%s: the crumb has no close link Home at its end:\n%s", p, crumb) } if strings.Count(body, `aria-label="Close"`) != 1 && p != "/ask" { t.Errorf("%s: want one Close, got %d", p, strings.Count(body, `aria-label="Close"`)) } } } // Settings is in sections, each under a small heading: You, This Base and // (with the chat on) Ask Base, each row in its section, and signing out is // the last thing on the page, its explanation under it. func TestSettingsSections(t *testing.T) { sections := regexp.MustCompile(`(?s)
]*>(?:

(.*?)

)?(.*?)
`) labels := regexp.MustCompile(`
]*>([^<]*)`) layout := func(body string) []string { var got []string for _, s := range sections.FindAllStringSubmatch(body, -1) { line := s[1] + ":" for _, l := range labels.FindAllStringSubmatch(s[2], -1) { line += " " + l[1] + "," } if strings.Contains(s[2], `action="/auth/signout"`) { line += " sign out" } got = append(got, line) } return got } check := func(name, body string, want []string) { t.Helper() if got := layout(body); strings.Join(got, "\n") != strings.Join(want, "\n") { t.Errorf("%s: Settings is laid out as\n%s\nwant\n%s", name, strings.Join(got, "\n"), strings.Join(want, "\n")) } // Signing out closes the page, the explanation right under its button. if !regexp.MustCompile(`(?s)\s*

This signs you out of Base only\. [^<]*

\s*\s*
\s*\s*