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_money_gates_journal_markers.py · 5.6 KB · 112 lines
| 1 | """fin_journals.generated_from is the machine's: a person cannot plant, |
| 2 | change or clear the marker a composer uses to know it has booked a fact. |
| 3 | |
| 4 | object_finance.compose_posted_journal -- every booking handler, the |
| 5 | reversal action, the cash count -- scans fin_journals for its marker |
| 6 | across every owner and, finding it, answers "already composed". The |
| 7 | field is not read_only (the composers and object_materialize write it |
| 8 | through ordinary storage-level calls), and the permission rule lets any |
| 9 | signed-in user create journals of their own. So a user who typed |
| 10 | `payments/<id>` into a draft of their own made that payment's journal |
| 11 | unbookable for whoever owned it, and blanking the marker on a |
| 12 | machine-made draft let the next pass compose the same fact again. |
| 13 | hook_fin_journals now refuses both for anybody who is not an admin. |
| 14 | """ |
| 15 | |
| 16 | from test_findings_money_gates import ( # noqa: F401 (fresh_handler_index is autouse) |
| 17 | books_site, fresh_handler_index, must, ok, |
| 18 | ) |
| 19 | |
| 20 | import object_finance |
| 21 | |
| 22 | LINES = [{"account_id": "cash", "debit_cents": 10000}, |
| 23 | {"account_id": "revenue", "credit_cents": 10000}] |
| 24 | |
| 25 | |
| 26 | def refused(result, what): |
| 27 | status, body = result |
| 28 | assert status == 409 and "generated_from" in body.get("error", ""), (what, status, body) |
| 29 | |
| 30 | |
| 31 | def compose(site, marker, *, owner="ann", post=True): |
| 32 | return object_finance.compose_posted_journal( |
| 33 | site.data, generated_from=marker, date="2026-03-10", description="payment p1", |
| 34 | lines=LINES, owner_id=owner, actor="payments_books", post=post) |
| 35 | |
| 36 | |
| 37 | def test_a_planted_marker_does_not_stop_the_payment_being_booked(tmp_path, monkeypatch): |
| 38 | """Seen before the fix: POST answered 201, and the payment's composer |
| 39 | then answered "already composed: payments/p1" and booked nothing.""" |
| 40 | site = books_site(tmp_path, monkeypatch) |
| 41 | |
| 42 | refused(site.post("fin_journals", "ann", {"id": "plant", "date": "2026-03-10", |
| 43 | "description": "nothing", |
| 44 | "generated_from": "payments/p1"}), |
| 45 | "plant a payment's marker on a new draft") |
| 46 | assert not site.exists("fin_journals", "plant") |
| 47 | |
| 48 | result = compose(site, "payments/p1") |
| 49 | assert result.get("ok") and "skipped" not in result, result |
| 50 | (booked,) = site.rows("fin_journals", generated_from="payments/p1") |
| 51 | assert booked["status"] == "posted" and booked["owner_id"] == "ann" |
| 52 | |
| 53 | |
| 54 | def test_a_marker_cannot_be_added_to_a_journal_after_it_is_created(tmp_path, monkeypatch): |
| 55 | site = books_site(tmp_path, monkeypatch) |
| 56 | ok(site.post("fin_journals", "ann", {"id": "j1", "date": "2026-03-10", |
| 57 | "description": "typed by hand"}), "a hand-typed draft") |
| 58 | |
| 59 | refused(site.put("fin_journals", "j1", "ann", {"generated_from": "payments/p1"}), |
| 60 | "add a marker") |
| 61 | assert site.get("fin_journals", "j1")["generated_from"] == "" |
| 62 | assert "skipped" not in compose(site, "payments/p1") |
| 63 | |
| 64 | |
| 65 | def test_a_machine_drafts_marker_is_neither_changed_nor_cleared_but_the_draft_is_still_edited( |
| 66 | tmp_path, monkeypatch): |
| 67 | """A recurring template without auto_post leaves a draft for a person to |
| 68 | finish. They edit it as before -- a client that sends the whole row back |
| 69 | with the marker unchanged edits nothing about it -- and post it; they |
| 70 | cannot blank the marker (the next pass would compose it again) or |
| 71 | re-point it at another fact.""" |
| 72 | site = books_site(tmp_path, monkeypatch) |
| 73 | drafted = compose(site, "fin_recurring/r1:2026-03-01", post=False) |
| 74 | journal_id = drafted["journal_id"] |
| 75 | must(site.get("fin_journals", journal_id)["status"] == "draft", f"not a draft: {drafted}") |
| 76 | |
| 77 | for marker in ("", "payments/p1"): |
| 78 | refused(site.put("fin_journals", journal_id, "ann", {"generated_from": marker}), |
| 79 | f"set the marker to {marker!r}") |
| 80 | |
| 81 | row = site.get("fin_journals", journal_id) |
| 82 | ok(site.put("fin_journals", journal_id, "ann", {**{k: row[k] for k in ( |
| 83 | "date", "description", "generated_from", "reference")}, "description": "March rent"}), |
| 84 | "edit the draft, sending the marker back as it is") |
| 85 | ok(site.put("fin_journals", journal_id, "ann", {"status": "posted"}), "post it") |
| 86 | |
| 87 | stored = site.get("fin_journals", journal_id) |
| 88 | assert (stored["description"], stored["status"], stored["generated_from"]) == ( |
| 89 | "March rent", "posted", "fin_recurring/r1:2026-03-01") |
| 90 | assert compose(site, "fin_recurring/r1:2026-03-01")["journal_id"] == journal_id |
| 91 | |
| 92 | |
| 93 | def test_a_hand_typed_journal_with_a_blank_marker_is_unaffected(tmp_path, monkeypatch): |
| 94 | site = books_site(tmp_path, monkeypatch) |
| 95 | ok(site.post("fin_journals", "ann", {"id": "j1", "date": "2026-03-10", "description": "sale", |
| 96 | "generated_from": ""}), "blank marker sent explicitly") |
| 97 | ok(site.put("fin_journals", "j1", "ann", {"description": "sale, corrected", |
| 98 | "generated_from": " "}), "whitespace is blank") |
| 99 | ok(site.delete("fin_journals", "j1", "ann"), "and a draft is still deleted") |
| 100 | |
| 101 | |
| 102 | def test_an_admin_can_still_repair_a_marker(tmp_path, monkeypatch): |
| 103 | """The admin token is the repair path (a journal booked by hand for a |
| 104 | payment whose handler was off, given the marker so the handler never |
| 105 | books it twice).""" |
| 106 | site = books_site(tmp_path, monkeypatch) |
| 107 | ok(site.post("fin_journals", "admin", {"id": "j1", "date": "2026-03-10", |
| 108 | "description": "booked by hand", "owner_id": "ann", |
| 109 | "generated_from": "payments/p1"}), "admin sets a marker") |
| 110 | assert compose(site, "payments/p1") == { |
| 111 | "ok": True, "skipped": "already composed: payments/p1", "journal_id": "j1", |
| 112 | "posted": False} |