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_forum_package.py · 12.9 KB · 320 lines
| 1 | """Structural tests for packages/app-forum (forum_categories, forum_topics, |
| 2 | forum_replies). |
| 3 | |
| 4 | Mirrors the package/schema/permission testing conventions used for |
| 5 | packages/app-invoices in tests/test_app_invoices_package.py. app-forum has |
| 6 | no HANDLES handler and no behavior module, so this is the whole suite. |
| 7 | """ |
| 8 | |
| 9 | import json |
| 10 | import re |
| 11 | from pathlib import Path |
| 12 | |
| 13 | import object_packages |
| 14 | import object_permissions |
| 15 | import object_schemas |
| 16 | from package_pins import assert_includes_in_order |
| 17 | |
| 18 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 19 | APP_FORUM_DIR = PACKAGES_ROOT / "app-forum" |
| 20 | |
| 21 | |
| 22 | def _categories_schema(): |
| 23 | return json.loads((APP_FORUM_DIR / "schemas" / "forum_categories.json").read_text()) |
| 24 | |
| 25 | |
| 26 | def _topics_schema(): |
| 27 | return json.loads((APP_FORUM_DIR / "schemas" / "forum_topics.json").read_text()) |
| 28 | |
| 29 | |
| 30 | def _replies_schema(): |
| 31 | return json.loads((APP_FORUM_DIR / "schemas" / "forum_replies.json").read_text()) |
| 32 | |
| 33 | |
| 34 | def test_get_package_normalizes_app_forum_manifest(): |
| 35 | package = object_packages.get_package("app-forum", root=PACKAGES_ROOT) |
| 36 | |
| 37 | assert package["id"] == "app-forum" |
| 38 | assert package["name"] == "Forum" |
| 39 | assert {schema["collection"] for schema in package["schemas"]} >= { |
| 40 | "forum_categories", |
| 41 | "forum_topics", |
| 42 | "forum_replies", |
| 43 | } |
| 44 | # site_forum_topic was removed in the Stage-6 retrofit: the topic |
| 45 | # permalink is now a seeded 59 detail view (site_view_render) composed of |
| 46 | # detail + related(flat replies) + FK-locked form blocks -- see |
| 47 | # tests/test_app_forum_detail_retrofit.py. |
| 48 | # site_forum (the landing page) went the same way in 0.3.0: /forum is a |
| 49 | # seeded view (tests/test_pages_converted_to_views.py), so the package |
| 50 | # ships no objects at all. |
| 51 | assert package["objects"] == [] |
| 52 | assert {"path": "permissions/rules.json"} in package["permissions"] |
| 53 | assert {entry["collection"] for entry in package["seed"]} >= { |
| 54 | "forum_categories", |
| 55 | "forum_topics", |
| 56 | "forum_replies", |
| 57 | "views", |
| 58 | "site_routes", |
| 59 | } |
| 60 | |
| 61 | |
| 62 | def test_dry_run_app_forum_package_is_safe(tmp_path): |
| 63 | object_root = tmp_path / "objects" |
| 64 | object_root.mkdir() |
| 65 | |
| 66 | plan = object_packages.dry_run_package( |
| 67 | "app-forum", |
| 68 | root=PACKAGES_ROOT, |
| 69 | base_dir=tmp_path / "data", |
| 70 | object_roots=[object_root], |
| 71 | ) |
| 72 | |
| 73 | assert plan["safe_to_install"] is True |
| 74 | assert plan["warnings"] == [] |
| 75 | assert {schema["collection"] for schema in plan["schemas"]} >= { |
| 76 | "forum_categories", |
| 77 | "forum_topics", |
| 78 | "forum_replies", |
| 79 | } |
| 80 | |
| 81 | |
| 82 | def test_install_app_forum_package_loads_schemas(tmp_path): |
| 83 | data_dir = tmp_path / "data" |
| 84 | object_root = tmp_path / "objects" |
| 85 | object_root.mkdir() |
| 86 | |
| 87 | object_packages.install_package( |
| 88 | "app-forum", |
| 89 | root=PACKAGES_ROOT, |
| 90 | base_dir=data_dir, |
| 91 | object_roots=[object_root], |
| 92 | ) |
| 93 | |
| 94 | categories_schema = object_schemas.get_schema("forum_categories", base_dir=data_dir) |
| 95 | topics_schema = object_schemas.get_schema("forum_topics", base_dir=data_dir) |
| 96 | replies_schema = object_schemas.get_schema("forum_replies", base_dir=data_dir) |
| 97 | |
| 98 | assert categories_schema["name"] == "forum_categories" |
| 99 | assert topics_schema["name"] == "forum_topics" |
| 100 | assert replies_schema["name"] == "forum_replies" |
| 101 | assert not (object_root / "site" / "forum.py").exists() |
| 102 | # forum_topic.py was removed in the Stage-6 retrofit (replaced by a |
| 103 | # seeded detail view); it must no longer be installed. |
| 104 | assert not (object_root / "site" / "forum_topic.py").exists() |
| 105 | |
| 106 | |
| 107 | def test_schema_json_files_are_valid_and_versioned(): |
| 108 | # forum_topics/forum_replies went v1 -> v2 in the Stage-6 retrofit |
| 109 | # (ai_summary gained visible_when; forum_replies' compose form went flat, |
| 110 | # dropping parent_id from forms.default). forum_topics went to v3 when the |
| 111 | # landing page became a view (list_mode feed -> table, so pinned/solved/ |
| 112 | # views are columns). forum_categories is untouched. |
| 113 | expected_version = {"forum_categories": 1, "forum_topics": 3, "forum_replies": 2} |
| 114 | for name in ("forum_categories", "forum_topics", "forum_replies"): |
| 115 | payload = json.loads((APP_FORUM_DIR / "schemas" / f"{name}.json").read_text()) |
| 116 | assert payload["name"] == name |
| 117 | assert payload["version"] >= expected_version[name] |
| 118 | |
| 119 | |
| 120 | def test_three_collections_present_with_expected_field_names(): |
| 121 | cat_fields = [f["name"] for f in _categories_schema()["fields"]] |
| 122 | assert_includes_in_order(cat_fields, [ |
| 123 | "id", "name", "slug", "description", "icon", |
| 124 | "display_order", "is_active", "owner_id", "created_at", |
| 125 | ]) |
| 126 | |
| 127 | topic_fields = [f["name"] for f in _topics_schema()["fields"]] |
| 128 | assert_includes_in_order(topic_fields, [ |
| 129 | "id", "category_id", "project_id", "title", "content", "views", |
| 130 | "is_pinned", "is_locked", "is_solved", "ai_summary", |
| 131 | "is_ai_summarized", "owner_id", "created_at", |
| 132 | ]) |
| 133 | |
| 134 | reply_fields = [f["name"] for f in _replies_schema()["fields"]] |
| 135 | assert_includes_in_order(reply_fields, [ |
| 136 | "id", "topic_id", "parent_id", "content", "is_solution", |
| 137 | "owner_id", "created_at", |
| 138 | ]) |
| 139 | |
| 140 | |
| 141 | def test_topic_category_relation_is_required(): |
| 142 | by_name = {f["name"]: f for f in _topics_schema()["fields"]} |
| 143 | assert by_name["category_id"]["relation"]["collection"] == "forum_categories" |
| 144 | assert by_name["category_id"]["required"] is True |
| 145 | |
| 146 | |
| 147 | def test_topic_project_id_is_a_plain_optional_text_field(): |
| 148 | """The source's optional project link, carried but not wired to |
| 149 | app-projects as a relation -- app-forum has no dependency on it. |
| 150 | """ |
| 151 | by_name = {f["name"]: f for f in _topics_schema()["fields"]} |
| 152 | assert by_name["project_id"]["type"] == "text" |
| 153 | assert "relation" not in by_name["project_id"] |
| 154 | assert not by_name["project_id"].get("required") |
| 155 | |
| 156 | |
| 157 | def test_reply_parent_is_a_self_referencing_relation(): |
| 158 | by_name = {f["name"]: f for f in _replies_schema()["fields"]} |
| 159 | assert by_name["parent_id"]["relation"]["collection"] == "forum_replies" |
| 160 | assert not by_name["parent_id"].get("required") |
| 161 | |
| 162 | by_name_topic = {f["name"]: f for f in _replies_schema()["fields"]} |
| 163 | assert by_name_topic["topic_id"]["relation"]["collection"] == "forum_topics" |
| 164 | assert by_name_topic["topic_id"]["required"] is True |
| 165 | |
| 166 | |
| 167 | def test_reply_storage_is_explicitly_classic(): |
| 168 | assert _replies_schema()["storage"] == "classic" |
| 169 | |
| 170 | |
| 171 | def test_moderation_and_carried_flags_are_booleans_with_expected_defaults(): |
| 172 | by_name = {f["name"]: f for f in _topics_schema()["fields"]} |
| 173 | for name in ("is_pinned", "is_locked", "is_solved", "is_ai_summarized"): |
| 174 | assert by_name[name]["type"] == "boolean" |
| 175 | assert by_name[name]["default"] == "false" |
| 176 | |
| 177 | cat_by_name = {f["name"]: f for f in _categories_schema()["fields"]} |
| 178 | assert cat_by_name["is_active"]["type"] == "boolean" |
| 179 | assert cat_by_name["is_active"]["default"] == "true" |
| 180 | |
| 181 | reply_by_name = {f["name"]: f for f in _replies_schema()["fields"]} |
| 182 | assert reply_by_name["is_solution"]["type"] == "boolean" |
| 183 | assert reply_by_name["is_solution"]["default"] == "false" |
| 184 | |
| 185 | |
| 186 | def test_carried_but_undriven_fields_are_present(): |
| 187 | """ai_summary/ai_summarized and views are carried from the source model |
| 188 | with no generation or increment logic behind them -- see |
| 189 | dbbasic-package.json's deferred list. |
| 190 | """ |
| 191 | by_name = {f["name"]: f for f in _topics_schema()["fields"]} |
| 192 | assert by_name["ai_summary"]["type"] == "textarea" |
| 193 | assert by_name["views"]["type"] == "number" |
| 194 | assert by_name["views"]["default"] == "0" |
| 195 | |
| 196 | |
| 197 | def test_display_order_and_default_field_types(): |
| 198 | cat_by_name = {f["name"]: f for f in _categories_schema()["fields"]} |
| 199 | assert cat_by_name["display_order"]["type"] == "number" |
| 200 | assert cat_by_name["display_order"]["default"] == "0" |
| 201 | |
| 202 | |
| 203 | def test_search_fields_cover_the_content_fields(): |
| 204 | assert_includes_in_order(_categories_schema()["search"]["fields"], ["name", "description"]) |
| 205 | assert_includes_in_order(_topics_schema()["search"]["fields"], ["title", "content"]) |
| 206 | assert_includes_in_order(_replies_schema()["search"]["fields"], ["content"]) |
| 207 | |
| 208 | |
| 209 | def _app_forum_policy(): |
| 210 | payload = json.loads((APP_FORUM_DIR / "permissions" / "rules.json").read_text()) |
| 211 | return object_permissions.policy_from_dict({"access_mode": "role_based", "rules": payload["rules"]}) |
| 212 | |
| 213 | |
| 214 | def test_anonymous_can_read_any_category_topic_or_reply(): |
| 215 | """Public read on all three collections, no row_filter -- the whole |
| 216 | forum is visible to anonymous visitors. |
| 217 | """ |
| 218 | policy = _app_forum_policy() |
| 219 | someone_elses_category = {"owner_id": "7", "name": "General"} |
| 220 | someone_elses_topic = {"owner_id": "7", "category_id": "cat_1", "title": "Hello"} |
| 221 | someone_elses_reply = {"owner_id": "7", "topic_id": "topic_1", "content": "Hi"} |
| 222 | |
| 223 | for collection, record in ( |
| 224 | ("forum_categories", someone_elses_category), |
| 225 | ("forum_topics", someone_elses_topic), |
| 226 | ("forum_replies", someone_elses_reply), |
| 227 | ): |
| 228 | decision = object_permissions.check_permission( |
| 229 | None, object_permissions.READ, policy=policy, collection=collection, record=record |
| 230 | ) |
| 231 | assert decision.allowed is True, f"anonymous read should be allowed on {collection}" |
| 232 | |
| 233 | |
| 234 | def test_owner_can_crud_own_category_topic_and_reply(): |
| 235 | policy = _app_forum_policy() |
| 236 | subject = object_permissions.PermissionSubject(user_id="7") |
| 237 | records = { |
| 238 | "forum_categories": {"owner_id": "7", "name": "General"}, |
| 239 | "forum_topics": {"owner_id": "7", "category_id": "cat_1", "title": "Hello"}, |
| 240 | "forum_replies": {"owner_id": "7", "topic_id": "topic_1", "content": "Hi"}, |
| 241 | } |
| 242 | |
| 243 | for collection, record in records.items(): |
| 244 | for action in ( |
| 245 | object_permissions.CREATE, |
| 246 | object_permissions.READ, |
| 247 | object_permissions.UPDATE, |
| 248 | object_permissions.DELETE, |
| 249 | ): |
| 250 | decision = object_permissions.check_permission( |
| 251 | subject, action, policy=policy, collection=collection, record=record |
| 252 | ) |
| 253 | assert decision.allowed is True, f"owner {action} should be allowed on {collection}" |
| 254 | |
| 255 | |
| 256 | def test_others_cannot_write_someone_elses_category_topic_or_reply(): |
| 257 | """Public read still applies (tested above); this checks the |
| 258 | owner-only write side: a different signed-in user cannot update or |
| 259 | delete a record they do not own -- the v1 "no moderator role" posture |
| 260 | documented in dbbasic-package.json and permissions/rules.json. |
| 261 | """ |
| 262 | policy = _app_forum_policy() |
| 263 | subject = object_permissions.PermissionSubject(user_id="8") |
| 264 | records = { |
| 265 | "forum_categories": {"owner_id": "7", "name": "General"}, |
| 266 | "forum_topics": {"owner_id": "7", "category_id": "cat_1", "title": "Hello"}, |
| 267 | "forum_replies": {"owner_id": "7", "topic_id": "topic_1", "content": "Hi"}, |
| 268 | } |
| 269 | |
| 270 | for collection, record in records.items(): |
| 271 | for action in (object_permissions.UPDATE, object_permissions.DELETE): |
| 272 | decision = object_permissions.check_permission( |
| 273 | subject, action, policy=policy, collection=collection, record=record |
| 274 | ) |
| 275 | assert decision.allowed is False, f"non-owner {action} should be denied on {collection}" |
| 276 | |
| 277 | |
| 278 | def test_forum_pages_are_not_executable_objects_any_more(): |
| 279 | # Both pages are seeded views (site_view_render): the package grants |
| 280 | # execute on no object, and the retired grants are in `retracted` so a |
| 281 | # box installed before the conversion loses them too. |
| 282 | payload = json.loads((APP_FORUM_DIR / "permissions" / "rules.json").read_text()) |
| 283 | assert not [r for r in payload["rules"] if r.get("object_id")] |
| 284 | assert {r["object_id"] for r in payload["retracted"]} == {"site_forum", "site_forum_topic"} |
| 285 | |
| 286 | |
| 287 | def test_seed_tsvs_have_no_data_rows_and_match_schema_field_order(): |
| 288 | """Header-only seeds, matching the established precedent (app-tasks, |
| 289 | app-notes, app-invoices all ship header-only seeds). |
| 290 | """ |
| 291 | for name, schema in ( |
| 292 | ("forum_categories", _categories_schema()), |
| 293 | ("forum_topics", _topics_schema()), |
| 294 | ("forum_replies", _replies_schema()), |
| 295 | ): |
| 296 | path = APP_FORUM_DIR / "seed" / f"{name}.tsv" |
| 297 | lines = path.read_text().splitlines() |
| 298 | assert len(lines) == 1, f"{name}.tsv should be header-only" |
| 299 | header = lines[0].split("\t") |
| 300 | assert header == [f["name"] for f in schema["fields"]] |
| 301 | |
| 302 | |
| 303 | def test_no_disallowed_org_names_leak_into_the_package(): |
| 304 | """Public repo hygiene: no internal org/codename references anywhere |
| 305 | in this package's source. Ported from a private predecessor-system |
| 306 | audit, not part of this repo. |
| 307 | """ |
| 308 | # Built from fragments so this guard file itself stays clean of the |
| 309 | # very internal names it forbids. |
| 310 | banned = re.compile( |
| 311 | "|".join([r"\b" + "q" + "9" + r"\b", "ask" + "robots", r"\b" + "wo" + "ld" + r"\b"]), |
| 312 | re.IGNORECASE, |
| 313 | ) |
| 314 | for path in APP_FORUM_DIR.rglob("*"): |
| 315 | if "__pycache__" in path.parts: |
| 316 | continue # compiled bytes can match a name by chance |
| 317 | if path.is_dir(): |
| 318 | continue |
| 319 | text = path.read_text(encoding="utf-8", errors="ignore") |
| 320 | assert not banned.search(text), f"disallowed reference found in {path}" |