package slidestest import ( "encoding/json" "net/http" "strings" "testing" ) // A scene's author is whoever wrote its current code (scenes and clips // spec §2, §8.9): the server sets it from the acting principal whenever // the code changes, on both write paths, and an author a client sends // is never what is stored. const sceneCode = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" const sceneCode2 = "fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210" // sceneAuthor reads one scene's author over the API, "" when the deck // has no such scene. func sceneAuthor(t *testing.T, a *testApp, token, sqid, id string) string { t.Helper() out := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK) scenes, _ := out["scenes"].(map[string]any) s, _ := scenes[id].(map[string]any) author, _ := s["author"].(string) return author } func TestTheAPIStampsASceneAuthorWheneverTheCodeChanges(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) token := mintAgent(t, a, cookies, "scenewriter") agent := "agent:" + scalar(t, a, "SELECT id FROM agents WHERE name = ?", "scenewriter") sqid := apiDeck(t, a, token, "Scenes") // No id: the API mints one, as it does for slides and blocks. The // author the op carries is not the one stored. out := apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[{"op":"add_scene","name":"World","code":"`+sceneCode+`","states":[{"id":"a"},{"id":"b"}],"author":"person:999"}]}`, http.StatusOK) ids, _ := out["ids"].(map[string]any) id, _ := ids["0"].(string) if id == "" { t.Fatalf("no minted scene id in %v", out) } if got := sceneAuthor(t, a, token, sqid, id); got != agent { t.Fatalf("author after add_scene = %q, want %q", got, agent) } // A change that leaves the code alone leaves the author alone, and // cannot set one. apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[{"op":"set_scene","id":`+quote(id)+`,"name":"Renamed","author":"person:999"}]}`, http.StatusOK) if got := sceneAuthor(t, a, token, sqid, id); got != agent { t.Fatalf("author after a rename = %q, want %q", got, agent) } // A person changing the code over live sync becomes the author. log, _ := getUpdates(t, a, "/d/"+sqid, 0, cookies) m := newMirror(t, sqid, log) seq := postUpdate(t, a, "/d/"+sqid, m.ops(t, `[{"op":"set_scene","id":`+quote(id)+`,"code":"`+sceneCode2+`"}]`), cookies) person := "person:" + scalar(t, a, "SELECT id FROM users WHERE email = ?", allowedAddress) if got := sceneAuthor(t, a, token, sqid, id); got != person { t.Fatalf("author after a hand edit of the code = %q, want %q", got, person) } // The stamp is its own row after the person's, recorded as theirs, // and the answer names the person's own row: a client adopting it // as its cursor still fetches the stamp. tail, last := getUpdates(t, a, "/d/"+sqid, seq, cookies) if len(tail) != 1 || last != seq+1 { t.Fatalf("after the posted seq %d: %d frames up to %d, want 1 up to %d", seq, len(tail), last, seq+1) } if actor := scalar(t, a, "SELECT actor FROM deck_updates WHERE deck_id = (SELECT id FROM decks WHERE sqid = ?) AND seq = ?", sqid, last); actor != person { t.Errorf("the stamp recorded actor %q, want %q", actor, person) } // An author written straight into the document, code untouched, is // put back. m.apply(t, tail) forged := m.ops(t, `[{"op":"set_scene","id":`+quote(id)+`,"author":"agent:12345"}]`) postUpdate(t, a, "/d/"+sqid, forged, cookies) if got := sceneAuthor(t, a, token, sqid, id); got != person { t.Fatalf("author after a forged one = %q, want %q", got, person) } // An update that touches no scene adds no row of its own. tail, _ = getUpdates(t, a, "/d/"+sqid, 0, cookies) m = newMirror(t, sqid, tail) before := len(tail) postUpdate(t, a, "/d/"+sqid, m.ops(t, `[{"op":"set_meta","title":"Plain"}]`), cookies) tail, _ = getUpdates(t, a, "/d/"+sqid, 0, cookies) if len(tail) != before+1 { t.Errorf("a plain update made %d rows, want 1", len(tail)-before) } // The scene block and the clip block are in the API's vocabulary. var deckOut map[string]any ops := `{"ops":[{"op":"add_slide","id":"sceneslide1","layout":"flow"}, {"op":"add_block","slide":"sceneslide1","type":"scene","props":{"scene":` + quote(id) + `,"from":"a","to":"b"}}, {"op":"add_block","slide":"sceneslide1","type":"clip","props":{"asset":"v","play":"press"}}]}` deckOut = apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, ops, http.StatusOK) if b, _ := json.Marshal(deckOut["ids"]); len(b) < 10 { t.Errorf("ids for the two blocks: %s", b) } apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[{"op":"add_block","slide":"sceneslide1","type":"scene","props":{"scene":`+quote(id)+`,"from":"b","to":"a"}}]}`, http.StatusUnprocessableEntity) } // TestLiveSyncRefusesSceneDataOverTheLimit: a raw update can write what // the ops would refuse; the server reads what it leaves and refuses, // before anything is stored, a scene over 960 KiB of data — under a // well-formed id or under the empty one no op can write — a scene id // outside the document's pattern, and a "__proto__" key in data. // Removing a scene that blocks still name is an ordinary edit and goes // through. func TestLiveSyncRefusesSceneDataOverTheLimit(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) path := createDeck(t, a, cookies, "Big data") before, seqBefore := getUpdates(t, a, path, 0, cookies) for _, name := range []string{"scenedata", "scenenoid", "scenebadid", "sceneproto"} { rec := postBody(t, a, path+"/updates", "application/octet-stream", poisonUpdate(t, name), cookies) if rec.Code != http.StatusUnprocessableEntity { t.Errorf("%s: status %d, want 422\n%s", name, rec.Code, rec.Body) } if after, seqAfter := getUpdates(t, a, path, 0, cookies); len(after) != len(before) || seqAfter != seqBefore { t.Fatalf("%s: a refused update was stored: %d frames at seq %d, was %d at %d", name, len(after), seqAfter, len(before), seqBefore) } } m := newMirror(t, sqidOf(path), before) postUpdate(t, a, path, m.ops(t, `[{"op":"add_scene","id":"keptsceneaaa","name":"W","code":"`+sceneCode+`","states":[{"id":"a"},{"id":"b"}]}, {"op":"add_slide","id":"sceneslide01","layout":"flow"}, {"op":"add_block","slide":"sceneslide01","id":"sceneblock01","type":"scene","props":{"scene":"keptsceneaaa","from":"a","to":"b"}}]`), cookies) log, _ := getUpdates(t, a, path, 0, cookies) m = newMirror(t, sqidOf(path), log) postUpdate(t, a, path, m.ops(t, `[{"op":"remove_scene","id":"keptsceneaaa"}]`), cookies) mustGet(t, a, path+"/slides/sceneslide01.html", http.StatusOK, cookies) } // tenthsJSON is a JSON list of n copies of 0.1: four bytes each in // JSON, nine in a Yjs update. func tenthsJSON(n int) string { return "[" + strings.TrimSuffix(strings.Repeat("0.1,", n), ",") + "]" } // TestTheAPIRefusesASceneChangePastOneUpdate (final review call 3): // 150,000 tenths are within the 960 KiB data limit as JSON but would be // a 1.35 MB update, past the 1 MiB an editor may post, so no deck can // hold a scene an editor could not save again. The core refuses the // batch with a message saying why, and nothing is stored; 116,000, // just under one update, are taken. func TestTheAPIRefusesASceneChangePastOneUpdate(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) token := mintAgent(t, a, cookies, "bigdata") sqid := apiDeck(t, a, token, "Numbers") out := apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[{"op":"add_scene","name":"World","code":"`+sceneCode+`","states":[{"id":"a"}]}]}`, http.StatusOK) ids, _ := out["ids"].(map[string]any) id, _ := ids["0"].(string) _, seq := getUpdates(t, a, "/d/"+sqid, 0, cookies) out = apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[{"op":"set_scene","id":`+quote(id)+`,"data":`+tenthsJSON(150000)+`}]}`, http.StatusUnprocessableEntity) if msg, _ := out["message"].(string); !strings.Contains(msg, "would make an update larger than 1 MiB") { t.Errorf("the refusal says %v, want why", out) } if _, after := getUpdates(t, a, "/d/"+sqid, 0, cookies); after != seq { t.Errorf("a refused batch moved the deck from seq %d to %d", seq, after) } apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[{"op":"set_scene","id":`+quote(id)+`,"data":`+tenthsJSON(116000)+`}]}`, http.StatusOK) view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK) scenes, _ := view["scenes"].(map[string]any) s, _ := scenes[id].(map[string]any) if l, _ := s["data"].([]any); len(l) != 116000 { t.Errorf("just under one update: the scene holds %d values, want 116000", len(l)) } }