| 1 | package webtest |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "regexp" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestSettingsShowsBaseAndAddress(t *testing.T) { |
| 11 | a := newApp(t) |
| 12 | a.signedInOwner() |
| 13 | page := a.get("/settings") |
| 14 | if page.Code != 200 { |
| 15 | t.Fatalf("settings: %d", page.Code) |
| 16 | } |
| 17 | for _, want := range []string{"Acme", ownerAddress, "Sign out"} { |
| 18 | if !strings.Contains(page.Body, want) { |
| 19 | t.Errorf("settings lacks %q:\n%s", want, page.Body) |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func TestSettingsSignOut(t *testing.T) { |
| 25 | a := newApp(t) |
| 26 | a.signedInOwner() |
| 27 | if r := a.post("/auth/signout", nil); r.Code != 303 || !strings.HasPrefix(r.Location(), "/signin") { |
| 28 | t.Fatalf("signout: %d %q", r.Code, r.Location()) |
| 29 | } |
| 30 | if r := a.get("/"); r.Code != 303 || !strings.HasPrefix(r.Location(), "/signin") { |
| 31 | t.Fatalf("still signed in: %d %q", r.Code, r.Location()) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // Finding 7 (R30): Base's sign-out says it is Base's alone. |
| 36 | func TestSettingsSignOutSaysBaseOnly(t *testing.T) { |
| 37 | a := newApp(t) |
| 38 | a.signedInOwner() |
| 39 | body := a.get("/settings").Body |
| 40 | for _, want := range []string{">Sign out of Base</button>", "This signs you out of Base only. Apps you've opened stay signed in until you sign out of each one."} { |
| 41 | if !contains(body, want) { |
| 42 | t.Errorf("settings lacks %q:\n%s", want, body) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // Finding 8: every inner page's "base" wordmark is a link to Home. |
| 48 | func TestInnerPagesLinkHome(t *testing.T) { |
| 49 | a := newApp(t) |
| 50 | a.signedInOwner() |
| 51 | a.setInstall("scratchpad", "installed", "") |
| 52 | for _, p := range []string{"/people", "/store", "/settings", "/store/scratchpad/uninstall"} { |
| 53 | if body := a.get(p).Body; !strings.Contains(body, `<div class="crumb"><a class="wordmark" href="/">base</a>`) { |
| 54 | t.Errorf("%s has no link to Home:\n%s", p, body) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | var appearanceRE = regexp.MustCompile(`(?s)<form class="segmented" method="post" action="/settings/appearance" role="group" aria-labelledby="appearance">(.*?)</form>`) |
| 60 | |
| 61 | // pressed is the Appearance choice Settings shows as current. |
| 62 | func pressed(t *testing.T, body string) string { |
| 63 | t.Helper() |
| 64 | m := appearanceRE.FindStringSubmatch(body) |
| 65 | if m == nil { |
| 66 | t.Fatalf("no Appearance choice:\n%s", body) |
| 67 | } |
| 68 | on := regexp.MustCompile(`<button type="submit" name="theme" value="(\w+)" aria-pressed="true">`).FindAllStringSubmatch(m[1], -1) |
| 69 | if len(on) != 1 || len(regexp.MustCompile(`<button type="submit" name="theme" value="(system|light|dark)" aria-pressed="(true|false)">(System|Light|Dark)</button>`).FindAllString(m[1], -1)) != 3 { |
| 70 | t.Fatalf("want System, Light and Dark with one pressed:\n%s", m[1]) |
| 71 | } |
| 72 | return on[0][1] |
| 73 | } |
| 74 | |
| 75 | // Appearance follows the device until the member picks Light or Dark in |
| 76 | // Settings (a plain form): then every page Base draws for them holds it, as |
| 77 | // <html data-theme>, on every device. System goes back to the device's. |
| 78 | func TestSettingsAppearance(t *testing.T) { |
| 79 | a := newApp(t) |
| 80 | a.signedInOwner() |
| 81 | page := a.get("/settings").Body |
| 82 | if got := pressed(t, page); got != "system" || strings.Contains(page, "data-theme") { |
| 83 | t.Fatalf("fresh member: %s pressed, data-theme drawn %v", got, strings.Contains(page, "data-theme")) |
| 84 | } |
| 85 | for _, theme := range []string{"light", "dark", "system"} { |
| 86 | if r := a.post("/settings/appearance", url.Values{"theme": {theme}}); r.Code != 303 || r.Location() != "/settings" { |
| 87 | t.Fatalf("%s: %d %q", theme, r.Code, r.Location()) |
| 88 | } |
| 89 | want := `<html lang="en" data-theme="` + theme + `">` |
| 90 | if theme == "system" { |
| 91 | want = `<html lang="en">` |
| 92 | } |
| 93 | page := a.get("/settings").Body |
| 94 | if got := pressed(t, page); got != theme { |
| 95 | t.Errorf("after %s: %s pressed", theme, got) |
| 96 | } |
| 97 | for _, path := range []string{"/settings", "/", "/people", "/store"} { |
| 98 | if body := a.get(path).Body; !strings.Contains(body, want) { |
| 99 | t.Errorf("after %s, %s lacks %s", theme, path, want) |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | other := a.newBrowser() |
| 104 | other.signedInOwner() |
| 105 | a.post("/settings/appearance", url.Values{"theme": {"dark"}}) |
| 106 | if body := other.get("/").Body; !strings.Contains(body, `data-theme="dark"`) { |
| 107 | t.Error("the member's other browser doesn't hold Dark") |
| 108 | } |
| 109 | for _, bad := range []url.Values{{"theme": {"purple"}}, {}} { |
| 110 | if r := a.post("/settings/appearance", bad); r.Code != 400 { |
| 111 | t.Errorf("%v: %d, want 400", bad, r.Code) |
| 112 | } |
| 113 | } |
| 114 | // Signed out there is no member: the sign-in page follows the device. |
| 115 | a.post("/auth/signout", nil) |
| 116 | if body := a.get("/signin").Body; strings.Contains(body, "data-theme") { |
| 117 | t.Error("the sign-in page forces a theme") |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Settings says which Equipo person the member is connected as, once Base |
| 122 | // has read it from Equipo, and keeps up when it changes. |
| 123 | func TestSettingsShowsEquipoPerson(t *testing.T) { |
| 124 | a, f := newAskApp(t) |
| 125 | f.SetAddress("ana@equipo.example") |
| 126 | a.connectEquipo(f) |
| 127 | a.get("/ask") |
| 128 | if s := a.get("/settings").Body; !strings.Contains(s, "Connection<small>Connected as ana@equipo.example</small>") { |
| 129 | t.Fatalf("Settings:\n%s", s) |
| 130 | } |
| 131 | f.SetAddress("bea@equipo.example") |
| 132 | a.get("/ask/state") |
| 133 | if s := a.get("/settings").Body; !strings.Contains(s, "Connection<small>Connected as bea@equipo.example</small>") { |
| 134 | t.Fatalf("Settings after a change:\n%s", s) |
| 135 | } |
| 136 | // Connecting again may be someone else: until Base reads who, it says |
| 137 | // only that it is connected. |
| 138 | a.approveEquipo(f) |
| 139 | if s := a.get("/settings").Body; !strings.Contains(s, "Connection<small>Connected</small>") { |
| 140 | t.Fatalf("Settings after reconnecting:\n%s", s) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Every inner page reached from Home has a close control, an X at the top |
| 145 | // right: a plain link Home, so it works without scripts, named with the |
| 146 | // chat's approved "Close". |
| 147 | func TestInnerPagesCloseToHome(t *testing.T) { |
| 148 | const closeLink = `<a class="round close" href="/" aria-label="Close"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6 6l12 12M18 6L6 18"/></svg></a></div>` |
| 149 | a := newApp(t) |
| 150 | a.signedInOwner() |
| 151 | a.setInstall("scratchpad", "installed", "") |
| 152 | pages := map[string]string{} |
| 153 | for _, p := range []string{"/people", "/store", "/settings", "/store/scratchpad/uninstall"} { |
| 154 | pages[p] = a.get(p).Body |
| 155 | } |
| 156 | ask, _ := newAskApp(t) |
| 157 | pages["/ask"] = ask.get("/ask").Body |
| 158 | for p, body := range pages { |
| 159 | crumb := regexp.MustCompile(`(?s)<div class="crumb">.*?</div>`).FindString(body) |
| 160 | if !strings.HasSuffix(crumb, closeLink) { |
| 161 | t.Errorf("%s: the crumb has no close link Home at its end:\n%s", p, crumb) |
| 162 | } |
| 163 | if strings.Count(body, `aria-label="Close"`) != 1 && p != "/ask" { |
| 164 | t.Errorf("%s: want one Close, got %d", p, strings.Count(body, `aria-label="Close"`)) |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // Settings is in sections, each under a small heading: You, This Base and |
| 170 | // (with the chat on) Ask Base, each row in its section, and signing out is |
| 171 | // the last thing on the page, its explanation under it. |
| 172 | func TestSettingsSections(t *testing.T) { |
| 173 | sections := regexp.MustCompile(`(?s)<section class="group[^"]*"[^>]*>(?:<h3 id="[^"]+">(.*?)</h3>)?(.*?)</section>`) |
| 174 | labels := regexp.MustCompile(`<div class="who"[^>]*>([^<]*)`) |
| 175 | layout := func(body string) []string { |
| 176 | var got []string |
| 177 | for _, s := range sections.FindAllStringSubmatch(body, -1) { |
| 178 | line := s[1] + ":" |
| 179 | for _, l := range labels.FindAllStringSubmatch(s[2], -1) { |
| 180 | line += " " + l[1] + "," |
| 181 | } |
| 182 | if strings.Contains(s[2], `action="/auth/signout"`) { |
| 183 | line += " sign out" |
| 184 | } |
| 185 | got = append(got, line) |
| 186 | } |
| 187 | return got |
| 188 | } |
| 189 | check := func(name, body string, want []string) { |
| 190 | t.Helper() |
| 191 | if got := layout(body); strings.Join(got, "\n") != strings.Join(want, "\n") { |
| 192 | t.Errorf("%s: Settings is laid out as\n%s\nwant\n%s", name, strings.Join(got, "\n"), strings.Join(want, "\n")) |
| 193 | } |
| 194 | // Signing out closes the page, the explanation right under its button. |
| 195 | if !regexp.MustCompile(`(?s)<button class="btn ghost" type="submit">Sign out of Base</button></form>\s*<p class="note">This signs you out of Base only\. [^<]*</p>\s*</section>\s*</div>\s*</div>\s*<script`).MatchString(body) { |
| 196 | t.Errorf("%s: signing out is not the last thing, its explanation under it:\n%s", name, body) |
| 197 | } |
| 198 | } |
| 199 | a := newApp(t) |
| 200 | a.signedInOwner() |
| 201 | check("chat off", a.get("/settings").Body, []string{ |
| 202 | "You: Signed in as, Appearance, Passkeys,", |
| 203 | "This Base: Base name,", |
| 204 | ": sign out", |
| 205 | }) |
| 206 | ask, _ := newAskApp(t) |
| 207 | check("chat on", ask.get("/settings").Body, []string{ |
| 208 | "You: Signed in as, Appearance, Passkeys,", |
| 209 | "This Base: Base name,", |
| 210 | "Ask Base: Connection,", |
| 211 | ": sign out", |
| 212 | }) |
| 213 | } |
| 214 | |