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_unencodable_result.py · 4.9 KB · 112 lines
| 1 | """An object that returns a value JSON cannot hold gets an error that says so. |
| 2 | |
| 3 | With warm workers (DBBASIC_WORKER_POOL_SIZE) the worker died encoding its |
| 4 | reply: 500 "Object worker exited without a result (exit code 0)" and nothing |
| 5 | in the log. |
| 6 | """ |
| 7 | |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | import pytest |
| 11 | |
| 12 | import object_server |
| 13 | import object_worker_pool |
| 14 | from tests.test_object_server import TEST_ADMIN_TOKEN, auth_headers, request as http |
| 15 | |
| 16 | |
| 17 | # pooled: the worker encodes its reply as JSON. spawned (a timeout, no pool) |
| 18 | # and in_process (the default here): the value reaches the server whole and |
| 19 | # the server's own JSON answer could not hold it -- an unhandled TypeError. |
| 20 | @pytest.fixture(params=["pooled", "spawned", "in_process"]) |
| 21 | def box(tmp_path, monkeypatch, request): |
| 22 | root = tmp_path / "objects" |
| 23 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(root)) |
| 24 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(tmp_path / "data")) |
| 25 | monkeypatch.setenv("DBBASIC_ADMIN_TOKEN", TEST_ADMIN_TOKEN) |
| 26 | monkeypatch.delenv(object_server.OBJECT_TIMEOUT_SECONDS_ENV, raising=False) |
| 27 | monkeypatch.delenv(object_worker_pool.WORKER_POOL_SIZE_ENV, raising=False) |
| 28 | if request.param != "in_process": |
| 29 | monkeypatch.setenv(object_server.OBJECT_TIMEOUT_SECONDS_ENV, "10") |
| 30 | if request.param == "pooled": |
| 31 | monkeypatch.setenv(object_worker_pool.WORKER_POOL_SIZE_ENV, "1") |
| 32 | monkeypatch.delenv(object_server.TRUSTED_IN_PROCESS_OBJECTS_ENV, raising=False) |
| 33 | (root / "demo").mkdir(parents=True) |
| 34 | return root |
| 35 | |
| 36 | |
| 37 | def _run(box, name, source): |
| 38 | (box / "demo" / f"{name}.py").write_text(source) |
| 39 | return http(f"/objects/demo_{name}", headers=auth_headers()) |
| 40 | |
| 41 | |
| 42 | @pytest.mark.parametrize( |
| 43 | "returned, said", |
| 44 | [ |
| 45 | ('{"body": b"\\x89PNG-secret-bytes"}', "bytes at body"), |
| 46 | ('{"rows": [{"id": 1}, {"tags": {"a-secret-tag"}}]}', "set at rows[1].tags"), |
| 47 | ('{"a", "b"}', "set at the top level"), |
| 48 | ('{"k": {("x", 1): "v"}}', "tuple key at k"), |
| 49 | ], |
| 50 | ) |
| 51 | def test_the_error_names_the_type_and_where(box, capfd, returned, said): |
| 52 | status, _, payload = _run(box, "bad", f"def GET(request):\n return {returned}\n") |
| 53 | assert status == 500, payload |
| 54 | assert payload["error"] == f"Execution failed: object returned a value that is not JSON: {said}", payload |
| 55 | assert "exited without a result" not in str(payload) |
| 56 | # One log line: the object id and the type, never the value. |
| 57 | lines = [line for line in capfd.readouterr().err.splitlines() if "not JSON" in line] |
| 58 | assert len(lines) == 1, lines |
| 59 | assert "'demo_bad'" in lines[0] and said.split()[0] in lines[0] |
| 60 | assert "secret" not in lines[0] and "secret" not in str(payload) |
| 61 | |
| 62 | |
| 63 | def test_a_value_that_refers_to_itself_is_an_error_too(box): |
| 64 | status, _, payload = _run( |
| 65 | box, "loop", "def GET(request):\n d = {}\n d['self'] = d\n return d\n" |
| 66 | ) |
| 67 | assert status == 500, payload |
| 68 | assert "object returned a value that is not JSON" in payload["error"], payload |
| 69 | |
| 70 | |
| 71 | def test_the_worker_survives_and_good_values_still_work(box): |
| 72 | assert _run(box, "bad", 'def GET(request):\n return {"body": b"x"}\n')[0] == 500 |
| 73 | status, _, payload = _run( |
| 74 | box, "good", 'def GET(request):\n return {"n": 1, "items": [1, "two", None], 3: "int key"}\n' |
| 75 | ) |
| 76 | assert status == 200, payload |
| 77 | assert payload == {"n": 1, "items": [1, "two", None], "3": "int key"} |
| 78 | |
| 79 | |
| 80 | def test_a_bytes_body_with_a_content_type_is_still_served(box, request): |
| 81 | """What is accepted has not changed: this never went through JSON.""" |
| 82 | if request.node.callspec.params["box"] == "pooled": |
| 83 | pytest.skip("bytes cannot cross the pool's JSON wire (unchanged)") |
| 84 | from tests.test_object_server import raw_request |
| 85 | |
| 86 | (box / "demo" / "img.py").write_text( |
| 87 | 'def GET(request):\n return {"content_type": "image/png", "body": b"\\x89PNG"}\n' |
| 88 | ) |
| 89 | status, _, body = raw_request("/objects/demo_img", headers=auth_headers()) |
| 90 | assert status == 200 and body == b"\x89PNG" |
| 91 | |
| 92 | |
| 93 | @pytest.mark.parametrize( |
| 94 | "returned, said", |
| 95 | [ |
| 96 | # Bytes cross the sandbox wire; the server's JSON answer names them. |
| 97 | ('{"body": b"\\x89PNG"}', "Execution failed: object returned a value that is not JSON: bytes at body"), |
| 98 | # The sandbox worker already refuses a set with its own message. |
| 99 | ('{"tags": {"a"}}', "Execution failed: Object result can't be returned: " |
| 100 | "Object of type set is not JSON serializable"), |
| 101 | ], |
| 102 | ) |
| 103 | def test_a_sandboxed_user_object(tmp_path, monkeypatch, returned, said): |
| 104 | root = tmp_path / "objects" |
| 105 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(root)) |
| 106 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(tmp_path / "data")) |
| 107 | monkeypatch.setenv("DBBASIC_ADMIN_TOKEN", TEST_ADMIN_TOKEN) |
| 108 | (root / "users" / "1").mkdir(parents=True) |
| 109 | (root / "users" / "1" / "bad.py").write_text(f"def GET(request):\n return {returned}\n") |
| 110 | status, _, payload = http("/objects/u_1_bad", headers=auth_headers()) |
| 111 | assert status == 500, payload |
| 112 | assert payload["error"] == said, payload |