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_user_quotas.py · 6.3 KB · 168 lines
| 1 | """Per-user quotas: objects, state bytes, file bytes. |
| 2 | |
| 3 | The sandbox simulation stored 30 MiB of state and created 60 objects as one |
| 4 | customer without a single refusal. Each limit is tested at the limit (allowed) |
| 5 | and one past it (refused with a message naming the env var), and through the |
| 6 | paths a customer actually uses: POST /objects, the state helper and the |
| 7 | ``_files`` helper (through the sandbox proxy, under unsafe-dev), plus the |
| 8 | direct managers. |
| 9 | """ |
| 10 | from __future__ import annotations |
| 11 | |
| 12 | import json |
| 13 | |
| 14 | import pytest |
| 15 | |
| 16 | import object_files |
| 17 | import object_sandbox |
| 18 | import object_server |
| 19 | import object_state |
| 20 | import object_user_quotas as quotas |
| 21 | from runtime_modes import configure, run_object, write_object |
| 22 | from test_object_server import ( |
| 23 | create_identity_session, |
| 24 | enable_admin_token, |
| 25 | request, |
| 26 | session_headers, |
| 27 | ) |
| 28 | |
| 29 | |
| 30 | @pytest.fixture(autouse=True) |
| 31 | def _cleanup(): |
| 32 | yield |
| 33 | object_sandbox.shutdown() |
| 34 | |
| 35 | |
| 36 | @pytest.fixture |
| 37 | def box(tmp_path, monkeypatch): |
| 38 | data_dir, root = configure(monkeypatch, tmp_path) |
| 39 | enable_admin_token(monkeypatch) |
| 40 | monkeypatch.setenv(object_server.USER_SOURCE_WRITES_ENV, "true") |
| 41 | monkeypatch.setenv(object_sandbox.ISOLATION_ENV, object_sandbox.UNSAFE_DEV) |
| 42 | token, _ = create_identity_session({"user_id": "5"}) |
| 43 | return data_dir, root, session_headers(token) |
| 44 | |
| 45 | |
| 46 | def _create(headers, name): |
| 47 | return request( |
| 48 | "/objects", |
| 49 | method="POST", |
| 50 | body=json.dumps({"name": name, "code": "def GET(request):\n return {}\n"}).encode(), |
| 51 | headers=headers, |
| 52 | ) |
| 53 | |
| 54 | |
| 55 | # -- object count ---------------------------------------------------------------- |
| 56 | |
| 57 | |
| 58 | def test_object_count_allows_up_to_the_limit_then_refuses(box, monkeypatch): |
| 59 | _data_dir, _root, headers = box |
| 60 | monkeypatch.setenv(quotas.MAX_OBJECTS_ENV, "3") |
| 61 | for i in range(3): |
| 62 | status, _, payload = _create(headers, f"o{i}") |
| 63 | assert status == 201, payload |
| 64 | status, _, payload = _create(headers, "o3") |
| 65 | assert status == 403 |
| 66 | assert quotas.MAX_OBJECTS_ENV in payload["error"] and "limit is 3" in payload["error"] |
| 67 | |
| 68 | |
| 69 | def test_object_count_is_per_user_and_zero_means_unlimited(box, monkeypatch): |
| 70 | _data_dir, _root, headers = box |
| 71 | monkeypatch.setenv(quotas.MAX_OBJECTS_ENV, "1") |
| 72 | assert _create(headers, "a")[0] == 201 |
| 73 | assert _create(headers, "b")[0] == 403 |
| 74 | other, _ = create_identity_session({"user_id": "6"}) |
| 75 | assert _create(session_headers(other), "a")[0] == 201 |
| 76 | monkeypatch.setenv(quotas.MAX_OBJECTS_ENV, "0") |
| 77 | assert _create(headers, "b")[0] == 201 |
| 78 | |
| 79 | |
| 80 | # -- state bytes ----------------------------------------------------------------- |
| 81 | |
| 82 | |
| 83 | def test_state_manager_refuses_a_user_object_past_the_limit(tmp_path, monkeypatch): |
| 84 | monkeypatch.setenv(quotas.MAX_STATE_BYTES_ENV, "2000") |
| 85 | mgr = object_state.ObjectStateManager("u_5_x", tmp_path) |
| 86 | mgr.set("k", "a" * 500) |
| 87 | with pytest.raises(quotas.UserQuotaExceededError) as err: |
| 88 | mgr.set("big", "b" * 2000) |
| 89 | assert quotas.MAX_STATE_BYTES_ENV in str(err.value) |
| 90 | assert object_state.get_object_state("u_5_x", tmp_path) == {"k": "a" * 500} |
| 91 | |
| 92 | |
| 93 | def test_state_file_exactly_at_the_limit_is_allowed_one_byte_over_is_not(tmp_path, monkeypatch): |
| 94 | # fixed timestamp: the row is "k\t<value>\t1.0\n", so the size is exact |
| 95 | state_file = object_state.object_state_file("u_5_x", tmp_path) |
| 96 | exact = len("k\t\t1.0\n") + 1000 |
| 97 | monkeypatch.setenv(quotas.MAX_STATE_BYTES_ENV, str(exact)) |
| 98 | object_state._write_object_state(state_file, {"k": "a" * 1000}, timestamp=1.0) |
| 99 | assert state_file.stat().st_size == exact |
| 100 | state_file.unlink() |
| 101 | with pytest.raises(quotas.UserQuotaExceededError): |
| 102 | object_state._write_object_state(state_file, {"k": "a" * 1001}, timestamp=1.0) |
| 103 | assert not state_file.exists() |
| 104 | |
| 105 | |
| 106 | def test_state_limit_never_applies_to_system_objects(tmp_path, monkeypatch): |
| 107 | monkeypatch.setenv(quotas.MAX_STATE_BYTES_ENV, "100") |
| 108 | object_state.ObjectStateManager("tools_big", tmp_path).set("k", "a" * 10_000) |
| 109 | |
| 110 | |
| 111 | def test_over_limit_user_can_still_delete_keys_to_recover(tmp_path, monkeypatch): |
| 112 | mgr = object_state.ObjectStateManager("u_5_x", tmp_path) |
| 113 | mgr.set("a", "x" * 1500) |
| 114 | mgr.set("b", "y" * 1500) |
| 115 | monkeypatch.setenv(quotas.MAX_STATE_BYTES_ENV, "1000") |
| 116 | mgr.delete("a") # still over, but smaller: allowed |
| 117 | |
| 118 | |
| 119 | STATE_WRITER = """ |
| 120 | def POST(request): |
| 121 | _state_manager.set("blob", "z" * int(request["size"])) |
| 122 | return {"stored": True} |
| 123 | """ |
| 124 | |
| 125 | |
| 126 | def test_state_quota_applies_through_the_sandbox_proxy(box, monkeypatch): |
| 127 | _data_dir, root, _headers = box |
| 128 | monkeypatch.setenv(quotas.MAX_STATE_BYTES_ENV, "4000") |
| 129 | write_object(root, "u_5_w", STATE_WRITER) |
| 130 | ok = run_object("in_process", root, "u_5_w", "POST", {"size": 1000}) |
| 131 | assert ok.ok, ok.error |
| 132 | over = run_object("in_process", root, "u_5_w", "POST", {"size": 5000}) |
| 133 | assert not over.ok |
| 134 | assert quotas.MAX_STATE_BYTES_ENV in str(over.error) |
| 135 | |
| 136 | |
| 137 | # -- file bytes ------------------------------------------------------------------ |
| 138 | |
| 139 | |
| 140 | def test_files_quota_counts_all_of_a_users_objects(tmp_path, monkeypatch): |
| 141 | monkeypatch.setenv(quotas.MAX_FILES_BYTES_ENV, "1000") |
| 142 | object_files.write_object_file("u_5_a", "one.bin", b"x" * 600, tmp_path) |
| 143 | object_files.write_object_file("u_5_b", "two.bin", b"x" * 400, tmp_path) # exactly 1000 |
| 144 | with pytest.raises(object_files.ObjectFileTooLargeError) as err: |
| 145 | object_files.write_object_file("u_5_a", "three.bin", b"x", tmp_path) |
| 146 | assert quotas.MAX_FILES_BYTES_ENV in str(err.value) |
| 147 | # replacing a file counts the difference, not the sum |
| 148 | object_files.write_object_file("u_5_a", "one.bin", b"y" * 600, tmp_path, overwrite=True) |
| 149 | # another user and system objects are unaffected |
| 150 | object_files.write_object_file("u_6_a", "one.bin", b"x" * 1000, tmp_path) |
| 151 | object_files.write_object_file("tools_t", "big.bin", b"x" * 5000, tmp_path) |
| 152 | |
| 153 | |
| 154 | FILE_WRITER = """ |
| 155 | def POST(request): |
| 156 | _files.put("f.bin", b"q" * int(request["size"])) |
| 157 | return {"stored": True} |
| 158 | """ |
| 159 | |
| 160 | |
| 161 | def test_files_quota_applies_through_the_sandbox_proxy(box, monkeypatch): |
| 162 | _data_dir, root, _headers = box |
| 163 | monkeypatch.setenv(quotas.MAX_FILES_BYTES_ENV, "2000") |
| 164 | write_object(root, "u_5_f", FILE_WRITER) |
| 165 | assert run_object("in_process", root, "u_5_f", "POST", {"size": 2000}).ok |
| 166 | over = run_object("in_process", root, "u_5_f", "POST", {"size": 2001}) |
| 167 | assert not over.ok |
| 168 | assert quotas.MAX_FILES_BYTES_ENV in str(over.error) |