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.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_app_finance_setup_accounts.py · 5.1 KB · 134 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 | for mode, count in (("simple", 2), ("standard", 15), ("double_entry", 29)): |
| 65 | eid = _entity(data_dir, "7", mode) |
| 66 | status, body = _post(mod, "7", eid) |
| 67 | assert status == 200 and body["mode"] == mode and body["created"] == count |
| 68 | accts = _accounts_for(data_dir, eid) |
| 69 | assert len(accts) == count |
| 70 | # every account is owned by the caller and scoped to this entity |
| 71 | assert all(a["owner_id"] == "7" and a["entity_id"] == eid for a in accts) |
| 72 | # a known account carries its predecessor code |
| 73 | assert all(a["account_type"] in {"asset", "liability", "equity", "income", "expense"} for a in accts) |
| 74 | |
| 75 | |
| 76 | def test_idempotent_second_run_creates_nothing(tmp_path, monkeypatch): |
| 77 | data_dir = _install(tmp_path) |
| 78 | mod = _load_object(data_dir, monkeypatch) |
| 79 | eid = _entity(data_dir, "7", "standard") |
| 80 | _post(mod, "7", eid) |
| 81 | status, body = _post(mod, "7", eid) |
| 82 | assert status == 200 and body["created"] == 0 and body["skipped"] == 15 |
| 83 | assert len(_accounts_for(data_dir, eid)) == 15 # no duplicates |
| 84 | |
| 85 | |
| 86 | def test_owner_gated(tmp_path, monkeypatch): |
| 87 | data_dir = _install(tmp_path) |
| 88 | mod = _load_object(data_dir, monkeypatch) |
| 89 | eid = _entity(data_dir, "7", "standard") |
| 90 | # another owner -> 403, nothing created |
| 91 | status, _ = _post(mod, "8", eid) |
| 92 | assert status == 403 |
| 93 | assert _accounts_for(data_dir, eid) == [] |
| 94 | # anonymous -> 401 |
| 95 | status, _ = _post(mod, None, eid) |
| 96 | assert status == 401 |
| 97 | |
| 98 | |
| 99 | def test_unknown_entity_and_missing_id(tmp_path, monkeypatch): |
| 100 | data_dir = _install(tmp_path) |
| 101 | mod = _load_object(data_dir, monkeypatch) |
| 102 | assert _post(mod, "7", "00000000-0000-4000-8000-000000000000")[0] == 404 |
| 103 | assert mod.POST({"_identity": {"user_id": "7"}})["status"] == 400 # no entity_id |
| 104 | |
| 105 | |
| 106 | def test_package_wires_the_object_route_and_rule(): |
| 107 | package = object_packages.get_package("app-finance", root=PACKAGES_ROOT) |
| 108 | assert "site_setup_accounts" in {o["id"] for o in package["objects"]} |
| 109 | assert "site_routes" in {e["collection"] for e in package["seed"]} |
| 110 | # dependencies are normalized to {id, version} dicts by object_packages. |
| 111 | assert any(d.get("id") == "app-entities" for d in package.get("dependencies", [])) |
| 112 | rules = json.loads((APP_FINANCE_DIR / "permissions" / "rules.json").read_text())["rules"] |
| 113 | assert any(r.get("object_id") == "site_setup_accounts" and r.get("actions") == ["execute"] |
| 114 | for r in rules) |
| 115 | import csv |
| 116 | routes = list(csv.DictReader(open(APP_FINANCE_DIR / "seed" / "site_routes.tsv"), delimiter="\t")) |
| 117 | assert routes[0]["pattern"] == "/finance/setup-accounts" |
| 118 | assert routes[0]["object_id"] == "site_setup_accounts" |
| 119 | |
| 120 | |
| 121 | def test_mcp_setup_finance_accounts_verb_routes_to_the_object(): |
| 122 | method, path, _query, body = object_mcp.tool_route( |
| 123 | "setup_finance_accounts", {"entity_id": "E1"}, |
| 124 | ) |
| 125 | assert method == "POST" |
| 126 | assert path == "/admin/objects/site_setup_accounts/execute" |
| 127 | assert json.loads(body)["payload"] == {"entity_id": "E1"} |
| 128 | |
| 129 | |
| 130 | def test_mcp_setup_finance_accounts_requires_entity_id(): |
| 131 | import pytest |
| 132 | |
| 133 | with pytest.raises(ValueError): |
| 134 | object_mcp.tool_route("setup_finance_accounts", {}) |