oficina / home Public

Clone
git clone https://amadan.net/oficina/home

Plain git — no account needed to clone.

Download

Download this file

1package home
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "net/url"
7 "os"
8 "path"
9 "path/filepath"
10 "regexp"
11 "strings"
12 "testing"
13
14 "amadan.net/oficina/estilo.git/theme"
15 "amadan.net/oficina/home/internal/home/sealtest"
16)
17
18// The shared chrome (estilo spec shared-chrome, §12.1 row 8, Home): every
19// page Home draws for a person, other than the public front page, sits
20// under estilo's 52px bar with the brand "Oficina". On the directory, a
21// signed-in person has one account menu (who, Passkeys and Recovery codes
22// on id.*, Appearance, Help → Open source, one Sign out); signed out, the
23// corner is "Sign in". id.* pages are the door: the bar carries the brand
24// only, linking back to the directory.
25
26// chromeGet is a GET with an Accept header (empty sends none).
27func chromeGet(h http.Handler, target, accept string, cookies ...*http.Cookie) *httptest.ResponseRecorder {
28 r := httptest.NewRequest(http.MethodGet, target, nil)
29 if accept != "" {
30 r.Header.Set("Accept", accept)
31 }
32 for _, c := range cookies {
33 if c != nil {
34 r.AddCookie(c)
35 }
36 }
37 rec := httptest.NewRecorder()
38 h.ServeHTTP(rec, r)
39 return rec
40}
41
42var pageTitle = regexp.MustCompile(`<title>([^<]*)</title>`)
43
44func titleOf(body string) string {
45 m := pageTitle.FindStringSubmatch(body)
46 if m == nil {
47 return ""
48 }
49 return m[1]
50}
51
52const acceptHTML = "text/html,application/xhtml+xml"
53
54func TestHomePagesWearTheSharedBar(t *testing.T) {
55 f := newMountedChooser(t)
56 directory := f.p.directory.Router()
57 credential := f.p.credential.Router()
58 person := f.session(t, chooserPerson)
59 idSession := signInFully(t, f.p.credential, sealtest.CanaryAddress)
60
61 type want struct {
62 title string
63 account bool // the account menu, and with it exactly one Sign out
64 signIn bool // the signed-out corner's "Sign in"
65 sky bool
66 brand string // the brand link's href
67 }
68 cases := []struct {
69 name string
70 h http.Handler
71 target string
72 cookie *http.Cookie
73 want want
74 }{
75 {"directory", directory, directoryOrigin + "/", person, want{"Oficina", true, false, true, "/"}},
76 {"team chooser", directory, directoryOrigin + docsEntryPath, person, want{"No teams available · Oficina", true, false, false, "/"}},
77 {"open source, signed in", directory, directoryOrigin + "/help/open-source", person, want{"Open source · Oficina", true, false, false, "/"}},
78 {"open source, signed out", directory, directoryOrigin + "/help/open-source", nil, want{"Open source · Oficina", false, true, false, "/"}},
79 {"signed out", directory, directoryOrigin + "/signed-out", nil, want{"Signed out · Oficina", false, false, true, "/"}},
80 {"id.* sign in", credential, Origin + "/signin", nil, want{"Sign in to Oficina ID", false, false, true, directoryOrigin + "/"}},
81 {"id.* check your email", credential, Origin + "/signin/sent", nil, want{"Check your email to sign in to Oficina ID", false, false, true, directoryOrigin + "/"}},
82 {"id.* recovery codes", credential, Origin + "/passkey/codes", idSession, want{"Recovery codes · Oficina", false, false, true, directoryOrigin + "/"}},
83 {"id.* add a passkey", credential, Origin + "/passkey/enrol", idSession, want{"Add a passkey · Oficina", false, false, true, directoryOrigin + "/"}},
84 {"id.* open source", credential, Origin + "/help/open-source", nil, want{"Open source · Oficina", false, false, false, directoryOrigin + "/"}},
85 {"id.* sign out", credential, Origin + "/signout", nil, want{"Finish signing out · Oficina", false, false, true, directoryOrigin + "/"}},
86 }
87 for _, c := range cases {
88 t.Run(c.name, func(t *testing.T) {
89 rec := chromeGet(c.h, c.target, acceptHTML, c.cookie)
90 body := rec.Body.String()
91 if rec.Code != http.StatusOK {
92 t.Fatalf("status %d", rec.Code)
93 }
94 if got := titleOf(body); got != c.want.title {
95 t.Errorf("title %q, want %q", got, c.want.title)
96 }
97 if strings.Count(body, "<header estilo-bar") != 1 {
98 t.Errorf("want exactly one estilo-bar")
99 }
100 if !strings.Contains(body, `<a estilo-bar-brand href="`+c.want.brand+`">`) || !strings.Contains(body, `<span estilo-bar-name>Oficina</span>`) {
101 t.Errorf("the brand is not \"Oficina\" linking to %s", c.want.brand)
102 }
103 if strings.Contains(body, "rst-dropdown") || strings.Contains(body, "home-hub-head><h1 estilo-brand") {
104 t.Errorf("the old lone Help menu or hub header is still drawn")
105 }
106 if got := strings.Contains(body, "<details estilo-account"); got != c.want.account {
107 t.Errorf("account menu: %v, want %v", got, c.want.account)
108 }
109 signOuts := strings.Count(body, ">Sign out<")
110 if c.want.account {
111 if signOuts != 1 {
112 t.Errorf("%d Sign out buttons, want exactly one", signOuts)
113 }
114 for _, item := range []string{
115 `href="` + Origin + `/passkey/enrol"`,
116 `href="` + Origin + `/passkey/codes"`,
117 `action="/appearance"`,
118 `href="/help/open-source"`,
119 `<form method="post" action="/signout">`,
120 } {
121 if !strings.Contains(body, item) {
122 t.Errorf("the account menu lacks %s", item)
123 }
124 }
125 } else if c.name != "id.* sign out" && signOuts != 0 {
126 t.Errorf("%d Sign out buttons with no account menu", signOuts)
127 }
128 if got := strings.Contains(body, "estilo-bar-signin"); got != c.want.signIn {
129 t.Errorf("Sign in corner: %v, want %v", got, c.want.signIn)
130 }
131 if got := strings.Contains(body, "<div estilo-sky "); got != c.want.sky {
132 t.Errorf("sky: %v, want %v (sign-in, team home header and front page only)", got, c.want.sky)
133 }
134 })
135 }
136}
137
138// The front page keeps its own design: no app bar. Its title is the hub's
139// name alone (spec §10), not "Oficina — open your new office suite".
140func TestFrontPageKeepsItsOwnDesign(t *testing.T) {
141 f := newMountedChooser(t)
142 body := chromeGet(f.p.directory.Router(), directoryOrigin+"/", acceptHTML).Body.String()
143 if strings.Contains(body, "estilo-bar") {
144 t.Error("the public front page has the app bar")
145 }
146 if got := titleOf(body); got != "Oficina" {
147 t.Errorf("front page title %q, want \"Oficina\"", got)
148 }
149}
150
151// Appearance: a signed-in person's choice, kept per host (spec §8.3).
152func TestAppearanceSetsTheTheme(t *testing.T) {
153 f := newMountedChooser(t)
154 person := f.session(t, chooserPerson)
155 rec := f.request("POST", "/appearance", directoryOrigin, url.Values{"mode": {"dark"}, "return_to": {"/"}}.Encode(), person)
156 if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/" {
157 t.Fatalf("POST /appearance: %d %q", rec.Code, rec.Header().Get("Location"))
158 }
159 var mode *http.Cookie
160 for _, c := range rec.Result().Cookies() {
161 if c.Name == "estilo_appearance" {
162 mode = c
163 }
164 }
165 if mode == nil || mode.Value != "dark" {
166 t.Fatalf("no estilo_appearance=dark cookie: %v", rec.Result().Cookies())
167 }
168 body := chromeGet(f.p.directory.Router(), directoryOrigin+"/", acceptHTML, person, mode).Body.String()
169 if !strings.Contains(body, `<html lang="en" data-theme="dark">`) {
170 t.Errorf("the directory does not draw the chosen theme: %.120s", body)
171 }
172 if !strings.Contains(body, `value="dark" aria-pressed="true"`) {
173 t.Error("Dark is not shown as the current appearance")
174 }
175
176 // return_to is the attacker's half of the form: anything that would
177 // leave the directory goes to "/" instead. Raw form bodies, as a
178 // hostile page would post them: %09 and %5c decode to a tab and a
179 // backslash, which browsers strip or fold into "/" before navigating.
180 for _, hostile := range []string{"//evil", "/%09/evil", "/%5c/evil", "/%5C/evil", "https://evil", "https:%2F%2Fevil", "/%0d%0a/evil"} {
181 rec := f.request("POST", "/appearance", directoryOrigin, "mode=dark&return_to="+hostile, person)
182 if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/" {
183 t.Errorf("return_to=%s: %d to %q, want 303 to /", hostile, rec.Code, rec.Header().Get("Location"))
184 }
185 }
186}
187
188// Appearance comes back to the page it was chosen on only if that page
189// is there to come back to: a GET that drew a 200. A 404's own path, an
190// error page's, or the path of a POST that drew a page would 303 back to
191// a page that is not there (a POST-only path answers a GET with a 404).
192func TestAppearanceReturnsOnlyToARealPage(t *testing.T) {
193 f := newMountedChooser(t)
194 person := f.session(t, chooserPerson)
195 directory := f.p.directory.Router()
196 returnTo := regexp.MustCompile(`name="return_to" value="([^"]*)"`)
197 for _, c := range []struct {
198 name, target, want string
199 }{
200 {"the directory", "/", "/"},
201 {"open source", "/help/open-source", "/help/open-source"},
202 {"a 404", "/no/such/page", "/"},
203 {"a GET to a POST-only path", "/appearance", "/"},
204 } {
205 t.Run(c.name, func(t *testing.T) {
206 body := chromeGet(directory, directoryOrigin+c.target, acceptHTML, person).Body.String()
207 m := returnTo.FindStringSubmatch(body)
208 if m == nil {
209 t.Fatalf("no Appearance return_to on %s", c.target)
210 }
211 if m[1] != c.want {
212 t.Errorf("return_to %q, want %q", m[1], c.want)
213 }
214 })
215 }
216 // A page drawn in answer to a POST, or with any status but 200.
217 for _, c := range []struct {
218 method string
219 status int
220 }{
221 {http.MethodPost, http.StatusOK},
222 {http.MethodGet, http.StatusServiceUnavailable},
223 {http.MethodGet, http.StatusInternalServerError},
224 } {
225 r := httptest.NewRequest(c.method, directoryOrigin+"/help/open-source", nil)
226 r.Header.Set("Accept", acceptHTML)
227 r.AddCookie(person)
228 var got string
229 f.p.directory.sessions.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
230 got = f.p.directory.account(r, c.status).Appearance.Return
231 })).ServeHTTP(httptest.NewRecorder(), r)
232 if got != "/" {
233 t.Errorf("%s drawing a %d: return_to %q, want \"/\"", c.method, c.status, got)
234 }
235 }
236}
237
238// A 500 for a GET offers to try the page again beside the way home; it
239// does not replace the way home, which is the one link known to work.
240func TestServerErrorKeepsTheWayHome(t *testing.T) {
241 f := newMountedChooser(t)
242 for _, c := range []struct {
243 name string
244 app *App
245 method string
246 target string
247 home string
248 tryAgain bool
249 }{
250 {"directory, GET", f.p.directory, http.MethodGet, directoryOrigin + "/help/open-source?x=1", "/", true},
251 {"directory, POST", f.p.directory, http.MethodPost, directoryOrigin + "/appearance", "/", false},
252 {"id.*, GET", f.p.credential.App, http.MethodGet, Origin + "/passkey/codes", directoryOrigin + "/", true},
253 } {
254 t.Run(c.name, func(t *testing.T) {
255 r := httptest.NewRequest(c.method, c.target, nil)
256 r.Header.Set("Accept", acceptHTML)
257 rec := httptest.NewRecorder()
258 c.app.serverError(rec, r)
259 body := rec.Body.String()
260 if rec.Code != http.StatusInternalServerError {
261 t.Fatalf("status %d", rec.Code)
262 }
263 // id.* draws the error in the door's card (id_error.html), so its
264 // way home is the card's main action and Try again a quiet one.
265 primary, secondary := `<a rst-btn="primary" href="`, `<a rst-btn href="`
266 if c.app == f.p.credential.App {
267 primary, secondary = `<a rst-btn="primary" estilo-signin-action href="`, `<a rst-btn="ghost" estilo-signin-action="quiet" href="`
268 }
269 if !strings.Contains(body, primary+c.home+`">Go to Oficina</a>`) {
270 t.Errorf("no Go to Oficina link to %s: %s", c.home, errorCTA(body))
271 }
272 again := secondary + strings.TrimPrefix(strings.TrimPrefix(c.target, directoryOrigin), Origin) + `">Try again</a>`
273 if got := strings.Contains(body, ">Try again</a>"); got != c.tryAgain {
274 t.Errorf("Try again shown %v, want %v: %s", got, c.tryAgain, errorCTA(body))
275 }
276 if c.tryAgain && !strings.Contains(body, again) {
277 t.Errorf("Try again does not retry the page (want %s): %s", again, errorCTA(body))
278 }
279 })
280 }
281}
282
283// errorCTA is the error page's links, for a failure message.
284func errorCTA(body string) string {
285 if i := strings.Index(body, "<p rst-error-cta>"); i >= 0 {
286 end := strings.Index(body[i:], "</p>")
287 if end > 0 {
288 return body[i : i+end]
289 }
290 }
291 return "(no rst-error-cta)"
292}
293
294// Every status the error page can be asked for has its own words; one
295// nobody wrote words for gets a general page, never a blank heading.
296func TestErrorPageHasCopyForEveryStatus(t *testing.T) {
297 f := newMountedChooser(t)
298 headings := map[string]int{}
299 for _, status := range []int{
300 http.StatusBadRequest, http.StatusUnauthorized, http.StatusForbidden,
301 http.StatusNotFound, http.StatusMethodNotAllowed, http.StatusRequestEntityTooLarge,
302 http.StatusTooManyRequests, http.StatusInternalServerError, http.StatusServiceUnavailable,
303 } {
304 heading := checkErrorCopy(t, f.p.directory, status)
305 if other, ok := headings[heading]; ok {
306 t.Errorf("%d and %d share the heading %q", status, other, heading)
307 }
308 headings[heading] = status
309 }
310 // Statuses with no words of their own: a general page.
311 for _, status := range []int{http.StatusTeapot, http.StatusConflict, http.StatusBadGateway} {
312 checkErrorCopy(t, f.p.directory, status)
313 }
314}
315
316var (
317 errorHeading = regexp.MustCompile(`<h1 rst-error-title>([^<]*)</h1>`)
318 errorBody = regexp.MustCompile(`<p rst-error-body>([^<]*)</p>`)
319)
320
321// checkErrorCopy draws status's error page and fails on any part of its
322// copy left blank. It returns the heading.
323func checkErrorCopy(t *testing.T, a *App, status int) string {
324 t.Helper()
325 r := httptest.NewRequest(http.MethodGet, directoryOrigin+"/x", nil)
326 r.Header.Set("Accept", acceptHTML)
327 rec := httptest.NewRecorder()
328 a.errorPage(rec, r, status)
329 body := rec.Body.String()
330 if rec.Code != status {
331 t.Errorf("%d: drew a %d", status, rec.Code)
332 }
333 title := titleOf(body)
334 h := errorHeading.FindStringSubmatch(body)
335 b := errorBody.FindStringSubmatch(body)
336 if title == "" || strings.HasPrefix(title, " ·") || strings.HasPrefix(title, "·") {
337 t.Errorf("%d: tab title %q", status, title)
338 }
339 if h == nil || strings.TrimSpace(h[1]) == "" {
340 t.Errorf("%d: no heading", status)
341 return ""
342 }
343 if b == nil || strings.TrimSpace(b[1]) == "" {
344 t.Errorf("%d: no body copy", status)
345 }
346 return h[1]
347}
348
349// estilo's error page on both origins, for a browser; everything else
350// keeps a plain-text answer (spec §3.3). The 404 used to be chi's bare
351// "404 page not found" whatever was asking.
352func TestErrorPagesSplitByAccept(t *testing.T) {
353 f := newMountedChooser(t)
354 person := f.session(t, chooserPerson)
355 cases := []struct {
356 name string
357 h http.Handler
358 target string
359 cookie *http.Cookie
360 account bool
361 }{
362 {"directory, signed in", f.p.directory.Router(), directoryOrigin + "/no/such/page", person, true},
363 {"directory, signed out", f.p.directory.Router(), directoryOrigin + "/no/such/page", nil, false},
364 {"directory, GET to a POST-only path", f.p.directory.Router(), directoryOrigin + "/appearance", person, true},
365 {"id.*", f.p.credential.Router(), Origin + "/no/such/page", nil, false},
366 {"id.*, GET to a POST-only path", f.p.credential.Router(), Origin + "/passkey/signin/begin", nil, false},
367 }
368 for _, c := range cases {
369 t.Run(c.name, func(t *testing.T) {
370 rec := chromeGet(c.h, c.target, acceptHTML, c.cookie)
371 body := rec.Body.String()
372 if rec.Code != http.StatusNotFound || !strings.HasPrefix(rec.Header().Get("Content-Type"), "text/html") {
373 t.Fatalf("browser: %d %q, want a 404 page", rec.Code, rec.Header().Get("Content-Type"))
374 }
375 // On id.* the same words sit in the door's card, branded
376 // Oficina ID (Paul, 2026-11-20: every id.* page in the card).
377 onID := strings.HasPrefix(c.name, "id.*")
378 if titleOf(body) != "Page not found · Oficina" || strings.Contains(body, "<div rst-error>") == onID || !strings.Contains(body, "We can’t find that page") || !strings.Contains(body, "<header estilo-bar") {
379 t.Errorf("not the error page under the bar: %q", titleOf(body))
380 }
381 if onID && (!strings.Contains(body, "<div estilo-door estilo-sky-host>") || !strings.Contains(body, "<span>Oficina ID</span>") || !strings.Contains(body, `<a rst-btn="primary" estilo-signin-action href="`+directoryOrigin+`/">Go to Oficina</a>`)) {
382 t.Errorf("id.*'s error page is not in the door's card:\n%s", body)
383 }
384 if got := strings.Contains(body, "<details estilo-account"); got != c.account {
385 t.Errorf("account menu %v, want %v", got, c.account)
386 }
387 if rec.Header().Get("Cache-Control") != "no-store" {
388 t.Errorf("Cache-Control %q", rec.Header().Get("Cache-Control"))
389 }
390 for _, accept := range []string{"", "*/*", "application/json"} {
391 rec := chromeGet(c.h, c.target, accept, c.cookie)
392 if (rec.Code != http.StatusNotFound && rec.Code != http.StatusMethodNotAllowed) || !strings.HasPrefix(rec.Header().Get("Content-Type"), "text/plain") {
393 t.Errorf("Accept %q: %d %q, want a plain 404 or 405", accept, rec.Code, rec.Header().Get("Content-Type"))
394 }
395 }
396 })
397 }
398 // An unknown host is not a page anyone navigates to: still empty.
399 rec := chromeGet(f.p.directory.Router(), "https://elsewhere.example/no/such/page", acceptHTML)
400 if rec.Code != http.StatusNotFound || rec.Body.Len() != 0 {
401 t.Errorf("unknown host: %d %q", rec.Code, rec.Body.String())
402 }
403}
404
405// "Use a different email" and "Start again" keep where you were going
406// (r3 C1). Both were a bare /signin, which forgets return_to: the magic
407// link then ended at id.* "/", which sends you to the signed-out front
408// page, and a sign-in from an app lost the app. On the door, "Use a
409// different email" is Forget, a POST that carries return_to.
410func TestStartingAgainKeepsTheDestination(t *testing.T) {
411 app := newTestApp(t)
412 h := app.Router()
413 form := chromeGet(h, Origin+"/signin?return_to=%2Fauthorize%2Fresume", acceptHTML)
414 var keep *http.Cookie
415 for _, c := range form.Result().Cookies() {
416 if c.Name == returnCookie && c.Value != "" {
417 keep = c
418 }
419 }
420 if keep == nil {
421 t.Fatal("the form did not remember return_to")
422 }
423 const again = `href="/signin?return_to=%2Fauthorize%2Fresume"`
424 const forget = `<form rst-signin-form method="post" action="/signin/forget"><input type="hidden" name="return_to" value="/authorize/resume">`
425 sent := chromeGet(h, Origin+"/signin/sent", acceptHTML, keep).Body.String()
426 if !strings.Contains(sent, forget) || !strings.Contains(sent, "<span>Use a different email</span>") {
427 t.Errorf("Use a different email forgets the destination:\n%s", sent)
428 }
429 r := httptest.NewRequest(http.MethodPost, Origin+linkPath, strings.NewReader("token=nope"))
430 r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
431 r.Header.Set("Origin", Origin)
432 r.AddCookie(keep)
433 rec := httptest.NewRecorder()
434 h.ServeHTTP(rec, r)
435 if !strings.Contains(rec.Body.String(), again+">Start again") {
436 t.Errorf("Start again forgets the destination:\n%s", rec.Body.String())
437 }
438 // With nowhere remembered, Forget posts no destination.
439 if body := chromeGet(h, Origin+"/signin/sent", acceptHTML).Body.String(); !strings.Contains(body, `action="/signin/forget"><input type="hidden" name="way"`) {
440 t.Errorf("with no destination: %s", body)
441 }
442}
443
444// The hand-off hop keeps the sky's inline placeholder (r3 C2): it used to
445// draw neither sky nor card, so every sign-in flashed a blank page between
446// two skies. Only the placeholder: it is a data: URI, so the hop still
447// fetches nothing it will not show.
448func TestHopKeepsTheSkyPlaceholder(t *testing.T) {
449 p := newExchangePair(t)
450 credential := signInFully(t, p.credential, sealtest.CanaryAddress)
451 _, intent := p.start(t)
452 rec := exchangeRequest(p.credential.App, "POST", Origin+"/authorize/resume", Origin, nil, intent, credential)
453 body := rec.Body.String()
454 if rec.Code != http.StatusOK || !strings.Contains(body, `name="assertion"`) {
455 t.Fatalf("resume: %d", rec.Code)
456 }
457 if !strings.Contains(body, `estilo-sky-layer="placeholder" estilo-sky-tone="light" src="data:`) {
458 t.Error("the hop draws no sky")
459 }
460 if strings.Contains(body, "grain.svg") || strings.Contains(body, "filaments.svg") || strings.Contains(body, "busy.js") {
461 t.Error("the hop fetches sky layers or scripts it will not show")
462 }
463}
464
465// A cold first load paints with the mark at once (r3 C4): the brand
466// pictures are preloaded, on the front page and every page in the
467// layout. And it paints in the system UI face with no webfont to wait
468// for or swap in: Paul, 2026-10-25, "There should never be a flash of
469// unstyled text. Ever." (estilo AGENTS.md row 9). Onest used to be
470// preloaded here; no page links or preloads it, or any font file, now.
471// estilo's v1 files stay mounted (mountCalma) so a page cached before
472// this still resolves its stylesheet.
473func TestColdLoadPreloadsTheMarksAndNoWebfont(t *testing.T) {
474 f := newMountedChooser(t)
475 front := chromeGet(f.p.directory.Router(), directoryOrigin+"/", acceptHTML).Body.String()
476 signin := chromeGet(f.p.credential.Router(), Origin+"/signin", acceptHTML).Body.String()
477 for name, body := range map[string]string{"front page": front, "id.* sign in": signin} {
478 for _, why := range chromeWebfonts(body) {
479 t.Errorf("%s %s", name, why)
480 }
481 if !strings.Contains(body, `<link rel="preload" href="/static/brand/icon.svg?v=`) {
482 t.Errorf("%s: the Oficina mark is not preloaded", name)
483 }
484 }
485 for _, app := range []string{"docs", "sheets", "slides", "calendar", "calls", "memoria"} {
486 if !strings.Contains(front, `<link rel="preload" href="/static/brand/`+app+`.svg?v=`) {
487 t.Errorf("front page: %s's mark is not preloaded", app)
488 }
489 }
490
491 // Every page's source, whichever route draws it: the layout's
492 // templates and the front page.
493 pages := 0
494 for _, glob := range []string{"templates/*.html", "static/site/*.html"} {
495 names, err := filepath.Glob(glob)
496 if err != nil {
497 t.Fatal(err)
498 }
499 for _, p := range names {
500 b, err := os.ReadFile(p)
501 if err != nil {
502 t.Fatal(err)
503 }
504 pages++
505 for _, why := range chromeWebfonts(string(b)) {
506 t.Errorf("%s %s", p, why)
507 }
508 }
509 }
510 if pages < 15 {
511 t.Fatalf("read only %d page sources; the scan would pass vacuously", pages)
512 }
513
514 // The retired files still answer, on both origins, for old pages.
515 for origin, h := range map[string]http.Handler{directoryOrigin: f.p.directory.Router(), Origin: f.p.credential.Router()} {
516 for _, file := range []string{"fonts.css", "onest-latin.woff2"} {
517 if rec := chromeGet(h, origin+theme.FontPrefix+file, ""); rec.Code != http.StatusOK {
518 t.Errorf("GET %s%s%s = %d; keep estilo's font handler mounted for old pages", origin, theme.FontPrefix, file, rec.Code)
519 }
520 }
521 }
522}
523
524var (
525 linkTag = regexp.MustCompile(`(?is)<link\b[^>]*>`)
526 asFont = regexp.MustCompile(`(?i)\bas\s*=\s*["']?font\b`)
527 fontFile = regexp.MustCompile(`(?i)\.(woff2?|ttf|otf|eot)\b`)
528)
529
530// chromeWebfonts names each way html would download a font for the
531// chrome: estilo's font path at any version, a <link> that preloads a
532// font or names a font file, or a face declared inline.
533func chromeWebfonts(html string) []string {
534 var found []string
535 if dir := path.Dir(strings.TrimSuffix(theme.FontPrefix, "/")) + "/"; strings.Contains(html, dir) {
536 found = append(found, "names estilo's webfont path "+dir)
537 }
538 for _, tag := range linkTag.FindAllString(html, -1) {
539 if asFont.MatchString(tag) || fontFile.MatchString(tag) {
540 found = append(found, "links or preloads a font: "+tag)
541 }
542 }
543 if strings.Contains(strings.ToLower(html), "@font-face") {
544 found = append(found, "declares a font face inline")
545 }
546 return found
547}
548