| 1 | package slidestest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // A scene's author is whoever wrote its current code (scenes and clips |
| 11 | // spec §2, §8.9): the server sets it from the acting principal whenever |
| 12 | // the code changes, on both write paths, and an author a client sends |
| 13 | // is never what is stored. |
| 14 | |
| 15 | const sceneCode = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" |
| 16 | const sceneCode2 = "fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210" |
| 17 | |
| 18 | // sceneAuthor reads one scene's author over the API, "" when the deck |
| 19 | // has no such scene. |
| 20 | func sceneAuthor(t *testing.T, a *testApp, token, sqid, id string) string { |
| 21 | t.Helper() |
| 22 | out := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK) |
| 23 | scenes, _ := out["scenes"].(map[string]any) |
| 24 | s, _ := scenes[id].(map[string]any) |
| 25 | author, _ := s["author"].(string) |
| 26 | return author |
| 27 | } |
| 28 | |
| 29 | func TestTheAPIStampsASceneAuthorWheneverTheCodeChanges(t *testing.T) { |
| 30 | a := newApp(t) |
| 31 | cookies := signedIn(t, a, allowedAddress) |
| 32 | token := mintAgent(t, a, cookies, "scenewriter") |
| 33 | agent := "agent:" + scalar(t, a, "SELECT id FROM agents WHERE name = ?", "scenewriter") |
| 34 | sqid := apiDeck(t, a, token, "Scenes") |
| 35 | |
| 36 | // No id: the API mints one, as it does for slides and blocks. The |
| 37 | // author the op carries is not the one stored. |
| 38 | out := apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, |
| 39 | `{"ops":[{"op":"add_scene","name":"World","code":"`+sceneCode+`","states":[{"id":"a"},{"id":"b"}],"author":"person:999"}]}`, |
| 40 | http.StatusOK) |
| 41 | ids, _ := out["ids"].(map[string]any) |
| 42 | id, _ := ids["0"].(string) |
| 43 | if id == "" { |
| 44 | t.Fatalf("no minted scene id in %v", out) |
| 45 | } |
| 46 | if got := sceneAuthor(t, a, token, sqid, id); got != agent { |
| 47 | t.Fatalf("author after add_scene = %q, want %q", got, agent) |
| 48 | } |
| 49 | |
| 50 | // A change that leaves the code alone leaves the author alone, and |
| 51 | // cannot set one. |
| 52 | apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, |
| 53 | `{"ops":[{"op":"set_scene","id":`+quote(id)+`,"name":"Renamed","author":"person:999"}]}`, http.StatusOK) |
| 54 | if got := sceneAuthor(t, a, token, sqid, id); got != agent { |
| 55 | t.Fatalf("author after a rename = %q, want %q", got, agent) |
| 56 | } |
| 57 | |
| 58 | // A person changing the code over live sync becomes the author. |
| 59 | log, _ := getUpdates(t, a, "/d/"+sqid, 0, cookies) |
| 60 | m := newMirror(t, sqid, log) |
| 61 | seq := postUpdate(t, a, "/d/"+sqid, m.ops(t, `[{"op":"set_scene","id":`+quote(id)+`,"code":"`+sceneCode2+`"}]`), cookies) |
| 62 | person := "person:" + scalar(t, a, "SELECT id FROM users WHERE email = ?", allowedAddress) |
| 63 | if got := sceneAuthor(t, a, token, sqid, id); got != person { |
| 64 | t.Fatalf("author after a hand edit of the code = %q, want %q", got, person) |
| 65 | } |
| 66 | |
| 67 | // The stamp is its own row after the person's, recorded as theirs, |
| 68 | // and the answer names the person's own row: a client adopting it |
| 69 | // as its cursor still fetches the stamp. |
| 70 | tail, last := getUpdates(t, a, "/d/"+sqid, seq, cookies) |
| 71 | if len(tail) != 1 || last != seq+1 { |
| 72 | t.Fatalf("after the posted seq %d: %d frames up to %d, want 1 up to %d", seq, len(tail), last, seq+1) |
| 73 | } |
| 74 | 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 { |
| 75 | t.Errorf("the stamp recorded actor %q, want %q", actor, person) |
| 76 | } |
| 77 | |
| 78 | // An author written straight into the document, code untouched, is |
| 79 | // put back. |
| 80 | m.apply(t, tail) |
| 81 | forged := m.ops(t, `[{"op":"set_scene","id":`+quote(id)+`,"author":"agent:12345"}]`) |
| 82 | postUpdate(t, a, "/d/"+sqid, forged, cookies) |
| 83 | if got := sceneAuthor(t, a, token, sqid, id); got != person { |
| 84 | t.Fatalf("author after a forged one = %q, want %q", got, person) |
| 85 | } |
| 86 | |
| 87 | // An update that touches no scene adds no row of its own. |
| 88 | tail, _ = getUpdates(t, a, "/d/"+sqid, 0, cookies) |
| 89 | m = newMirror(t, sqid, tail) |
| 90 | before := len(tail) |
| 91 | postUpdate(t, a, "/d/"+sqid, m.ops(t, `[{"op":"set_meta","title":"Plain"}]`), cookies) |
| 92 | tail, _ = getUpdates(t, a, "/d/"+sqid, 0, cookies) |
| 93 | if len(tail) != before+1 { |
| 94 | t.Errorf("a plain update made %d rows, want 1", len(tail)-before) |
| 95 | } |
| 96 | |
| 97 | // The scene block and the clip block are in the API's vocabulary. |
| 98 | var deckOut map[string]any |
| 99 | ops := `{"ops":[{"op":"add_slide","id":"sceneslide1","layout":"flow"}, |
| 100 | {"op":"add_block","slide":"sceneslide1","type":"scene","props":{"scene":` + quote(id) + `,"from":"a","to":"b"}}, |
| 101 | {"op":"add_block","slide":"sceneslide1","type":"clip","props":{"asset":"v","play":"press"}}]}` |
| 102 | deckOut = apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, ops, http.StatusOK) |
| 103 | if b, _ := json.Marshal(deckOut["ids"]); len(b) < 10 { |
| 104 | t.Errorf("ids for the two blocks: %s", b) |
| 105 | } |
| 106 | apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, |
| 107 | `{"ops":[{"op":"add_block","slide":"sceneslide1","type":"scene","props":{"scene":`+quote(id)+`,"from":"b","to":"a"}}]}`, |
| 108 | http.StatusUnprocessableEntity) |
| 109 | } |
| 110 | |
| 111 | // TestLiveSyncRefusesSceneDataOverTheLimit: a raw update can write what |
| 112 | // the ops would refuse; the server reads what it leaves and refuses, |
| 113 | // before anything is stored, a scene over 960 KiB of data — under a |
| 114 | // well-formed id or under the empty one no op can write — a scene id |
| 115 | // outside the document's pattern, and a "__proto__" key in data. |
| 116 | // Removing a scene that blocks still name is an ordinary edit and goes |
| 117 | // through. |
| 118 | func TestLiveSyncRefusesSceneDataOverTheLimit(t *testing.T) { |
| 119 | a := newApp(t) |
| 120 | cookies := signedIn(t, a, allowedAddress) |
| 121 | path := createDeck(t, a, cookies, "Big data") |
| 122 | before, seqBefore := getUpdates(t, a, path, 0, cookies) |
| 123 | for _, name := range []string{"scenedata", "scenenoid", "scenebadid", "sceneproto"} { |
| 124 | rec := postBody(t, a, path+"/updates", "application/octet-stream", poisonUpdate(t, name), cookies) |
| 125 | if rec.Code != http.StatusUnprocessableEntity { |
| 126 | t.Errorf("%s: status %d, want 422\n%s", name, rec.Code, rec.Body) |
| 127 | } |
| 128 | if after, seqAfter := getUpdates(t, a, path, 0, cookies); len(after) != len(before) || seqAfter != seqBefore { |
| 129 | t.Fatalf("%s: a refused update was stored: %d frames at seq %d, was %d at %d", name, len(after), seqAfter, len(before), seqBefore) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | m := newMirror(t, sqidOf(path), before) |
| 134 | postUpdate(t, a, path, m.ops(t, `[{"op":"add_scene","id":"keptsceneaaa","name":"W","code":"`+sceneCode+`","states":[{"id":"a"},{"id":"b"}]}, |
| 135 | {"op":"add_slide","id":"sceneslide01","layout":"flow"}, |
| 136 | {"op":"add_block","slide":"sceneslide01","id":"sceneblock01","type":"scene","props":{"scene":"keptsceneaaa","from":"a","to":"b"}}]`), cookies) |
| 137 | log, _ := getUpdates(t, a, path, 0, cookies) |
| 138 | m = newMirror(t, sqidOf(path), log) |
| 139 | postUpdate(t, a, path, m.ops(t, `[{"op":"remove_scene","id":"keptsceneaaa"}]`), cookies) |
| 140 | mustGet(t, a, path+"/slides/sceneslide01.html", http.StatusOK, cookies) |
| 141 | } |
| 142 | |
| 143 | // tenthsJSON is a JSON list of n copies of 0.1: four bytes each in |
| 144 | // JSON, nine in a Yjs update. |
| 145 | func tenthsJSON(n int) string { |
| 146 | return "[" + strings.TrimSuffix(strings.Repeat("0.1,", n), ",") + "]" |
| 147 | } |
| 148 | |
| 149 | // TestTheAPIRefusesASceneChangePastOneUpdate (final review call 3): |
| 150 | // 150,000 tenths are within the 960 KiB data limit as JSON but would be |
| 151 | // a 1.35 MB update, past the 1 MiB an editor may post, so no deck can |
| 152 | // hold a scene an editor could not save again. The core refuses the |
| 153 | // batch with a message saying why, and nothing is stored; 116,000, |
| 154 | // just under one update, are taken. |
| 155 | func TestTheAPIRefusesASceneChangePastOneUpdate(t *testing.T) { |
| 156 | a := newApp(t) |
| 157 | cookies := signedIn(t, a, allowedAddress) |
| 158 | token := mintAgent(t, a, cookies, "bigdata") |
| 159 | sqid := apiDeck(t, a, token, "Numbers") |
| 160 | out := apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, |
| 161 | `{"ops":[{"op":"add_scene","name":"World","code":"`+sceneCode+`","states":[{"id":"a"}]}]}`, http.StatusOK) |
| 162 | ids, _ := out["ids"].(map[string]any) |
| 163 | id, _ := ids["0"].(string) |
| 164 | _, seq := getUpdates(t, a, "/d/"+sqid, 0, cookies) |
| 165 | |
| 166 | out = apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, |
| 167 | `{"ops":[{"op":"set_scene","id":`+quote(id)+`,"data":`+tenthsJSON(150000)+`}]}`, http.StatusUnprocessableEntity) |
| 168 | if msg, _ := out["message"].(string); !strings.Contains(msg, "would make an update larger than 1 MiB") { |
| 169 | t.Errorf("the refusal says %v, want why", out) |
| 170 | } |
| 171 | if _, after := getUpdates(t, a, "/d/"+sqid, 0, cookies); after != seq { |
| 172 | t.Errorf("a refused batch moved the deck from seq %d to %d", seq, after) |
| 173 | } |
| 174 | apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, |
| 175 | `{"ops":[{"op":"set_scene","id":`+quote(id)+`,"data":`+tenthsJSON(116000)+`}]}`, http.StatusOK) |
| 176 | view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK) |
| 177 | scenes, _ := view["scenes"].(map[string]any) |
| 178 | s, _ := scenes[id].(map[string]any) |
| 179 | if l, _ := s["data"].([]any); len(l) != 116000 { |
| 180 | t.Errorf("just under one update: the scene holds %d values, want 116000", len(l)) |
| 181 | } |
| 182 | } |
| 183 | |