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_materialize_package.py · 11.1 KB · 271 lines
| 1 | """Structural + permission tests for packages/app-materialize |
| 2 | (plan/vocabulary/61-materialize-spec.md). |
| 3 | |
| 4 | Mirrors tests/test_app_rollup_package.py's package/schema/permission |
| 5 | conventions. The worked end-to-end examples (recurring journal, |
| 6 | depreciation, CreateWork) and mapping-vocabulary unit coverage live in |
| 7 | tests/test_object_materialize.py instead; this file exercises the real |
| 8 | package manifest, real permission policy, and the real installed |
| 9 | materialize_seed.py's HANDLES-regeneration mechanism. |
| 10 | """ |
| 11 | |
| 12 | import json |
| 13 | import re |
| 14 | from pathlib import Path |
| 15 | |
| 16 | import object_handlers |
| 17 | import object_materialize |
| 18 | import object_packages |
| 19 | import object_permissions |
| 20 | import object_records |
| 21 | import object_schemas |
| 22 | |
| 23 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 24 | APP_MATERIALIZE_DIR = PACKAGES_ROOT / "app-materialize" |
| 25 | |
| 26 | |
| 27 | def _write_schema(data_dir, collection, fields, storage=None): |
| 28 | payload = {"name": collection, "fields": fields} |
| 29 | if storage: |
| 30 | payload["storage"] = storage |
| 31 | path = data_dir / "schemas" / f"{collection}.json" |
| 32 | path.parent.mkdir(parents=True, exist_ok=True) |
| 33 | path.write_text(json.dumps(payload)) |
| 34 | return path |
| 35 | |
| 36 | |
| 37 | # --- Package manifest / install -------------------------------------------- |
| 38 | |
| 39 | def test_get_package_normalizes_app_materialize_manifest(): |
| 40 | package = object_packages.get_package("app-materialize", root=PACKAGES_ROOT) |
| 41 | |
| 42 | assert package["id"] == "app-materialize" |
| 43 | assert package["name"] == "Materialize" |
| 44 | assert package["seed"] == [] |
| 45 | assert package["permissions"] == [{"path": "permissions/rules.json"}] |
| 46 | assert {schema["collection"] for schema in package["schemas"]} == {"materialize_definitions"} |
| 47 | assert {obj["id"] for obj in package["objects"]} == { |
| 48 | "system_materialize_run", "system_materialize_seed", "site_materialize_run_button", |
| 49 | } |
| 50 | |
| 51 | |
| 52 | def test_dry_run_app_materialize_package_is_safe(tmp_path): |
| 53 | object_root = tmp_path / "objects" |
| 54 | object_root.mkdir() |
| 55 | |
| 56 | plan = object_packages.dry_run_package( |
| 57 | "app-materialize", root=PACKAGES_ROOT, base_dir=tmp_path / "data", object_roots=[object_root], |
| 58 | ) |
| 59 | |
| 60 | assert plan["safe_to_install"] is True |
| 61 | assert plan["warnings"] == [] |
| 62 | assert {schema["collection"] for schema in plan["schemas"]} == {"materialize_definitions"} |
| 63 | |
| 64 | |
| 65 | def test_install_app_materialize_package_loads_schema_and_objects(tmp_path): |
| 66 | data_dir = tmp_path / "data" |
| 67 | object_root = tmp_path / "objects" |
| 68 | object_root.mkdir() |
| 69 | |
| 70 | object_packages.install_package( |
| 71 | "app-materialize", root=PACKAGES_ROOT, base_dir=data_dir, object_roots=[object_root], |
| 72 | ) |
| 73 | |
| 74 | schema = object_schemas.get_schema("materialize_definitions", base_dir=data_dir) |
| 75 | assert schema["name"] == "materialize_definitions" |
| 76 | field_names = [f["name"] for f in schema["fields"]] |
| 77 | assert field_names == [ |
| 78 | "id", "name", "source_collection", "source_filter", "trigger", "output_collection", |
| 79 | "child_collection", "child_source_field", "child_link_field", "idempotency_key", |
| 80 | "mapping", "balance_check", "debit_account_id", "credit_account_id", "actor", |
| 81 | "last_run_at", "enabled", "block", |
| 82 | ] |
| 83 | by_name = {f["name"]: f for f in schema["fields"]} |
| 84 | assert by_name["last_run_at"].get("read_only") is True |
| 85 | assert by_name["enabled"].get("default") == "true" |
| 86 | assert by_name["block"].get("default") == "false" |
| 87 | assert schema["views"]["list_mode"] == "table" |
| 88 | |
| 89 | assert (object_root / "system" / "materialize_run.py").is_file() |
| 90 | assert (object_root / "system" / "materialize_seed.py").is_file() |
| 91 | assert (object_root / "site" / "materialize_run_button.py").is_file() |
| 92 | |
| 93 | |
| 94 | def test_schema_json_file_is_valid_and_versioned(): |
| 95 | payload = json.loads((APP_MATERIALIZE_DIR / "schemas" / "materialize_definitions.json").read_text()) |
| 96 | assert payload["name"] == "materialize_definitions" |
| 97 | assert payload["version"] == 1 |
| 98 | assert payload["views"]["list_mode"] == "table" |
| 99 | |
| 100 | |
| 101 | def test_no_disallowed_org_names_leak_into_the_package(): |
| 102 | """Public repo hygiene: no internal org/codename references anywhere |
| 103 | in this package's source (same guard as tests/test_app_rollup_package.py). |
| 104 | """ |
| 105 | banned = re.compile( |
| 106 | "|".join([r"\b" + "q" + "9" + r"\b", "ask" + "robots", r"\b" + "wo" + "ld" + r"\b"]), |
| 107 | re.IGNORECASE, |
| 108 | ) |
| 109 | for path in APP_MATERIALIZE_DIR.rglob("*"): |
| 110 | if path.is_dir(): |
| 111 | continue |
| 112 | text = path.read_text(encoding="utf-8", errors="ignore") |
| 113 | assert not banned.search(text), f"disallowed reference found in {path}" |
| 114 | |
| 115 | |
| 116 | def test_materialize_seed_object_source_declares_no_handles(): |
| 117 | """HANDLES ships empty and stays empty: v1 does NOT rewrite the installed |
| 118 | object at runtime to track the definition set (no self-modifying source |
| 119 | under the poll loop). Event-mode runs via the manual path instead; the |
| 120 | shipped file never hand-lists collections. |
| 121 | """ |
| 122 | text = (APP_MATERIALIZE_DIR / "objects" / "system" / "materialize_seed.py").read_text() |
| 123 | assert object_handlers.extract_handles(text) == [] |
| 124 | |
| 125 | |
| 126 | # --- Permissions: materialize_definitions is admin-owned, full stop ------- |
| 127 | |
| 128 | def _app_materialize_policy(extra_rules=()): |
| 129 | payload = json.loads((APP_MATERIALIZE_DIR / "permissions" / "rules.json").read_text()) |
| 130 | rules = list(payload["rules"]) + list(extra_rules) |
| 131 | return object_permissions.policy_from_dict({"access_mode": "role_based", "rules": rules}) |
| 132 | |
| 133 | |
| 134 | def test_materialize_definitions_denied_to_registered_non_admin_for_every_action(): |
| 135 | policy = _app_materialize_policy() |
| 136 | subject = object_permissions.PermissionSubject(user_id="7") |
| 137 | record = {"id": "m1", "name": "Recurring journals", "source_collection": "fin_recurring"} |
| 138 | |
| 139 | for action in ( |
| 140 | object_permissions.CREATE, object_permissions.READ, |
| 141 | object_permissions.UPDATE, object_permissions.DELETE, |
| 142 | ): |
| 143 | decision = object_permissions.check_permission( |
| 144 | subject, action, policy=policy, collection="materialize_definitions", record=record, |
| 145 | ) |
| 146 | assert decision.allowed is False, action |
| 147 | |
| 148 | |
| 149 | def test_materialize_definitions_unreachable_by_anonymous_reads(): |
| 150 | policy = _app_materialize_policy() |
| 151 | decision = object_permissions.check_permission( |
| 152 | None, object_permissions.READ, policy=policy, collection="materialize_definitions", |
| 153 | ) |
| 154 | assert decision.allowed is False |
| 155 | |
| 156 | |
| 157 | def test_materialize_definitions_allowed_for_admin_role(): |
| 158 | policy = _app_materialize_policy() |
| 159 | admin = object_permissions.PermissionSubject(user_id="1", roles=("admin",)) |
| 160 | |
| 161 | for action in ( |
| 162 | object_permissions.CREATE, object_permissions.READ, |
| 163 | object_permissions.UPDATE, object_permissions.DELETE, |
| 164 | ): |
| 165 | decision = object_permissions.check_permission( |
| 166 | admin, action, policy=policy, collection="materialize_definitions", |
| 167 | ) |
| 168 | assert decision.allowed is True, action |
| 169 | |
| 170 | |
| 171 | def test_materialize_run_execute_denied_to_non_admin_and_allowed_for_admin(): |
| 172 | policy = _app_materialize_policy() |
| 173 | non_admin = object_permissions.PermissionSubject(user_id="7") |
| 174 | admin = object_permissions.PermissionSubject(user_id="1", roles=("admin",)) |
| 175 | |
| 176 | denied = object_permissions.check_permission( |
| 177 | non_admin, object_permissions.EXECUTE, policy=policy, object_id="system_materialize_run", |
| 178 | ) |
| 179 | allowed = object_permissions.check_permission( |
| 180 | admin, object_permissions.EXECUTE, policy=policy, object_id="system_materialize_run", |
| 181 | ) |
| 182 | assert denied.allowed is False |
| 183 | assert allowed.allowed is True |
| 184 | |
| 185 | |
| 186 | # --- Event-mode fixtures + CreateWork worked example --------------------- |
| 187 | # (No HANDLES-rewrite tests: v1 deliberately has no runtime source rewriter; |
| 188 | # event-mode runs through the manual path, exercised by the worked example |
| 189 | # below.) |
| 190 | |
| 191 | def _install_with_tasks_fixture(data_dir, object_root): |
| 192 | object_packages.install_package( |
| 193 | "app-materialize", root=PACKAGES_ROOT, base_dir=data_dir, object_roots=[object_root], |
| 194 | ) |
| 195 | _write_schema(data_dir, "tasks", [ |
| 196 | {"name": "id"}, {"name": "title", "type": "text"}, |
| 197 | {"name": "template_id", "type": "text", "relation": {"collection": "templates", "display_field": "name"}}, |
| 198 | ]) |
| 199 | |
| 200 | |
| 201 | def _event_definition(**overrides): |
| 202 | base = { |
| 203 | "id": "matgen_task_seed", |
| 204 | "name": "CreateWork seed", |
| 205 | "source_collection": "tasks", |
| 206 | "trigger": json.dumps({"mode": "event", "on": "record.created"}), |
| 207 | "output_collection": "tasks", |
| 208 | "idempotency_key": "{definition_id}_{source_id}", |
| 209 | "mapping": json.dumps({}), |
| 210 | "enabled": "true", |
| 211 | "block": "false", |
| 212 | } |
| 213 | base.update(overrides) |
| 214 | return base |
| 215 | |
| 216 | |
| 217 | def test_event_mode_definition_runs_through_the_manual_path(tmp_path): |
| 218 | """Event-mode is valid but v1 runs it manually (no auto HANDLES): a |
| 219 | definition with trigger.mode == "event" generates correctly when driven |
| 220 | through generate_one_event, the same call the (inert) seed handler would |
| 221 | make -- proving degrade-to-manual works without any source rewriting.""" |
| 222 | data_dir = tmp_path / "data" |
| 223 | object_root = tmp_path / "objects" |
| 224 | object_root.mkdir() |
| 225 | _install_with_tasks_fixture(data_dir, object_root) |
| 226 | definition = _event_definition() |
| 227 | object_records.create_collection_record( |
| 228 | "materialize_definitions", definition, base_dir=data_dir, roots=[object_root], actor="admin", |
| 229 | ) |
| 230 | # The seed object ships inert -- nothing rewrote it. |
| 231 | installed_text = (object_root / "system" / "materialize_seed.py").read_text() |
| 232 | assert object_handlers.extract_handles(installed_text) == [] |
| 233 | |
| 234 | # compute_event_handles still reports what a future deliberate sync WOULD |
| 235 | # wire, but it is pure computation with no side effect on the object. |
| 236 | assert object_materialize.compute_event_handles(base_dir=data_dir, roots=[object_root]) == [ |
| 237 | "tasks.record.created" |
| 238 | ] |
| 239 | assert (object_root / "system" / "materialize_seed.py").read_text() == installed_text |
| 240 | |
| 241 | |
| 242 | # --- Worked example: CreateWork end-to-end through the real package ------- |
| 243 | |
| 244 | def test_worked_example_creatework_seed_via_materialize_run(tmp_path): |
| 245 | data_dir = tmp_path / "data" |
| 246 | object_root = tmp_path / "objects" |
| 247 | object_root.mkdir() |
| 248 | _install_with_tasks_fixture(data_dir, object_root) |
| 249 | _write_schema(data_dir, "templates", [ |
| 250 | {"name": "id"}, {"name": "name", "type": "text"}, {"name": "default_values", "type": "textarea"}, |
| 251 | ]) |
| 252 | object_records.create_collection_record( |
| 253 | "templates", |
| 254 | {"id": "tmpl1", "name": "Onboarding", "default_values": json.dumps({"title": "Seeded title"})}, |
| 255 | base_dir=data_dir, roots=[object_root], |
| 256 | ) |
| 257 | object_records.create_collection_record( |
| 258 | "tasks", {"id": "task1", "title": "", "template_id": "tmpl1"}, base_dir=data_dir, roots=[object_root], |
| 259 | ) |
| 260 | object_records.create_collection_record( |
| 261 | "materialize_definitions", _event_definition(), base_dir=data_dir, roots=[object_root], actor="admin", |
| 262 | ) |
| 263 | |
| 264 | result = object_materialize.generate_definition( |
| 265 | object_records.get_collection_record("materialize_definitions", "matgen_task_seed", base_dir=data_dir, roots=[object_root]), |
| 266 | base_dir=data_dir, roots=[object_root], |
| 267 | ) |
| 268 | assert result["generated"] == 1 |
| 269 | |
| 270 | updated = object_records.get_collection_record("tasks", "task1", base_dir=data_dir, roots=[object_root]) |
| 271 | assert updated["title"] == "Seeded title" |