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_cash_counts.py · 11.8 KB · 271 lines
| 1 | """Counting the accounts nobody sends a statement for |
| 2 | (plan/value-accounts-and-denominations-spec.md §4-5). |
| 3 | |
| 4 | A till is real money with no independent witness, so the interesting tests |
| 5 | are not the arithmetic -- they are the controls: a witness can be required, |
| 6 | the witness cannot be the counter, and a variance is booked as a real |
| 7 | expense rather than quietly dropped. |
| 8 | """ |
| 9 | |
| 10 | import pathlib |
| 11 | |
| 12 | import pytest |
| 13 | |
| 14 | import object_execution |
| 15 | import object_identity |
| 16 | import object_records |
| 17 | import python_object_runtime |
| 18 | from conftest import stage_collection |
| 19 | |
| 20 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 21 | PACKAGES = REPO_ROOT / "packages" |
| 22 | BANKING_OBJECTS = PACKAGES / "app-banking" / "objects" |
| 23 | RUNTIME = python_object_runtime.PythonObjectRuntime() |
| 24 | |
| 25 | CASH, OVER_SHORT = "acct-cash", "acct-over-short" |
| 26 | TILL = "va-till" |
| 27 | |
| 28 | |
| 29 | def setup_env(tmp_path, monkeypatch, *, settings=(("reconcile.journal.cash_over_short_account", OVER_SHORT),)): |
| 30 | data_dir = tmp_path / "data" |
| 31 | for pkg, name in (("app-banking", "value_accounts"), |
| 32 | ("app-banking", "value_account_counts"), |
| 33 | ("app-finance", "fin_accounts"), |
| 34 | ("app-finance", "fin_journals"), |
| 35 | ("app-finance", "fin_journal_lines"), |
| 36 | ("app-finance", "denominations")): |
| 37 | stage_collection(data_dir, pkg, name) |
| 38 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 39 | |
| 40 | rows = "" |
| 41 | for i, (k, v) in enumerate(settings): |
| 42 | rows += f"s{i}\t{k}\t{v}\t\n" |
| 43 | stage_collection(data_dir, "app-settings", "app_settings", rows=rows) |
| 44 | |
| 45 | for acct, kind in ((CASH, "asset"), (OVER_SHORT, "expense")): |
| 46 | object_records.create_collection_record( |
| 47 | "fin_accounts", {"id": acct, "name": acct, "account_type": kind, |
| 48 | "owner_id": "dan"}, base_dir=data_dir) |
| 49 | return data_dir |
| 50 | |
| 51 | |
| 52 | def make_till(data_dir, *, requires_witness=False, verification="physical_count", |
| 53 | aid=TILL): |
| 54 | return object_records.create_collection_record( |
| 55 | "value_accounts", |
| 56 | {"id": aid, "name": "Front Register", "kind": "cash_box", |
| 57 | "fin_account_id": CASH, "custody": "self", "custodian": "front register", |
| 58 | "verification": verification, |
| 59 | "requires_second_attestor": "true" if requires_witness else "false", |
| 60 | "owner_id": "dan"}, |
| 61 | base_dir=data_dir) |
| 62 | |
| 63 | |
| 64 | def book_cash(data_dir, amount_minor, *, jid="j-open", on="2026-07-01"): |
| 65 | """Put a posted balance in the till's book account.""" |
| 66 | object_records.create_collection_record( |
| 67 | "fin_journals", |
| 68 | {"id": jid, "date": on, "description": "Opening float", "status": "posted", |
| 69 | "kind": "standard", "owner_id": "dan"}, base_dir=data_dir) |
| 70 | object_records.create_collection_record( |
| 71 | "fin_journal_lines", |
| 72 | {"id": f"{jid}-dr", "journal_id": jid, "account_id": CASH, |
| 73 | "debit_cents": str(amount_minor), "credit_cents": "0", "owner_id": "dan"}, |
| 74 | base_dir=data_dir) |
| 75 | object_records.create_collection_record( |
| 76 | "fin_journal_lines", |
| 77 | {"id": f"{jid}-cr", "journal_id": jid, "account_id": OVER_SHORT, |
| 78 | "debit_cents": "0", "credit_cents": str(amount_minor), "owner_id": "dan"}, |
| 79 | base_dir=data_dir) |
| 80 | |
| 81 | |
| 82 | def add_user(data_dir, user_id, *, status="active", display_name="", email=None, |
| 83 | account_id=None): |
| 84 | if account_id: |
| 85 | try: |
| 86 | object_identity.create_account({"account_id": account_id, "name": account_id}, |
| 87 | base_dir=data_dir) |
| 88 | except object_identity.InvalidIdentityPayloadError: |
| 89 | pass |
| 90 | return object_identity.create_user( |
| 91 | {"user_id": user_id, "status": status, "display_name": display_name, |
| 92 | "email": email, "account_id": account_id}, base_dir=data_dir) |
| 93 | |
| 94 | |
| 95 | def count(payload, *, user="dan"): |
| 96 | return object_execution.execute_object( |
| 97 | RUNTIME, |
| 98 | object_execution.ObjectExecutionRequest( |
| 99 | "action_record_count", method="POST", |
| 100 | payload={"_identity": {"user_id": user}, **payload}), |
| 101 | roots=[BANKING_OBJECTS]).result |
| 102 | |
| 103 | |
| 104 | def counts(data_dir): |
| 105 | return object_records.read_collection_records("value_account_counts", base_dir=data_dir) |
| 106 | |
| 107 | |
| 108 | def journal_lines(data_dir, journal_id): |
| 109 | return {l["account_id"]: l for l in |
| 110 | object_records.read_collection_records("fin_journal_lines", base_dir=data_dir) |
| 111 | if l["journal_id"] == journal_id} |
| 112 | |
| 113 | |
| 114 | def test_a_matching_count_records_evidence_and_books_nothing(tmp_path, monkeypatch): |
| 115 | data_dir = setup_env(tmp_path, monkeypatch) |
| 116 | make_till(data_dir) |
| 117 | book_cash(data_dir, 20000) |
| 118 | result = count({"value_account_id": TILL, "counted_minor": 20000, |
| 119 | "counted_on": "2026-07-31"}) |
| 120 | assert result["status"] == 200 and result["variance_minor"] == 0 |
| 121 | assert result["journal"] is None |
| 122 | row = counts(data_dir)[0] |
| 123 | # The book figure is stamped, so the count stays meaningful later. |
| 124 | assert row["book_balance_minor"] == "20000" |
| 125 | assert row["counted_by"] == "dan" |
| 126 | |
| 127 | |
| 128 | def test_a_shortage_books_cash_over_short(tmp_path, monkeypatch): |
| 129 | """The drawer is light: that is an expense the business really incurred, |
| 130 | and booking it is what makes a slow leak visible as a number.""" |
| 131 | data_dir = setup_env(tmp_path, monkeypatch) |
| 132 | make_till(data_dir) |
| 133 | book_cash(data_dir, 20000) |
| 134 | result = count({"value_account_id": TILL, "counted_minor": 19750, |
| 135 | "counted_on": "2026-07-31"}) |
| 136 | assert result["variance_minor"] == -250 |
| 137 | assert result["journal"]["posted"] is True |
| 138 | booked = journal_lines(data_dir, result["journal"]["journal_id"]) |
| 139 | assert booked[OVER_SHORT]["debit_cents"] == "250" # expense up |
| 140 | assert booked[CASH]["credit_cents"] == "250" # cash down |
| 141 | assert counts(data_dir)[0]["journal_id"] == result["journal"]["journal_id"] |
| 142 | |
| 143 | |
| 144 | def test_an_overage_books_the_mirror(tmp_path, monkeypatch): |
| 145 | data_dir = setup_env(tmp_path, monkeypatch) |
| 146 | make_till(data_dir) |
| 147 | book_cash(data_dir, 20000) |
| 148 | result = count({"value_account_id": TILL, "counted_minor": 20100}) |
| 149 | assert result["variance_minor"] == 100 |
| 150 | booked = journal_lines(data_dir, result["journal"]["journal_id"]) |
| 151 | assert booked[CASH]["debit_cents"] == "100" |
| 152 | assert booked[OVER_SHORT]["credit_cents"] == "100" |
| 153 | |
| 154 | |
| 155 | def test_an_account_can_refuse_a_self_certified_count(tmp_path, monkeypatch): |
| 156 | """The oldest control in retail, as one field: the person counting the |
| 157 | till is usually the person who could take from it.""" |
| 158 | data_dir = setup_env(tmp_path, monkeypatch) |
| 159 | make_till(data_dir, requires_witness=True) |
| 160 | book_cash(data_dir, 20000) |
| 161 | unwitnessed = count({"value_account_id": TILL, "counted_minor": 19750}) |
| 162 | assert unwitnessed["status"] == 409 and "witness" in unwitnessed["error"] |
| 163 | assert counts(data_dir) == [] |
| 164 | |
| 165 | add_user(data_dir, "pat") |
| 166 | witnessed = count({"value_account_id": TILL, "counted_minor": 19750, |
| 167 | "witnessed_by": "pat"}) |
| 168 | assert witnessed["status"] == 200 |
| 169 | # Named, not yet witnessed: pat has to confirm it from their own sign-in |
| 170 | # (test_cash_count_witness.py) before it is lifted to medium. |
| 171 | assert witnessed["assurance"] == "weak" |
| 172 | assert witnessed["witness"]["second_attestor_met"] is False |
| 173 | assert counts(data_dir)[0]["witnessed_by"] == "pat" |
| 174 | assert counts(data_dir)[0]["witness_status"] == "named" |
| 175 | |
| 176 | |
| 177 | def test_the_witness_cannot_be_the_counter(tmp_path, monkeypatch): |
| 178 | data_dir = setup_env(tmp_path, monkeypatch) |
| 179 | make_till(data_dir, requires_witness=True) |
| 180 | book_cash(data_dir, 20000) |
| 181 | result = count({"value_account_id": TILL, "counted_minor": 19750, |
| 182 | "witnessed_by": "dan"}, user="dan") |
| 183 | assert result["status"] == 409 |
| 184 | assert "not the person counting" in result["error"] |
| 185 | assert counts(data_dir) == [] |
| 186 | |
| 187 | |
| 188 | def test_an_unwitnessed_count_is_rated_weakest(tmp_path, monkeypatch): |
| 189 | data_dir = setup_env(tmp_path, monkeypatch) |
| 190 | make_till(data_dir) # witness not required |
| 191 | book_cash(data_dir, 20000) |
| 192 | result = count({"value_account_id": TILL, "counted_minor": 20000}) |
| 193 | assert result["assurance"] == "weak" # self-certification, and it says so |
| 194 | |
| 195 | |
| 196 | def test_a_variance_with_nowhere_to_go_is_refused(tmp_path, monkeypatch): |
| 197 | """Better to refuse than to record a count whose difference silently |
| 198 | never reaches the ledger.""" |
| 199 | data_dir = setup_env(tmp_path, monkeypatch, settings=()) |
| 200 | make_till(data_dir) |
| 201 | book_cash(data_dir, 20000) |
| 202 | result = count({"value_account_id": TILL, "counted_minor": 19750}) |
| 203 | assert result["status"] == 409 and "cash_over_short_account" in result["error"] |
| 204 | assert counts(data_dir) == [] |
| 205 | |
| 206 | |
| 207 | def test_only_counted_accounts_can_be_counted(tmp_path, monkeypatch): |
| 208 | data_dir = setup_env(tmp_path, monkeypatch) |
| 209 | make_till(data_dir, verification="statement_import", aid="va-bank") |
| 210 | result = count({"value_account_id": "va-bank", "counted_minor": 100}) |
| 211 | assert result["status"] == 409 |
| 212 | assert "Import its statement instead" in result["error"] |
| 213 | |
| 214 | |
| 215 | def test_an_account_with_no_owner_is_counted_by_an_admin_only(tmp_path, monkeypatch): |
| 216 | """The check used to be "has an owner who is not me", so any signed-in |
| 217 | user could count against, and book a variance into, an account nobody |
| 218 | owns; the value_accounts API shows it to nobody but an admin.""" |
| 219 | data_dir = setup_env(tmp_path, monkeypatch) |
| 220 | make_till(data_dir) |
| 221 | object_records.update_collection_record( |
| 222 | "value_accounts", TILL, {"owner_id": ""}, base_dir=data_dir, actor="test") |
| 223 | refused = count({"value_account_id": TILL, "counted_minor": 100}, user="mallory") |
| 224 | assert refused["status"] == 404 |
| 225 | assert counts(data_dir) == [] |
| 226 | admin = object_execution.execute_object( |
| 227 | RUNTIME, |
| 228 | object_execution.ObjectExecutionRequest( |
| 229 | "action_record_count", method="POST", |
| 230 | payload={"_identity": {"user_id": "ops", "roles": ["admin"]}, |
| 231 | "value_account_id": TILL, "counted_minor": 0}), |
| 232 | roots=[BANKING_OBJECTS]).result |
| 233 | assert admin.get("status") not in (403, 404), admin |
| 234 | |
| 235 | |
| 236 | def test_refusals(tmp_path, monkeypatch): |
| 237 | data_dir = setup_env(tmp_path, monkeypatch) |
| 238 | make_till(data_dir) |
| 239 | assert count({"value_account_id": TILL})["status"] == 400 |
| 240 | assert count({"value_account_id": TILL, "counted_minor": -5})["status"] == 400 |
| 241 | assert count({"value_account_id": "nope", "counted_minor": 100})["status"] == 404 |
| 242 | other = count({"value_account_id": TILL, "counted_minor": 100}, user="mallory") |
| 243 | assert other["status"] == 404 # refused reads like missing |
| 244 | assert counts(data_dir) == [] |
| 245 | anon = object_execution.execute_object( |
| 246 | RUNTIME, |
| 247 | object_execution.ObjectExecutionRequest( |
| 248 | "action_record_count", method="POST", |
| 249 | payload={"value_account_id": TILL, "counted_minor": 100}), |
| 250 | roots=[BANKING_OBJECTS]).result |
| 251 | assert anon["status"] == 403 |
| 252 | |
| 253 | |
| 254 | def test_counted_by_is_set_by_the_action_and_not_by_the_record_api(tmp_path, monkeypatch): |
| 255 | """A count is evidence and who counted is part of it. counted_by is |
| 256 | read_only: an ordinary (client-submitted) write may not set it, so one |
| 257 | user cannot file a count in another's name, while the action, which |
| 258 | writes with preserve_read_only, still stamps the caller.""" |
| 259 | data_dir = setup_env(tmp_path, monkeypatch) |
| 260 | make_till(data_dir) |
| 261 | book_cash(data_dir, 20000) |
| 262 | forged = {"value_account_id": TILL, "counted_minor": "20000", |
| 263 | "counted_on": "2026-07-31", "counted_by": "pat", "owner_id": "dan"} |
| 264 | with pytest.raises(object_records.InvalidRecordPayloadError): |
| 265 | object_records.create_collection_record( |
| 266 | "value_account_counts", dict(forged), base_dir=data_dir) |
| 267 | assert counts(data_dir) == [] |
| 268 | result = count({"value_account_id": TILL, "counted_minor": 20000, |
| 269 | "counted_on": "2026-07-31"}) |
| 270 | assert result["status"] == 200, result |
| 271 | assert [row["counted_by"] for row in counts(data_dir)] == ["dan"] |