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