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_destroy_object.py · 5.0 KB · 138 lines
| 1 | """DELETE /objects/{id}?destroy=true removes the object; it never executes it.""" |
| 2 | |
| 3 | import json |
| 4 | |
| 5 | import object_server |
| 6 | import object_source_changes |
| 7 | import object_versions |
| 8 | |
| 9 | from test_object_server import ( |
| 10 | auth_headers, |
| 11 | enable_admin_token, |
| 12 | enable_source_writes, |
| 13 | request, |
| 14 | write_source, |
| 15 | ) |
| 16 | |
| 17 | DELETING = "def GET(request):\n return {'ok': True}\n\ndef DELETE(request):\n return {'ran_delete': True}\n" |
| 18 | PLAIN = "def GET(request):\n return {'ok': True}\n" |
| 19 | |
| 20 | |
| 21 | def destroy(object_id, headers=None, query="destroy=true"): |
| 22 | return request( |
| 23 | f"/objects/{object_id}", |
| 24 | method="DELETE", |
| 25 | query_string=query, |
| 26 | headers=headers, |
| 27 | ) |
| 28 | |
| 29 | |
| 30 | def test_destroy_requires_authentication(tmp_path, monkeypatch): |
| 31 | root = tmp_path / "objects" |
| 32 | source = write_source(root / "basics" / "hello.py", PLAIN) |
| 33 | enable_source_writes(monkeypatch, root, tmp_path / "data") |
| 34 | |
| 35 | status, _, payload = destroy("basics_hello") |
| 36 | |
| 37 | assert status == 401 |
| 38 | assert payload["status"] == "error" |
| 39 | assert source.exists() |
| 40 | |
| 41 | |
| 42 | def test_destroy_forbidden_when_source_writes_disabled(tmp_path, monkeypatch): |
| 43 | root = tmp_path / "objects" |
| 44 | source = write_source(root / "basics" / "hello.py", PLAIN) |
| 45 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(root)) |
| 46 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(tmp_path / "data")) |
| 47 | monkeypatch.delenv("DBBASIC_ENABLE_SOURCE_WRITES", raising=False) |
| 48 | enable_admin_token(monkeypatch) |
| 49 | |
| 50 | status, _, payload = destroy("basics_hello", headers=auth_headers()) |
| 51 | |
| 52 | assert status == 403 |
| 53 | assert payload["error"].startswith("Source writes are disabled") |
| 54 | assert source.exists() |
| 55 | |
| 56 | |
| 57 | def test_destroy_refuses_non_admin_when_enforcement_on(tmp_path, monkeypatch): |
| 58 | # The admin-token gate runs before the permission engine and the engine |
| 59 | # always allows the admin role, so a non-admin never reaches it: a wrong |
| 60 | # token or a gateway-asserted non-admin user is refused at the gate. |
| 61 | root = tmp_path / "objects" |
| 62 | source = write_source(root / "basics" / "hello.py", PLAIN) |
| 63 | enable_source_writes(monkeypatch, root, tmp_path / "data") |
| 64 | monkeypatch.setenv(object_server.PERMISSION_ENFORCEMENT_ENV, "true") |
| 65 | monkeypatch.setenv(object_server.PERMISSION_UNREADY_ENFORCEMENT_ENV, "true") |
| 66 | monkeypatch.setenv(object_server.PERMISSION_TRUST_HEADERS_ENV, "true") |
| 67 | |
| 68 | wrong_token = [("authorization", "Token not-the-admin-token")] |
| 69 | sales = [("x-dbbasic-user-id", "7"), ("x-dbbasic-roles", "sales")] |
| 70 | for headers in (wrong_token, sales): |
| 71 | status, _, payload = destroy("basics_hello", headers=headers) |
| 72 | assert status in (401, 403) |
| 73 | assert payload["status"] == "error" |
| 74 | assert source.exists() |
| 75 | |
| 76 | # Admin token with enforcement on is allowed through the engine. |
| 77 | status, _, _ = destroy("basics_hello", headers=auth_headers()) |
| 78 | assert status == 200 |
| 79 | assert not source.exists() |
| 80 | |
| 81 | |
| 82 | def test_destroy_unknown_object_is_404(tmp_path, monkeypatch): |
| 83 | root = tmp_path / "objects" |
| 84 | root.mkdir() |
| 85 | enable_source_writes(monkeypatch, root, tmp_path / "data") |
| 86 | |
| 87 | status, _, payload = destroy("basics_missing", headers=auth_headers()) |
| 88 | |
| 89 | assert status == 404 |
| 90 | assert payload["status"] == "error" |
| 91 | |
| 92 | |
| 93 | def test_destroy_removes_object_and_keeps_versions(tmp_path, monkeypatch): |
| 94 | root = tmp_path / "objects" |
| 95 | source = write_source(root / "basics" / "hello.py", PLAIN) |
| 96 | data_dir = tmp_path / "data" |
| 97 | enable_source_writes(monkeypatch, root, data_dir) |
| 98 | assert request("/objects/basics_hello")[0] == 200 |
| 99 | |
| 100 | status, _, payload = destroy("basics_hello", headers=auth_headers()) |
| 101 | |
| 102 | assert status == 200 |
| 103 | assert payload["status"] == "ok" |
| 104 | assert payload["message"] == "Object destroyed: basics_hello" |
| 105 | assert payload["object_id"] == "basics_hello" |
| 106 | assert not source.exists() |
| 107 | assert request("/objects/basics_hello")[0] == 404 |
| 108 | |
| 109 | # Source is restorable from history, and the destroy is on the change log. |
| 110 | manager = object_versions.VersionManager(data_dir) |
| 111 | assert manager.get_version("basics_hello")["content"] == PLAIN |
| 112 | changes = object_source_changes.list_source_changes("basics_hello", base_dir=data_dir) |
| 113 | assert "source_destroy" in json.dumps(changes) |
| 114 | |
| 115 | |
| 116 | def test_destroy_does_not_execute_objects_own_delete_method(tmp_path, monkeypatch): |
| 117 | root = tmp_path / "objects" |
| 118 | source = write_source(root / "basics" / "hello.py", DELETING) |
| 119 | enable_source_writes(monkeypatch, root, tmp_path / "data") |
| 120 | |
| 121 | status, _, payload = destroy("basics_hello", headers=auth_headers()) |
| 122 | |
| 123 | assert status == 200 |
| 124 | assert "ran_delete" not in payload |
| 125 | assert payload["message"] == "Object destroyed: basics_hello" |
| 126 | assert not source.exists() |
| 127 | |
| 128 | |
| 129 | def test_plain_delete_still_executes_objects_delete_method(tmp_path, monkeypatch): |
| 130 | root = tmp_path / "objects" |
| 131 | source = write_source(root / "basics" / "hello.py", DELETING) |
| 132 | enable_source_writes(monkeypatch, root, tmp_path / "data") |
| 133 | |
| 134 | for query in ("", "destroy=false", "destroy=1"): |
| 135 | status, _, payload = destroy("basics_hello", query=query) |
| 136 | assert status == 200 |
| 137 | assert payload == {"ran_delete": True} |
| 138 | assert source.exists() |