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