| 1 | package fichastest |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "log/slog" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "net/url" |
| 9 | "path/filepath" |
| 10 | "regexp" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | fichas "amadan.net/oficina/fichas/internal/fichas" |
| 15 | "amadan.net/rastrillo/rastrillo/db" |
| 16 | ) |
| 17 | |
| 18 | // appWith is newApp with the config adjusted first. |
| 19 | func appWith(t *testing.T, adjust func(*fichas.Config)) http.Handler { |
| 20 | t.Helper() |
| 21 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 22 | d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) |
| 23 | if err != nil { |
| 24 | t.Fatalf("db.Open: %v", err) |
| 25 | } |
| 26 | t.Cleanup(func() { d.Close() }) |
| 27 | cfg := testConfig(t) |
| 28 | adjust(&cfg) |
| 29 | mux, err := fichas.AppForTest(d, cfg, logger, TestOwner) |
| 30 | if err != nil { |
| 31 | t.Fatalf("AppForTest: %v", err) |
| 32 | } |
| 33 | return mux |
| 34 | } |
| 35 | |
| 36 | // The account is the suite's account menu, estilo's, carrying what every |
| 37 | // app's carries. It sat at the rail's foot (Paul, 2026-10-01); the level-up |
| 38 | // moves it to the end of the bar, as Docs', Slides' and Mapa's (level-up |
| 39 | // §2, §3.1), and the rail's foot holds the fold. |
| 40 | func TestTheBarEndsWithTheAccountMenu(t *testing.T) { |
| 41 | body := get(t, newApp(t), "/").Body.String() |
| 42 | foot := body[strings.Index(body, "<header estilo-bar"):] |
| 43 | foot = foot[:strings.Index(foot, "</header>")] |
| 44 | for _, want := range []string{ |
| 45 | `<details estilo-account name="estilo-menus">`, |
| 46 | `aria-label="Account: ` + TestOwner + `"`, |
| 47 | `<span estilo-menu-identity-label>Signed in as</span>`, |
| 48 | `<form estilo-appearance method="post" action="/appearance" role="group" aria-labelledby="estilo-appearance"`, |
| 49 | `<input type="hidden" name="return_to" value="/">`, |
| 50 | `<form method="post" action="/signout">`, |
| 51 | } { |
| 52 | if !strings.Contains(foot, want) { |
| 53 | t.Errorf("the bar lacks %s:\n%s", want, foot) |
| 54 | } |
| 55 | } |
| 56 | if n := strings.Count(body, ">Sign out<"); n != 1 { |
| 57 | t.Errorf("Sign out appears %d times; once, in the account menu", n) |
| 58 | } |
| 59 | if strings.Contains(body, `rst-btn="ghost" type="submit">Sign out`) { |
| 60 | t.Error("the old bordered Sign out button is still in the rail") |
| 61 | } |
| 62 | // Nothing Fichas does not have: no Home connect, no credits page, and |
| 63 | // no hub directory configured here. |
| 64 | for _, absent := range []string{"Connect your Oficina account", "Open source", "All apps", "estilo-menu-heading>Help"} { |
| 65 | if strings.Contains(body, absent) { |
| 66 | t.Errorf("the account menu offers %q, which Fichas does not have", absent) |
| 67 | } |
| 68 | } |
| 69 | pressed := regexp.MustCompile(`value="(light|dark|system)" aria-pressed="(true|false)"`).FindAllStringSubmatch(foot, -1) |
| 70 | if len(pressed) != 3 { |
| 71 | t.Fatalf("want Light, Dark and System, got %v", pressed) |
| 72 | } |
| 73 | for _, p := range pressed { |
| 74 | if (p[1] == "system") != (p[2] == "true") { |
| 75 | t.Errorf("%s pressed=%s; with no preference only System is checked", p[1], p[2]) |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // "All apps" goes to the hub directory when one is configured. |
| 81 | func TestAllAppsWhenThereIsAHubDirectory(t *testing.T) { |
| 82 | h := appWith(t, func(c *fichas.Config) { c.Directory = "https://oficina.example" }) |
| 83 | body := get(t, h, "/").Body.String() |
| 84 | if !strings.Contains(body, `<a estilo-menu-item href="https://oficina.example/"><span estilo-menu-label>All apps</span></a>`) { |
| 85 | t.Errorf("no All apps row to the hub directory:\n%s", body) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Appearance is a per-person preference set by a form: estilo's handler, |
| 90 | // inside the CSRF check, and every page — the shell and the plain layout |
| 91 | // alike — draws the chosen scheme from the first byte. |
| 92 | func TestAppearanceIsChosenInTheAccountMenu(t *testing.T) { |
| 93 | h := newApp(t) |
| 94 | |
| 95 | // No same-origin evidence: refused by the CSRF check like any POST. |
| 96 | req := httptest.NewRequest(http.MethodPost, "/appearance", strings.NewReader("mode=dark&return_to=%2Fcards%2Fnew")) |
| 97 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 98 | rec := httptest.NewRecorder() |
| 99 | h.ServeHTTP(rec, req) |
| 100 | if rec.Code != http.StatusForbidden { |
| 101 | t.Errorf("a cross-site Appearance POST answered %d; want 403", rec.Code) |
| 102 | } |
| 103 | |
| 104 | rec = post(t, h, "/appearance", url.Values{"mode": {"dark"}, "return_to": {"/cards/new"}}) |
| 105 | if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/cards/new" { |
| 106 | t.Fatalf("POST /appearance: %d to %q; want 303 back to /cards/new", rec.Code, rec.Header().Get("Location")) |
| 107 | } |
| 108 | // return_to is the page the menu was opened on, carried in the form as |
| 109 | // in every app (spec §8.3), and only ever a path on this origin: |
| 110 | // anything else lands on the list. |
| 111 | for _, off := range []string{"https://evil.example/", "//evil.example/", "/\\evil.example", "/\t/evil.example", "javascript:alert(1)", ""} { |
| 112 | r := post(t, h, "/appearance", url.Values{"mode": {"dark"}, "return_to": {off}}) |
| 113 | if loc := r.Header().Get("Location"); r.Code != http.StatusSeeOther || loc != "/" { |
| 114 | t.Errorf("return_to %q: %d to %q; want 303 to /", off, r.Code, loc) |
| 115 | } |
| 116 | } |
| 117 | cookies := rec.Result().Cookies() |
| 118 | if len(cookies) != 1 || cookies[0].Value != "dark" { |
| 119 | t.Fatalf("POST /appearance set %v; want the dark preference", cookies) |
| 120 | } |
| 121 | for _, path := range []string{"/", "/signin?sent=1", "/signed-out"} { |
| 122 | r := httptest.NewRequest(http.MethodGet, path, nil) |
| 123 | r.AddCookie(cookies[0]) |
| 124 | rec := httptest.NewRecorder() |
| 125 | h.ServeHTTP(rec, r) |
| 126 | if !strings.Contains(rec.Body.String(), `<html lang="en" dir="ltr" data-theme="dark">`) { |
| 127 | t.Errorf("%s does not draw the dark scheme the person chose", path) |
| 128 | } |
| 129 | if path == "/" && !strings.Contains(rec.Body.String(), `value="dark" aria-pressed="true"`) { |
| 130 | t.Error("the account menu does not check Dark") |
| 131 | } |
| 132 | } |
| 133 | // No preference, no attribute: the scheme follows the system. |
| 134 | if strings.Contains(get(t, h, "/").Body.String(), "data-theme") { |
| 135 | t.Error("data-theme drawn without a preference") |
| 136 | } |
| 137 | } |
| 138 | |