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_cash_count_witness.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_cash_count_witness.py · 11.2 KB · 243 lines
1"""A named witness is a claim until that person confirms it.
2
3`witnessed_by` used to be any text, so a closer could pocket cash, type a
4colleague's name, and the books showed a witnessed shortage that wrongly
5implicated the colleague. The accepted rule: the witness is a real active
6user other than the counter; the count is recorded and its variance booked
7at once, but it stays "named, not confirmed" -- assurance weak, a
8second-attestor requirement unmet -- until the named user confirms the same
9figure from their own sign-in (action_confirm_count).
10"""
11
12import json
13import pathlib
14
15import pytest
16
17import object_access
18import object_attention
19import object_execution
20import object_identity
21import object_records
22from test_cash_counts import (BANKING_OBJECTS, RUNTIME, TILL, add_user, book_cash, count,
23 counts, make_till, setup_env)
24
25REPO_ROOT = pathlib.Path(__file__).resolve().parents[1]
26MANIFEST = REPO_ROOT / "packages" / "app-banking" / "dbbasic-package.json"
27
28
29def confirm(payload, *, user, roles=(), auth_method=None, method="POST"):
30 identity = {"user_id": user, "roles": list(roles)}
31 if auth_method:
32 identity["auth_method"] = auth_method
33 return object_execution.execute_object(
34 RUNTIME,
35 object_execution.ObjectExecutionRequest(
36 "action_confirm_count", method=method,
37 payload={"_identity": identity, **payload}),
38 roots=[BANKING_OBJECTS]).result
39
40
41@pytest.fixture
42def box(tmp_path, monkeypatch):
43 data_dir = setup_env(tmp_path, monkeypatch)
44 make_till(data_dir, requires_witness=True)
45 book_cash(data_dir, 20000)
46 add_user(data_dir, "dan", display_name="Dan Counter", email="dan@example.test")
47 add_user(data_dir, "pat", display_name="Pat Witness")
48 add_user(data_dir, "sam")
49 return data_dir
50
51
52def short_count(data_dir, witness="pat"):
53 result = count({"value_account_id": TILL, "counted_minor": 19750,
54 "counted_on": "2026-07-31", "witnessed_by": witness})
55 assert result["status"] == 200, result
56 return result
57
58
59def books(data_dir):
60 return (object_records.read_collection_records("fin_journals", base_dir=data_dir),
61 object_records.read_collection_records("fin_journal_lines", base_dir=data_dir))
62
63
64# --- naming the witness ------------------------------------------------------
65
66def test_unknown_or_inactive_witness_is_refused_alike(box):
67 add_user(box, "lee", status="disabled")
68 unknown = count({"value_account_id": TILL, "counted_minor": 19750, "witnessed_by": "nobody"})
69 inactive = count({"value_account_id": TILL, "counted_minor": 19750, "witnessed_by": "lee"})
70 by_name = count({"value_account_id": TILL, "counted_minor": 19750,
71 "witnessed_by": "Pat Witness"}) # a display name is not looked up
72 assert unknown["status"] == inactive["status"] == by_name["status"] == 409
73 # One answer: it does not say which users exist or what state they are in.
74 assert unknown["error"] == inactive["error"] == by_name["error"]
75 assert counts(box) == []
76 assert books(box)[0] == [books(box)[0][0]] # only the opening float
77
78
79@pytest.mark.parametrize("named", ["dan", "Dan Counter", "dan counter", "DAN@example.test"])
80def test_the_counter_cannot_name_themself_by_id_or_name(box, named):
81 result = count({"value_account_id": TILL, "counted_minor": 19750, "witnessed_by": named})
82 unknown = count({"value_account_id": TILL, "counted_minor": 19750, "witnessed_by": "nobody"})
83 assert result["status"] == 409 and result["error"] == unknown["error"]
84 assert counts(box) == []
85
86
87def test_the_counters_own_name_is_refused_even_when_it_is_somebody_elses_id(box):
88 # dan goes by "pat" on screen, and pat is also another user's id: typing
89 # your own name is refused even when it resolves to somebody else.
90 object_identity.set_user_status("dan", "active", display_name="pat", base_dir=box)
91 result = count({"value_account_id": TILL, "counted_minor": 19750, "witnessed_by": "pat"})
92 assert result["status"] == 409 and counts(box) == []
93
94
95def test_a_user_from_another_identity_account_is_refused(box):
96 add_user(box, "outsider", account_id="acct-other")
97 result = object_execution.execute_object(
98 RUNTIME,
99 object_execution.ObjectExecutionRequest(
100 "action_record_count", method="POST",
101 payload={"_identity": {"user_id": "dan", "account_id": "acct-shop"},
102 "value_account_id": TILL, "counted_minor": 19750,
103 "witnessed_by": "outsider"}),
104 roots=[BANKING_OBJECTS]).result
105 assert result["status"] == 409 and counts(box) == []
106
107
108def test_a_named_only_count_is_weak_and_the_requirement_unmet(box):
109 result = short_count(box)
110 assert result["assurance"] == "weak"
111 assert result["witness"] == {"user_id": "pat", "status": "named",
112 "second_attestor_required": True,
113 "second_attestor_met": False}
114 assert "has not confirmed" in result["witness_note"]
115 # The count is recorded and the shortage booked at once all the same.
116 row = counts(box)[0]
117 assert row["witness_status"] == "named" and row["journal_id"] == result["journal"]["journal_id"]
118 assert result["journal"]["posted"] is True
119
120
121# --- confirming --------------------------------------------------------------
122
123def test_the_counter_cannot_confirm_their_own_count(box):
124 cid = short_count(box)["count_id"]
125 refused = confirm({"count_id": cid, "counted_minor": 19750}, user="dan")
126 assert refused["status"] == 403
127 assert counts(box)[0]["witness_status"] == "named"
128
129
130def test_a_third_user_cannot_confirm(box):
131 cid = short_count(box)["count_id"]
132 refused = confirm({"count_id": cid, "counted_minor": 19750}, user="sam")
133 missing = confirm({"count_id": "no-such-count", "counted_minor": 19750}, user="sam")
134 assert refused["status"] == 404 and refused == missing # a stranger learns nothing
135 assert counts(box)[0]["witness_status"] == "named"
136
137
138def test_an_admin_cannot_confirm_on_the_witnesss_behalf(box):
139 cid = short_count(box)["count_id"]
140 as_admin = confirm({"count_id": cid, "counted_minor": 19750}, user="ops", roles=["admin"])
141 assert as_admin["status"] == 404
142 # The admin token is nobody's sign-in, even if a user called "admin" were named.
143 token = confirm({"count_id": cid, "counted_minor": 19750}, user="pat",
144 roles=["admin"], auth_method="admin_token")
145 assert token["status"] == 403
146 assert counts(box)[0]["witness_status"] == "named"
147
148
149def test_a_wrong_figure_does_not_confirm_and_is_recorded(box):
150 cid = short_count(box)["count_id"]
151 disputed = confirm({"count_id": cid, "counted_minor": 20000}, user="pat")
152 assert disputed["status"] == 409 and disputed["witness_status"] == "disputed"
153 assert disputed["assurance"] == "weak"
154 assert "19750" not in json.dumps(disputed) # the recorded figure is not told
155 row = counts(box)[0]
156 assert row["witness_status"] == "disputed"
157 assert row["witness_stated_minor"] == "20000" and row["witness_answered_at"]
158 # Once only: the right figure afterwards does not turn a dispute into a confirmation.
159 again = confirm({"count_id": cid, "counted_minor": 19750}, user="pat")
160 assert again["status"] == 409 and counts(box)[0]["witness_status"] == "disputed"
161
162
163def test_the_named_witness_confirms_once_and_lifts_it_to_medium(box):
164 cid = short_count(box)["count_id"]
165 before = books(box)
166 confirmed = confirm({"count_id": cid, "counted_minor": 19750}, user="pat")
167 assert confirmed["status"] == 200
168 assert confirmed["witness_status"] == "confirmed" and confirmed["assurance"] == "medium"
169 row = counts(box)[0]
170 assert row["witness_status"] == "confirmed" and row["witness_stated_minor"] == "19750"
171 # The variance journal is exactly what it was: confirming books nothing.
172 assert books(box) == before
173 again = confirm({"count_id": cid, "counted_minor": 19750}, user="pat")
174 assert again["status"] == 409
175 assert books(box) == before
176
177
178def test_the_witness_lists_what_waits_for_them_without_the_figure(box):
179 cid = short_count(box)["count_id"]
180 mine = confirm({}, user="pat", method="GET")
181 assert mine["status"] == 200
182 assert [c["count_id"] for c in mine["counts"]] == [cid]
183 assert mine["counts"][0]["account_name"] == "Front Register"
184 assert mine["counts"][0]["counted_by"] == "dan"
185 assert "19750" not in json.dumps(mine) and "20000" not in json.dumps(mine)
186 assert confirm({}, user="sam", method="GET")["counts"] == []
187 confirm({"count_id": cid, "counted_minor": 19750}, user="pat")
188 assert confirm({}, user="pat", method="GET")["counts"] == []
189
190
191# --- the fields only the server writes ---------------------------------------
192
193@pytest.mark.parametrize("field,value", [("witness_status", "confirmed"),
194 ("witness_stated_minor", "19750"),
195 ("witness_answered_at", "2026-07-31T00:00:00+00:00"),
196 ("witnessed_by", "pat")])
197def test_the_record_api_cannot_set_the_witness_fields(box, field, value):
198 cid = short_count(box)["count_id"]
199 with pytest.raises(object_records.InvalidRecordPayloadError):
200 object_records.create_collection_record(
201 "value_account_counts",
202 {"value_account_id": TILL, "counted_on": "2026-07-31", "counted_minor": "1",
203 "owner_id": "dan", field: value}, base_dir=box, actor="dan")
204 with pytest.raises(object_records.InvalidRecordPayloadError):
205 object_records.update_collection_record(
206 "value_account_counts", cid, {field: value}, base_dir=box, actor="dan")
207 assert counts(box)[0]["witness_status"] == "named"
208
209
210# --- accounts with no witness ------------------------------------------------
211
212def test_an_account_without_a_witness_requirement_counts_as_before(tmp_path, monkeypatch):
213 data_dir = setup_env(tmp_path, monkeypatch)
214 make_till(data_dir)
215 book_cash(data_dir, 20000)
216 result = count({"value_account_id": TILL, "counted_minor": 19750})
217 assert result["status"] == 200 and result["assurance"] == "weak"
218 assert "witness" not in result and "witness_note" not in result
219 assert result["journal"]["posted"] is True
220 row = counts(data_dir)[0]
221 assert row["witnessed_by"] == "" and row["witness_status"] == ""
222
223
224# --- the owner sees it -------------------------------------------------------
225
226def test_the_owner_is_shown_counts_awaiting_and_disputed(box):
227 declared = {e["id"]: e for e in json.loads(MANIFEST.read_text())["attention"]}
228
229 def shown(attention_id, user):
230 entry = declared[attention_id]
231 declaration = {"collection": entry["collection"],
232 "where": object_attention.parse_where(entry["where"]), "detail": []}
233 access = object_access.Access({"user_id": user}, base_dir=box)
234 return object_attention.count(declaration, access, base_dir=box)["count"]
235
236 first = short_count(box)["count_id"]
237 book_cash(box, 100, jid="j-more", on="2026-07-01")
238 short_count(box)
239 assert shown("counts_awaiting_witness", "dan") == 2
240 assert shown("counts_awaiting_witness", "sam") == 0 # not his counts
241 confirm({"count_id": first, "counted_minor": 1}, user="pat")
242 assert shown("counts_awaiting_witness", "dan") == 1
243 assert shown("counts_witness_disputed", "dan") == 1