Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/tests/test_pre_write_hooks.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_pre_write_hooks.py · 12.7 KB · 373 lines
1"""Pre-write hooks (plan/pre-write-hook-spec.md): a collection declares
2`hooks: {"before_write": "<object_id>"}` and that object runs synchronously
3inside the generic HTTP write path -- after permission checks, before persist.
4It can reject (4xx with its own message) or transform the record. Fail CLOSED:
5a declared hook that is missing, raises, or returns a non-contract shape
6rejects the write. Trusted server-side writes (object_records called directly)
7bypass hooks deliberately.
8
9This is the escape hatch for logic the schema can't express (cross-field /
10cross-collection validation) that keeps the generative form working -- the
11form still POSTs to /collections/{c}/records; the hook lives server-side.
12"""
13
14import json
15
16import object_records
17import object_server
18from test_object_server import (
19 TEST_ADMIN_TOKEN,
20 enable_admin_token,
21 request,
22 write_records,
23)
24
25AUTH = [("authorization", f"Token {TEST_ADMIN_TOKEN}")]
26
27SCHEMA = {
28 "name": "gadgets",
29 "version": 1,
30 "fields": [
31 {"name": "id"},
32 {"name": "name", "type": "text", "required": True},
33 {"name": "qty", "type": "integer"},
34 {"name": "created_at", "type": "datetime", "read_only": True},
35 {"name": "owner_id", "type": "text"},
36 ],
37}
38
39
40def setup_env(tmp_path, monkeypatch, hook_source=None, declare_hook=True):
41 data_dir = tmp_path / "data"
42 objects_root = tmp_path / "objects"
43 objects_root.mkdir(parents=True, exist_ok=True)
44 write_records(data_dir, "gadgets", "id\tname\tqty\tcreated_at\towner_id\n")
45 schema = dict(SCHEMA)
46 if declare_hook:
47 schema["hooks"] = {"before_write": "hook_gadgets"}
48 schema_dir = data_dir / "schemas"
49 schema_dir.mkdir(parents=True, exist_ok=True)
50 (schema_dir / "gadgets.json").write_text(json.dumps(schema))
51 if hook_source is not None:
52 hook_dir = objects_root / "hook"
53 hook_dir.mkdir(parents=True, exist_ok=True)
54 (hook_dir / "gadgets.py").write_text(hook_source)
55 monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir))
56 monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(objects_root))
57 enable_admin_token(monkeypatch)
58 return data_dir
59
60
61def create_gadget(record):
62 return request(
63 "/collections/gadgets/records",
64 method="POST",
65 body=json.dumps(record).encode("utf-8"),
66 headers=AUTH,
67 )
68
69
70def list_gadgets():
71 status, _, payload = request("/collections/gadgets/records", headers=AUTH)
72 assert status == 200
73 return payload["records"]
74
75
76def test_collection_without_hook_writes_unchanged(tmp_path, monkeypatch):
77 setup_env(tmp_path, monkeypatch, declare_hook=False)
78 status, _, payload = create_gadget({"id": "g1", "name": "widget", "qty": "3"})
79 assert status in (200, 201), payload
80 assert [r["id"] for r in list_gadgets()] == ["g1"]
81
82
83def test_hook_rejects_with_its_own_message_and_status(tmp_path, monkeypatch):
84 setup_env(
85 tmp_path,
86 monkeypatch,
87 hook_source=(
88 "def BEFORE_WRITE(request):\n"
89 " if int(request['record'].get('qty') or 0) > 100:\n"
90 " return {'error': 'Quantity over 100 needs approval', 'status': 422}\n"
91 " return None\n"
92 ),
93 )
94 status, _, payload = create_gadget({"id": "g1", "name": "widget", "qty": "500"})
95 assert status == 422
96 assert payload["code"] == "hook_rejected"
97 assert payload["error"] == "Quantity over 100 needs approval"
98 assert list_gadgets() == []
99
100 # Under the limit passes through the same hook.
101 status, _, _ = create_gadget({"id": "g2", "name": "widget", "qty": "5"})
102 assert status in (200, 201)
103 assert [r["id"] for r in list_gadgets()] == ["g2"]
104
105
106def test_hook_reject_status_is_clamped_to_4xx(tmp_path, monkeypatch):
107 setup_env(
108 tmp_path,
109 monkeypatch,
110 hook_source=(
111 "def BEFORE_WRITE(request):\n"
112 " return {'error': 'no', 'status': 200}\n"
113 ),
114 )
115 status, _, payload = create_gadget({"id": "g1", "name": "widget", "qty": "1"})
116 assert status == 400
117 assert payload["code"] == "hook_rejected"
118
119
120def test_hook_transform_persists_and_still_schema_validates(tmp_path, monkeypatch):
121 setup_env(
122 tmp_path,
123 monkeypatch,
124 hook_source=(
125 "def BEFORE_WRITE(request):\n"
126 " record = dict(request['record'])\n"
127 " record['name'] = record.get('name', '').upper()\n"
128 " if record.get('qty') == '13':\n"
129 " record['qty'] = 'thirteen' # invalid for integer field\n"
130 " return {'record': record}\n"
131 ),
132 )
133 status, _, _ = create_gadget({"id": "g1", "name": "widget", "qty": "2"})
134 assert status in (200, 201)
135 assert list_gadgets()[0]["name"] == "WIDGET"
136
137 # A transform that violates the schema is still caught by validation.
138 status, _, payload = create_gadget({"id": "g2", "name": "widget", "qty": "13"})
139 assert status == 400
140 assert "qty" in payload["error"]
141
142
143def test_hook_cannot_touch_id_owner_or_read_only_fields(tmp_path, monkeypatch):
144 setup_env(
145 tmp_path,
146 monkeypatch,
147 hook_source=(
148 "def BEFORE_WRITE(request):\n"
149 " record = dict(request['record'])\n"
150 " record['id'] = 'hijacked'\n"
151 " record['owner_id'] = 'attacker'\n"
152 " record['created_at'] = '1999-01-01T00:00:00Z'\n"
153 " return {'record': record}\n"
154 ),
155 )
156 status, _, _ = create_gadget({"id": "g1", "name": "widget", "qty": "1", "owner_id": "dan"})
157 assert status in (200, 201)
158 rows = list_gadgets()
159 assert [r["id"] for r in rows] == ["g1"]
160 assert rows[0]["owner_id"] == "dan"
161 assert rows[0]["created_at"] != "1999-01-01T00:00:00Z"
162
163
164def test_declared_but_missing_or_raising_hook_fails_closed(tmp_path, monkeypatch):
165 # Declared, object file absent -> reject, nothing persisted.
166 setup_env(tmp_path, monkeypatch, hook_source=None)
167 status, _, payload = create_gadget({"id": "g1", "name": "widget", "qty": "1"})
168 assert status == 500
169 assert payload["code"] == "hook_failed"
170 assert list_gadgets() == []
171
172
173def test_raising_hook_fails_closed(tmp_path, monkeypatch):
174 setup_env(
175 tmp_path,
176 monkeypatch,
177 hook_source="def BEFORE_WRITE(request):\n raise RuntimeError('boom')\n",
178 )
179 status, _, payload = create_gadget({"id": "g1", "name": "widget", "qty": "1"})
180 assert status == 500
181 assert payload["code"] == "hook_failed"
182 assert list_gadgets() == []
183
184
185def test_non_contract_return_fails_closed(tmp_path, monkeypatch):
186 setup_env(
187 tmp_path,
188 monkeypatch,
189 hook_source="def BEFORE_WRITE(request):\n return 'nope'\n",
190 )
191 status, _, payload = create_gadget({"id": "g1", "name": "widget", "qty": "1"})
192 assert status == 500
193 assert payload["code"] == "hook_failed"
194
195
196def test_denied_write_never_reaches_the_hook(tmp_path, monkeypatch):
197 marker = tmp_path / "hook-ran"
198 setup_env(
199 tmp_path,
200 monkeypatch,
201 hook_source=(
202 "def BEFORE_WRITE(request):\n"
203 f" open({str(marker)!r}, 'w').write('ran')\n"
204 " return None\n"
205 ),
206 )
207 # No admin token header -> the write is denied before any hook runs.
208 status, _, _ = request(
209 "/collections/gadgets/records",
210 method="POST",
211 body=json.dumps({"id": "g1", "name": "widget"}).encode("utf-8"),
212 )
213 assert status in (401, 403)
214 assert not marker.exists()
215
216
217def test_trusted_server_side_writes_bypass_hooks(tmp_path, monkeypatch):
218 marker = tmp_path / "hook-ran"
219 data_dir = setup_env(
220 tmp_path,
221 monkeypatch,
222 hook_source=(
223 "def BEFORE_WRITE(request):\n"
224 f" open({str(marker)!r}, 'w').write('ran')\n"
225 " return {'error': 'would reject'}\n"
226 ),
227 )
228 stored = object_records.create_collection_record(
229 "gadgets",
230 {"id": "sys1", "name": "seeded", "qty": "1"},
231 base_dir=data_dir,
232 )
233 assert stored["id"] == "sys1"
234 assert not marker.exists()
235
236
237def test_update_path_runs_hook_with_existing_and_action(tmp_path, monkeypatch):
238 setup_env(
239 tmp_path,
240 monkeypatch,
241 hook_source=(
242 "def BEFORE_WRITE(request):\n"
243 " if request['action'] != 'update':\n"
244 " return None\n"
245 " before = int(request['existing'].get('qty') or 0)\n"
246 " after = int(request['record'].get('qty') or 0)\n"
247 " if after < before:\n"
248 " return {'error': 'Quantity can only grow', 'status': 409}\n"
249 " record = dict(request['record'])\n"
250 " record['name'] = record.get('name', '') + '-checked'\n"
251 " return {'record': record}\n"
252 ),
253 )
254 status, _, _ = create_gadget({"id": "g1", "name": "widget", "qty": "5"})
255 assert status in (200, 201)
256
257 # Shrinking qty is rejected by the hook with its own status.
258 status, _, payload = request(
259 "/collections/gadgets/records/g1",
260 method="PUT",
261 body=json.dumps({"qty": "2"}).encode("utf-8"),
262 headers=AUTH,
263 )
264 assert status == 409
265 assert payload["code"] == "hook_rejected"
266 assert list_gadgets()[0]["qty"] == "5"
267
268 # Growing qty passes and the hook's transform lands on the stored row.
269 status, _, _ = request(
270 "/collections/gadgets/records/g1",
271 method="PUT",
272 body=json.dumps({"qty": "9"}).encode("utf-8"),
273 headers=AUTH,
274 )
275 assert status == 200
276 row = list_gadgets()[0]
277 assert row["qty"] == "9"
278 assert row["name"] == "widget-checked"
279
280
281# ---------------------------------------------------------------------
282# First adopter: fin_journals balance enforcement (packages/app-finance).
283# Cross-collection validation the schema can't express -- the canonical
284# reason this primitive exists.
285# ---------------------------------------------------------------------
286
287import pathlib
288
289REPO_ROOT = pathlib.Path(__file__).resolve().parents[1]
290FINANCE = REPO_ROOT / "packages" / "app-finance"
291
292
293def setup_finance(tmp_path, monkeypatch):
294 data_dir = tmp_path / "data"
295 write_records(
296 data_dir,
297 "fin_journals",
298 "id\tdate\tdescription\tstatus\towner_id\tcreated_at\n"
299 "j1\t2026-07-01\tOpening entry\tdraft\tadmin\t\n",
300 )
301 write_records(
302 data_dir,
303 "fin_journal_lines",
304 "id\tjournal_id\taccount_id\tdebit_cents\tcredit_cents\towner_id\n",
305 )
306 # account_id is a validated relation -> the target record must exist.
307 write_records(data_dir, "fin_accounts", "id\tname\na1\tCash\n")
308 schema_dir = data_dir / "schemas"
309 schema_dir.mkdir(parents=True, exist_ok=True)
310 for name in ("fin_journals", "fin_journal_lines"):
311 (schema_dir / f"{name}.json").write_text(
312 (FINANCE / "schemas" / f"{name}.json").read_text()
313 )
314 monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir))
315 monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(FINANCE / "objects"))
316 enable_admin_token(monkeypatch)
317 return data_dir
318
319
320def post_journal():
321 return request(
322 "/collections/fin_journals/records/j1",
323 method="PUT",
324 body=json.dumps({"status": "posted"}).encode("utf-8"),
325 headers=AUTH,
326 )
327
328
329def add_line(data_dir, line_id, debit, credit):
330 object_records.create_collection_record(
331 "fin_journal_lines",
332 {"id": line_id, "journal_id": "j1", "account_id": "a1",
333 "debit_cents": str(debit), "credit_cents": str(credit)},
334 base_dir=data_dir,
335 )
336
337
338def test_fin_journal_cannot_post_empty(tmp_path, monkeypatch):
339 setup_finance(tmp_path, monkeypatch)
340 status, _, payload = post_journal()
341 assert status == 409
342 assert payload["code"] == "hook_rejected"
343 assert "empty journal" in payload["error"]
344
345
346def test_fin_journal_cannot_post_unbalanced_but_posts_balanced(tmp_path, monkeypatch):
347 data_dir = setup_finance(tmp_path, monkeypatch)
348 add_line(data_dir, "l1", 1000, 0)
349 add_line(data_dir, "l2", 0, 900)
350
351 status, _, payload = post_journal()
352 assert status == 409
353 assert "debits 1000 != credits 900" in payload["error"]
354 # still draft
355 status, _, row = request("/collections/fin_journals/records/j1", headers=AUTH)
356 assert row["record"]["status"] == "draft"
357
358 add_line(data_dir, "l3", 0, 100)
359 status, _, _ = post_journal()
360 assert status == 200
361 status, _, row = request("/collections/fin_journals/records/j1", headers=AUTH)
362 assert row["record"]["status"] == "posted"
363
364
365def test_fin_journal_draft_edits_pass_the_hook_untouched(tmp_path, monkeypatch):
366 setup_finance(tmp_path, monkeypatch)
367 status, _, _ = request(
368 "/collections/fin_journals/records/j1",
369 method="PUT",
370 body=json.dumps({"description": "renamed"}).encode("utf-8"),
371 headers=AUTH,
372 )
373 assert status == 200