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

9 downloads

dbbasic-object-server/tests/test_money_gates_journals.py
Install on DigitalOcean 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.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 19 minutes ago History
test_money_gates_journals.py · 13.5 KB · 277 lines
1"""Posted journals, closed periods and their lines: what is final, and that
2ordinary bookkeeping and the machine composers still work.
3
4tests/test_findings_money_gates.py holds the reproductions. This file is
5the rest of the same change:
6
7 * `fin_journals` declares `locked_when` on `status: posted` -- nobody,
8 the admin token included, edits or deletes a posted journal;
9 * hook_fin_journals refuses the delete of a journal dated in a closed
10 period (it already refused the edit), posted or not;
11 * hook_fin_journal_lines refuses a create, update or delete that touches
12 a posted journal or one in a closed period, checking the journal a
13 line LEAVES as well as the one it joins;
14 * a draft in an open period is still freely composed, and
15 object_finance.compose_posted_journal (every booking handler) still
16 drafts, writes lines and posts.
17"""
18
19import json
20
21import pytest
22
23import object_finance
24import object_records
25from test_findings_money_gates import ( # noqa: F401 (fresh_handler_index is autouse)
26 PACKAGES, books_site, closed_year, failing_reads, fresh_handler_index, journal, must, ok,
27 totals,
28)
29
30
31def refused(result, *, code, status=409):
32 got, body = result
33 assert got == status and body.get("code") == code, (got, body)
34 return body
35
36
37def test_the_schema_declares_posted_final_and_terminal():
38 """`status` is deliberately NOT in `except`: with it there, the
39 transitions map would decide whether posted can be left, and a later
40 edit to that map would quietly reopen every posted journal. The one
41 exception is period_review, the review flag a composer sets on a
42 journal it had to book after its period closed: marking it reviewed
43 changes nothing about the money, and it must be possible on the
44 posted journal it is about."""
45 schema = json.loads((PACKAGES / "app-finance" / "schemas" / "fin_journals.json").read_text())
46 assert schema["locked_when"] == [{
47 "when": {"status": ["posted"]}, "except": ["period_review"],
48 "message": schema["locked_when"][0]["message"]}]
49 assert "reversing journal" in schema["locked_when"][0]["message"]
50
51
52# --- the journal ----------------------------------------------------------------
53
54
55def test_a_draft_journal_in_an_open_period_is_still_freely_edited_and_deleted(tmp_path, monkeypatch):
56 site = books_site(tmp_path, monkeypatch)
57 journal(site, "j1", "2026-03-10", post=False)
58
59 ok(site.put("fin_journals", "j1", "ann", {"date": "2026-03-11", "description": "resale"}),
60 "edit the draft")
61 ok(site.put("fin_journal_lines", "j1-dr", "ann", {"debit_cents": "12000"}), "edit a line")
62 ok(site.put("fin_journal_lines", "j1-cr", "ann", {"credit_cents": "12000"}), "edit the other")
63 ok(site.post("fin_journal_lines", "ann", {"id": "j1-x", "journal_id": "j1",
64 "account_id": "cash", "debit_cents": "5"}), "add")
65 ok(site.delete("fin_journal_lines", "j1-x", "ann"), "delete a line")
66 ok(site.put("fin_journals", "j1", "ann", {"status": "posted"}), "post")
67 assert totals(site, "j1") == (12000, 12000)
68
69 journal(site, "j2", "2026-03-12", post=False)
70 ok(site.delete("fin_journal_lines", "j2-dr", "ann"), "delete a draft's line")
71 ok(site.delete("fin_journal_lines", "j2-cr", "ann"), "and the other")
72 ok(site.delete("fin_journals", "j2", "ann"), "delete the draft")
73 assert not site.exists("fin_journals", "j2")
74
75
76def test_nobody_edits_or_deletes_a_posted_journal_not_even_the_admin_token(tmp_path, monkeypatch):
77 """locked_when is about the record, not the caller: there is no bypass,
78 which is the difference between it and the hooks around it."""
79 site = books_site(tmp_path, monkeypatch)
80 journal(site, "j1", "2026-03-10")
81 before = site.get("fin_journals", "j1")
82
83 for who in ("ann", "admin"):
84 body = refused(site.put("fin_journals", "j1", who, {"reference": "tidy"}),
85 code="record_locked")
86 assert body["fields"] == ["reference"] and "reversing journal" in body["error"]
87 refused(site.delete("fin_journals", "j1", who), code="record_locked")
88 # Un-posting is refused twice over: by the transitions map, which
89 # answers first (400), and -- were that map ever loosened -- by the
90 # lock, because `status` is not one of its `except` fields (pinned by
91 # the schema test above).
92 status, body = site.put("fin_journals", "j1", "admin", {"status": "draft"})
93 assert status in (400, 409), (status, body)
94
95 assert site.get("fin_journals", "j1") == before
96
97
98def test_a_storage_level_writer_cannot_touch_a_posted_journal_either(tmp_path, monkeypatch):
99 site = books_site(tmp_path, monkeypatch)
100 journal(site, "j1", "2026-03-10")
101
102 with pytest.raises(object_records.RecordLockedError):
103 object_records.update_collection_record("fin_journals", "j1", {"date": "2026-01-05"},
104 base_dir=site.data, actor="some_handler")
105 with pytest.raises(object_records.RecordLockedError):
106 object_records.delete_collection_record("fin_journals", "j1", base_dir=site.data,
107 actor="some_handler")
108 assert site.get("fin_journals", "j1")["date"] == "2026-03-10"
109
110
111def test_a_posted_journals_rollups_still_follow_its_lines(tmp_path, monkeypatch):
112 """Computed fields are never frozen: the totals on a posted journal are
113 recomputed by the server when a trusted writer adds a line, which is
114 not somebody editing settled data. (The hook is what stops a person
115 adding that line.)"""
116 site = books_site(tmp_path, monkeypatch)
117 journal(site, "j1", "2026-03-10")
118 assert site.get("fin_journals", "j1")["debit_total_cents"] == "10000"
119
120 for line_id, side in (("extra-dr", "debit_cents"), ("extra-cr", "credit_cents")):
121 object_records.create_collection_record(
122 "fin_journal_lines", {"id": line_id, "journal_id": "j1", side: "500",
123 "account_id": "cash", "owner_id": "ann"},
124 base_dir=site.data, actor="some_composer")
125
126 stored = site.get("fin_journals", "j1")
127 assert (stored["debit_total_cents"], stored["credit_total_cents"]) == ("10500", "10500")
128
129
130def test_a_draft_journal_in_a_closed_period_cannot_be_deleted(tmp_path, monkeypatch):
131 """The posted journal in the closed year is covered twice (the lock and
132 the hook). A DRAFT dated in a closed year is covered only by the hook,
133 and removing it changes what the filed year's journal list says just
134 the same."""
135 site = books_site(tmp_path, monkeypatch)
136 journal(site, "d2025", "2025-06-10", post=False)
137 ok(site.post("fin_closed_periods", "ann", {"id": "fy2025", "start_date": "2025-01-01",
138 "end_date": "2025-12-31",
139 "reason": "FY2025 filed"}), "close FY2025")
140
141 body = refused(site.delete("fin_journals", "d2025", "ann"), code="hook_rejected")
142 assert "are closed (FY2025 filed)" in body["error"]
143 refused(site.delete("fin_journal_lines", "d2025-dr", "ann"), code="hook_rejected")
144 refused(site.delete("fin_journals", "d2025", "admin"), code="hook_rejected")
145 assert site.exists("fin_journals", "d2025") and totals(site, "d2025") == (10000, 10000)
146
147
148def test_reopening_the_period_is_still_the_deliberate_way_back_in(tmp_path, monkeypatch):
149 """Deleting the fin_closed_periods row is the documented reopening act.
150 It reopens DRAFTS in the period; a posted journal stays final."""
151 site = books_site(tmp_path, monkeypatch)
152 journal(site, "d2025", "2025-06-10", post=False)
153 closed_year(site)
154
155 ok(site.delete("fin_closed_periods", "fy2025", "ann"), "reopen FY2025")
156
157 ok(site.put("fin_journal_lines", "d2025-dr", "ann", {"memo": "now editable"}), "draft line")
158 refused(site.put("fin_journal_lines", "j2025-dr", "ann", {"debit_cents": "1"}),
159 code="hook_rejected")
160 refused(site.delete("fin_journals", "j2025", "ann"), code="record_locked")
161
162
163# --- the lines --------------------------------------------------------------------
164
165
166def test_a_line_cannot_be_moved_into_a_posted_journal(tmp_path, monkeypatch):
167 """The mirror of the reproduction: moving a line OUT of a settled
168 journal is refused because of the journal it leaves, moving one IN
169 because of the journal it joins."""
170 site = books_site(tmp_path, monkeypatch)
171 journal(site, "posted", "2026-03-10")
172 journal(site, "draft", "2026-03-11", post=False)
173
174 body = refused(site.put("fin_journal_lines", "draft-dr", "ann", {"journal_id": "posted"}),
175 code="hook_rejected")
176
177 assert "Journal posted is posted" in body["error"]
178 assert totals(site, "posted") == (10000, 10000) and totals(site, "draft") == (10000, 10000)
179
180
181def test_a_line_moves_freely_between_two_open_drafts(tmp_path, monkeypatch):
182 site = books_site(tmp_path, monkeypatch)
183 journal(site, "a", "2026-03-10", post=False)
184 journal(site, "b", "2026-03-11", post=False)
185
186 ok(site.put("fin_journal_lines", "a-dr", "ann", {"journal_id": "b"}), "move between drafts")
187
188 assert totals(site, "a") == (0, 10000) and totals(site, "b") == (20000, 10000)
189
190
191def test_an_orphan_line_can_be_cleaned_up(tmp_path, monkeypatch):
192 """A line whose journal no longer exists settles nothing. Before posted
193 journals were locked, deleting one left exactly these behind."""
194 site = books_site(tmp_path, monkeypatch)
195 journal(site, "j1", "2026-03-10", post=False)
196 ok(site.delete("fin_journals", "j1", "ann"), "delete the draft, leaving its lines")
197 must(len(site.rows("fin_journal_lines", journal_id="j1")) == 2, "expected orphans")
198
199 ok(site.delete("fin_journal_lines", "j1-dr", "ann"), "delete an orphan")
200 assert [row["id"] for row in site.rows("fin_journal_lines")] == ["j1-cr"]
201
202
203def test_the_admin_token_cannot_edit_a_posted_journals_lines(tmp_path, monkeypatch):
204 """Unlike payments, there is no admin repair path here: the correction
205 for posted books is a reversing journal for everybody."""
206 site = books_site(tmp_path, monkeypatch)
207 journal(site, "j1", "2026-03-10")
208
209 refused(site.put("fin_journal_lines", "j1-dr", "admin", {"debit_cents": "1"}),
210 code="hook_rejected")
211 refused(site.delete("fin_journal_lines", "j1-cr", "admin"), code="hook_rejected")
212 refused(site.post("fin_journal_lines", "admin", {
213 "id": "x", "journal_id": "j1", "account_id": "cash", "debit_cents": "7",
214 "owner_id": "ann"}), code="hook_rejected")
215 assert totals(site, "j1") == (10000, 10000)
216
217
218# --- the composers ----------------------------------------------------------------
219
220
221def test_the_machine_composer_still_drafts_writes_lines_and_posts(tmp_path, monkeypatch):
222 """Every booking handler (payments, stock, expenses, bank resolution)
223 goes through compose_posted_journal: a draft, its lines, then the move
224 to posted. The lock applies to the row as STORED, and the stored row is
225 a draft until that last write."""
226 site = books_site(tmp_path, monkeypatch)
227
228 result = object_finance.compose_posted_journal(
229 site.data, generated_from="payments/p1", date="2026-03-10", description="Payment p1",
230 lines=[{"account_id": "cash", "debit_cents": 2500, "credit_cents": 0},
231 {"account_id": "revenue", "debit_cents": 0, "credit_cents": 2500}],
232 owner_id="ann", actor="system_books")
233
234 assert result["ok"] and result["posted"] is True
235 stored = site.get("fin_journals", result["journal_id"])
236 assert (stored["status"], stored["debit_total_cents"], stored["credit_total_cents"]) == (
237 "posted", "2500", "2500")
238 again = object_finance.compose_posted_journal(
239 site.data, generated_from="payments/p1", date="2026-03-10", description="Payment p1",
240 lines=[{"account_id": "cash", "debit_cents": 2500, "credit_cents": 0},
241 {"account_id": "revenue", "debit_cents": 0, "credit_cents": 2500}],
242 owner_id="ann", actor="system_books")
243 assert "already composed" in again["skipped"]
244
245
246# --- reads fail closed --------------------------------------------------------------
247
248
249def unreadable(result):
250 """The hook answers 503 for a read it could not make, and the server
251 relays it (object_server._apply_before_write_hook), so the client sees
252 something to retry rather than a bad request."""
253 got, body = result
254 assert got == 503 and body.get("code") == "hook_rejected", (got, body)
255 assert "could not be read" in body["error"], body
256
257
258def test_the_period_lock_refuses_when_the_closed_periods_cannot_be_read(tmp_path, monkeypatch):
259 """object_finance.closed_period_for answered None -- "not closed" --
260 when its read failed, so the one gate on settled books opened exactly
261 when it could not see. Both hooks now ask strictly."""
262 site = books_site(tmp_path, monkeypatch)
263 journal(site, "d2025", "2025-06-10", post=False)
264 closed_year(site)
265
266 with failing_reads("fin_closed_periods", "object_finance.py") as fired:
267 unreadable(site.put("fin_journals", "d2025", "ann", {"description": "tidied"}))
268 unreadable(site.delete("fin_journals", "d2025", "ann"))
269 unreadable(site.put("fin_journal_lines", "d2025-dr", "ann", {"debit_cents": "1"}))
270 unreadable(site.delete("fin_journal_lines", "d2025-cr", "ann"))
271 unreadable(site.post("fin_journals", "ann", {"id": "new", "date": "2025-07-01",
272 "description": "backdated"}))
273 must(fired and set(fired) == {"closed_period_for"}, f"the failure fired in {fired}")
274
275 assert site.get("fin_journals", "d2025")["description"] == "sale"
276 assert totals(site, "d2025") == (10000, 10000)
277 assert not site.exists("fin_journals", "new")