askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_object_backup_index.py · 21.4 KB · 563 lines
| 1 | """Tests for the backup inventory module and the admin backup endpoints.""" |
| 2 | |
| 3 | import pytest |
| 4 | from test_object_server import ( |
| 5 | auth_headers, |
| 6 | enable_admin_token, |
| 7 | raw_request, |
| 8 | request, |
| 9 | ) |
| 10 | |
| 11 | import object_backup |
| 12 | import object_backup_index |
| 13 | import object_server |
| 14 | |
| 15 | |
| 16 | def _write_backup(directory, name, content=b"archive-bytes"): |
| 17 | directory.mkdir(parents=True, exist_ok=True) |
| 18 | (directory / name).write_bytes(content) |
| 19 | return directory / name |
| 20 | |
| 21 | |
| 22 | # --- Module ------------------------------------------------------------------ |
| 23 | |
| 24 | def test_validate_backup_id_and_path_guard(tmp_path, monkeypatch): |
| 25 | monkeypatch.delenv(object_backup.BACKUPS_DIR_ENV, raising=False) |
| 26 | assert object_backup_index.validate_backup_id("20260101T000000Z-manual.tar.gz") |
| 27 | assert not object_backup_index.validate_backup_id("../etc/passwd") |
| 28 | assert not object_backup_index.validate_backup_id("notes.txt") |
| 29 | assert not object_backup_index.validate_backup_id("a/b.tar.gz") |
| 30 | assert not object_backup_index.validate_backup_id("..%2f.tar.gz") |
| 31 | |
| 32 | with pytest.raises(ValueError): |
| 33 | object_backup_index.backup_path("../secret.tar.gz", data_dir=tmp_path) |
| 34 | |
| 35 | |
| 36 | def test_list_backups_parses_kind_and_scope(tmp_path, monkeypatch): |
| 37 | monkeypatch.delenv(object_backup.BACKUPS_DIR_ENV, raising=False) |
| 38 | bdir = tmp_path / "backups" |
| 39 | _write_backup(bdir, "20260101T000000Z-manual.tar.gz", b"aa") |
| 40 | _write_backup(bdir, "20260102T000000Z-package-app-notes.tar.gz", b"bbb") |
| 41 | _write_backup(bdir, "not-a-backup.txt", b"ignore me") |
| 42 | |
| 43 | backups = object_backup_index.list_backups(data_dir=tmp_path) |
| 44 | assert len(backups) == 2 |
| 45 | by_id = {b["id"]: b for b in backups} |
| 46 | manual = by_id["20260101T000000Z-manual.tar.gz"] |
| 47 | assert manual["kind"] == "manual" and manual["scope"] == "runtime" and manual["size"] == 2 |
| 48 | pkg = by_id["20260102T000000Z-package-app-notes.tar.gz"] |
| 49 | assert pkg["kind"] == "package" and pkg["scope"] == "app-notes" |
| 50 | # newest first |
| 51 | assert backups[0]["created_at"] >= backups[1]["created_at"] |
| 52 | |
| 53 | |
| 54 | def test_create_backup_writes_an_archive(tmp_path, monkeypatch): |
| 55 | monkeypatch.delenv(object_backup.BACKUPS_DIR_ENV, raising=False) |
| 56 | data_dir = tmp_path / "data" |
| 57 | objects_dir = tmp_path / "objects" |
| 58 | (data_dir / "collections" / "notes").mkdir(parents=True) |
| 59 | (data_dir / "collections" / "notes" / "records.tsv").write_text("id\tcontent\nn1\thi\n") |
| 60 | objects_dir.mkdir() |
| 61 | monkeypatch.setenv(object_backup.OBJECTS_DIR_ENV, str(objects_dir)) |
| 62 | |
| 63 | backup = object_backup_index.create_backup(data_dir=data_dir) |
| 64 | assert backup["kind"] == "manual" |
| 65 | path = object_backup_index.backup_path(backup["id"], data_dir=data_dir) |
| 66 | assert path.is_file() |
| 67 | verification = object_backup.verify_runtime_backup(path) |
| 68 | assert verification.ok |
| 69 | |
| 70 | |
| 71 | # --- Endpoints --------------------------------------------------------------- |
| 72 | |
| 73 | def test_admin_backups_requires_admin(tmp_path, monkeypatch): |
| 74 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 75 | enable_admin_token(monkeypatch) |
| 76 | status, _, payload = request("/admin/backups") # no auth |
| 77 | assert status == 401 |
| 78 | assert "error" in payload |
| 79 | |
| 80 | |
| 81 | def test_admin_backups_list_create_download(tmp_path, monkeypatch): |
| 82 | monkeypatch.delenv(object_backup.BACKUPS_DIR_ENV, raising=False) |
| 83 | data_dir = tmp_path / "data" |
| 84 | objects_dir = tmp_path / "objects" |
| 85 | (data_dir / "collections" / "notes").mkdir(parents=True) |
| 86 | (data_dir / "collections" / "notes" / "records.tsv").write_text("id\tcontent\nn1\thi\n") |
| 87 | objects_dir.mkdir() |
| 88 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 89 | monkeypatch.setenv(object_backup.OBJECTS_DIR_ENV, str(objects_dir)) |
| 90 | enable_admin_token(monkeypatch) |
| 91 | |
| 92 | # empty to start |
| 93 | status, _, listed = request("/admin/backups", headers=auth_headers()) |
| 94 | assert status == 200 and listed["count"] == 0 |
| 95 | assert listed["schedule"]["scheduled"] is False |
| 96 | |
| 97 | # create one |
| 98 | status, _, created = request("/admin/backups", method="POST", headers=auth_headers()) |
| 99 | assert status == 201, created |
| 100 | backup_id = created["backup"]["id"] |
| 101 | assert created["backup"]["kind"] == "manual" |
| 102 | |
| 103 | # it shows up in the inventory |
| 104 | status, _, listed = request("/admin/backups", headers=auth_headers()) |
| 105 | assert status == 200 and listed["count"] == 1 |
| 106 | assert listed["backups"][0]["id"] == backup_id |
| 107 | |
| 108 | # download returns the archive bytes (gzip magic), admin-gated |
| 109 | status, resp_headers, body = raw_request( |
| 110 | f"/admin/backups/{backup_id}/download", headers=auth_headers() |
| 111 | ) |
| 112 | assert status == 200 |
| 113 | assert resp_headers[b"content-type"] == b"application/gzip" |
| 114 | assert body[:2] == b"\x1f\x8b" # gzip magic |
| 115 | assert object_backup.verify_runtime_backup( |
| 116 | object_backup_index.backup_path(backup_id, data_dir=data_dir) |
| 117 | ).ok |
| 118 | |
| 119 | # download is admin-gated and traversal-safe |
| 120 | status, _, _ = raw_request(f"/admin/backups/{backup_id}/download") # no auth |
| 121 | assert status == 401 |
| 122 | status, _, payload = request("/admin/backups/..%2Fpasswd/download", headers=auth_headers()) |
| 123 | assert status in (400, 404) |
| 124 | |
| 125 | |
| 126 | def test_admin_status_reports_backup_capability(tmp_path, monkeypatch): |
| 127 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 128 | monkeypatch.setenv(object_server.BACKUP_SCHEDULE_ENV, "daily") |
| 129 | enable_admin_token(monkeypatch) |
| 130 | status, _, payload = request("/admin/status", headers=auth_headers()) |
| 131 | assert status == 200 |
| 132 | caps = payload["capabilities"]["backups"] |
| 133 | assert caps["can_create"] and caps["can_download"] and caps["can_restore"] is False |
| 134 | assert caps["can_preview"] is True |
| 135 | assert caps["scheduled"] is True and caps["schedule"] == "daily" |
| 136 | |
| 137 | |
| 138 | # --- preview_collection / preview_record -------------------------------------- |
| 139 | |
| 140 | def _make_backup_with_collection(tmp_path, monkeypatch, rows_tsv): |
| 141 | """Build a runtime backup whose only collection is "notes" with the given TSV body.""" |
| 142 | monkeypatch.delenv(object_backup.BACKUPS_DIR_ENV, raising=False) |
| 143 | data_dir = tmp_path / "data" |
| 144 | objects_dir = tmp_path / "objects" |
| 145 | (data_dir / "collections" / "notes").mkdir(parents=True) |
| 146 | (data_dir / "collections" / "notes" / "records.tsv").write_text(rows_tsv) |
| 147 | objects_dir.mkdir() |
| 148 | monkeypatch.setenv(object_backup.OBJECTS_DIR_ENV, str(objects_dir)) |
| 149 | backup = object_backup_index.create_backup(data_dir=data_dir) |
| 150 | return data_dir, backup["id"] |
| 151 | |
| 152 | |
| 153 | def test_preview_collection_reports_added_removed_changed_unchanged(tmp_path, monkeypatch): |
| 154 | data_dir, backup_id = _make_backup_with_collection( |
| 155 | tmp_path, |
| 156 | monkeypatch, |
| 157 | "id\tname\tcolor\n" |
| 158 | "n1\tFirst\tred\n" |
| 159 | "n2\tSecond\tblue\n" |
| 160 | "n3\tThird\tgreen\n", |
| 161 | ) |
| 162 | |
| 163 | # mutate the live dir: n2 gets a field change, n3 is deleted, n4 is added. |
| 164 | (data_dir / "collections" / "notes" / "records.tsv").write_text( |
| 165 | "id\tname\tcolor\n" |
| 166 | "n1\tFirst\tred\n" |
| 167 | "n2\tSecond\tpurple\n" |
| 168 | "n4\tFourth\tyellow\n" |
| 169 | ) |
| 170 | |
| 171 | preview = object_backup_index.preview_collection(backup_id, "notes", data_dir=data_dir) |
| 172 | |
| 173 | assert preview["target"] == {"kind": "collection", "name": "notes"} |
| 174 | assert preview["backup_id"] == backup_id |
| 175 | assert preview["present_in_backup"] is True |
| 176 | assert preview["added"] == ["n3"] |
| 177 | assert preview["removed"] == ["n4"] |
| 178 | assert preview["changed"] == [{"id": "n2", "fields": ["color"]}] |
| 179 | assert preview["unchanged"] == 1 |
| 180 | assert isinstance(preview["diff_hash"], str) and len(preview["diff_hash"]) == 64 |
| 181 | |
| 182 | # same inputs -> same hash |
| 183 | again = object_backup_index.preview_collection(backup_id, "notes", data_dir=data_dir) |
| 184 | assert again["diff_hash"] == preview["diff_hash"] |
| 185 | |
| 186 | |
| 187 | def test_preview_collection_absent_from_backup(tmp_path, monkeypatch): |
| 188 | data_dir, backup_id = _make_backup_with_collection(tmp_path, monkeypatch, "id\tname\nn1\tFirst\n") |
| 189 | |
| 190 | preview = object_backup_index.preview_collection(backup_id, "other", data_dir=data_dir) |
| 191 | assert preview["present_in_backup"] is False |
| 192 | assert preview["added"] == [] |
| 193 | assert preview["removed"] == [] |
| 194 | assert preview["changed"] == [] |
| 195 | assert preview["unchanged"] == 0 |
| 196 | |
| 197 | |
| 198 | def test_preview_collection_rejects_invalid_collection_name(tmp_path, monkeypatch): |
| 199 | data_dir, backup_id = _make_backup_with_collection(tmp_path, monkeypatch, "id\tname\nn1\tFirst\n") |
| 200 | with pytest.raises(ValueError): |
| 201 | object_backup_index.preview_collection(backup_id, "../etc", data_dir=data_dir) |
| 202 | |
| 203 | |
| 204 | def test_preview_record_present_in_both_backup_only_and_absent(tmp_path, monkeypatch): |
| 205 | data_dir, backup_id = _make_backup_with_collection( |
| 206 | tmp_path, |
| 207 | monkeypatch, |
| 208 | "id\tname\nn1\tFirst\nn2\tSecond\n", |
| 209 | ) |
| 210 | (data_dir / "collections" / "notes" / "records.tsv").write_text("id\tname\nn1\tFirst\n") |
| 211 | |
| 212 | both = object_backup_index.preview_record(backup_id, "notes", "n1", data_dir=data_dir) |
| 213 | assert both["present_in_backup"] is True and both["present_in_live"] is True |
| 214 | assert both["record"] == {"id": "n1", "name": "First"} |
| 215 | |
| 216 | backup_only = object_backup_index.preview_record(backup_id, "notes", "n2", data_dir=data_dir) |
| 217 | assert backup_only["present_in_backup"] is True and backup_only["present_in_live"] is False |
| 218 | assert backup_only["record"] == {"id": "n2", "name": "Second"} |
| 219 | |
| 220 | absent = object_backup_index.preview_record(backup_id, "notes", "nope", data_dir=data_dir) |
| 221 | assert absent["present_in_backup"] is False and absent["present_in_live"] is False |
| 222 | assert absent["record"] is None |
| 223 | |
| 224 | |
| 225 | # --- preview_collection / preview_record: append-mode ("_op") folding --------- |
| 226 | # |
| 227 | # docs/storage-modes.md "Current limits" used to note that preview/diff did |
| 228 | # not interpret `_op`. These build a records.tsv payload by hand (the |
| 229 | # physical on-disk format append mode writes -- see object_records.py's |
| 230 | # OP_FIELD/OP_UPSERT/OP_DELETE and _fold_append_rows) rather than going |
| 231 | # through the record engine, since object_backup_index's fold is meant to |
| 232 | # be a small, independent replica of that format, not a round-trip through |
| 233 | # object_records itself. |
| 234 | |
| 235 | def _live_path(data_dir): |
| 236 | return data_dir / "collections" / "notes" / "records.tsv" |
| 237 | |
| 238 | |
| 239 | def test_preview_collection_append_backup_vs_classic_live(tmp_path, monkeypatch): |
| 240 | # backup: append-mode log. n2 is superseded once (final color: purple); |
| 241 | # n3 is created then tombstoned (must not appear as live in the backup). |
| 242 | data_dir, backup_id = _make_backup_with_collection( |
| 243 | tmp_path, |
| 244 | monkeypatch, |
| 245 | "_op\tid\tname\tcolor\n" |
| 246 | "\tn1\tFirst\tred\n" |
| 247 | "\tn2\tSecond\tblue\n" |
| 248 | "\tn2\tSecond\tpurple\n" |
| 249 | "\tn3\tThird\tgreen\n" |
| 250 | "del\tn3\t\t\n", |
| 251 | ) |
| 252 | |
| 253 | # live: classic, mutated after the backup. |
| 254 | _live_path(data_dir).write_text( |
| 255 | "id\tname\tcolor\n" |
| 256 | "n1\tFirst\tred\n" |
| 257 | "n2\tSecond\tblue\n" |
| 258 | "n4\tFourth\tyellow\n" |
| 259 | ) |
| 260 | |
| 261 | preview = object_backup_index.preview_collection(backup_id, "notes", data_dir=data_dir) |
| 262 | assert preview["added"] == [] # n3 is tombstoned in the backup -> not live there |
| 263 | assert preview["removed"] == ["n4"] # restoring would drop n4 from live |
| 264 | assert preview["changed"] == [{"id": "n2", "fields": ["color"]}] # folds to purple |
| 265 | assert preview["unchanged"] == 1 # n1 |
| 266 | |
| 267 | |
| 268 | def test_preview_collection_classic_backup_vs_append_live(tmp_path, monkeypatch): |
| 269 | # backup: classic. |
| 270 | data_dir, backup_id = _make_backup_with_collection( |
| 271 | tmp_path, |
| 272 | monkeypatch, |
| 273 | "id\tname\tcolor\n" |
| 274 | "n1\tFirst\tred\n" |
| 275 | "n2\tSecond\tpurple\n", |
| 276 | ) |
| 277 | |
| 278 | # live: append-mode log, mutated after the backup. |
| 279 | _live_path(data_dir).write_text( |
| 280 | "_op\tid\tname\tcolor\n" |
| 281 | "\tn1\tFirst\tred\n" |
| 282 | "\tn2\tSecond\tblue\n" |
| 283 | "\tn4\tFourth\tyellow\n" |
| 284 | ) |
| 285 | |
| 286 | preview = object_backup_index.preview_collection(backup_id, "notes", data_dir=data_dir) |
| 287 | assert preview["added"] == [] |
| 288 | assert preview["removed"] == ["n4"] |
| 289 | assert preview["changed"] == [{"id": "n2", "fields": ["color"]}] |
| 290 | assert preview["unchanged"] == 1 |
| 291 | |
| 292 | |
| 293 | def test_preview_collection_superseded_rows_fold_to_final_values_only(tmp_path, monkeypatch): |
| 294 | # id updated 3x in the log -> counts once, and the diff only ever sees |
| 295 | # the final value (not any intermediate one). |
| 296 | data_dir, backup_id = _make_backup_with_collection( |
| 297 | tmp_path, |
| 298 | monkeypatch, |
| 299 | "_op\tid\tname\n" |
| 300 | "\tn1\tv1\n" |
| 301 | "\tn1\tv2\n" |
| 302 | "\tn1\tv3\n", |
| 303 | ) |
| 304 | _live_path(data_dir).write_text("id\tname\nn1\tv3\n") |
| 305 | |
| 306 | preview = object_backup_index.preview_collection(backup_id, "notes", data_dir=data_dir) |
| 307 | assert preview["added"] == [] |
| 308 | assert preview["removed"] == [] |
| 309 | assert preview["changed"] == [] |
| 310 | assert preview["unchanged"] == 1 |
| 311 | |
| 312 | |
| 313 | def test_preview_collection_tombstone_in_backup_reports_removed(tmp_path, monkeypatch): |
| 314 | # Restore semantics are "live becomes the backup": a tombstoned id in |
| 315 | # the backup, with the id still live now, would be dropped by a |
| 316 | # restore -- so it belongs in "removed", not "added". |
| 317 | data_dir, backup_id = _make_backup_with_collection( |
| 318 | tmp_path, |
| 319 | monkeypatch, |
| 320 | "_op\tid\tname\n" |
| 321 | "\tn1\tFirst\n" |
| 322 | "del\tn1\t\n", |
| 323 | ) |
| 324 | _live_path(data_dir).write_text("id\tname\nn1\tFirst\n") |
| 325 | |
| 326 | preview = object_backup_index.preview_collection(backup_id, "notes", data_dir=data_dir) |
| 327 | assert preview["added"] == [] |
| 328 | assert preview["removed"] == ["n1"] |
| 329 | assert preview["changed"] == [] |
| 330 | |
| 331 | |
| 332 | def test_preview_collection_tombstone_in_live_reports_added(tmp_path, monkeypatch): |
| 333 | # Mirror image: a tombstoned id in LIVE, with the id present in the |
| 334 | # backup, would reappear on restore -- so it belongs in "added". |
| 335 | data_dir, backup_id = _make_backup_with_collection( |
| 336 | tmp_path, |
| 337 | monkeypatch, |
| 338 | "id\tname\nn1\tFirst\n", |
| 339 | ) |
| 340 | _live_path(data_dir).write_text( |
| 341 | "_op\tid\tname\n" |
| 342 | "\tn1\tFirst\n" |
| 343 | "del\tn1\t\n" |
| 344 | ) |
| 345 | |
| 346 | preview = object_backup_index.preview_collection(backup_id, "notes", data_dir=data_dir) |
| 347 | assert preview["added"] == ["n1"] |
| 348 | assert preview["removed"] == [] |
| 349 | assert preview["changed"] == [] |
| 350 | |
| 351 | |
| 352 | def test_preview_collection_resurrection_uses_final_values(tmp_path, monkeypatch): |
| 353 | # del then re-create in the log -> treated as live, holding only the |
| 354 | # values from after the re-create. |
| 355 | data_dir, backup_id = _make_backup_with_collection( |
| 356 | tmp_path, |
| 357 | monkeypatch, |
| 358 | "_op\tid\tname\n" |
| 359 | "\tn1\tFirst\n" |
| 360 | "del\tn1\t\n" |
| 361 | "\tn1\tResurrected\n", |
| 362 | ) |
| 363 | _live_path(data_dir).write_text("id\tname\nn1\tFirst\n") |
| 364 | |
| 365 | preview = object_backup_index.preview_collection(backup_id, "notes", data_dir=data_dir) |
| 366 | assert preview["added"] == [] |
| 367 | assert preview["removed"] == [] |
| 368 | assert preview["changed"] == [{"id": "n1", "fields": ["name"]}] |
| 369 | assert preview["unchanged"] == 0 |
| 370 | |
| 371 | record = object_backup_index.preview_record(backup_id, "notes", "n1", data_dir=data_dir) |
| 372 | assert record["present_in_backup"] is True |
| 373 | assert record["record"] == {"id": "n1", "name": "Resurrected"} |
| 374 | |
| 375 | |
| 376 | def test_preview_op_field_never_leaks_into_changed_fields_or_record(tmp_path, monkeypatch): |
| 377 | data_dir, backup_id = _make_backup_with_collection( |
| 378 | tmp_path, |
| 379 | monkeypatch, |
| 380 | "_op\tid\tname\tcolor\n" |
| 381 | "\tn1\tFirst\tred\n" |
| 382 | "\tn1\tFirst\tblue\n", |
| 383 | ) |
| 384 | _live_path(data_dir).write_text( |
| 385 | "_op\tid\tname\tcolor\n" |
| 386 | "\tn1\tFirst\tgreen\n" |
| 387 | ) |
| 388 | |
| 389 | preview = object_backup_index.preview_collection(backup_id, "notes", data_dir=data_dir) |
| 390 | for entry in preview["changed"]: |
| 391 | assert "_op" not in entry["fields"] |
| 392 | |
| 393 | record = object_backup_index.preview_record(backup_id, "notes", "n1", data_dir=data_dir) |
| 394 | assert "_op" not in record["record"] |
| 395 | |
| 396 | |
| 397 | def test_preview_torn_final_line_ignored_on_backup_and_live(tmp_path, monkeypatch): |
| 398 | # A torn (unterminated) final physical line represents an in-flight |
| 399 | # write and must be dropped, on either side. |
| 400 | data_dir, backup_id = _make_backup_with_collection( |
| 401 | tmp_path, |
| 402 | monkeypatch, |
| 403 | "_op\tid\tname\n" |
| 404 | "\tn1\tFirst\n" |
| 405 | "\tn2\tSec", # torn: no trailing newline |
| 406 | ) |
| 407 | live_text = ( |
| 408 | "_op\tid\tname\n" |
| 409 | "\tn1\tFirst\n" |
| 410 | "\tn1\tUpdated" # torn: no trailing newline -- must not apply |
| 411 | ) |
| 412 | _live_path(data_dir).write_text(live_text) |
| 413 | assert not live_text.endswith("\n") |
| 414 | |
| 415 | preview = object_backup_index.preview_collection(backup_id, "notes", data_dir=data_dir) |
| 416 | # backup: n2's torn row is dropped, so only n1 is live in the backup. |
| 417 | # live: n1's torn update is dropped, so live n1 keeps its prior value. |
| 418 | assert preview["added"] == [] |
| 419 | assert preview["removed"] == [] |
| 420 | assert preview["changed"] == [] |
| 421 | assert preview["unchanged"] == 1 |
| 422 | |
| 423 | |
| 424 | def test_preview_diff_hash_stable_across_physical_layout(tmp_path, monkeypatch): |
| 425 | # Same logical content via two different physical log layouts (one has |
| 426 | # an extra superseded row) must hash identically, since diff_hash is |
| 427 | # computed over the folded diff, not the raw rows. |
| 428 | data_dir_a, backup_id_a = _make_backup_with_collection( |
| 429 | tmp_path / "a", |
| 430 | monkeypatch, |
| 431 | "_op\tid\tname\n\tn1\tFirst\n", |
| 432 | ) |
| 433 | data_dir_b, backup_id_b = _make_backup_with_collection( |
| 434 | tmp_path / "b", |
| 435 | monkeypatch, |
| 436 | "_op\tid\tname\n\tn1\tOld\n\tn1\tFirst\n", |
| 437 | ) |
| 438 | _live_path(data_dir_a).write_text("id\tname\nn1\tFirst\n") |
| 439 | _live_path(data_dir_b).write_text("id\tname\nn1\tFirst\n") |
| 440 | |
| 441 | preview_a = object_backup_index.preview_collection(backup_id_a, "notes", data_dir=data_dir_a) |
| 442 | preview_b = object_backup_index.preview_collection(backup_id_b, "notes", data_dir=data_dir_b) |
| 443 | assert preview_a["diff_hash"] == preview_b["diff_hash"] |
| 444 | |
| 445 | |
| 446 | def test_preview_record_tombstoned_in_backup_reports_absent(tmp_path, monkeypatch): |
| 447 | data_dir, backup_id = _make_backup_with_collection( |
| 448 | tmp_path, |
| 449 | monkeypatch, |
| 450 | "_op\tid\tname\n" |
| 451 | "\tn1\tFirst\n" |
| 452 | "del\tn1\t\n", |
| 453 | ) |
| 454 | _live_path(data_dir).write_text("id\tname\nn1\tFirst\n") |
| 455 | |
| 456 | record = object_backup_index.preview_record(backup_id, "notes", "n1", data_dir=data_dir) |
| 457 | assert record["record"] is None |
| 458 | assert record["present_in_backup"] is False |
| 459 | assert record["present_in_live"] is True |
| 460 | |
| 461 | |
| 462 | # --- preview endpoints --------------------------------------------------------- |
| 463 | |
| 464 | def test_admin_backup_preview_and_record_require_admin(tmp_path, monkeypatch): |
| 465 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 466 | enable_admin_token(monkeypatch) |
| 467 | status, _, _ = request( |
| 468 | "/admin/backups/whatever.tar.gz/preview", query_string="kind=collection&name=notes" |
| 469 | ) |
| 470 | assert status == 401 |
| 471 | status, _, _ = request( |
| 472 | "/admin/backups/whatever.tar.gz/record", query_string="collection=notes&id=n1" |
| 473 | ) |
| 474 | assert status == 401 |
| 475 | |
| 476 | |
| 477 | def test_admin_backup_preview_and_record_endpoints(tmp_path, monkeypatch): |
| 478 | monkeypatch.delenv(object_backup.BACKUPS_DIR_ENV, raising=False) |
| 479 | data_dir = tmp_path / "data" |
| 480 | objects_dir = tmp_path / "objects" |
| 481 | (data_dir / "collections" / "notes").mkdir(parents=True) |
| 482 | (data_dir / "collections" / "notes" / "records.tsv").write_text( |
| 483 | "id\tname\nn1\tFirst\nn2\tSecond\n" |
| 484 | ) |
| 485 | objects_dir.mkdir() |
| 486 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 487 | monkeypatch.setenv(object_backup.OBJECTS_DIR_ENV, str(objects_dir)) |
| 488 | enable_admin_token(monkeypatch) |
| 489 | |
| 490 | status, _, created = request("/admin/backups", method="POST", headers=auth_headers()) |
| 491 | assert status == 201, created |
| 492 | backup_id = created["backup"]["id"] |
| 493 | |
| 494 | # mutate live after the backup was taken |
| 495 | (data_dir / "collections" / "notes" / "records.tsv").write_text("id\tname\nn1\tFirst\n") |
| 496 | |
| 497 | status, _, payload = request( |
| 498 | f"/admin/backups/{backup_id}/preview", |
| 499 | query_string="kind=collection&name=notes", |
| 500 | headers=auth_headers(), |
| 501 | ) |
| 502 | assert status == 200, payload |
| 503 | assert payload["status"] == "ok" |
| 504 | preview = payload["preview"] |
| 505 | assert preview["target"] == {"kind": "collection", "name": "notes"} |
| 506 | assert preview["added"] == ["n2"] |
| 507 | assert preview["present_in_backup"] is True |
| 508 | |
| 509 | status, _, payload = request( |
| 510 | f"/admin/backups/{backup_id}/record", |
| 511 | query_string="collection=notes&id=n2", |
| 512 | headers=auth_headers(), |
| 513 | ) |
| 514 | assert status == 200, payload |
| 515 | record = payload["record"] |
| 516 | assert record["present_in_backup"] is True |
| 517 | assert record["present_in_live"] is False |
| 518 | assert record["record"] == {"id": "n2", "name": "Second"} |
| 519 | |
| 520 | # bad kind |
| 521 | status, _, payload = request( |
| 522 | f"/admin/backups/{backup_id}/preview", |
| 523 | query_string="kind=record&name=notes", |
| 524 | headers=auth_headers(), |
| 525 | ) |
| 526 | assert status == 400 |
| 527 | |
| 528 | # missing params |
| 529 | status, _, payload = request( |
| 530 | f"/admin/backups/{backup_id}/preview", |
| 531 | query_string="kind=collection", |
| 532 | headers=auth_headers(), |
| 533 | ) |
| 534 | assert status == 400 |
| 535 | status, _, payload = request( |
| 536 | f"/admin/backups/{backup_id}/record", |
| 537 | query_string="collection=notes", |
| 538 | headers=auth_headers(), |
| 539 | ) |
| 540 | assert status == 400 |
| 541 | |
| 542 | # unknown backup id -> 404; malformed id -> 400 |
| 543 | status, _, payload = request( |
| 544 | "/admin/backups/does-not-exist.tar.gz/preview", |
| 545 | query_string="kind=collection&name=notes", |
| 546 | headers=auth_headers(), |
| 547 | ) |
| 548 | assert status == 404 |
| 549 | status, _, payload = request( |
| 550 | "/admin/backups/..%2Fpasswd/preview", |
| 551 | query_string="kind=collection&name=notes", |
| 552 | headers=auth_headers(), |
| 553 | ) |
| 554 | assert status in (400, 404) |
| 555 | |
| 556 | # method not allowed |
| 557 | status, _, payload = request( |
| 558 | f"/admin/backups/{backup_id}/preview", |
| 559 | query_string="kind=collection&name=notes", |
| 560 | method="POST", |
| 561 | headers=auth_headers(), |
| 562 | ) |
| 563 | assert status == 405 |