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.
test_app_messaging_package.py · 18.1 KB · 438 lines
| 1 | """Structural tests for packages/app-messaging (message_threads, messages, |
| 2 | message_recipients, message_drafts). |
| 3 | |
| 4 | Mirrors the package/schema/permission testing conventions used for |
| 5 | packages/app-invoices and packages/app-worker in |
| 6 | tests/test_app_invoices_package.py and tests/test_app_worker_package.py. |
| 7 | app-messaging has no HANDLES handler and no behavior module, so this is |
| 8 | the whole suite. |
| 9 | """ |
| 10 | |
| 11 | import json |
| 12 | import re |
| 13 | from pathlib import Path |
| 14 | |
| 15 | import object_packages |
| 16 | import object_permissions |
| 17 | import object_schemas |
| 18 | from package_pins import assert_includes_in_order |
| 19 | |
| 20 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 21 | APP_MESSAGING_DIR = PACKAGES_ROOT / "app-messaging" |
| 22 | |
| 23 | |
| 24 | def _threads_schema(): |
| 25 | return json.loads((APP_MESSAGING_DIR / "schemas" / "message_threads.json").read_text()) |
| 26 | |
| 27 | |
| 28 | def _messages_schema(): |
| 29 | return json.loads((APP_MESSAGING_DIR / "schemas" / "messages.json").read_text()) |
| 30 | |
| 31 | |
| 32 | def _recipients_schema(): |
| 33 | return json.loads((APP_MESSAGING_DIR / "schemas" / "message_recipients.json").read_text()) |
| 34 | |
| 35 | |
| 36 | def _drafts_schema(): |
| 37 | return json.loads((APP_MESSAGING_DIR / "schemas" / "message_drafts.json").read_text()) |
| 38 | |
| 39 | |
| 40 | def test_get_package_normalizes_app_messaging_manifest(): |
| 41 | package = object_packages.get_package("app-messaging", root=PACKAGES_ROOT) |
| 42 | |
| 43 | assert package["id"] == "app-messaging" |
| 44 | assert package["name"] == "Messaging" |
| 45 | assert {schema["collection"] for schema in package["schemas"]} >= { |
| 46 | "message_threads", |
| 47 | "messages", |
| 48 | "message_recipients", |
| 49 | "message_drafts", |
| 50 | } |
| 51 | assert {obj["id"] for obj in package["objects"]} >= { |
| 52 | "site_inbox", |
| 53 | } |
| 54 | assert {"path": "permissions/rules.json"} in package["permissions"] |
| 55 | assert {entry["collection"] for entry in package["seed"]} >= { |
| 56 | "message_threads", |
| 57 | "messages", |
| 58 | "message_recipients", |
| 59 | "message_drafts", |
| 60 | "views", |
| 61 | "site_routes", |
| 62 | } |
| 63 | assert {"id": "app-views", "version": None} in package["dependencies"] |
| 64 | |
| 65 | |
| 66 | def test_dry_run_app_messaging_package_is_safe(tmp_path): |
| 67 | object_root = tmp_path / "objects" |
| 68 | object_root.mkdir() |
| 69 | |
| 70 | plan = object_packages.dry_run_package( |
| 71 | "app-messaging", |
| 72 | root=PACKAGES_ROOT, |
| 73 | base_dir=tmp_path / "data", |
| 74 | object_roots=[object_root], |
| 75 | ) |
| 76 | |
| 77 | assert plan["safe_to_install"] is True |
| 78 | assert plan["warnings"] == [] |
| 79 | assert {schema["collection"] for schema in plan["schemas"]} >= { |
| 80 | "message_threads", |
| 81 | "messages", |
| 82 | "message_recipients", |
| 83 | "message_drafts", |
| 84 | } |
| 85 | |
| 86 | |
| 87 | def test_install_app_messaging_package_loads_schemas(tmp_path): |
| 88 | data_dir = tmp_path / "data" |
| 89 | object_root = tmp_path / "objects" |
| 90 | object_root.mkdir() |
| 91 | |
| 92 | object_packages.install_package( |
| 93 | "app-messaging", |
| 94 | root=PACKAGES_ROOT, |
| 95 | base_dir=data_dir, |
| 96 | object_roots=[object_root], |
| 97 | ) |
| 98 | |
| 99 | threads_schema = object_schemas.get_schema("message_threads", base_dir=data_dir) |
| 100 | messages_schema = object_schemas.get_schema("messages", base_dir=data_dir) |
| 101 | recipients_schema = object_schemas.get_schema("message_recipients", base_dir=data_dir) |
| 102 | drafts_schema = object_schemas.get_schema("message_drafts", base_dir=data_dir) |
| 103 | |
| 104 | assert threads_schema["name"] == "message_threads" |
| 105 | assert messages_schema["name"] == "messages" |
| 106 | assert recipients_schema["name"] == "message_recipients" |
| 107 | assert drafts_schema["name"] == "message_drafts" |
| 108 | assert (object_root / "site" / "inbox.py").is_file() |
| 109 | # site_message_thread was retrofitted away: the thread permalink is now |
| 110 | # a seeded 59 detail view (site_view_render, in app-views), not a |
| 111 | # bespoke object shipped by this package. |
| 112 | assert not (object_root / "site" / "message_thread.py").exists() |
| 113 | |
| 114 | |
| 115 | def test_schema_json_files_are_valid_and_versioned(): |
| 116 | for name in ("message_threads", "messages", "message_recipients", "message_drafts"): |
| 117 | payload = json.loads((APP_MESSAGING_DIR / "schemas" / f"{name}.json").read_text()) |
| 118 | assert payload["name"] == name |
| 119 | assert payload["version"] >= 1 |
| 120 | |
| 121 | |
| 122 | def test_four_collections_present_with_expected_field_names(): |
| 123 | thread_fields = [f["name"] for f in _threads_schema()["fields"]] |
| 124 | assert_includes_in_order(thread_fields, [ |
| 125 | "id", "thread_type", "subject", "participant_summary", "last_message_at", |
| 126 | "message_count", "is_read", "is_starred", "is_archived", "is_trashed", |
| 127 | "labels", "owner_id", "created_at", |
| 128 | ]) |
| 129 | |
| 130 | message_fields = [f["name"] for f in _messages_schema()["fields"]] |
| 131 | assert_includes_in_order(message_fields, [ |
| 132 | "id", "thread_id", "direction", "from_address", "subject", "body_text", |
| 133 | "is_read", "sent_at", "received_at", "in_reply_to", "external_message_id", |
| 134 | "owner_id", "created_at", |
| 135 | ]) |
| 136 | |
| 137 | recipient_fields = [f["name"] for f in _recipients_schema()["fields"]] |
| 138 | assert_includes_in_order(recipient_fields, [ |
| 139 | "id", "message_id", "kind", "address", "owner_id", "created_at", |
| 140 | ]) |
| 141 | |
| 142 | draft_fields = [f["name"] for f in _drafts_schema()["fields"]] |
| 143 | assert_includes_in_order(draft_fields, [ |
| 144 | "id", "thread_id", "to_addresses", "cc_addresses", "subject", "body_text", |
| 145 | "owner_id", "created_at", |
| 146 | ]) |
| 147 | |
| 148 | |
| 149 | def test_no_money_fields_anywhere(): |
| 150 | """SCOPE: the source messaging model carried no monetary fields. No |
| 151 | *_cents field, and no float/currency-typed field, exists anywhere in |
| 152 | this package -- unlike app-invoices, money was never part of this |
| 153 | model. |
| 154 | """ |
| 155 | for schema in (_threads_schema(), _messages_schema(), _recipients_schema(), _drafts_schema()): |
| 156 | for field in schema["fields"]: |
| 157 | assert "cents" not in field["name"] |
| 158 | assert field.get("type") not in {"float", "currency"} |
| 159 | |
| 160 | |
| 161 | def test_message_threads_carries_the_source_flags(): |
| 162 | by_name = {f["name"]: f for f in _threads_schema()["fields"]} |
| 163 | assert by_name["thread_type"]["type"] == "enum" |
| 164 | assert_includes_in_order(by_name["thread_type"]["enum"], ["internal", "email"]) |
| 165 | assert by_name["thread_type"]["default"] == "email" |
| 166 | |
| 167 | for name in ("is_read", "is_starred", "is_archived", "is_trashed"): |
| 168 | assert by_name[name]["type"] == "boolean" |
| 169 | |
| 170 | assert by_name["message_count"]["default"] == "0" |
| 171 | assert by_name["labels"]["type"] == "text" |
| 172 | assert "relation" not in by_name["labels"] |
| 173 | |
| 174 | |
| 175 | def test_message_threads_participant_summary_is_free_text_not_a_relation(): |
| 176 | """SCOPE RULE: the source's real participants M2M collapses to a |
| 177 | denormalized display snapshot here, same trade-off app-invoices' |
| 178 | customer_name makes for its customer relation -- no participants |
| 179 | collection or M2M join is built. |
| 180 | """ |
| 181 | by_name = {f["name"]: f for f in _threads_schema()["fields"]} |
| 182 | assert by_name["participant_summary"]["type"] == "text" |
| 183 | assert "relation" not in by_name["participant_summary"] |
| 184 | |
| 185 | |
| 186 | def test_message_threads_forms_and_views(): |
| 187 | schema = _threads_schema() |
| 188 | assert_includes_in_order(schema["forms"]["default"]["fields"], ["thread_type", "subject", "labels"]) |
| 189 | # Moderation-style flags (star/archive/trash/read) are toggled by |
| 190 | # dedicated buttons in objects/site/message_thread.py, not through the |
| 191 | # generic form -- same posture as forum_topics keeping |
| 192 | # is_pinned/is_locked/is_solved out of its own forms.default. |
| 193 | for flag in ("is_read", "is_starred", "is_archived", "is_trashed"): |
| 194 | assert flag not in schema["forms"]["default"]["fields"] |
| 195 | |
| 196 | |
| 197 | def test_messages_direction_enum_and_relation_to_thread(): |
| 198 | by_name = {f["name"]: f for f in _messages_schema()["fields"]} |
| 199 | assert by_name["direction"]["type"] == "enum" |
| 200 | assert_includes_in_order(by_name["direction"]["enum"], ["inbound", "outbound"]) |
| 201 | assert by_name["direction"]["required"] is True |
| 202 | |
| 203 | assert by_name["thread_id"]["type"] == "relation" |
| 204 | assert by_name["thread_id"]["relation"]["collection"] == "message_threads" |
| 205 | assert by_name["thread_id"]["required"] is True |
| 206 | |
| 207 | |
| 208 | def test_messages_deferred_transport_linkage_fields_present(): |
| 209 | """Fields carried for the deferred IMAP linkage (task brief + source |
| 210 | audit), never populated by anything in this package. |
| 211 | """ |
| 212 | by_name = {f["name"]: f for f in _messages_schema()["fields"]} |
| 213 | assert "in_reply_to" in by_name |
| 214 | assert "external_message_id" in by_name |
| 215 | assert "sent_at" in by_name |
| 216 | assert "received_at" in by_name |
| 217 | |
| 218 | |
| 219 | def test_messages_storage_is_append_and_other_collections_are_classic(): |
| 220 | """The storage choice tied to the audit: a message's content is |
| 221 | write-once (only is_read flips after creation) -- the same profile |
| 222 | that put app-thread's thread_comments and app-catalog's stock_moves on |
| 223 | append storage. message_threads/message_recipients/message_drafts are |
| 224 | smaller, more frequently flag-toggled containers and stay classic |
| 225 | (default), matching forum_topics' choice. |
| 226 | """ |
| 227 | assert _messages_schema()["storage"] == "append" |
| 228 | assert "storage" not in _threads_schema() |
| 229 | assert "storage" not in _recipients_schema() |
| 230 | assert "storage" not in _drafts_schema() |
| 231 | |
| 232 | |
| 233 | def test_message_recipients_normalized_not_flattened_onto_message(): |
| 234 | """SCOPE RULE: the source normalized recipients into their own |
| 235 | MessageRecipient table rather than flattening a To/Cc/Bcc address list |
| 236 | onto Message -- this package follows that same split. messages.json |
| 237 | itself carries no to_addresses/cc field. |
| 238 | """ |
| 239 | message_field_names = {f["name"] for f in _messages_schema()["fields"]} |
| 240 | assert "to_addresses" not in message_field_names |
| 241 | assert "cc" not in message_field_names |
| 242 | assert "cc_addresses" not in message_field_names |
| 243 | |
| 244 | by_name = {f["name"]: f for f in _recipients_schema()["fields"]} |
| 245 | assert by_name["message_id"]["relation"]["collection"] == "messages" |
| 246 | assert by_name["message_id"]["required"] is True |
| 247 | assert by_name["kind"]["type"] == "enum" |
| 248 | assert_includes_in_order(by_name["kind"]["enum"], ["to", "cc", "bcc"]) |
| 249 | assert by_name["address"]["required"] is True |
| 250 | |
| 251 | |
| 252 | def test_message_drafts_is_its_own_collection_not_a_status_on_messages(): |
| 253 | """SCOPE RULE: the source kept drafts as their own model (MessageDraft), |
| 254 | not a status value on Message -- this package follows that split. |
| 255 | messages.json's direction enum has no "draft" value, and there is no |
| 256 | status field on messages at all. |
| 257 | """ |
| 258 | by_name = {f["name"]: f for f in _messages_schema()["fields"]} |
| 259 | assert "status" not in by_name |
| 260 | assert "draft" not in by_name["direction"]["enum"] |
| 261 | |
| 262 | draft_by_name = {f["name"]: f for f in _drafts_schema()["fields"]} |
| 263 | assert draft_by_name["thread_id"]["relation"]["collection"] == "message_threads" |
| 264 | assert draft_by_name["thread_id"].get("required") is not True # optional: new compose has no thread yet |
| 265 | |
| 266 | |
| 267 | def test_no_ai_triage_or_transport_fields_invented(): |
| 268 | """SCOPE RULE: dove's AI classification (state/summary/commitments/ |
| 269 | draft_reply), sender reputation, a rules engine, and mailcow/Twilio |
| 270 | fields are all separate, infra/AI-bound, and out of scope -- none of |
| 271 | their vocabulary should leak into this package's schemas as invented |
| 272 | fields. |
| 273 | """ |
| 274 | banned_field_names = { |
| 275 | "ai_summary", "is_ai_summarized", "classification", "sender_trust", |
| 276 | "twilio_sid", "call_transcript", "mailcow_id", "imap_uid", |
| 277 | } |
| 278 | for schema in (_threads_schema(), _messages_schema(), _recipients_schema(), _drafts_schema()): |
| 279 | field_names = {f["name"] for f in schema["fields"]} |
| 280 | assert not (field_names & banned_field_names), ( |
| 281 | f"{schema['name']} carries an invented out-of-scope field" |
| 282 | ) |
| 283 | |
| 284 | |
| 285 | def _app_messaging_policy(): |
| 286 | payload = json.loads((APP_MESSAGING_DIR / "permissions" / "rules.json").read_text()) |
| 287 | return object_permissions.policy_from_dict({"access_mode": "role_based", "rules": payload["rules"]}) |
| 288 | |
| 289 | |
| 290 | def test_owner_can_crud_own_records_on_every_collection(): |
| 291 | policy = _app_messaging_policy() |
| 292 | subject = object_permissions.PermissionSubject(user_id="7") |
| 293 | records = { |
| 294 | "message_threads": {"owner_id": "7", "subject": "Hello"}, |
| 295 | "messages": {"owner_id": "7", "thread_id": "t1", "direction": "inbound", "from_address": "a@example.com"}, |
| 296 | "message_recipients": {"owner_id": "7", "message_id": "m1", "address": "a@example.com"}, |
| 297 | "message_drafts": {"owner_id": "7", "subject": "Draft"}, |
| 298 | } |
| 299 | |
| 300 | for collection, record in records.items(): |
| 301 | for action in ( |
| 302 | object_permissions.CREATE, |
| 303 | object_permissions.READ, |
| 304 | object_permissions.UPDATE, |
| 305 | object_permissions.DELETE, |
| 306 | ): |
| 307 | decision = object_permissions.check_permission( |
| 308 | subject, action, policy=policy, collection=collection, record=record |
| 309 | ) |
| 310 | assert decision.allowed is True, f"owner {action} should be allowed on {collection}" |
| 311 | |
| 312 | |
| 313 | def test_others_cannot_read_or_write_someone_elses_records(): |
| 314 | """Cross-owner denial: a different signed-in user gets nothing on any |
| 315 | collection -- a mailbox is private, not just write-protected. |
| 316 | """ |
| 317 | policy = _app_messaging_policy() |
| 318 | subject = object_permissions.PermissionSubject(user_id="8") |
| 319 | records = { |
| 320 | "message_threads": {"owner_id": "7", "subject": "Hello"}, |
| 321 | "messages": {"owner_id": "7", "thread_id": "t1", "direction": "inbound", "from_address": "a@example.com"}, |
| 322 | "message_recipients": {"owner_id": "7", "message_id": "m1", "address": "a@example.com"}, |
| 323 | "message_drafts": {"owner_id": "7", "subject": "Draft"}, |
| 324 | } |
| 325 | |
| 326 | for collection, record in records.items(): |
| 327 | for action in ( |
| 328 | object_permissions.READ, |
| 329 | object_permissions.UPDATE, |
| 330 | object_permissions.DELETE, |
| 331 | ): |
| 332 | decision = object_permissions.check_permission( |
| 333 | subject, action, policy=policy, collection=collection, record=record |
| 334 | ) |
| 335 | assert decision.allowed is False, f"non-owner {action} should be denied on {collection}" |
| 336 | |
| 337 | |
| 338 | def test_anonymous_cannot_read_any_collection(): |
| 339 | """No public read rule is granted on any collection in this package -- |
| 340 | a mailbox is private, unlike app-forum/app-worker's public-read |
| 341 | collections. |
| 342 | """ |
| 343 | policy = _app_messaging_policy() |
| 344 | records = { |
| 345 | "message_threads": {"owner_id": "7", "subject": "Hello"}, |
| 346 | "messages": {"owner_id": "7", "thread_id": "t1", "direction": "inbound", "from_address": "a@example.com"}, |
| 347 | "message_recipients": {"owner_id": "7", "message_id": "m1", "address": "a@example.com"}, |
| 348 | "message_drafts": {"owner_id": "7", "subject": "Draft"}, |
| 349 | } |
| 350 | |
| 351 | for collection, record in records.items(): |
| 352 | decision = object_permissions.check_permission( |
| 353 | None, object_permissions.READ, policy=policy, collection=collection, record=record |
| 354 | ) |
| 355 | assert decision.allowed is False, f"anonymous read should be denied on {collection}" |
| 356 | |
| 357 | |
| 358 | def test_anonymous_cannot_create_on_any_collection(): |
| 359 | policy = _app_messaging_policy() |
| 360 | records = { |
| 361 | "message_threads": {"subject": "Hello"}, |
| 362 | "messages": {"thread_id": "t1", "direction": "inbound", "from_address": "a@example.com"}, |
| 363 | "message_recipients": {"message_id": "m1", "address": "a@example.com"}, |
| 364 | "message_drafts": {"subject": "Draft"}, |
| 365 | } |
| 366 | |
| 367 | for collection, record in records.items(): |
| 368 | decision = object_permissions.check_permission( |
| 369 | None, object_permissions.CREATE, policy=policy, collection=collection, record=record |
| 370 | ) |
| 371 | assert decision.allowed is False, f"anonymous create should be denied on {collection}" |
| 372 | |
| 373 | |
| 374 | def test_messaging_pages_are_publicly_executable(): |
| 375 | """Public execute on the *page object* (it shows a sign-in prompt to |
| 376 | visitors), never public read on the *collections* -- same split |
| 377 | app-invoices uses for site_invoices/site_invoice_view. The thread |
| 378 | permalink is no longer a bespoke object here -- it's a seeded 59 detail |
| 379 | view served by site_view_render (app-views), whose own execute rule |
| 380 | lives in that package, not this one. |
| 381 | """ |
| 382 | policy = _app_messaging_policy() |
| 383 | |
| 384 | decision = object_permissions.check_permission( |
| 385 | None, object_permissions.EXECUTE, policy=policy, object_id="site_inbox" |
| 386 | ) |
| 387 | assert decision.allowed is True |
| 388 | |
| 389 | |
| 390 | def test_seed_tsvs_have_no_data_rows_and_match_schema_field_order(): |
| 391 | """Header-only seeds, matching the established precedent (app-tasks, |
| 392 | app-notes, app-invoices, app-forum, app-worker all ship header-only |
| 393 | seeds). |
| 394 | """ |
| 395 | for name, schema in ( |
| 396 | ("message_threads", _threads_schema()), |
| 397 | ("messages", _messages_schema()), |
| 398 | ("message_recipients", _recipients_schema()), |
| 399 | ("message_drafts", _drafts_schema()), |
| 400 | ): |
| 401 | path = APP_MESSAGING_DIR / "seed" / f"{name}.tsv" |
| 402 | lines = path.read_text().splitlines() |
| 403 | assert len(lines) == 1, f"{name}.tsv should be header-only" |
| 404 | header = lines[0].split("\t") |
| 405 | assert header == [f["name"] for f in schema["fields"]] |
| 406 | |
| 407 | |
| 408 | def test_no_disallowed_org_names_leak_into_the_package(): |
| 409 | """Public repo hygiene: no internal org/codename references anywhere |
| 410 | in this package's source, including in any source-doc path reference |
| 411 | (this package describes its source only as "a private |
| 412 | predecessor-system audit, not part of this repo"). |
| 413 | """ |
| 414 | # Built from fragments so this guard file itself stays clean of the |
| 415 | # very internal names it forbids. |
| 416 | banned = re.compile( |
| 417 | "|".join([r"\b" + "q" + "9" + r"\b", "ask" + "robots", r"\b" + "wo" + "ld" + r"\b"]), |
| 418 | re.IGNORECASE, |
| 419 | ) |
| 420 | for path in APP_MESSAGING_DIR.rglob("*"): |
| 421 | if "__pycache__" in path.parts: |
| 422 | continue # compiled bytes can match a name by chance |
| 423 | if path.is_dir(): |
| 424 | continue |
| 425 | text = path.read_text(encoding="utf-8", errors="ignore") |
| 426 | assert not banned.search(text), f"disallowed reference found in {path}" |
| 427 | |
| 428 | |
| 429 | def test_package_does_not_touch_app_shell_or_app_theme(): |
| 430 | """This package's brief explicitly forbids editing packages/app-shell |
| 431 | or packages/app-theme (nav/home wiring is the main loop's job). This |
| 432 | test only asserts app-messaging's own manifest declares no dependency |
| 433 | on them; it cannot detect a stray edit elsewhere in the repo, but |
| 434 | keeps the intent on record. |
| 435 | """ |
| 436 | manifest = json.loads((APP_MESSAGING_DIR / "dbbasic-package.json").read_text()) |
| 437 | assert "app-shell" not in manifest.get("dependencies", []) |
| 438 | assert "app-theme" not in manifest.get("dependencies", []) |