oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1package slidestest
2
3import (
4 "bytes"
5 "context"
6 "crypto/sha256"
7 "encoding/hex"
8 "fmt"
9 "html"
10 "mime/multipart"
11 "net/http"
12 "net/http/httptest"
13 "net/url"
14 "os"
15 "path/filepath"
16 "strings"
17 "sync"
18 "testing"
19
20 "amadan.net/oficina/slides/internal/slides"
21 "amadan.net/oficina/slides/internal/slides/fonts"
22 "amadan.net/rastrillo/rastrillo/blobs"
23)
24
25// The library tests upload Liberation (SIL OFL), which the app already
26// ships, so no font file is added for them.
27
28type upFile struct {
29 name string
30 data []byte
31}
32
33func libFont(t *testing.T, name string) []byte {
34 t.Helper()
35 b, err := os.ReadFile(filepath.Join("..", "slides", "static", "fonts", name))
36 if err != nil {
37 t.Fatalf("read %s: %v", name, err)
38 }
39 return b
40}
41
42func uploadFonts(t *testing.T, a *testApp, cookies []*http.Cookie, files ...upFile) *httptest.ResponseRecorder {
43 t.Helper()
44 var body bytes.Buffer
45 mw := multipart.NewWriter(&body)
46 for _, f := range files {
47 w, err := mw.CreateFormFile("file", f.name)
48 if err != nil {
49 t.Fatal(err)
50 }
51 w.Write(f.data)
52 }
53 mw.Close()
54 req := httptest.NewRequest(http.MethodPost, "/fonts", &body)
55 req.Header.Set("Content-Type", mw.FormDataContentType())
56 req.Header.Set("Origin", testOrigin)
57 req.Header.Set("Sec-Fetch-Site", "same-origin")
58 for _, c := range cookies {
59 req.AddCookie(c)
60 }
61 rec := httptest.NewRecorder()
62 a.ServeHTTP(rec, req)
63 // The page escapes an apostrophe; the messages are read as text.
64 rec.Body = bytes.NewBufferString(html.UnescapeString(rec.Body.String()))
65 return rec
66}
67
68func countRows(t *testing.T, a *testApp, where string, args ...any) int64 {
69 t.Helper()
70 var n int64
71 if err := a.db.G.Model(&slides.LibraryFont{}).Where(where, args...).Count(&n).Error; err != nil {
72 t.Fatal(err)
73 }
74 return n
75}
76
77func hasObject(store blobs.Store, ref string) bool {
78 rc, err := store.Get(context.Background(), ref)
79 if err != nil {
80 return false
81 }
82 rc.Close()
83 return true
84}
85
86// cssOf is the css name the library gave the face with this ref. A
87// catalog family's own name is reserved (library.go's
88// reservedFontName), so an upload of a Liberation file is suffixed.
89func cssOf(t *testing.T, a *testApp, ref string) string {
90 t.Helper()
91 var row slides.LibraryFont
92 if err := a.db.G.Where("ref = ?", ref).First(&row).Error; err != nil {
93 t.Fatal(err)
94 }
95 return row.CSSName
96}
97
98func sha(b []byte) string {
99 s := sha256.Sum256(b)
100 return hex.EncodeToString(s[:])
101}
102
103func TestUploadAFontAndSeeItListed(t *testing.T) {
104 store := dirStore(t)
105 a := newAppWithStore(t, store)
106 cookies := signedIn(t, a, allowedAddress)
107
108 empty := mustGet(t, a, "/fonts", http.StatusOK, cookies).Body.String()
109 if !strings.Contains(empty, "No fonts yet.") {
110 t.Errorf("an empty library is not said to be empty:\n%s", empty)
111 }
112
113 reg, bold := libFont(t, "LiberationSans-Regular.ttf"), libFont(t, "LiberationSans-Bold.ttf")
114 rec := uploadFonts(t, a, cookies, upFile{"a.ttf", reg}, upFile{"b.ttf", bold})
115 if rec.Code != http.StatusOK {
116 t.Fatalf("POST /fonts: %d\n%s", rec.Code, rec.Body)
117 }
118 if !strings.Contains(rec.Body.String(), "2 files added.") {
119 t.Errorf("the count of files added is not shown:\n%s", rec.Body)
120 }
121 var rows []slides.LibraryFont
122 if err := a.db.G.Order("weight").Find(&rows).Error; err != nil {
123 t.Fatal(err)
124 }
125 if len(rows) != 2 {
126 t.Fatalf("%d rows, want 2", len(rows))
127 }
128 if rows[0].Ref != sha(reg) || rows[0].Family != "Liberation Sans" || rows[0].CSSName != "Liberation Sans 2" ||
129 rows[0].Weight != 400 || rows[0].Size != int64(len(reg)) || rows[0].Owner == "" || rows[0].Format != "ttf" {
130 t.Errorf("row %+v does not describe the uploaded face", rows[0])
131 }
132 if !hasObject(store, rows[0].Ref) || !hasObject(store, rows[1].Ref) {
133 t.Errorf("the bytes are not in the store under their refs")
134 }
135 page := html.UnescapeString(mustGet(t, a, "/fonts", http.StatusOK, cookies).Body.String())
136 if !strings.Contains(page, "Liberation Sans") || !strings.Contains(page, "2 styles") {
137 t.Errorf("the family and its styles are not listed:\n%s", page)
138 }
139 if !strings.Contains(page, "/fonts/delete?family=Liberation+Sans+2") && !strings.Contains(page, "/fonts/delete?family=Liberation%20Sans%202") {
140 t.Errorf("no delete link for the family:\n%s", page)
141 }
142
143 // The same file again is refused, and adds nothing.
144 again := uploadFonts(t, a, cookies, upFile{"a.ttf", reg})
145 if again.Code != http.StatusUnprocessableEntity || !strings.Contains(again.Body.String(), "a.ttf is already in your fonts.") {
146 t.Errorf("a repeat upload: %d\n%s", again.Code, again.Body)
147 }
148 if n := countRows(t, a, "1 = 1"); n != 2 {
149 t.Errorf("%d rows after a repeat, want 2", n)
150 }
151}
152
153func TestUploadRefusesWhatIsNotAFont(t *testing.T) {
154 store := dirStore(t)
155 a := newAppWithStore(t, store)
156 cookies := signedIn(t, a, allowedAddress)
157
158 png := append([]byte("\x89PNG\r\n\x1a\n"), bytes.Repeat([]byte{1}, 200)...)
159 good := libFont(t, "LiberationSans-Regular.ttf")
160 cases := []struct {
161 name string
162 file upFile
163 want string
164 }{
165 {"png", upFile{"pic.ttf", png}, "pic.ttf isn't a font file."},
166 {"big", upFile{"huge.ttf", append(append([]byte{}, good...), make([]byte, 11<<20)...)}, "huge.ttf is over 10 MB."},
167 {"truncated", upFile{"cut.ttf", good[:len(good)/3]}, "cut.ttf looks damaged and can't be used."},
168 }
169 for _, c := range cases {
170 t.Run(c.name, func(t *testing.T) {
171 rec := uploadFonts(t, a, cookies, c.file)
172 if rec.Code != http.StatusUnprocessableEntity {
173 t.Fatalf("status %d, want 422\n%s", rec.Code, rec.Body)
174 }
175 if !strings.Contains(rec.Body.String(), c.want) {
176 t.Errorf("missing %q:\n%s", c.want, rec.Body)
177 }
178 if n := countRows(t, a, "1 = 1"); n != 0 {
179 t.Errorf("%d rows after a refusal", n)
180 }
181 if hasObject(store, sha(c.file.data)) {
182 t.Errorf("a refused file was stored")
183 }
184 })
185 }
186
187 // One refusal does not stop the good file beside it.
188 rec := uploadFonts(t, a, cookies, upFile{"pic.ttf", png}, upFile{"ok.ttf", good})
189 body := rec.Body.String()
190 if rec.Code != http.StatusOK || !strings.Contains(body, "pic.ttf isn't a font file.") || !strings.Contains(body, "1 file added.") {
191 t.Errorf("mixed upload: %d\n%s", rec.Code, body)
192 }
193}
194
195func TestTheLibraryLimits(t *testing.T) {
196 a := newAppWithStore(t, dirStore(t))
197 cookies := signedIn(t, a, allowedAddress)
198 mustGet(t, a, "/fonts", http.StatusOK, cookies) // the person's row exists once they have been seen
199 var u slides.User
200 if err := a.db.G.Where("email = ?", allowedAddress).Take(&u).Error; err != nil {
201 t.Fatal(err)
202 }
203 owner := fmt.Sprintf("person:%d", u.ID)
204 seed := func(family, css string, i int) {
205 row := slides.LibraryFont{Owner: owner, Family: family, CSSName: css, Ref: sha([]byte(fmt.Sprintf("%s-%d", family, i))), Format: "ttf", Outline: "truetype", Weight: 400}
206 if err := a.db.G.Create(&row).Error; err != nil {
207 t.Fatal(err)
208 }
209 }
210 for i := 0; i < 16; i++ {
211 seed("Liberation Sans", "Liberation Sans", i)
212 }
213 rec := uploadFonts(t, a, cookies, upFile{"x.ttf", libFont(t, "LiberationSans-Regular.ttf")})
214 if rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), "Liberation Sans already has 16 styles.") {
215 t.Errorf("17th face: %d\n%s", rec.Code, rec.Body)
216 }
217
218 if err := a.db.G.Where("1 = 1").Delete(&slides.LibraryFont{}).Error; err != nil {
219 t.Fatal(err)
220 }
221 for i := 0; i < 32; i++ {
222 seed(fmt.Sprintf("Family %d", i), fmt.Sprintf("Family %d", i), 0)
223 }
224 rec = uploadFonts(t, a, cookies, upFile{"x.ttf", libFont(t, "LiberationSans-Regular.ttf")})
225 if rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), "You can keep up to 32 font families.") {
226 t.Errorf("33rd family: %d\n%s", rec.Code, rec.Body)
227 }
228 if n := countRows(t, a, "ref = ?", sha(libFont(t, "LiberationSans-Regular.ttf"))); n != 0 {
229 t.Errorf("the refused face was recorded")
230 }
231}
232
233func TestALibraryIsOnlyItsOwners(t *testing.T) {
234 a := newAppWithStore(t, dirStore(t))
235 one := signedIn(t, a, allowedAddress)
236 if rec := uploadFonts(t, a, one, upFile{"a.ttf", libFont(t, "LiberationSans-Regular.ttf")}); rec.Code != http.StatusOK {
237 t.Fatalf("upload: %d", rec.Code)
238 }
239 // Another person's library, seeded: the first person sees none of it.
240 other := slides.LibraryFont{Owner: "person:999", Family: "Secret Face", CSSName: "Secret Face", Ref: sha([]byte("x")), Format: "ttf", Outline: "truetype", Weight: 400}
241 if err := a.db.G.Create(&other).Error; err != nil {
242 t.Fatal(err)
243 }
244 page := mustGet(t, a, "/fonts", http.StatusOK, one).Body.String()
245 if strings.Contains(page, "Secret Face") {
246 t.Errorf("another person's family is listed")
247 }
248 if rec := get(t, a, "/fonts/delete?family=Secret+Face", one...); rec.Code != http.StatusNotFound {
249 t.Errorf("confirm page for another person's family: %d, want 404", rec.Code)
250 }
251 post(t, a, "/fonts/delete", url.Values{"family": {"Secret Face"}}, one...)
252 if n := countRows(t, a, "owner = ?", "person:999"); n != 1 {
253 t.Errorf("another person's family was deleted")
254 }
255}
256
257func TestDeletingAFamilyRemovesItsRows(t *testing.T) {
258 store := dirStore(t)
259 a := newAppWithStore(t, store)
260 cookies := signedIn(t, a, allowedAddress)
261 reg, mono := libFont(t, "LiberationSans-Regular.ttf"), libFont(t, "LiberationMono-Regular.ttf")
262 uploadFonts(t, a, cookies, upFile{"a.ttf", reg}, upFile{"m.ttf", mono})
263 sans, monoCSS := cssOf(t, a, sha(reg)), cssOf(t, a, sha(mono))
264
265 confirm := mustGet(t, a, "/fonts/delete?family="+url.QueryEscape(sans), http.StatusOK, cookies).Body.String()
266 if !strings.Contains(confirm, "Delete Liberation Sans?") || !strings.Contains(confirm, "Delete font") {
267 t.Errorf("confirm page:\n%s", confirm)
268 }
269 if n := countRows(t, a, "1 = 1"); n != 2 {
270 t.Fatalf("the GET deleted something: %d rows", n)
271 }
272
273 // A second owner holds the same bytes as the mono face: that object
274 // must stay when the first person's family goes.
275 if err := a.db.G.Create(&slides.LibraryFont{Owner: "person:999", Family: "Liberation Mono", CSSName: "Liberation Mono", Ref: sha(mono), Format: "ttf", Outline: "truetype", Weight: 400}).Error; err != nil {
276 t.Fatal(err)
277 }
278 rec := post(t, a, "/fonts/delete", url.Values{"family": {sans}}, cookies...)
279 if rec.Code != http.StatusSeeOther {
280 t.Fatalf("delete: %d\n%s", rec.Code, rec.Body)
281 }
282 if hasObject(store, sha(reg)) {
283 t.Errorf("an unreferenced object was kept")
284 }
285 if n := countRows(t, a, "ref = ?", sha(reg)); n != 0 {
286 t.Errorf("rows remain")
287 }
288 post(t, a, "/fonts/delete", url.Values{"family": {monoCSS}}, cookies...)
289 if !hasObject(store, sha(mono)) {
290 t.Errorf("an object another library still holds was deleted")
291 }
292 if n := countRows(t, a, "owner = ?", "person:999"); n != 1 {
293 t.Errorf("the other library lost its row")
294 }
295}
296
297func TestTheFontsPageWorksWithoutScripts(t *testing.T) {
298 a := newAppWithStore(t, dirStore(t))
299 cookies := signedIn(t, a, allowedAddress)
300 body := mustGet(t, a, "/fonts", http.StatusOK, cookies).Body.String()
301 for _, want := range []string{`method="post"`, `action="/fonts"`, `enctype="multipart/form-data"`, `type="file"`, `name="file"`, "multiple", "Upload", "Upload only fonts your licence lets you use on the web."} {
302 if !strings.Contains(body, want) {
303 t.Errorf("the form lacks %q:\n%s", want, body)
304 }
305 }
306
307 // No store: the page says so, and the POST answers 503.
308 b := newApp(t)
309 c := signedIn(t, b, allowedAddress)
310 page := mustGet(t, b, "/fonts", http.StatusOK, c).Body.String()
311 if !strings.Contains(page, "Fonts aren&#39;t available here yet.") && !strings.Contains(page, "Fonts aren't available here yet.") {
312 t.Errorf("no-store page lacks the unavailable line:\n%s", page)
313 }
314 if strings.Contains(page, `type="file"`) {
315 t.Errorf("no-store page still offers the form")
316 }
317 if rec := uploadFonts(t, b, c, upFile{"a.ttf", libFont(t, "LiberationSans-Regular.ttf")}); rec.Code != http.StatusServiceUnavailable {
318 t.Errorf("POST without a store: %d, want 503", rec.Code)
319 }
320}
321
322func TestTheAccountMenuListsFonts(t *testing.T) {
323 a := newApp(t)
324 cookies := signedIn(t, a, allowedAddress)
325 body := mustGet(t, a, "/", http.StatusOK, cookies).Body.String()
326 if !strings.Contains(body, `href="/fonts"`) || !strings.Contains(body, ">Fonts<") {
327 t.Errorf("the account menu has no Fonts link:\n%s", body)
328 }
329}
330
331// gate is a SetFontTrace hook that parks the first request to reach
332// stage until release is called; reached is closed when it has.
333//
334// waiter, if set, is a second stage whose Nth arrival (nth, counting
335// from 1) closes arrived: the competing request, confirmed to be at its
336// point rather than guessed at with a sleep.
337type gate struct {
338 stage string
339 reached chan struct{}
340 hold chan struct{}
341 once sync.Once
342
343 waiter string
344 nth int
345 seen int
346 arrived chan struct{}
347 mu sync.Mutex
348}
349
350func newGate(stage string) *gate {
351 return &gate{stage: stage, reached: make(chan struct{}), hold: make(chan struct{}), arrived: make(chan struct{})}
352}
353
354func (g *gate) waitFor(stage string, nth int) *gate {
355 g.waiter, g.nth = stage, nth
356 return g
357}
358
359func (g *gate) trace(stage string) {
360 if stage == g.waiter {
361 g.mu.Lock()
362 g.seen++
363 if g.seen == g.nth {
364 close(g.arrived)
365 }
366 g.mu.Unlock()
367 }
368 if stage != g.stage {
369 return
370 }
371 g.once.Do(func() {
372 close(g.reached)
373 <-g.hold
374 })
375}
376
377func (g *gate) release() { close(g.hold) }
378
379// Two uploads race for the last place in a family: the second must see
380// the first's row.
381func TestConcurrentUploadsRespectTheFamilyLimit(t *testing.T) {
382 a := newAppWithStore(t, dirStore(t))
383 cookies := signedIn(t, a, allowedAddress)
384 mustGet(t, a, "/fonts", http.StatusOK, cookies)
385 var u slides.User
386 if err := a.db.G.Where("email = ?", allowedAddress).Take(&u).Error; err != nil {
387 t.Fatal(err)
388 }
389 for i := 0; i < 15; i++ {
390 row := slides.LibraryFont{Owner: fmt.Sprintf("person:%d", u.ID), Family: "Liberation Sans", CSSName: "Liberation Sans", Ref: sha([]byte{byte(i)}), Format: "ttf", Outline: "truetype", Weight: 400}
391 if err := a.db.G.Create(&row).Error; err != nil {
392 t.Fatal(err)
393 }
394 }
395 g := newGate("checked").waitFor("queued", 2)
396 a.app.SetFontTrace(g.trace)
397 t.Cleanup(func() { a.app.SetFontTrace(nil) })
398
399 codes := make(chan int, 2)
400 go func() {
401 codes <- uploadFonts(t, a, cookies, upFile{"a.ttf", libFont(t, "LiberationSans-Regular.ttf")}).Code
402 }()
403 <-g.reached
404 go func() {
405 codes <- uploadFonts(t, a, cookies, upFile{"b.ttf", libFont(t, "LiberationSans-Bold.ttf")}).Code
406 }()
407 <-g.arrived // the second upload is at the lock
408 g.release()
409 c1, c2 := <-codes, <-codes
410 if n := countRows(t, a, "1 = 1"); n != 16 {
411 t.Errorf("%d rows, want 16 (codes %d, %d)", n, c1, c2)
412 }
413 if !((c1 == 200 && c2 == 422) || (c1 == 422 && c2 == 200)) {
414 t.Errorf("codes %d and %d: one upload fits, one is refused", c1, c2)
415 }
416}
417
418// A family whose name cleans to another's CSS name gets a name of its own.
419func TestCollidingCSSNamesAreKeptApart(t *testing.T) {
420 a := newAppWithStore(t, dirStore(t))
421 cookies := signedIn(t, a, allowedAddress)
422 mustGet(t, a, "/fonts", http.StatusOK, cookies)
423 var u slides.User
424 a.db.G.Where("email = ?", allowedAddress).Take(&u)
425 if err := a.db.G.Create(&slides.LibraryFont{Owner: fmt.Sprintf("person:%d", u.ID), Family: "Liberation Sans!", CSSName: "Liberation Sans", Ref: sha([]byte("z")), Format: "ttf", Outline: "truetype", Weight: 400}).Error; err != nil {
426 t.Fatal(err)
427 }
428 if rec := uploadFonts(t, a, cookies, upFile{"a.ttf", libFont(t, "LiberationSans-Regular.ttf")}); rec.Code != http.StatusOK {
429 t.Fatalf("upload: %d", rec.Code)
430 }
431 var css []string
432 a.db.G.Model(&slides.LibraryFont{}).Distinct().Pluck("css_name", &css)
433 if len(css) != 2 {
434 t.Errorf("css names %v, want two distinct", css)
435 }
436}
437
438// An upload that has stored its bytes but not yet its row must not lose
439// them to another person's deletion of the same file.
440func TestADeleteCannotTakeAnUploadsObject(t *testing.T) {
441 store := dirStore(t)
442 cfg, mailer := newConfig(t)
443 cfg.Blobs = store
444 const second = "second@example.test"
445 cfg.AllowedAddresses = append(cfg.AllowedAddresses, second)
446 a := newAppWith(t, cfg, mailer)
447 one := signedIn(t, a, allowedAddress)
448 two := signedIn(t, a, second)
449 face := libFont(t, "LiberationSans-Regular.ttf")
450 if rec := uploadFonts(t, a, two, upFile{"a.ttf", face}); rec.Code != http.StatusOK {
451 t.Fatalf("second person's upload: %d", rec.Code)
452 }
453 css := cssOf(t, a, sha(face))
454
455 g := newGate("stored").waitFor("delete", 1)
456 a.app.SetFontTrace(g.trace)
457 t.Cleanup(func() { a.app.SetFontTrace(nil) })
458 done := make(chan int, 1)
459 go func() { done <- uploadFonts(t, a, one, upFile{"a.ttf", face}).Code }()
460 <-g.reached
461 deleted := make(chan int, 1)
462 go func() {
463 deleted <- post(t, a, "/fonts/delete", url.Values{"family": {css}}, two...).Code
464 }()
465 <-g.arrived // the delete is at the lock
466 g.release()
467 if c := <-done; c != http.StatusOK {
468 t.Fatalf("upload: %d", c)
469 }
470 <-deleted
471 if n := countRows(t, a, "1 = 1"); n != 1 {
472 t.Errorf("%d rows, want the first person's one", n)
473 }
474 if !hasObject(store, sha(face)) {
475 t.Errorf("a row points at bytes that are gone")
476 }
477}
478
479func TestAMalformedMultipartStreamIsNotSilentlyDone(t *testing.T) {
480 a := newAppWithStore(t, dirStore(t))
481 cookies := signedIn(t, a, allowedAddress)
482 good := libFont(t, "LiberationSans-Regular.ttf")
483
484 raw := func(parts ...string) *httptest.ResponseRecorder {
485 body := strings.Join(parts, "")
486 req := httptest.NewRequest(http.MethodPost, "/fonts", strings.NewReader(body))
487 req.Header.Set("Content-Type", "multipart/form-data; boundary=XX")
488 req.Header.Set("Origin", testOrigin)
489 req.Header.Set("Sec-Fetch-Site", "same-origin")
490 req.Header.Set("Accept", "text/html")
491 for _, c := range cookies {
492 req.AddCookie(c)
493 }
494 rec := httptest.NewRecorder()
495 a.ServeHTTP(rec, req)
496 return rec
497 }
498 file := "--XX\r\nContent-Disposition: form-data; name=\"file\"; filename=\"a.ttf\"\r\n\r\n" + string(good) + "\r\n"
499 bad := "--XX\r\nContent-Disposition form-data; name=\"file\"\r\n\r\nx\r\n"
500
501 if rec := raw(bad); rec.Code != http.StatusBadRequest {
502 t.Errorf("only a malformed part: %d, want 400", rec.Code)
503 }
504 rec := raw(file, bad)
505 if n := countRows(t, a, "1 = 1"); n != 1 {
506 t.Errorf("the good file was not kept: %d rows", n)
507 }
508 if !strings.Contains(html.UnescapeString(rec.Body.String()), "That didn’t work") {
509 t.Errorf("a malformed part after a file says nothing:\n%s", rec.Body)
510 }
511}
512
513// A valid file followed by a part cut off mid-body: Go's reader wraps
514// io.EOF for that, and it is not the end of the stream.
515func TestATruncatedMultipartStreamIsNotSilentlyDone(t *testing.T) {
516 a := newAppWithStore(t, dirStore(t))
517 cookies := signedIn(t, a, allowedAddress)
518 good := libFont(t, "LiberationSans-Regular.ttf")
519 body := "--XX\r\nContent-Disposition: form-data; name=\"file\"; filename=\"a.ttf\"\r\n\r\n" + string(good) + "\r\n" +
520 "--XX\r\nContent-Disposition: form-data; name=\"file\"; filename=\"b.ttf\"\r\n\r\nabc"
521 req := httptest.NewRequest(http.MethodPost, "/fonts", strings.NewReader(body))
522 req.Header.Set("Content-Type", "multipart/form-data; boundary=XX")
523 req.Header.Set("Origin", testOrigin)
524 req.Header.Set("Sec-Fetch-Site", "same-origin")
525 for _, c := range cookies {
526 req.AddCookie(c)
527 }
528 rec := httptest.NewRecorder()
529 a.ServeHTTP(rec, req)
530 if n := countRows(t, a, "1 = 1"); n != 1 {
531 t.Errorf("the complete file was not kept: %d rows", n)
532 }
533 if !strings.Contains(html.UnescapeString(rec.Body.String()), "That didn’t work") {
534 t.Errorf("a truncated part after a file says nothing:\n%s", rec.Body)
535 }
536}
537
538// A family is never given a css name a stylesheet already means
539// something by: a catalog family's (an upload called "Liberation Serif"
540// would join the built-in Serif in the browser) or a generic keyword.
541// Such a name is treated as taken, and gets the same numeric suffix as
542// two families that clean to one name.
543func TestAnUploadedFamilyNeverTakesAReservedName(t *testing.T) {
544 a := newAppWithStore(t, dirStore(t))
545 cookies := signedIn(t, a, allowedAddress)
546 reserved := map[string]bool{}
547 for _, n := range []string{"Liberation Sans", "Liberation Serif", "Liberation Mono", "serif", "sans-serif", "monospace",
548 "cursive", "fantasy", "system-ui", "ui-serif", "ui-sans-serif", "ui-monospace", "ui-rounded", "math", "emoji", "fangsong"} {
549 reserved[strings.ToLower(n)] = true
550 }
551 f, err := fonts.Parse(libFont(t, "LiberationSans-Regular.ttf"))
552 if err != nil {
553 t.Fatal(err)
554 }
555 for _, name := range []string{"Liberation Serif", "SANS-SERIF", "System-UI", "liberation mono"} {
556 data, err := f.Renamed(name, 400, false)
557 if err != nil {
558 t.Fatal(err)
559 }
560 if rec := uploadFonts(t, a, cookies, upFile{"f.ttf", data}); rec.Code != http.StatusOK {
561 t.Fatalf("upload %q: %d\n%s", name, rec.Code, rec.Body)
562 }
563 var row slides.LibraryFont
564 if err := a.db.G.Where("ref = ?", sha(data)).First(&row).Error; err != nil {
565 t.Fatal(err)
566 }
567 if reserved[strings.ToLower(row.CSSName)] || !strings.HasPrefix(row.CSSName, name+" ") {
568 t.Errorf("%q was given the css name %q, want it with a numeric suffix", name, row.CSSName)
569 }
570 }
571}
572