oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3import (
4 "io/fs"
5 "path"
6
7 "amadan.net/oficina/estilo.git/busy"
8 "bytes"
9 "crypto/sha256"
10 "fmt"
11 "net/http"
12 "net/http/httptest"
13 "strings"
14 "testing"
15 "testing/fstest"
16
17 "amadan.net/oficina/estilo.git/theme"
18 "amadan.net/rastrillo/rastrillo"
19 "amadan.net/rastrillo/rastrillo/ui"
20)
21
22func TestSharedThemeAssetFingerprintAndCache(t *testing.T) {
23 app, _ := appWithRecorder(t)
24 page := do(t, app, httptest.NewRequest(http.MethodGet, "/signin", nil))
25 if page.Code != http.StatusOK {
26 t.Fatalf("signin status %d", page.Code)
27 }
28 // Calma with Sheets' accent (estilo row 8), not rastrillo's day.
29 want, ok := theme.ForApp("sheets")
30 if !ok || !bytes.Equal(sheetsTheme, want) {
31 t.Fatal("Sheets does not serve estilo's theme for sheets")
32 }
33 for _, item := range []struct {
34 name string
35 data []byte
36 }{
37 {"theme", sheetsTheme}, {"tokens", ui.TokensCSS()},
38 } {
39 served := servedAs("static/"+item.name+".css", item.data)
40 sum := sha256.Sum256(served)
41 hashed := fmt.Sprintf("/static/%s.%x.css", item.name, sum[:8])
42 if !strings.Contains(page.Body.String(), `href="`+hashed+`"`) {
43 t.Fatalf("missing selected %s fingerprint", item.name)
44 }
45 for _, path := range []string{hashed, "/static/" + item.name + ".css", "/static/" + item.name + ".0000000000000000.css"} {
46 res := do(t, app, httptest.NewRequest(http.MethodGet, path, nil))
47 if res.Code != http.StatusOK || !bytes.Equal(res.Body.Bytes(), served) {
48 t.Fatalf("%s asset response mismatch status%d", item.name, res.Code)
49 }
50 if !strings.HasPrefix(res.Header().Get("Content-Type"), "text/css") {
51 t.Fatal("asset MIME mismatch")
52 }
53 want := "no-cache"
54 if path == hashed {
55 want = "public, max-age=31536000, immutable"
56 }
57 if res.Header().Get("Cache-Control") != want {
58 t.Fatal("asset cache policy mismatch")
59 }
60 }
61 }
62}
63
64// The busy rule (estilo's busy): the page links busy.css after tokens.css
65// and busy.js after the shim, all three are served, and the shim served is
66// estilo's — rastrillo's without the old busy rule, which would fight the
67// new one.
68func TestBusyRuleIsLinkedAndServed(t *testing.T) {
69 app, _ := appWithRecorder(t)
70 page := do(t, app, httptest.NewRequest(http.MethodGet, "/signin", nil)).Body.String()
71 for _, name := range []string{"static/estilo/busy.css", "static/rastrillo.js", "static/estilo/busy.js"} {
72 body := servedAs(name, map[string][]byte{"static/estilo/busy.css": busy.CSS(), "static/rastrillo.js": busy.ShimJS(), "static/estilo/busy.js": busy.JS()}[name])
73 sum := sha256.Sum256(body)
74 hashed := "/" + strings.TrimSuffix(name, path.Ext(name)) + fmt.Sprintf(".%x", sum[:8]) + path.Ext(name)
75 if !strings.Contains(page, `"`+hashed+`"`) {
76 t.Errorf("sign-in page does not link %s", hashed)
77 }
78 if res := do(t, app, httptest.NewRequest(http.MethodGet, hashed, nil)); res.Code != http.StatusOK || !bytes.Equal(res.Body.Bytes(), body) {
79 t.Errorf("GET %s = %d or the wrong bytes", hashed, res.Code)
80 }
81 }
82 if i, j := strings.Index(page, "static/rastrillo."), strings.Index(page, "static/estilo/busy."); i < 0 || j < 0 {
83 t.Error("the shim or busy.js is missing")
84 }
85 // No rastrillo script on disk. `rastrillo doctor --fix` adds the ones
86 // the library ships and the app lacks, and the shim it adds at this
87 // pin is the old one, with the busy rule inside: loaded beside
88 // estilo's busy.js, the two rules fight. Delete what --fix adds.
89 for _, name := range []string{"static/rastrillo.js", "static/busy.js", "static/select.js", "static/datetime.js", "static/calendar.js"} {
90 if _, err := fs.Stat(staticFS, name); err == nil {
91 t.Errorf("%s is on disk; Sheets serves estilo's shim and busy.js, and no other rastrillo script", name)
92 }
93 }
94}
95
96// servedAs is what rastrillo.Assets itself sends for name with body at
97// that path, asked of rastrillo rather than re-derived: since v0.31 it
98// minifies a file at one of the framework's vendored paths
99// (static/tokens.css, static/theme.css, static/rastrillo.js and the rest),
100// and serves any other as written. Sheets puts estilo's theme and busy
101// shim at two of those paths, so they are minified too.
102func servedAs(name string, body []byte) []byte {
103 assets := rastrillo.NewAssets(fstest.MapFS{name: {Data: body}})
104 rec := httptest.NewRecorder()
105 assets.Handler().ServeHTTP(rec, httptest.NewRequest(http.MethodGet, assets.Path(name), nil))
106 if rec.Code != http.StatusOK {
107 panic(fmt.Sprintf("rastrillo.Assets would not serve %s: %d", name, rec.Code))
108 }
109 return rec.Body.Bytes()
110}
111