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_notify_package.py · 2.5 KB · 53 lines
| 1 | """Structural tests for packages/app-notify (12 notify): the notify_rules |
| 2 | config collection (admin-owned) + the seeded task-assigned example rule.""" |
| 3 | |
| 4 | import json |
| 5 | from pathlib import Path |
| 6 | |
| 7 | import object_packages |
| 8 | import object_permissions |
| 9 | |
| 10 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 11 | APP_NOTIFY_DIR = PACKAGES_ROOT / "app-notify" |
| 12 | |
| 13 | |
| 14 | def test_manifest_and_schema(): |
| 15 | package = object_packages.get_package("app-notify", root=PACKAGES_ROOT) |
| 16 | assert package["id"] == "app-notify" |
| 17 | assert {s["collection"] for s in package["schemas"]} == {"notify_rules"} |
| 18 | assert package["objects"] == [] # engine is object_notify + the daemon pass, not a page |
| 19 | assert {e["collection"] for e in package["seed"]} == {"notify_rules"} |
| 20 | schema = json.loads((APP_NOTIFY_DIR / "schemas" / "notify_rules.json").read_text()) |
| 21 | names = {f["name"] for f in schema["fields"]} |
| 22 | assert {"event_pattern", "match", "recipients", "suppress_self", "channels"} <= names |
| 23 | |
| 24 | |
| 25 | def test_notify_rules_is_admin_owned(): |
| 26 | payload = json.loads((APP_NOTIFY_DIR / "permissions" / "rules.json").read_text()) |
| 27 | policy = object_permissions.policy_from_dict({"access_mode": "role_based", "rules": payload["rules"]}) |
| 28 | admin = object_permissions.PermissionSubject(user_id="1", roles=("admin",)) |
| 29 | plain = object_permissions.PermissionSubject(user_id="7") |
| 30 | for action in (object_permissions.CREATE, object_permissions.READ, |
| 31 | object_permissions.UPDATE, object_permissions.DELETE): |
| 32 | assert object_permissions.check_permission(admin, action, policy=policy, collection="notify_rules").allowed is True |
| 33 | assert object_permissions.check_permission(plain, action, policy=policy, collection="notify_rules").allowed is False |
| 34 | |
| 35 | |
| 36 | def test_seed_ships_the_task_assigned_example_rule(): |
| 37 | import csv |
| 38 | rows = list(csv.DictReader(open(APP_NOTIFY_DIR / "seed" / "notify_rules.tsv"), delimiter="\t")) |
| 39 | assert len(rows) == 1 |
| 40 | rule = rows[0] |
| 41 | assert rule["event_pattern"] == "tasks.record.updated" |
| 42 | assert json.loads(rule["match"]) == {"status": "assigned"} |
| 43 | assert json.loads(rule["recipients"]) == {"mode": "field", "field": "assigned_to"} |
| 44 | channels = json.loads(rule["channels"]) |
| 45 | assert any(c["channel"] == "in_app" for c in channels) |
| 46 | |
| 47 | |
| 48 | def test_dry_run_is_safe(tmp_path): |
| 49 | object_root = tmp_path / "objects" |
| 50 | object_root.mkdir() |
| 51 | plan = object_packages.dry_run_package( |
| 52 | "app-notify", root=PACKAGES_ROOT, base_dir=tmp_path / "data", object_roots=[object_root]) |
| 53 | assert plan["safe_to_install"] is True and plan["warnings"] == [] |