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_contacts_package.py · 13.1 KB · 316 lines
| 1 | """Structural tests for packages/app-contacts (contacts, organizations, |
| 2 | interactions, tags). |
| 3 | |
| 4 | Mirrors the package/schema/permission testing conventions used for |
| 5 | packages/app-finance and packages/app-invoices in tests/test_app_finance_ |
| 6 | package.py and tests/test_app_invoices_package.py. There was no dedicated |
| 7 | test file for app-contacts before this one; it fills that gap while also |
| 8 | covering the CRM-depth field additions (lead_status/job_title on contacts; |
| 9 | kind=task/outcome/duration_minutes/subject on interactions) reconciled |
| 10 | against a private predecessor-system audit, not part of this repo. |
| 11 | """ |
| 12 | |
| 13 | import json |
| 14 | import re |
| 15 | from pathlib import Path |
| 16 | |
| 17 | import object_packages |
| 18 | import object_permissions |
| 19 | import object_schemas |
| 20 | |
| 21 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 22 | APP_CONTACTS_DIR = PACKAGES_ROOT / "app-contacts" |
| 23 | |
| 24 | _SCHEMA_NAMES = ("contacts", "organizations", "interactions", "tags") |
| 25 | |
| 26 | |
| 27 | def _schema(name): |
| 28 | return json.loads((APP_CONTACTS_DIR / "schemas" / f"{name}.json").read_text()) |
| 29 | |
| 30 | |
| 31 | def test_get_package_normalizes_app_contacts_manifest(): |
| 32 | package = object_packages.get_package("app-contacts", root=PACKAGES_ROOT) |
| 33 | |
| 34 | assert package["id"] == "app-contacts" |
| 35 | assert package["name"] == "Contacts" |
| 36 | assert {schema["collection"] for schema in package["schemas"]} == set(_SCHEMA_NAMES) |
| 37 | # 0.2.0 deletes the bespoke /contacts list page object: it held no |
| 38 | # business logic and is now a seeded VIEW record over the generic |
| 39 | # `list` block. |
| 40 | assert package["objects"] == [] |
| 41 | assert package["permissions"] == [{"path": "permissions/rules.json"}] |
| 42 | # Plus "views"/"site_routes": one 59 detail view + route per collection, |
| 43 | # seeded into app-views'/site routing's own shared collections (a soft |
| 44 | # reference, like template_id's into app-templates -- see |
| 45 | # dbbasic-package.json's description). |
| 46 | assert {entry["collection"] for entry in package["seed"]} == set(_SCHEMA_NAMES) | { |
| 47 | "views", "site_routes", |
| 48 | } |
| 49 | # No migration mechanism is exercised here: additive field-adds don't |
| 50 | # need one (see test_additive_field_adds_need_no_migration_entry below). |
| 51 | assert package["migrations"] == [] |
| 52 | |
| 53 | |
| 54 | def test_dry_run_app_contacts_package_is_safe(tmp_path): |
| 55 | object_root = tmp_path / "objects" |
| 56 | object_root.mkdir() |
| 57 | |
| 58 | plan = object_packages.dry_run_package( |
| 59 | "app-contacts", |
| 60 | root=PACKAGES_ROOT, |
| 61 | base_dir=tmp_path / "data", |
| 62 | object_roots=[object_root], |
| 63 | ) |
| 64 | |
| 65 | assert plan["safe_to_install"] is True |
| 66 | assert plan["warnings"] == [] |
| 67 | assert {schema["collection"] for schema in plan["schemas"]} == set(_SCHEMA_NAMES) |
| 68 | |
| 69 | |
| 70 | def test_install_app_contacts_package_loads_schemas(tmp_path): |
| 71 | data_dir = tmp_path / "data" |
| 72 | object_root = tmp_path / "objects" |
| 73 | object_root.mkdir() |
| 74 | |
| 75 | object_packages.install_package( |
| 76 | "app-contacts", root=PACKAGES_ROOT, base_dir=data_dir, object_roots=[object_root], |
| 77 | ) |
| 78 | |
| 79 | for name in _SCHEMA_NAMES: |
| 80 | schema = object_schemas.get_schema(name, base_dir=data_dir) |
| 81 | assert schema["name"] == name |
| 82 | |
| 83 | # contacts.py was deleted (0.2.0): the /contacts list page is now a |
| 84 | # seeded VIEW record over the generic `list` block. |
| 85 | assert not (object_root / "site" / "contacts.py").exists() |
| 86 | |
| 87 | |
| 88 | def test_schema_json_files_are_valid(): |
| 89 | for name in _SCHEMA_NAMES: |
| 90 | payload = _schema(name) |
| 91 | assert payload["name"] == name |
| 92 | assert isinstance(payload["version"], int) |
| 93 | |
| 94 | |
| 95 | def test_contacts_schema_is_now_version_3_additive_only(): |
| 96 | """contacts.json went 2 -> 3 adding job_title and lead_status. Every |
| 97 | field that existed at v2 is still present, unchanged in name/type -- |
| 98 | this schema change is purely additive (live records with no value for |
| 99 | the new fields just read those fields back as empty, same as any other |
| 100 | optional field on a schemaless TSV collection). |
| 101 | |
| 102 | v4 -> v5 added views.filter_fields (lead_status) for the generative |
| 103 | filter bar -- a views-only, additive change; no field was touched. |
| 104 | v5 -> v6 added full_name (computed formula over first/last -- the |
| 105 | formula-fields adopter; derived, never client-written). |
| 106 | """ |
| 107 | schema = _schema("contacts") |
| 108 | assert schema["version"] == 7 |
| 109 | assert schema["views"]["filter_fields"] == ["lead_status"] |
| 110 | by_name = {f["name"]: f for f in schema["fields"]} |
| 111 | |
| 112 | # Fields present since v2, untouched. |
| 113 | for name in ("id", "first_name", "last_name", "email", "phone", |
| 114 | "organization_id", "project_id", "tags", "notes", |
| 115 | "created_at", "owner_id"): |
| 116 | assert name in by_name, f"v2 field {name!r} must not be removed" |
| 117 | assert by_name["first_name"]["required"] is True |
| 118 | assert by_name["organization_id"]["relation"]["collection"] == "organizations" |
| 119 | assert by_name["created_at"]["read_only"] is True |
| 120 | |
| 121 | # New v3 fields. |
| 122 | assert by_name["job_title"]["type"] == "text" |
| 123 | assert "required" not in by_name["job_title"] or not by_name["job_title"]["required"] |
| 124 | |
| 125 | lead_status = by_name["lead_status"] |
| 126 | assert lead_status["type"] == "enum" |
| 127 | assert lead_status["enum"] == ["cold", "warm", "hot", "customer", "inactive"] |
| 128 | assert "required" not in lead_status or not lead_status["required"] |
| 129 | # No default is forced onto lead_status -- it's optional pipeline state. |
| 130 | assert "default" not in lead_status |
| 131 | |
| 132 | |
| 133 | def test_contacts_has_no_redundant_free_text_organization_name(): |
| 134 | """The source's create_contact also accepts a free-text `organization` |
| 135 | name alongside organization_id. This schema already has organization_id |
| 136 | as a relation into the organizations collection (display_field=name), |
| 137 | which covers the same need, so no separate organization_name field was |
| 138 | added -- adding one would just duplicate the relation. |
| 139 | """ |
| 140 | by_name = {f["name"]: f for f in _schema("contacts")["fields"]} |
| 141 | assert "organization_name" not in by_name |
| 142 | assert by_name["organization_id"]["relation"]["display_field"] == "name" |
| 143 | |
| 144 | |
| 145 | def test_contacts_form_and_list_wire_the_new_fields(): |
| 146 | schema = _schema("contacts") |
| 147 | assert schema["forms"]["default"]["fields"] == [ |
| 148 | "first_name", "last_name", "email", "phone", "job_title", |
| 149 | "organization_id", "project_id", "lead_status", "tags", "notes", |
| 150 | ] |
| 151 | assert "lead_status" in schema["views"]["list_fields"] |
| 152 | |
| 153 | |
| 154 | def test_interactions_schema_is_now_version_2_additive_only(): |
| 155 | """interactions.json went 1 -> 2 adding "task" to kind's enum plus |
| 156 | subject/duration_minutes/outcome, then 2 -> 3 adding created_at (baseline |
| 157 | record metadata). occurred_on and summary are untouched. |
| 158 | """ |
| 159 | schema = _schema("interactions") |
| 160 | assert schema["version"] == 3 |
| 161 | by_name = {f["name"]: f for f in schema["fields"]} |
| 162 | |
| 163 | for name in ("id", "contact_id", "kind", "occurred_on", "summary", "created_at", "owner_id"): |
| 164 | assert name in by_name, f"v1 field {name!r} must not be removed" |
| 165 | assert by_name["contact_id"]["required"] is True |
| 166 | assert by_name["summary"]["required"] is True |
| 167 | assert by_name["occurred_on"]["type"] == "date" |
| 168 | |
| 169 | kind = by_name["kind"] |
| 170 | assert kind["enum"] == ["call", "email", "meeting", "note", "task"] |
| 171 | assert kind["default"] == "note" |
| 172 | |
| 173 | assert by_name["subject"]["type"] == "text" |
| 174 | |
| 175 | duration = by_name["duration_minutes"] |
| 176 | assert duration["type"] == "integer" |
| 177 | |
| 178 | outcome = by_name["outcome"] |
| 179 | assert outcome["type"] == "enum" |
| 180 | assert outcome["enum"] == ["positive", "neutral", "negative", "no_answer"] |
| 181 | assert "default" not in outcome |
| 182 | |
| 183 | |
| 184 | def test_interactions_form_and_list_wire_the_new_fields(): |
| 185 | schema = _schema("interactions") |
| 186 | assert schema["forms"]["default"]["fields"] == [ |
| 187 | "contact_id", "kind", "subject", "occurred_on", |
| 188 | "duration_minutes", "outcome", "summary", |
| 189 | ] |
| 190 | assert "subject" in schema["views"]["list_fields"] |
| 191 | |
| 192 | |
| 193 | def test_organizations_and_tags_schemas_gain_only_created_at(): |
| 194 | """Both got the baseline created_at metadata field (additive, v1 -> v2) |
| 195 | and nothing else -- every original field is still present, unchanged. |
| 196 | """ |
| 197 | orgs = _schema("organizations") |
| 198 | assert orgs["version"] == 2 |
| 199 | assert {f["name"] for f in orgs["fields"]} == { |
| 200 | "id", "name", "website", "notes", "created_at", "owner_id", |
| 201 | } |
| 202 | |
| 203 | tags = _schema("tags") |
| 204 | assert tags["version"] == 2 |
| 205 | assert {f["name"] for f in tags["fields"]} == {"id", "name", "created_at", "owner_id"} |
| 206 | |
| 207 | |
| 208 | def test_additive_field_adds_need_no_migration_entry(): |
| 209 | """This system has no migration *execution* at all yet -- every |
| 210 | package in the repo ships an empty "migrations": [] array, and |
| 211 | object_packages.py's own install-plan builder refuses to proceed if a |
| 212 | package declares any migration ("Package migration execution is not |
| 213 | implemented yet"). Records are schemaless TSV rows (object_records.py's |
| 214 | _project_record fills any field missing from a row with ""), so an |
| 215 | additive field-add is already fully handled by the schema change alone |
| 216 | -- there is nothing for a migration entry to do, and inventing one here |
| 217 | would be simulating a mechanism this codebase doesn't have yet. |
| 218 | """ |
| 219 | package = object_packages.get_package("app-contacts", root=PACKAGES_ROOT) |
| 220 | assert package["migrations"] == [] |
| 221 | |
| 222 | source = Path(object_packages.__file__).read_text() |
| 223 | assert "Package migration execution is not implemented yet" in source |
| 224 | |
| 225 | |
| 226 | def _app_contacts_policy(): |
| 227 | payload = json.loads((APP_CONTACTS_DIR / "permissions" / "rules.json").read_text()) |
| 228 | return object_permissions.policy_from_dict({"access_mode": "role_based", "rules": payload["rules"]}) |
| 229 | |
| 230 | |
| 231 | def test_owner_can_crud_own_records_in_every_collection(): |
| 232 | policy = _app_contacts_policy() |
| 233 | subject = object_permissions.PermissionSubject(user_id="7") |
| 234 | records = { |
| 235 | "contacts": {"owner_id": "7", "first_name": "Ada", "lead_status": "warm"}, |
| 236 | "organizations": {"owner_id": "7", "name": "Acme"}, |
| 237 | "interactions": {"owner_id": "7", "contact_id": "c_1", "kind": "task", "summary": "Follow up"}, |
| 238 | "tags": {"owner_id": "7", "name": "vip"}, |
| 239 | } |
| 240 | for collection, record in records.items(): |
| 241 | for action in ( |
| 242 | object_permissions.CREATE, object_permissions.READ, |
| 243 | object_permissions.UPDATE, object_permissions.DELETE, |
| 244 | ): |
| 245 | decision = object_permissions.check_permission( |
| 246 | subject, action, policy=policy, collection=collection, record=record, |
| 247 | ) |
| 248 | assert decision.allowed is True, f"{collection}/{action} should be allowed for the owner" |
| 249 | |
| 250 | |
| 251 | def test_others_cannot_touch_someone_elses_records(): |
| 252 | policy = _app_contacts_policy() |
| 253 | subject = object_permissions.PermissionSubject(user_id="8") |
| 254 | records = { |
| 255 | "contacts": {"owner_id": "7", "first_name": "Ada"}, |
| 256 | "organizations": {"owner_id": "7", "name": "Acme"}, |
| 257 | "interactions": {"owner_id": "7", "contact_id": "c_1", "kind": "task", "summary": "Follow up"}, |
| 258 | "tags": {"owner_id": "7", "name": "vip"}, |
| 259 | } |
| 260 | for collection, record in records.items(): |
| 261 | for action in ( |
| 262 | object_permissions.READ, object_permissions.UPDATE, object_permissions.DELETE, |
| 263 | ): |
| 264 | decision = object_permissions.check_permission( |
| 265 | subject, action, policy=policy, collection=collection, record=record, |
| 266 | ) |
| 267 | assert decision.allowed is False, f"{collection}/{action} should be denied for a non-owner" |
| 268 | |
| 269 | |
| 270 | def test_anonymous_cannot_read_any_collection(): |
| 271 | policy = _app_contacts_policy() |
| 272 | records = { |
| 273 | "contacts": {"owner_id": "7", "first_name": "Ada"}, |
| 274 | "organizations": {"owner_id": "7", "name": "Acme"}, |
| 275 | "interactions": {"owner_id": "7", "contact_id": "c_1", "kind": "task", "summary": "Follow up"}, |
| 276 | "tags": {"owner_id": "7", "name": "vip"}, |
| 277 | } |
| 278 | for collection, record in records.items(): |
| 279 | decision = object_permissions.check_permission( |
| 280 | None, object_permissions.READ, policy=policy, collection=collection, record=record, |
| 281 | ) |
| 282 | assert decision.allowed is False |
| 283 | |
| 284 | |
| 285 | def test_site_contacts_page_is_publicly_executable(): |
| 286 | policy = _app_contacts_policy() |
| 287 | decision = object_permissions.check_permission( |
| 288 | None, object_permissions.EXECUTE, policy=policy, object_id="site_contacts", |
| 289 | ) |
| 290 | assert decision.allowed is True |
| 291 | |
| 292 | |
| 293 | def test_no_disallowed_org_names_leak_into_the_package(): |
| 294 | """Public repo hygiene: no internal org/codename references anywhere |
| 295 | in this package's source. |
| 296 | """ |
| 297 | # Built from fragments so this guard file itself stays clean of the very |
| 298 | # internal names it forbids (otherwise the test would flag its own source). |
| 299 | banned = re.compile( |
| 300 | "|".join([r"\b" + "q" + "9" + r"\b", "ask" + "robots", r"\b" + "wo" + "ld" + r"\b"]), |
| 301 | re.IGNORECASE, |
| 302 | ) |
| 303 | for path in APP_CONTACTS_DIR.rglob("*"): |
| 304 | if path.is_dir(): |
| 305 | continue |
| 306 | text = path.read_text(encoding="utf-8", errors="ignore") |
| 307 | assert not banned.search(text), f"disallowed reference found in {path}" |
| 308 | |
| 309 | |
| 310 | def test_this_test_file_has_no_disallowed_org_names(): |
| 311 | banned = re.compile( |
| 312 | "|".join([r"\b" + "q" + "9" + r"\b", "ask" + "robots", r"\b" + "wo" + "ld" + r"\b"]), |
| 313 | re.IGNORECASE, |
| 314 | ) |
| 315 | text = Path(__file__).read_text(encoding="utf-8", errors="ignore") |
| 316 | assert not banned.search(text), "disallowed reference found in this test file itself" |