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_time_logs.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_time_logs.py · 6.5 KB · 129 lines
1"""Approved and billed time: a settled entry stays put, and the people and
2the writer who are supposed to move it still can.
3
4tests/test_findings_money_gates.py holds the reproductions (an approved
524000-cent entry, and a billed one, deleted by the person who logged
6them). This file is the rest of the change: `time_logs` declares
7`locked_when` on approved/billed -- the same allow-list the hook carries
8(status, invoice_id, notes), now inside the write lock, for every writer,
9and covering DELETE.
10"""
11
12import json
13
14import pytest
15
16import object_records
17from test_findings_money_gates import ( # noqa: F401 (fresh_handler_index is autouse)
18 PACKAGES, build_site, fresh_handler_index, ok, timers_site,
19)
20
21
22def refused(result, *, code, status=409):
23 got, body = result
24 assert got == status and body.get("code") == code, (got, body)
25 return body
26
27
28# --- time logs ---------------------------------------------------------------------
29
30
31def test_the_time_log_lock_is_the_only_statement_of_the_freeze():
32 """The freeze used to be stated twice (the hook's allow-list and the
33 schema's `locked_when`) and a test held the two together. The hook's copy
34 is gone: it ran before the lock, never saw a delete or a storage-level
35 writer. The schema's rule is the whole of it."""
36 schema = json.loads((PACKAGES / "app-timers" / "schemas" / "time_logs.json").read_text())
37 (rule,) = schema["locked_when"]
38 hook = (PACKAGES / "app-timers" / "objects" / "hook" / "time_logs.py").read_text()
39
40 assert rule["when"] == {"status": ["approved", "billed"]}
41 assert rule.get("allow_delete") is not True
42 assert "_ALLOWED_AFTER_APPROVAL" not in hook
43 assert sorted(rule["except"]) == ["invoice_id", "notes", "status"]
44
45
46def test_a_draft_or_rejected_time_log_can_still_be_deleted_by_its_owner(tmp_path, monkeypatch):
47 """The lock is about settled entries. A mistaken draft, and an entry a
48 manager sent back, are still the owner's to remove."""
49 site = timers_site(tmp_path, monkeypatch)
50 ok(site.post("time_logs", "ann", {"id": "t2", "started_at": "2026-07-02T09:00:00Z",
51 "ended_at": "2026-07-02T10:00:00Z",
52 "duration_seconds": "3600", "billable": "true"}), "draft")
53 ok(site.post("time_logs", "ann", {"id": "t3", "started_at": "2026-07-03T09:00:00Z",
54 "ended_at": "2026-07-03T10:00:00Z",
55 "duration_seconds": "3600", "billable": "true"}), "another")
56 ok(site.put("time_logs", "t3", "ann", {"status": "submitted"}), "submit")
57 ok(site.put("time_logs", "t3", "bob", {"status": "rejected"}), "reject")
58
59 ok(site.delete("time_logs", "t2", "ann"), "delete a draft")
60 ok(site.delete("time_logs", "t3", "ann"), "delete a rejected entry")
61 assert [row["id"] for row in site.rows("time_logs")] == ["t1"]
62
63
64def test_nobody_deletes_approved_time_not_the_manager_and_not_the_admin_token(tmp_path, monkeypatch):
65 site = timers_site(tmp_path, monkeypatch)
66
67 refused(site.delete("time_logs", "t1", "ann"), code="record_locked")
68 body = refused(site.delete("time_logs", "t1", "admin"), code="record_locked")
69 assert "compensating entry" in body["error"]
70 # bob (a manager) holds read and update on time_logs, not delete: the
71 # permission layer answers him before the lock is asked.
72 status, _ = site.delete("time_logs", "t1", "bob")
73 assert status == 403
74
75 assert site.get("time_logs", "t1")["amount_cents"] == "24000"
76
77
78def test_approved_time_still_takes_a_note_and_a_rejection(tmp_path, monkeypatch):
79 """What the `except` list is for: a note, and the manager changing
80 their mind. Rejecting unlocks the entry; the lock is judged by the
81 stored row, so unlocking and editing are two writes."""
82 site = timers_site(tmp_path, monkeypatch)
83
84 ok(site.put("time_logs", "t1", "ann", {"notes": "client call, see ticket 88"}), "note")
85 status, body = site.put("time_logs", "t1", "bob",
86 {"status": "rejected", "duration_seconds": "60"})
87 assert status == 409 and site.get("time_logs", "t1")["duration_seconds"] == "7200", body
88
89 ok(site.put("time_logs", "t1", "bob", {"status": "rejected"}), "reject")
90 ok(site.put("time_logs", "t1", "ann", {"status": "draft"}), "back to draft")
91 ok(site.put("time_logs", "t1", "ann", {"duration_seconds": "3600"}), "now editable")
92 assert site.get("time_logs", "t1")["duration_seconds"] == "3600"
93
94
95def test_the_invoice_generator_still_bills_approved_time_through_the_except_fields(
96 tmp_path, monkeypatch):
97 """action_generate_tm_invoice writes {"status": "billed", "invoice_id"}
98 onto approved entries at the storage level. Nobody is exempt from
99 locked_when, so that write has to fit inside `except` -- and it does.
100 The same writer cannot reach past it."""
101 site = build_site(tmp_path, monkeypatch,
102 ["app-views", "app-settings", "app-projects", "app-tasks", "app-timers",
103 "app-invoices"],
104 users=("ann", "bob"), roles={"bob": ["manager"]})
105 ok(site.post("rate_cards", "bob", {"id": "rc1", "label": "Standard",
106 "hourly_rate_cents": "12000", "valid_from": "2026-01-01"}),
107 "rate card")
108 ok(site.post("time_logs", "ann", {"id": "t1", "started_at": "2026-07-01T09:00:00Z",
109 "ended_at": "2026-07-01T11:00:00Z",
110 "duration_seconds": "7200", "billable": "true"}), "time log")
111 ok(site.put("time_logs", "t1", "ann", {"status": "submitted"}), "submit")
112 ok(site.put("time_logs", "t1", "bob", {"status": "approved"}), "approve")
113 ok(site.post("invoices", "ann", {"id": "inv-77", "number": "INV-77", "customer_name": "Acme"}),
114 "the invoice the hours go on")
115
116 billed = object_records.update_collection_record(
117 "time_logs", "t1", {"status": "billed", "invoice_id": "inv-77"},
118 base_dir=site.data, actor="generate_tm_invoice")
119 assert (billed["status"], billed["invoice_id"], billed["amount_cents"]) == (
120 "billed", "inv-77", "24000")
121
122 with pytest.raises(object_records.RecordLockedError):
123 object_records.update_collection_record(
124 "time_logs", "t1", {"amount_cents": "1"}, base_dir=site.data,
125 actor="generate_tm_invoice")
126 with pytest.raises(object_records.RecordLockedError):
127 object_records.delete_collection_record("time_logs", "t1", base_dir=site.data,
128 actor="some_cleanup")
129 assert site.get("time_logs", "t1")["amount_cents"] == "24000"