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.
test_package_restore_preserves.py · 10.5 KB · 250 lines
| 1 | """A package restore rolls the runtime back; it does not roll back the audit |
| 2 | trail, the identity registry or any history (versions, change logs, logs): |
| 3 | the restore is added to the histories as a new entry instead. |
| 4 | |
| 5 | The permission audit log (data/permissions/audit.jsonl and its rotated |
| 6 | segments) records who was allowed or refused what. A package restore is a |
| 7 | whole-runtime rollback with prune, and data/permissions is a runtime root, |
| 8 | so it used to overwrite the log with the snapshot's copy and delete every |
| 9 | segment written since: the record of what happened between the install and |
| 10 | the rollback was gone, erased by the very action it should have recorded. |
| 11 | """ |
| 12 | |
| 13 | import json |
| 14 | |
| 15 | import pytest |
| 16 | |
| 17 | from test_object_server import ( |
| 18 | auth_headers, |
| 19 | enable_admin_token, |
| 20 | request, |
| 21 | write_package, |
| 22 | write_source, |
| 23 | ) |
| 24 | |
| 25 | |
| 26 | @pytest.fixture |
| 27 | def env(tmp_path, monkeypatch): |
| 28 | packages_root = tmp_path / "packages" |
| 29 | data_dir = tmp_path / "data" |
| 30 | object_root = tmp_path / "objects" |
| 31 | write_source(object_root / "site" / "home.py", "def GET(request): return {'before': True}\n") |
| 32 | write_package( |
| 33 | packages_root, |
| 34 | "hello-world", |
| 35 | { |
| 36 | "id": "hello-world", |
| 37 | "name": "Hello World", |
| 38 | "version": "0.1.0", |
| 39 | "objects": [{"id": "hello_world", "path": "objects/hello/world.py"}], |
| 40 | }, |
| 41 | files=(("objects/hello/world.py", "def GET(request): return {'new': True}\n"),), |
| 42 | ) |
| 43 | write_package( |
| 44 | packages_root, |
| 45 | "contacts-app", |
| 46 | { |
| 47 | "id": "contacts-app", |
| 48 | "name": "Contacts", |
| 49 | "version": "0.1.0", |
| 50 | "objects": [{"id": "contacts_page", "path": "objects/contacts/page.py"}], |
| 51 | "schemas": [{"collection": "contacts", "path": "schemas/contacts.json"}], |
| 52 | "seed": [{"collection": "contacts", "path": "seed/contacts.tsv"}], |
| 53 | }, |
| 54 | files=( |
| 55 | ("objects/contacts/page.py", "def GET(request): return {'page': 1}\n"), |
| 56 | ("schemas/contacts.json", |
| 57 | '{"name":"contacts","fields":[{"name":"id","type":"text"},{"name":"name","type":"text"}]}\n'), |
| 58 | ("seed/contacts.tsv", "id\tname\nc1\tAlice\n"), |
| 59 | ), |
| 60 | ) |
| 61 | monkeypatch.setenv("DBBASIC_PACKAGES_DIR", str(packages_root)) |
| 62 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 63 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(object_root)) |
| 64 | monkeypatch.setenv("DBBASIC_ENABLE_PACKAGE_INSTALLS", "true") |
| 65 | monkeypatch.setenv("DBBASIC_ENABLE_PACKAGE_RESTORE", "true") |
| 66 | monkeypatch.setenv("DBBASIC_ENABLE_SOURCE_WRITES", "true") |
| 67 | monkeypatch.delenv("DBBASIC_BACKUPS_DIR", raising=False) |
| 68 | enable_admin_token(monkeypatch) |
| 69 | return data_dir, object_root |
| 70 | |
| 71 | |
| 72 | def test_package_restore_keeps_the_permission_audit_log(env): |
| 73 | data_dir, object_root = env |
| 74 | permissions = data_dir / "permissions" |
| 75 | permissions.mkdir(parents=True, exist_ok=True) |
| 76 | (permissions / "audit.jsonl").write_text('{"before": "install"}\n') |
| 77 | |
| 78 | status, _, result = request( |
| 79 | "/packages/hello-world/install", method="POST", |
| 80 | body=json.dumps({"allow_replace": True}).encode(), headers=auth_headers(), |
| 81 | ) |
| 82 | assert status == 201, result |
| 83 | change = result["changes"]["installed"] |
| 84 | |
| 85 | # What happened after the install: written to the live log and rotated. |
| 86 | (permissions / "audit.jsonl").write_text( |
| 87 | '{"before": "install"}\n{"after": "install"}\n') |
| 88 | rotated = permissions / "audit-0000000001-20990101T000000000000Z.jsonl" |
| 89 | rotated.write_text('{"rotated": "after install"}\n') |
| 90 | |
| 91 | status, _, restored = request( |
| 92 | "/packages/hello-world/restore", method="POST", |
| 93 | body=json.dumps({"change_id": change["change_id"], "confirm": "restore-runtime"}).encode(), |
| 94 | headers=auth_headers(), |
| 95 | ) |
| 96 | assert status == 200, restored |
| 97 | # The runtime rolled back ... |
| 98 | assert not (object_root / "hello" / "world.py").exists() |
| 99 | # ... and the audit trail did not. |
| 100 | assert '{"after": "install"}' in (permissions / "audit.jsonl").read_text() |
| 101 | assert rotated.read_text() == '{"rotated": "after install"}\n' |
| 102 | |
| 103 | |
| 104 | def test_package_restore_does_not_reactivate_a_user_disabled_since_the_install(env): |
| 105 | """users.tsv rode back with the snapshot: a user disabled after the install |
| 106 | was active again, and their password (never in the archive) still worked.""" |
| 107 | status, _, payload = request( |
| 108 | "/identity/users", method="POST", |
| 109 | body=json.dumps({"user_id": "42", "display_name": "Pat", "roles": ["editor"]}).encode(), |
| 110 | headers=auth_headers(), |
| 111 | ) |
| 112 | assert status in (200, 201), payload |
| 113 | status, _, result = request( |
| 114 | "/packages/hello-world/install", method="POST", |
| 115 | body=json.dumps({"allow_replace": True}).encode(), headers=auth_headers(), |
| 116 | ) |
| 117 | assert status == 201, result |
| 118 | change = result["changes"]["installed"] |
| 119 | status, _, payload = request( |
| 120 | "/identity/users/42/status", method="PUT", |
| 121 | body=json.dumps({"status": "disabled"}).encode(), headers=auth_headers(), |
| 122 | ) |
| 123 | assert status == 200, payload |
| 124 | |
| 125 | status, _, restored = request( |
| 126 | "/packages/hello-world/restore", method="POST", |
| 127 | body=json.dumps({"change_id": change["change_id"], "confirm": "restore-runtime"}).encode(), |
| 128 | headers=auth_headers(), |
| 129 | ) |
| 130 | assert status == 200, restored |
| 131 | status, _, payload = request("/identity/users/42", headers=auth_headers()) |
| 132 | assert status == 200, payload |
| 133 | user = payload.get("user", payload) |
| 134 | assert user["status"] == "disabled" |
| 135 | |
| 136 | |
| 137 | def _install(package_id): |
| 138 | status, _, result = request( |
| 139 | f"/packages/{package_id}/install", method="POST", |
| 140 | body=json.dumps({"allow_replace": True}).encode(), headers=auth_headers(), |
| 141 | ) |
| 142 | assert status == 201, result |
| 143 | return result["changes"]["installed"] |
| 144 | |
| 145 | |
| 146 | def _restore(package_id, change): |
| 147 | status, _, restored = request( |
| 148 | f"/packages/{package_id}/restore", method="POST", |
| 149 | body=json.dumps({"change_id": change["change_id"], "confirm": "restore-runtime"}).encode(), |
| 150 | headers=auth_headers(), |
| 151 | ) |
| 152 | assert status == 200, restored |
| 153 | return restored |
| 154 | |
| 155 | |
| 156 | def _put_source(object_id, code): |
| 157 | status, _, payload = request( |
| 158 | f"/objects/{object_id}", method="PUT", query_string="source=true", |
| 159 | body=json.dumps({"code": code, "message": "edited after install"}).encode(), |
| 160 | headers=auth_headers(), |
| 161 | ) |
| 162 | assert status == 200, payload |
| 163 | |
| 164 | |
| 165 | def test_package_restore_keeps_the_package_change_log(env): |
| 166 | """The install's own `installed` entry was written after the snapshot, so |
| 167 | the rollback used to erase it: the log said the restore undid an install |
| 168 | that, by the log, never finished.""" |
| 169 | change = _install("hello-world") |
| 170 | _restore("hello-world", change) |
| 171 | |
| 172 | status, _, history = request("/packages/hello-world/changes", headers=auth_headers()) |
| 173 | assert status == 200, history |
| 174 | actions = [entry["action"] for entry in history["changes"]] |
| 175 | assert actions[:2] == ["rolled_back", "restore_requested"] |
| 176 | assert "installed" in actions |
| 177 | assert change["change_id"] in [entry["change_id"] for entry in history["changes"]] |
| 178 | |
| 179 | |
| 180 | def test_package_restore_keeps_source_history_and_records_itself(env): |
| 181 | import object_source_changes |
| 182 | import object_versions |
| 183 | |
| 184 | data_dir, object_root = env |
| 185 | change = _install("hello-world") |
| 186 | # Edited after the install: one object the snapshot has (rolled back to |
| 187 | # its old content), one it does not (removed by the rollback). |
| 188 | _put_source("site_home", "def GET(request): return {'edited': True}\n") |
| 189 | _put_source("hello_world", "def GET(request): return {'edited': 'hello'}\n") |
| 190 | |
| 191 | restored = _restore("hello-world", change) |
| 192 | |
| 193 | versions = object_versions.VersionManager(data_dir) |
| 194 | # site_home: the edit is still in its history, and the rollback is a new |
| 195 | # version holding the snapshot's content -- not a rewind. |
| 196 | home = versions.get_history("site_home") |
| 197 | assert [v["message"] for v in home][1] == "edited after install" |
| 198 | assert home[0]["message"].startswith("Package restore: hello-world") |
| 199 | assert versions.get_version("site_home")["content"] == "def GET(request): return {'before': True}\n" |
| 200 | assert (object_root / "site" / "home.py").read_text() == "def GET(request): return {'before': True}\n" |
| 201 | home_changes = object_source_changes.list_source_changes("site_home", base_dir=data_dir)["changes"] |
| 202 | assert home_changes[0]["action"] == "source_rollback" |
| 203 | assert home_changes[0]["details"]["package_restore"]["from_change_id"] == change["change_id"] |
| 204 | assert home_changes[0]["actor"] |
| 205 | assert any(entry["action"] == "source_update" for entry in home_changes[1:]) |
| 206 | |
| 207 | # hello_world: removed by the rollback, its edits and last content kept. |
| 208 | assert not (object_root / "hello" / "world.py").exists() |
| 209 | assert versions.get_version("hello_world")["content"] == "def GET(request): return {'edited': 'hello'}\n" |
| 210 | world_changes = object_source_changes.list_source_changes("hello_world", base_dir=data_dir)["changes"] |
| 211 | assert world_changes[0]["action"] == "source_destroy" |
| 212 | assert restored["history"]["source_changes"] >= 2 |
| 213 | assert restored["history"]["failed"] == 0 |
| 214 | |
| 215 | |
| 216 | def test_package_restore_keeps_record_history_and_records_the_rows_it_removed(env): |
| 217 | import object_record_changes |
| 218 | |
| 219 | data_dir, _ = env |
| 220 | change = _install("contacts-app") |
| 221 | status, _, payload = request( |
| 222 | "/collections/contacts/records", method="POST", |
| 223 | body=json.dumps({"id": "c2", "name": "Bob"}).encode(), headers=auth_headers(), |
| 224 | ) |
| 225 | assert status in (200, 201), payload |
| 226 | assert object_record_changes.list_record_changes("contacts", base_dir=data_dir)["total"] >= 1 |
| 227 | |
| 228 | restored = _restore("contacts-app", change) |
| 229 | |
| 230 | assert not (data_dir / "collections" / "contacts" / "records.tsv").exists() |
| 231 | changes = object_record_changes.list_record_changes("contacts", base_dir=data_dir)["changes"] |
| 232 | # Bob's create is still there, and the rollback's deletes are new entries. |
| 233 | assert any(c["record_id"] == "c2" and c["action"] == "create" for c in changes) |
| 234 | deletes = {c["record_id"]: c for c in changes if c["action"] == "delete"} |
| 235 | assert set(deletes) == {"c1", "c2"} |
| 236 | assert deletes["c2"]["before"]["name"] == "Bob" |
| 237 | assert deletes["c2"]["message"].startswith("Package restore: contacts-app") |
| 238 | assert restored["history"]["record_changes"] == 2 |
| 239 | |
| 240 | |
| 241 | def test_package_restore_keeps_object_logs(env): |
| 242 | data_dir, _ = env |
| 243 | change = _install("hello-world") |
| 244 | log_dir = data_dir / "logs" / "hello_world" |
| 245 | log_dir.mkdir(parents=True, exist_ok=True) |
| 246 | (log_dir / "log.tsv").write_text("timestamp\tlevel\tmessage\n2099-01-01\tINFO\tran after install\n") |
| 247 | |
| 248 | _restore("hello-world", change) |
| 249 | |
| 250 | assert "ran after install" in (log_dir / "log.tsv").read_text() |