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