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_fringe_benefits.py · 5.2 KB · 111 lines
| 1 | """The fringe-benefit register: recorded by finance, handed to payroll, computed by nobody here. |
| 2 | |
| 3 | A non-cash benefit an employee received -- personal use of the company car, |
| 4 | a gift card -- is pay the payroll provider has to know about. The box's job |
| 5 | is to keep the register and hand it over intact; valuing a benefit and |
| 6 | withholding on it are the provider's and the accountant's. So: finance and |
| 7 | admins only (it is pay information about named people), `taxable` has no |
| 8 | default (somebody has to decide), and the export is the rows as recorded. |
| 9 | |
| 10 | Through the real ASGI app, enforcement on: fin (role:finance), mia |
| 11 | (role:manager), ann (an employee). |
| 12 | """ |
| 13 | |
| 14 | from __future__ import annotations |
| 15 | |
| 16 | import csv |
| 17 | import io |
| 18 | |
| 19 | import pytest |
| 20 | |
| 21 | import object_records |
| 22 | from scripts.try_package import Sandbox |
| 23 | |
| 24 | RECORDS = "/collections/fringe_benefits/records" |
| 25 | EXPORT = "/objects/action_fringe_export" |
| 26 | |
| 27 | |
| 28 | @pytest.fixture |
| 29 | def 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 | |
| 37 | def 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 | |
| 43 | def 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 | |
| 51 | def 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 | |
| 59 | def 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 | |
| 69 | def 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 | |
| 82 | def 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 | |
| 92 | def 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 |