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_messaging_detail_retrofit.py · 4.6 KB · 114 lines
| 1 | """Stage-6 retrofit of packages/app-messaging: the bespoke |
| 2 | site_message_thread.py page is replaced by a seeded 59 detail view composed |
| 3 | of four GENERIC block renderers -- an editable+deletable `detail` block (the |
| 4 | thread; star/archive/trash/read toggles become boolean edits in the owner's |
| 5 | edit form, not one-click buttons), a `related` block (FLAT messages by |
| 6 | thread_id -- messages were never nested/self-threaded, unlike app-forum's |
| 7 | replies), a `markdown` block carrying the "nothing is sent" note, and a |
| 8 | `form` block with a FK locked to the page's thread (Save Draft). A mailbox |
| 9 | stays owner-private throughout: the row-filtered collection API only ever |
| 10 | returns real data to the signed-in owner, same as the page it replaces. |
| 11 | """ |
| 12 | |
| 13 | import json |
| 14 | from pathlib import Path |
| 15 | |
| 16 | import object_packages |
| 17 | import object_permissions |
| 18 | |
| 19 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 20 | APP_MESSAGING_DIR = PACKAGES_ROOT / "app-messaging" |
| 21 | |
| 22 | |
| 23 | def _seed_rows(name): |
| 24 | import csv |
| 25 | |
| 26 | with open(APP_MESSAGING_DIR / "seed" / f"{name}.tsv", newline="") as fh: |
| 27 | return list(csv.DictReader(fh, delimiter="\t")) |
| 28 | |
| 29 | |
| 30 | def test_manifest_drops_bespoke_thread_page_and_seeds_the_detail_view(): |
| 31 | package = object_packages.get_package("app-messaging", root=PACKAGES_ROOT) |
| 32 | assert {obj["id"] for obj in package["objects"]} == {"site_inbox"} |
| 33 | assert {entry["collection"] for entry in package["seed"]} == { |
| 34 | "message_threads", "messages", "message_recipients", "message_drafts", |
| 35 | "views", "site_routes", |
| 36 | } |
| 37 | assert {dep["id"] for dep in package["dependencies"]} == {"app-views"} |
| 38 | |
| 39 | |
| 40 | def test_message_thread_object_file_is_deleted(): |
| 41 | assert not (APP_MESSAGING_DIR / "objects" / "site" / "message_thread.py").exists() |
| 42 | |
| 43 | |
| 44 | def test_thread_view_composes_detail_flat_related_markdown_and_fk_locked_form(): |
| 45 | rows = _seed_rows("views") |
| 46 | assert len(rows) == 1 |
| 47 | view = rows[0] |
| 48 | assert view["id"] == "view_inbox_thread" |
| 49 | assert view["route"] == "/inbox/{thread_id}" |
| 50 | blocks = json.loads(view["blocks"]) |
| 51 | kinds = [b["kind"] for b in blocks] |
| 52 | assert kinds == ["detail", "related", "markdown", "form"] |
| 53 | |
| 54 | detail, related, markdown, form = blocks |
| 55 | |
| 56 | # 1. editable+deletable thread detail |
| 57 | assert detail["collection"] == "message_threads" |
| 58 | assert detail["record_id"] == "$record_id" |
| 59 | assert detail["editable"] is True and detail["deletable"] is True |
| 60 | assert detail["delete_redirect"] == "/inbox" |
| 61 | |
| 62 | # 2. FLAT messages (a plain related list by thread_id -- no nesting) |
| 63 | assert related["collection"] == "messages" |
| 64 | assert related["fk_field"] == "thread_id" |
| 65 | assert related["match"] == "$record_id" |
| 66 | |
| 67 | # 3. the "nothing is sent" note preserved as a markdown block |
| 68 | assert "nothing is sent" in markdown["text"] |
| 69 | assert "IMAP" in markdown["text"] and "SMTP" in markdown["text"] |
| 70 | |
| 71 | # 4. draft compose with thread_id locked to the page's thread |
| 72 | assert form["collection"] == "message_drafts" |
| 73 | assert form["fixed"] == {"thread_id": "$record_id"} |
| 74 | assert form["title"] == "Save Draft" |
| 75 | |
| 76 | |
| 77 | def test_permalink_route_points_to_view_render(): |
| 78 | rows = _seed_rows("site_routes") |
| 79 | assert len(rows) == 1 |
| 80 | assert rows[0]["pattern"] == "/inbox/{thread_id}" |
| 81 | assert rows[0]["object_id"] == "site_view_render" |
| 82 | |
| 83 | |
| 84 | def test_permissions_no_longer_reference_the_removed_object(): |
| 85 | payload = json.loads((APP_MESSAGING_DIR / "permissions" / "rules.json").read_text()) |
| 86 | object_ids = {rule.get("object_id") for rule in payload["rules"]} |
| 87 | assert "site_message_thread" not in object_ids |
| 88 | assert "site_inbox" in object_ids |
| 89 | |
| 90 | |
| 91 | def test_no_public_read_survives_the_retrofit(): |
| 92 | """A mailbox stays private: the retrofit must not have added a public |
| 93 | read rule on any collection, and the removed object's public-execute |
| 94 | rule must be gone too (already covered above).""" |
| 95 | payload = json.loads((APP_MESSAGING_DIR / "permissions" / "rules.json").read_text()) |
| 96 | policy = object_permissions.policy_from_dict( |
| 97 | {"access_mode": "role_based", "rules": payload["rules"]} |
| 98 | ) |
| 99 | for collection in ("message_threads", "messages", "message_recipients", "message_drafts"): |
| 100 | decision = object_permissions.check_permission( |
| 101 | None, object_permissions.READ, policy=policy, collection=collection, |
| 102 | record={"owner_id": "7"}, |
| 103 | ) |
| 104 | assert decision.allowed is False, f"anonymous read should be denied on {collection}" |
| 105 | |
| 106 | |
| 107 | def test_dry_run_is_safe(tmp_path): |
| 108 | object_root = tmp_path / "objects" |
| 109 | object_root.mkdir() |
| 110 | plan = object_packages.dry_run_package( |
| 111 | "app-messaging", root=PACKAGES_ROOT, base_dir=tmp_path / "data", object_roots=[object_root], |
| 112 | ) |
| 113 | assert plan["safe_to_install"] is True |
| 114 | assert plan["warnings"] == [] |