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(`
`
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)Sign out of Base\s*