package slidestest // A font from a person's library, put to use in a deck (deckfonts.go): // POST /d/{sqid}/fonts records which files the deck may serve, and the // three surfaces serve a file only through a deck that both recorded it // and lists it in meta.fontFiles. Liberation (SIL OFL) is the font // throughout, as in library_test.go. import ( "bytes" "encoding/json" "html" "net/http" "net/http/httptest" "net/url" "strings" "sync" "testing" "amadan.net/oficina/slides/internal/slides" "amadan.net/oficina/slides/internal/slides/deck" "amadan.net/oficina/slides/internal/slides/fonts" "amadan.net/rastrillo/rastrillo/blobs" ) // newFontsApp is two people on one instance with an object store. func newFontsApp(t *testing.T) (*testApp, blobs.Store) { t.Helper() cfg, mailer := newConfig(t) cfg.AllowedAddresses = []string{allowedAddress, otherAddress} cfg.Blobs = dirStore(t) return newAppWith(t, cfg, mailer), cfg.Blobs } // pickFont is the editor's POST /d/{sqid}/fonts for one family. func pickFont(t *testing.T, a *testApp, sqid, family string, cookies []*http.Cookie) *httptest.ResponseRecorder { t.Helper() return postJSON(t, a, "/d/"+sqid+"/fonts", `{"family":`+quote(family)+`}`, cookies) } // pickedFamily is pickFont insisting on a 200, decoded. func pickedFamily(t *testing.T, a *testApp, sqid, family string, cookies []*http.Cookie) deck.FontFamily { t.Helper() rec := pickFont(t, a, sqid, family, cookies) if rec.Code != http.StatusOK { t.Fatalf("POST /d/%s/fonts %q: status %d, want 200\n%s", sqid, family, rec.Code, rec.Body) } var fam deck.FontFamily if err := json.Unmarshal(rec.Body.Bytes(), &fam); err != nil { t.Fatalf("POST /d/%s/fonts: not a font family: %v\n%s", sqid, err, rec.Body) } return fam } // useFamily is the batch the editor commits after a pick, over the API: // the family in meta.fontFiles and the heading set in it. func useFamily(t *testing.T, a *testApp, token, sqid string, fam deck.FontFamily) { t.Helper() files, _ := json.Marshal([]deck.FontFamily{fam}) stack := "'" + fam.CSS + "', Liberation Sans, sans-serif" apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[{"op":"set_meta","fontFiles":`+string(files)+`,"fonts":{"heading":`+quote(stack)+`,"body":""}}]}`, http.StatusOK) } func deckFontRows(t *testing.T, a *testApp, sqid string) int64 { t.Helper() var n int64 if err := a.db.G.Model(&slides.DeckFont{}). Where("deck_id = (SELECT id FROM decks WHERE sqid = ?)", sqid).Count(&n).Error; err != nil { t.Fatal(err) } return n } // fontGet is a GET of a font route, with or without a session. func fontGet(t *testing.T, a *testApp, target string, cookies []*http.Cookie) *httptest.ResponseRecorder { t.Helper() return get(t, a, target, cookies...) } // served insists target answers the face's bytes with the font headers. func served(t *testing.T, a *testApp, target string, cookies []*http.Cookie, want []byte) { t.Helper() rec := fontGet(t, a, target, cookies) if rec.Code != http.StatusOK { t.Errorf("GET %s: status %d, want 200", target, rec.Code) return } if ct := rec.Header().Get("Content-Type"); ct != "font/ttf" { t.Errorf("GET %s: Content-Type %q, want font/ttf", target, ct) } if rec.Header().Get("X-Content-Type-Options") != "nosniff" { t.Errorf("GET %s: no nosniff", target) } if !strings.HasPrefix(rec.Header().Get("Cache-Control"), "private") { t.Errorf("GET %s: Cache-Control %q, want private", target, rec.Header().Get("Cache-Control")) } if rec.Header().Get("Content-Security-Policy") != "default-src 'none'" { t.Errorf("GET %s: CSP %q", target, rec.Header().Get("Content-Security-Policy")) } if !bytes.Equal(rec.Body.Bytes(), want) { t.Errorf("GET %s: %d bytes, not the face", target, rec.Body.Len()) } } func refused(t *testing.T, a *testApp, target string, cookies []*http.Cookie) { t.Helper() if rec := fontGet(t, a, target, cookies); rec.Code != http.StatusNotFound { t.Errorf("GET %s: status %d, want 404", target, rec.Code) } } func TestPickingAFontRecordsItForTheDeck(t *testing.T) { a, _ := newFontsApp(t) cookies := signedIn(t, a, allowedAddress) token := mintAgent(t, a, cookies, "claude") sqid := apiDeck(t, a, token, "Picked") reg, bold := libFont(t, "LiberationSans-Regular.ttf"), libFont(t, "LiberationSans-Bold.ttf") if rec := uploadFonts(t, a, cookies, upFile{"a.ttf", reg}, upFile{"b.ttf", bold}); rec.Code != http.StatusOK { t.Fatalf("upload: %d", rec.Code) } sans := cssOf(t, a, sha(reg)) fam := pickedFamily(t, a, sqid, sans, cookies) want := deck.FontFamily{Family: "Liberation Sans", CSS: sans, Faces: []deck.FontFace{ {Ref: sha(reg), Format: "ttf", Weight: [2]int{400, 400}, Pictures: true}, {Ref: sha(bold), Format: "ttf", Weight: [2]int{700, 700}, Pictures: true}, }} got, _ := json.Marshal(fam) wantJSON, _ := json.Marshal(want) if string(got) != string(wantJSON) { t.Errorf("the pick answered %s, want %s", got, wantJSON) } if n := deckFontRows(t, a, sqid); n != 2 { t.Errorf("%d DeckFont rows, want one per face (2)", n) } // The answer is what set_meta takes, as it is. useFamily(t, a, token, sqid, fam) // Again: the same two rows, not four. pickedFamily(t, a, sqid, sans, cookies) if n := deckFontRows(t, a, sqid); n != 2 { t.Errorf("%d DeckFont rows after a second pick, want 2", n) } // A family the person does not have, and a body that is not one. if rec := pickFont(t, a, sqid, "Liberation Serif", cookies); rec.Code != http.StatusNotFound { t.Errorf("a family not in the library: status %d, want 404", rec.Code) } if rec := postJSON(t, a, "/d/"+sqid+"/fonts", `{"family":`, cookies); rec.Code != http.StatusBadRequest { t.Errorf("a broken body: status %d, want 400", rec.Code) } if n := deckFontRows(t, a, sqid); n != 2 { t.Errorf("%d DeckFont rows after refusals, want 2", n) } } func TestAFontIsServedOnlyThroughADeckThatUsesIt(t *testing.T) { a, _ := newFontsApp(t) mine := signedIn(t, a, allowedAddress) theirs := signedIn(t, a, otherAddress) token := mintAgent(t, a, mine, "claude") theirToken := mintAgent(t, a, theirs, "theirs") reg := libFont(t, "LiberationSans-Regular.ttf") uploadFonts(t, a, mine, upFile{"a.ttf", reg}) ref := sha(reg) sans := cssOf(t, a, ref) using := apiDeck(t, a, token, "Uses it") fam := pickedFamily(t, a, using, sans, mine) useFamily(t, a, token, using, fam) // Another deck of mine that never picked it, but lists it. other := apiDeck(t, a, token, "Lists it") useFamily(t, a, token, other, fam) // A deck that picked it, then stopped listing it. dropped := apiDeck(t, a, token, "Dropped it") pickedFamily(t, a, dropped, sans, mine) // Another person's deck, listing the ref through their own agent. stranger := apiDeck(t, a, theirToken, "Theirs") useFamily(t, a, theirToken, stranger, fam) refused(t, a, "/d/"+other+"/fonts/"+ref, mine) // A deck that recorded it and no longer lists it serves it only to // the person whose library holds it (deckFont: a pick the editor is // still waiting on looks the same), never over a share link. served(t, a, "/d/"+dropped+"/fonts/"+ref, mine, reg) refused(t, a, "/s/"+mintShareLink(t, a, "/d/"+dropped, mine)+"/fonts/"+ref, nil) refused(t, a, "/d/"+stranger+"/fonts/"+ref, theirs) refused(t, a, "/d/"+using+"/fonts/"+ref, theirs) // a deck they cannot see // Signed out, the session route sends a person to sign in. if rec := fontGet(t, a, "/d/"+using+"/fonts/"+ref, nil); rec.Code != http.StatusSeeOther { t.Errorf("signed out: status %d, want 303 to sign in", rec.Code) } refused(t, a, "/d/"+using+"/fonts/"+strings.Repeat("0", 64), mine) refused(t, a, "/d/"+using+"/fonts/"+strings.ToUpper(ref), mine) otherLink := mintShareLink(t, a, "/d/"+other, mine) refused(t, a, "/s/"+otherLink+"/fonts/"+ref, nil) served(t, a, "/d/"+using+"/fonts/"+ref, mine, reg) link := mintShareLink(t, a, "/d/"+using, mine) served(t, a, "/s/"+link+"/fonts/"+ref, nil, reg) out := apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+using+"/present", token, "", http.StatusOK) session, _ := out["session"].(string) served(t, a, "/p/"+session+"/fonts/"+ref, nil, reg) refused(t, a, "/p/nosuchsess/fonts/"+ref, nil) } func TestOnlyTheLibraryOwnerCanAddTheirFont(t *testing.T) { a, _ := newFontsApp(t) mine := signedIn(t, a, allowedAddress) theirs := signedIn(t, a, otherAddress) uploadFonts(t, a, mine, upFile{"sans.ttf", libFont(t, "LiberationSans-Regular.ttf")}) uploadFonts(t, a, theirs, upFile{"serif.ttf", libFont(t, "LiberationSerif-Regular.ttf")}) sans := cssOf(t, a, sha(libFont(t, "LiberationSans-Regular.ttf"))) serif := cssOf(t, a, sha(libFont(t, "LiberationSerif-Regular.ttf"))) shared := createDeck(t, a, mine, "All hands") if rec := post(t, a, shared+"/settings", url.Values{ "title": {"All hands"}, "theme": {"ink"}, "rung": {"team"}, }, mine...); rec.Code != http.StatusSeeOther { t.Fatalf("raise the deck to the team: %d", rec.Code) } private := createDeck(t, a, mine, "Mine alone") // Sam can edit the team deck, but my family is not Sam's to add. if rec := pickFont(t, a, sqidOf(shared), sans, theirs); rec.Code != http.StatusNotFound { t.Errorf("someone else's family: status %d, want 404", rec.Code) } if n := deckFontRows(t, a, sqidOf(shared)); n != 0 { t.Errorf("%d DeckFont rows after a refused pick", n) } // Sam's own family goes into the team deck. if fam := pickedFamily(t, a, sqidOf(shared), serif, theirs); fam.CSS != serif { t.Errorf("Sam's pick answered %+v", fam) } // And not into a deck Sam cannot see. if rec := pickFont(t, a, sqidOf(private), serif, theirs); rec.Code != http.StatusNotFound { t.Errorf("a deck Sam cannot see: status %d, want 404", rec.Code) } // Nor is Sam's family mine to add. if rec := pickFont(t, a, sqidOf(private), serif, mine); rec.Code != http.StatusNotFound { t.Errorf("Sam's family into my deck: status %d, want 404", rec.Code) } // Each person's Deck settings offers their own families only. mySettings := html.UnescapeString(mustGet(t, a, shared+"/edit", http.StatusOK, mine).Body.String()) theirSettings := html.UnescapeString(mustGet(t, a, shared+"/edit", http.StatusOK, theirs).Body.String()) if !strings.Contains(mySettings, ``) || strings.Contains(mySettings, ``) || strings.Contains(theirSettings, `