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_mail_sync_package.py · 6.6 KB · 142 lines
| 1 | """packages/app-mail-sync: what it declares, and the lines it must not cross. |
| 2 | |
| 3 | The declarations are the product here as much as the code: a schedule that |
| 4 | lives only on a running box vanishes with the box, a password field on a |
| 5 | schema ends up in every backup, and a transport field on app-messaging's |
| 6 | schemas is the scope creep app-messaging's own tests refuse. Each is held |
| 7 | by a test, asserted on the manifest and schemas themselves. |
| 8 | """ |
| 9 | |
| 10 | import json |
| 11 | import re |
| 12 | from pathlib import Path |
| 13 | |
| 14 | import object_packages |
| 15 | import object_permissions |
| 16 | import object_schemas |
| 17 | import object_state |
| 18 | |
| 19 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 20 | PACKAGE_DIR = PACKAGES_ROOT / "app-mail-sync" |
| 21 | COLLECTIONS = ("mail_accounts", "mail_sync_state", "mail_synced_messages") |
| 22 | |
| 23 | |
| 24 | def _manifest(): |
| 25 | return json.loads((PACKAGE_DIR / "dbbasic-package.json").read_text()) |
| 26 | |
| 27 | |
| 28 | def _schema(name): |
| 29 | return json.loads((PACKAGE_DIR / "schemas" / f"{name}.json").read_text()) |
| 30 | |
| 31 | |
| 32 | def _policy(): |
| 33 | rules = json.loads((PACKAGE_DIR / "permissions" / "rules.json").read_text())["rules"] |
| 34 | return object_permissions.policy_from_dict({"access_mode": "role_based", "rules": rules}) |
| 35 | |
| 36 | |
| 37 | def test_the_manifest_normalizes_and_depends_on_app_messaging(): |
| 38 | package = object_packages.get_package("app-mail-sync", root=PACKAGES_ROOT) |
| 39 | assert {s["collection"] for s in package["schemas"]} == set(COLLECTIONS) |
| 40 | assert {o["id"] for o in package["objects"]} == {"system_mail_sync", "system_mail_sync_attention", |
| 41 | "site_mail_accounts"} |
| 42 | assert package["dependencies"] == [{"id": "app-messaging", "version": None}] |
| 43 | |
| 44 | |
| 45 | def test_the_sync_is_a_declared_schedule_so_a_rebuilt_box_keeps_it(): |
| 46 | schedules = _manifest()["schedules"] |
| 47 | assert [(s["id"], s["object_id"], s["schedule"]) for s in schedules] == [ |
| 48 | ("mail_sync_every_two_minutes", "system_mail_sync", "*/2 * * * *")] |
| 49 | |
| 50 | |
| 51 | def test_failing_accounts_are_a_declared_attention_queue(): |
| 52 | attention = _manifest()["attention"] |
| 53 | assert [(a["id"], a["object_id"], a["path"], a["severity"]) for a in attention] == [ |
| 54 | ("mail_accounts_failing", "system_mail_sync_attention", "/mail/accounts", |
| 55 | "warning")] |
| 56 | |
| 57 | |
| 58 | def test_install_registers_schemas_objects_and_the_schedule(tmp_path): |
| 59 | data_dir = tmp_path / "data" |
| 60 | objects = tmp_path / "objects" |
| 61 | objects.mkdir() |
| 62 | for dependency in ("app-views", "app-messaging"): |
| 63 | object_packages.install_package(dependency, root=PACKAGES_ROOT, base_dir=data_dir, |
| 64 | object_roots=[objects]) |
| 65 | plan = object_packages.dry_run_package("app-mail-sync", root=PACKAGES_ROOT, |
| 66 | base_dir=data_dir, object_roots=[objects]) |
| 67 | assert plan["safe_to_install"] is True, plan |
| 68 | object_packages.install_package("app-mail-sync", root=PACKAGES_ROOT, base_dir=data_dir, |
| 69 | object_roots=[objects]) |
| 70 | for name in COLLECTIONS: |
| 71 | assert object_schemas.get_schema(name, base_dir=data_dir)["name"] == name |
| 72 | assert (objects / "system" / "mail_sync.py").is_file() |
| 73 | assert (objects / "site" / "mail_accounts.py").is_file() |
| 74 | state = object_state.get_object_state("scheduler", base_dir=data_dir) |
| 75 | task = json.loads(state["task_mail_sync_every_two_minutes"]) |
| 76 | assert task["object_id"] == "system_mail_sync" and task["status"] == "active" |
| 77 | |
| 78 | |
| 79 | def test_no_schema_has_a_field_that_could_hold_a_secret(): |
| 80 | """The password lives in the owner's write-only vault. A field here |
| 81 | would put it in exports, backups and every API read.""" |
| 82 | for name in COLLECTIONS: |
| 83 | fields = {f["name"] for f in _schema(name)["fields"]} |
| 84 | assert not {f for f in fields if re.search(r"pass|secret|token|credential|api_key", f)}, name |
| 85 | |
| 86 | |
| 87 | def test_sync_rows_are_append_and_the_cursor_is_classic(): |
| 88 | assert _schema("mail_synced_messages").get("storage") == "append" |
| 89 | assert "storage" not in _schema("mail_sync_state") |
| 90 | |
| 91 | |
| 92 | def test_seeds_are_header_only_and_match_the_schemas(): |
| 93 | for name in COLLECTIONS: |
| 94 | lines = (PACKAGE_DIR / "seed" / f"{name}.tsv").read_text().splitlines() |
| 95 | assert lines == ["\t".join(f["name"] for f in _schema(name)["fields"])] |
| 96 | |
| 97 | |
| 98 | def test_the_transport_facts_live_here_not_on_app_messaging(): |
| 99 | """app-messaging's schemas stay a mailbox; its own test bans imap_uid |
| 100 | and friends there. This package is where they go instead.""" |
| 101 | fields = {f["name"] for f in _schema("mail_synced_messages")["fields"]} |
| 102 | assert {"account_id", "folder", "uid", "message_id", "dedupe_key", "references", |
| 103 | "list_unsubscribe", "list_unsubscribe_post", "list_id", "precedence"} <= fields |
| 104 | for schema_path in (PACKAGES_ROOT / "app-messaging" / "schemas").glob("*.json"): |
| 105 | names = {f["name"] for f in json.loads(schema_path.read_text())["fields"]} |
| 106 | assert not names & {"uid", "folder", "account_id", "dedupe_key", "list_unsubscribe"} |
| 107 | |
| 108 | |
| 109 | def test_each_person_sees_only_their_own_accounts_and_sync_rows(): |
| 110 | policy = _policy() |
| 111 | me = object_permissions.PermissionSubject(user_id="7") |
| 112 | other = object_permissions.PermissionSubject(user_id="8") |
| 113 | for name in COLLECTIONS: |
| 114 | mine = {"owner_id": "7", "account_id": "a1", "message_id": "m1"} |
| 115 | assert object_permissions.check_permission( |
| 116 | me, object_permissions.READ, policy=policy, collection=name, record=mine).allowed |
| 117 | assert not object_permissions.check_permission( |
| 118 | other, object_permissions.READ, policy=policy, collection=name, record=mine).allowed |
| 119 | assert not object_permissions.check_permission( |
| 120 | None, object_permissions.READ, policy=policy, collection=name, record=mine).allowed |
| 121 | |
| 122 | |
| 123 | def test_only_the_sync_writes_the_cursor_and_the_transport_rows(): |
| 124 | """A hand-edited cursor skips or repeats mail.""" |
| 125 | policy = _policy() |
| 126 | me = object_permissions.PermissionSubject(user_id="7") |
| 127 | for name in ("mail_sync_state", "mail_synced_messages"): |
| 128 | for action in (object_permissions.CREATE, object_permissions.UPDATE, |
| 129 | object_permissions.DELETE): |
| 130 | assert not object_permissions.check_permission( |
| 131 | me, action, policy=policy, collection=name, |
| 132 | record={"owner_id": "7"}).allowed, (name, action) |
| 133 | assert object_permissions.check_permission( |
| 134 | me, object_permissions.UPDATE, policy=policy, collection="mail_accounts", |
| 135 | record={"owner_id": "7"}).allowed |
| 136 | |
| 137 | |
| 138 | def test_no_internal_names_leak_into_the_package(): |
| 139 | banned = re.compile("|".join([r"\b" + "q" + "9" + r"\b", "ask" + "robots"]), re.IGNORECASE) |
| 140 | for path in PACKAGE_DIR.rglob("*"): |
| 141 | if path.is_file(): |
| 142 | assert not banned.search(path.read_text(errors="ignore")), path |