package slidestest import ( "encoding/json" "net/http" "net/http/httptest" "strings" "testing" ) // The Fonts page shows each family set in its own font (final review, // minor; the spec's "a sample line"): the family's own name, in a face // the page loads from a route that serves a library file to its owner // alone. func TestTheFontsPageShowsEachFamilyInItsOwnFont(t *testing.T) { a, _ := newFontsApp(t) mine := signedIn(t, a, allowedAddress) theirs := signedIn(t, a, otherAddress) regular := brandFont(t, "LiberationSerif-Regular.ttf", "Brand") uploadFonts(t, a, mine, upFile{"brand.ttf", regular}) page := mustGet(t, a, "/fonts", http.StatusOK, mine).Body.String() if !strings.Contains(page, "url(/fonts/files/"+sha(regular)+")") { t.Errorf("the page loads no face of Brand from its library route") } if !strings.Contains(page, `slides-font-sample="1">Brand`) { t.Errorf("the page shows no sample of Brand set in it") } served(t, a, "/fonts/files/"+sha(regular), mine, regular) refused(t, a, "/fonts/files/"+sha(regular), theirs) refused(t, a, "/fonts/files/"+strings.Repeat("0", 64), mine) refused(t, a, "/fonts/files/not-a-ref", mine) if rec := get(t, a, "/fonts/files/"+sha(regular)); rec.Code == http.StatusOK { t.Error("a library file was served to nobody in particular") } } // Deck settings asks for the person's families again when the editor's // window comes back (final review, minor): GET /fonts as JSON, the same // list the fields were drawn with. func TestTheLibraryAnswersItsFamiliesAsJSON(t *testing.T) { a, _ := newFontsApp(t) mine := signedIn(t, a, allowedAddress) theirs := signedIn(t, a, otherAddress) uploadFonts(t, a, mine, upFile{"brand.ttf", brandFont(t, "LiberationSerif-Regular.ttf", "Brand")}) list := func(cookies []*http.Cookie) string { req := httptest.NewRequest(http.MethodGet, "/fonts", nil) req.Header.Set("Accept", "application/json") for _, c := range cookies { req.AddCookie(c) } rec := httptest.NewRecorder() a.ServeHTTP(rec, req) if rec.Code != http.StatusOK || !strings.HasPrefix(rec.Header().Get("Content-Type"), "application/json") { t.Fatalf("GET /fonts as JSON: %d %q\n%s", rec.Code, rec.Header().Get("Content-Type"), rec.Body) } var v any if err := json.Unmarshal(rec.Body.Bytes(), &v); err != nil { t.Fatalf("not JSON: %v", err) } return strings.TrimSpace(rec.Body.String()) } if got := list(mine); got != `[{"value":"Brand","label":"Brand"}]` { t.Errorf("my families: %s", got) } if got := list(theirs); got != `[]` { t.Errorf("someone with none: %s", got) } }