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_fringe_benefits.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_fringe_benefits.py · 5.2 KB · 111 lines
1"""The fringe-benefit register: recorded by finance, handed to payroll, computed by nobody here.
2
3A non-cash benefit an employee received -- personal use of the company car,
4a gift card -- is pay the payroll provider has to know about. The box's job
5is to keep the register and hand it over intact; valuing a benefit and
6withholding on it are the provider's and the accountant's. So: finance and
7admins only (it is pay information about named people), `taxable` has no
8default (somebody has to decide), and the export is the rows as recorded.
9
10Through the real ASGI app, enforcement on: fin (role:finance), mia
11(role:manager), ann (an employee).
12"""
13
14from __future__ import annotations
15
16import csv
17import io
18
19import pytest
20
21import object_records
22from scripts.try_package import Sandbox
23
24RECORDS = "/collections/fringe_benefits/records"
25EXPORT = "/objects/action_fringe_export"
26
27
28@pytest.fixture
29def box():
30 with Sandbox(["app-nav", "app-settings", "app-finance"]) as sandbox:
31 sandbox.add_user("ann")
32 sandbox.add_user("mia", roles=["manager"])
33 sandbox.add_user("fin", roles=["finance"])
34 yield sandbox
35
36
37def ok(answer, status=(200, 201)):
38 code, body = answer
39 assert code in (status if isinstance(status, tuple) else (status,)), body
40 return body
41
42
43def benefit(**fields):
44 return {"employee_id": "E-104", "employee_name": "Ann Smith",
45 "benefit_type": "company_vehicle_personal_use", "period_start": "2025-01-01",
46 "period_end": "2025-12-31", "value_cents": 184250, "taxable": "yes",
47 "valuation_method": "cents-per-mile: 2,632 personal miles",
48 "source": "mileage log totals for the van, 2025", "owner_id": "fin", **fields}
49
50
51def test_finance_records_a_benefit_and_must_say_whether_it_is_taxable(box):
52 ok(box.call("fin", "POST", RECORDS, benefit()))
53 code, body = box.call("fin", "POST", RECORDS, {k: v for k, v in benefit().items() if k != "taxable"})
54 assert code == 400 and "taxable" in body["error"]
55 code, body = box.call("fin", "POST", RECORDS, benefit(taxable="maybe"))
56 assert code == 400
57
58
59def test_a_period_that_ends_before_it_starts_is_refused_for_every_writer(box):
60 code, body = box.call("fin", "POST", RECORDS, benefit(period_start="2025-06-01", period_end="2025-05-31"))
61 # over the API the hook answers first, with the check's own sentence
62 assert code == 409 and "ends (2025-05-31) before it starts" in body["error"]
63 with pytest.raises(object_records.CheckRefusedError):
64 object_records.create_collection_record(
65 "fringe_benefits", benefit(period_start="2025-06-01", period_end="2025-05-31"),
66 base_dir=box.data_dir, roots=[box.objects_dir])
67
68
69def test_only_finance_and_admins_see_or_touch_the_register(box):
70 record = ok(box.call("fin", "POST", RECORDS, benefit()))["record"]
71 for who in ("ann", "mia"):
72 code, body = box.call(who, "GET", RECORDS)
73 assert code == 403 or body.get("total") == 0, body
74 assert box.call(who, "GET", f"{RECORDS}/{record['id']}")[0] in (403, 404)
75 assert box.call(who, "POST", RECORDS, benefit(owner_id=who))[0] == 403
76 assert box.call(who, "GET", EXPORT)[0] == 403
77 assert ok(box.call("admin", "GET", RECORDS))["total"] == 1
78 assert box.call("fin", "DELETE", f"{RECORDS}/{record['id']}")[0] == 403
79 assert box.call("anon", "GET", EXPORT)[0] in (401, 403)
80
81
82def test_the_recorder_loses_the_register_when_they_leave_finance(box):
83 """The owner fallback would have kept it readable to whoever created the row."""
84 box.add_user("ex", roles=["finance"])
85 record = ok(box.call("ex", "POST", RECORDS, benefit(owner_id="ex")))["record"]
86 import object_identity
87 object_identity.set_user_roles("ex", [], base_dir=box.data_dir)
88 assert box.call("ex", "GET", f"{RECORDS}/{record['id']}")[0] in (403, 404)
89 assert box.call("ex", "PUT", f"{RECORDS}/{record['id']}", {"value_cents": 1})[0] in (403, 404)
90
91
92def test_the_export_hands_over_the_rows_as_recorded(box):
93 ok(box.call("fin", "POST", RECORDS, benefit()))
94 ok(box.call("fin", "POST", RECORDS, benefit(
95 employee_id="E-107", employee_name="Bo Diaz", benefit_type="gift_card_or_cash_equivalent",
96 period_start="2025-12-15", period_end="2025-12-15", value_cents=5000, taxable="yes",
97 valuation_method="face value", source="=HYPERLINK(\"x\")")))
98 ok(box.call("fin", "POST", RECORDS, benefit(
99 employee_id="E-108", employee_name="Cy Ng", benefit_type="de_minimis",
100 period_start="2024-12-20", period_end="2024-12-20", value_cents=2500, taxable="no")))
101 body = ok(box.call("fin", "GET", EXPORT + "?from=2025-01-01&to=2025-12-31"))
102 assert body["count"] == 2
103 assert (body["total_value_cents"], body["taxable_value_cents"]) == (189250, 189250)
104 assert [row["employee_id"] for row in body["rows"]] == ["E-104", "E-107"]
105 assert body["rows"][0]["value_cents"] == "184250"
106 code, text = box.call("fin", "GET", EXPORT + "?from=2025-01-01&to=2025-12-31&format=csv")
107 assert code == 200
108 rows = list(csv.DictReader(io.StringIO(text)))
109 assert [r["value_cents"] for r in rows] == ["184250", "5000"]
110 assert rows[1]["source"].startswith("'=") # a formula is not run by the spreadsheet
111 assert ok(box.call("admin", "GET", EXPORT))["count"] == 3