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_app_finance_setup_accounts.py · 7.4 KB · 170 lines
| 1 | """65 multi-entity slice 4: the Setup Accounts action -- seed an entity's chart |
| 2 | of accounts from its mode (a faithful port of the predecessor's |
| 3 | Entity.create_default_accounts / the setup_finance_accounts MCP tool). Idempotent, |
| 4 | owner-gated. |
| 5 | """ |
| 6 | |
| 7 | import json |
| 8 | import os |
| 9 | import tempfile |
| 10 | import types |
| 11 | from pathlib import Path |
| 12 | |
| 13 | import object_mcp |
| 14 | import object_packages |
| 15 | import object_records |
| 16 | |
| 17 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 18 | APP_FINANCE_DIR = PACKAGES_ROOT / "app-finance" |
| 19 | |
| 20 | |
| 21 | def _install(tmp_path): |
| 22 | data_dir = tmp_path / "data" |
| 23 | object_root = tmp_path / "objects" |
| 24 | object_root.mkdir() |
| 25 | for pkg in ("app-entities", "app-finance", "app-projects"): |
| 26 | object_packages.install_package( |
| 27 | pkg, root=PACKAGES_ROOT, base_dir=data_dir, object_roots=[object_root], allow_replace=True, |
| 28 | ) |
| 29 | return data_dir |
| 30 | |
| 31 | |
| 32 | def _load_object(data_dir, monkeypatch): |
| 33 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 34 | src = (APP_FINANCE_DIR / "objects" / "site" / "setup_accounts.py").read_text() |
| 35 | mod = types.ModuleType("setup_accounts_under_test") |
| 36 | mod._logger = types.SimpleNamespace(info=lambda *a, **k: None) |
| 37 | exec(compile(src, "setup_accounts.py", "exec"), mod.__dict__) |
| 38 | return mod |
| 39 | |
| 40 | |
| 41 | def _post(mod, user_id, entity_id): |
| 42 | req = {"entity_id": entity_id} |
| 43 | if user_id is not None: |
| 44 | req["_identity"] = {"user_id": user_id} |
| 45 | r = mod.POST(req) |
| 46 | return r.get("status", 200), json.loads(r["body"]) |
| 47 | |
| 48 | |
| 49 | def _entity(data_dir, owner, mode): |
| 50 | return object_records.create_collection_record( |
| 51 | "entities", {"name": mode.title(), "owner_id": owner, "mode": mode}, |
| 52 | base_dir=data_dir, actor="test", |
| 53 | )["id"] |
| 54 | |
| 55 | |
| 56 | def _accounts_for(data_dir, entity_id): |
| 57 | return [a for a in object_records.read_collection_records("fin_accounts", base_dir=data_dir) |
| 58 | if a.get("entity_id") == entity_id] |
| 59 | |
| 60 | |
| 61 | def test_each_mode_seeds_its_chart_owned_and_scoped(tmp_path, monkeypatch): |
| 62 | data_dir = _install(tmp_path) |
| 63 | mod = _load_object(data_dir, monkeypatch) |
| 64 | # double_entry: 29, plus Barter Clearing and the two contra-COGS accounts |
| 65 | # (decision 6), Deferred Revenue, Interest Income and Bank Fees (decisions 4, 7). |
| 66 | for mode, count in (("simple", 2), ("standard", 15), ("double_entry", 35)): |
| 67 | eid = _entity(data_dir, "7", mode) |
| 68 | status, body = _post(mod, "7", eid) |
| 69 | assert status == 200 and body["mode"] == mode and body["created"] == count |
| 70 | accts = _accounts_for(data_dir, eid) |
| 71 | assert len(accts) == count |
| 72 | # every account is owned by the caller and scoped to this entity |
| 73 | assert all(a["owner_id"] == "7" and a["entity_id"] == eid for a in accts) |
| 74 | # a known account carries its predecessor code |
| 75 | assert all(a["account_type"] in {"asset", "liability", "equity", "income", "expense"} for a in accts) |
| 76 | |
| 77 | |
| 78 | def test_idempotent_second_run_creates_nothing(tmp_path, monkeypatch): |
| 79 | data_dir = _install(tmp_path) |
| 80 | mod = _load_object(data_dir, monkeypatch) |
| 81 | eid = _entity(data_dir, "7", "standard") |
| 82 | _post(mod, "7", eid) |
| 83 | status, body = _post(mod, "7", eid) |
| 84 | assert status == 200 and body["created"] == 0 and body["skipped"] == 15 |
| 85 | assert len(_accounts_for(data_dir, eid)) == 15 # no duplicates |
| 86 | |
| 87 | |
| 88 | def test_owner_gated(tmp_path, monkeypatch): |
| 89 | data_dir = _install(tmp_path) |
| 90 | mod = _load_object(data_dir, monkeypatch) |
| 91 | eid = _entity(data_dir, "7", "standard") |
| 92 | # another owner -> 403, nothing created |
| 93 | status, _ = _post(mod, "8", eid) |
| 94 | assert status == 403 |
| 95 | assert _accounts_for(data_dir, eid) == [] |
| 96 | # anonymous -> 401 |
| 97 | status, _ = _post(mod, None, eid) |
| 98 | assert status == 401 |
| 99 | |
| 100 | |
| 101 | def test_unknown_entity_and_missing_id(tmp_path, monkeypatch): |
| 102 | data_dir = _install(tmp_path) |
| 103 | mod = _load_object(data_dir, monkeypatch) |
| 104 | assert _post(mod, "7", "00000000-0000-4000-8000-000000000000")[0] == 404 |
| 105 | assert mod.POST({"_identity": {"user_id": "7"}})["status"] == 400 # no entity_id |
| 106 | |
| 107 | |
| 108 | def test_package_wires_the_object_route_and_rule(): |
| 109 | package = object_packages.get_package("app-finance", root=PACKAGES_ROOT) |
| 110 | assert "site_setup_accounts" in {o["id"] for o in package["objects"]} |
| 111 | assert "site_routes" in {e["collection"] for e in package["seed"]} |
| 112 | # dependencies are normalized to {id, version} dicts by object_packages. |
| 113 | assert any(d.get("id") == "app-entities" for d in package.get("dependencies", [])) |
| 114 | rules = json.loads((APP_FINANCE_DIR / "permissions" / "rules.json").read_text())["rules"] |
| 115 | assert any(r.get("object_id") == "site_setup_accounts" and r.get("actions") == ["execute"] |
| 116 | for r in rules) |
| 117 | import csv |
| 118 | routes = list(csv.DictReader(open(APP_FINANCE_DIR / "seed" / "site_routes.tsv"), delimiter="\t")) |
| 119 | assert routes[0]["pattern"] == "/finance/setup-accounts" |
| 120 | assert routes[0]["object_id"] == "site_setup_accounts" |
| 121 | |
| 122 | |
| 123 | def test_mcp_setup_finance_accounts_verb_routes_to_the_object(): |
| 124 | method, path, _query, body = object_mcp.tool_route( |
| 125 | "setup_finance_accounts", {"entity_id": "E1"}, |
| 126 | ) |
| 127 | assert method == "POST" |
| 128 | assert path == "/admin/objects/site_setup_accounts/execute" |
| 129 | assert json.loads(body)["payload"] == {"entity_id": "E1"} |
| 130 | |
| 131 | |
| 132 | def test_mcp_setup_finance_accounts_requires_entity_id(): |
| 133 | import pytest |
| 134 | |
| 135 | with pytest.raises(ValueError): |
| 136 | object_mcp.tool_route("setup_finance_accounts", {}) |
| 137 | |
| 138 | |
| 139 | def test_double_entry_setup_maps_the_settings_its_accounts_answer(tmp_path, monkeypatch): |
| 140 | """Decision 7 (owner, 2026-11-30): the chart gains Interest Income 4910 |
| 141 | and Bank Fees 5840, and setup points reconcile.journal.interest_account |
| 142 | (and the other keys an account in the chart answers) at them. A fresh |
| 143 | double-entry box read "Books NOT READY" for banking because the chart |
| 144 | had no interest account to point at. An operator's own mapping is never |
| 145 | overwritten.""" |
| 146 | import object_books_status |
| 147 | data_dir = _install(tmp_path) |
| 148 | object_packages.install_package("app-settings", root=PACKAGES_ROOT, base_dir=data_dir, |
| 149 | object_roots=[tmp_path / "objects"], allow_replace=True) |
| 150 | object_records.create_collection_record( |
| 151 | "app_settings", {"key": "reconcile.journal.fees_account", "value": "mine"}, |
| 152 | base_dir=data_dir) |
| 153 | mod = _load_object(data_dir, monkeypatch) |
| 154 | eid = _entity(data_dir, "7", "double_entry") |
| 155 | status, body = _post(mod, "7", eid) |
| 156 | assert status == 200 |
| 157 | accounts = {a["name"]: a for a in _accounts_for(data_dir, eid)} |
| 158 | assert (accounts["Interest Income"]["code"], accounts["Interest Income"]["account_type"]) == ( |
| 159 | "4910", "income") |
| 160 | assert (accounts["Bank Fees"]["code"], accounts["Bank Fees"]["account_type"]) == ("5840", "expense") |
| 161 | settings = {r["key"]: r["value"] for r in object_records.read_collection_records( |
| 162 | "app_settings", base_dir=data_dir)} |
| 163 | assert settings["reconcile.journal.interest_account"] == accounts["Interest Income"]["id"] |
| 164 | assert settings["reconcile.journal.fees_account"] == "mine" # left alone |
| 165 | assert settings["payments.journal.tax_account"] == accounts["Sales Tax Payable"]["id"] |
| 166 | assert settings["payments.journal.deferred_revenue_account"] == accounts["Deferred Revenue"]["id"] |
| 167 | assert "reconcile.journal.fees_account" not in body["settings"] |
| 168 | checks = {c["id"]: c["status"] for c in object_books_status._banking_checks( |
| 169 | object_books_status._load_settings(data_dir), object_books_status._account_ids(data_dir))} |
| 170 | assert checks["banking.interest_account"] == "ok" |