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_entities_package.py · 5.9 KB · 137 lines
| 1 | """Structural tests for packages/app-entities (65 multi-entity, slice 1: the |
| 2 | entity concept itself). An entity is a distinct set of books under one login, |
| 3 | owner-scoped -- a faithful port of the predecessor's Entity model (its `user` |
| 4 | FK -> owner_id here). URL-addressable via seeded 59 views (site_view_render), |
| 5 | no bespoke page. Later slices thread entity_id onto the finance/commerce |
| 6 | collections and add the switcher + chart-of-accounts auto-seed. |
| 7 | """ |
| 8 | |
| 9 | import json |
| 10 | from pathlib import Path |
| 11 | |
| 12 | import object_packages |
| 13 | import object_permissions |
| 14 | import object_schemas |
| 15 | from package_pins import assert_includes_in_order |
| 16 | |
| 17 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 18 | APP_ENTITIES_DIR = PACKAGES_ROOT / "app-entities" |
| 19 | |
| 20 | |
| 21 | def _schema(): |
| 22 | return json.loads((APP_ENTITIES_DIR / "schemas" / "entities.json").read_text()) |
| 23 | |
| 24 | |
| 25 | def _seed_rows(name): |
| 26 | import csv |
| 27 | |
| 28 | with open(APP_ENTITIES_DIR / "seed" / f"{name}.tsv", newline="") as fh: |
| 29 | return list(csv.DictReader(fh, delimiter="\t")) |
| 30 | |
| 31 | |
| 32 | def test_manifest_normalizes(): |
| 33 | package = object_packages.get_package("app-entities", root=PACKAGES_ROOT) |
| 34 | assert package["id"] == "app-entities" |
| 35 | assert {s["collection"] for s in package["schemas"]} >= {"entities"} |
| 36 | assert package["objects"] == [] # no bespoke page -- pages are seeded views |
| 37 | assert {e["collection"] for e in package["seed"]} >= {"views", "site_routes"} |
| 38 | |
| 39 | |
| 40 | def test_entities_schema_is_owner_scoped_and_faithful_to_the_predecessor(): |
| 41 | schema = _schema() |
| 42 | by_name = {f["name"]: f for f in schema["fields"]} |
| 43 | # owner_id (the predecessor's `user` FK), required. |
| 44 | assert by_name["owner_id"]["required"] is True |
| 45 | assert by_name["name"]["required"] is True |
| 46 | # Faithful enum vocabularies (predecessor MODE/BUSINESS_TYPE/STATUS choices). |
| 47 | assert_includes_in_order(by_name["mode"]["enum"], ["simple", "standard", "double_entry"]) |
| 48 | assert by_name["mode"]["default"] == "simple" |
| 49 | assert_includes_in_order(by_name["business_type"]["enum"], ["freelancer", "small_business", "nonprofit", "other"]) |
| 50 | assert_includes_in_order(by_name["status"]["enum"], ["active", "closed", "archived"]) |
| 51 | assert by_name["base_currency"]["default"] == "USD" |
| 52 | assert by_name["created_at"]["read_only"] is True |
| 53 | |
| 54 | |
| 55 | def test_pages_are_seeded_views_url_addressable_no_bespoke_object(): |
| 56 | views = {r["id"]: r for r in _seed_rows("views")} |
| 57 | assert set(views) == {"view_entities_list", "view_entities_detail"} |
| 58 | # /entities -> a create form + a browsable list (generic blocks). |
| 59 | list_blocks = json.loads(views["view_entities_list"]["blocks"]) |
| 60 | assert [b["kind"] for b in list_blocks] == ["form", "list"] |
| 61 | assert views["view_entities_list"]["route"] == "/entities" |
| 62 | # /entities/{id} -> owner-editable detail (URL-addressable per record). |
| 63 | detail_blocks = json.loads(views["view_entities_detail"]["blocks"]) |
| 64 | assert len(detail_blocks) == 1 and detail_blocks[0]["kind"] == "detail" |
| 65 | assert detail_blocks[0]["editable"] is True and detail_blocks[0]["deletable"] is True |
| 66 | assert views["view_entities_detail"]["route"] == "/entities/{entity_id}" |
| 67 | |
| 68 | routes = {r["pattern"]: r for r in _seed_rows("site_routes")} |
| 69 | assert routes["/entities"]["object_id"] == "site_view_render" |
| 70 | assert routes["/entities/{entity_id}"]["object_id"] == "site_view_render" |
| 71 | |
| 72 | |
| 73 | def _policy(): |
| 74 | payload = json.loads((APP_ENTITIES_DIR / "permissions" / "rules.json").read_text()) |
| 75 | return object_permissions.policy_from_dict( |
| 76 | {"access_mode": "role_based", "rules": payload["rules"]} |
| 77 | ) |
| 78 | |
| 79 | |
| 80 | def test_owner_can_crud_their_own_entities(): |
| 81 | policy = _policy() |
| 82 | subject = object_permissions.PermissionSubject(user_id="7") |
| 83 | record = {"owner_id": "7", "name": "Acme Books"} |
| 84 | for action in (object_permissions.CREATE, object_permissions.READ, |
| 85 | object_permissions.UPDATE, object_permissions.DELETE): |
| 86 | decision = object_permissions.check_permission( |
| 87 | subject, action, policy=policy, collection="entities", record=record, |
| 88 | ) |
| 89 | assert decision.allowed is True, action |
| 90 | |
| 91 | |
| 92 | def test_others_and_anonymous_cannot_read_someone_elses_books(): |
| 93 | policy = _policy() |
| 94 | record = {"owner_id": "7", "name": "Acme Books"} |
| 95 | other = object_permissions.PermissionSubject(user_id="8") |
| 96 | assert object_permissions.check_permission( |
| 97 | other, object_permissions.READ, policy=policy, collection="entities", record=record, |
| 98 | ).allowed is False |
| 99 | assert object_permissions.check_permission( |
| 100 | None, object_permissions.READ, policy=policy, collection="entities", record=record, |
| 101 | ).allowed is False |
| 102 | |
| 103 | |
| 104 | def test_install_loads_the_schema(tmp_path): |
| 105 | data_dir = tmp_path / "data" |
| 106 | object_root = tmp_path / "objects" |
| 107 | object_root.mkdir() |
| 108 | object_packages.install_package( |
| 109 | "app-entities", root=PACKAGES_ROOT, base_dir=data_dir, object_roots=[object_root], |
| 110 | ) |
| 111 | schema = object_schemas.get_schema("entities", base_dir=data_dir) |
| 112 | assert schema["name"] == "entities" |
| 113 | |
| 114 | |
| 115 | def test_dry_run_is_safe(tmp_path): |
| 116 | object_root = tmp_path / "objects" |
| 117 | object_root.mkdir() |
| 118 | plan = object_packages.dry_run_package( |
| 119 | "app-entities", root=PACKAGES_ROOT, base_dir=tmp_path / "data", object_roots=[object_root], |
| 120 | ) |
| 121 | assert plan["safe_to_install"] is True |
| 122 | assert plan["warnings"] == [] |
| 123 | |
| 124 | |
| 125 | def test_nav_ships_an_entity_switcher(): |
| 126 | """65 multi-entity: the nav bar (app-theme/nav.py) gains a "Books" switcher |
| 127 | that lists the user's entities and stores the chosen one in localStorage |
| 128 | (window.dbbasicEntity) -- the client-held current entity the list/form |
| 129 | generators scope by. Hidden when the user has no entities.""" |
| 130 | nav = (PACKAGES_ROOT / "app-theme" / "objects" / "site" / "nav.py").read_text() |
| 131 | assert "nav-books" in nav |
| 132 | assert "window.dbbasicEntity" in nav |
| 133 | assert 'ENTITY_KEY = "dbbasic_entity"' in nav |
| 134 | assert "/collections/entities/records" in nav |
| 135 | # An "All entities" option clears the scope, and there's a way to manage. |
| 136 | assert "All entities" in nav |
| 137 | assert 'href="/entities"' in nav |