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_worker_package.py · 13.4 KB · 356 lines
| 1 | """Structural tests for packages/app-worker (profiles, follows, |
| 2 | profile_comments). |
| 3 | |
| 4 | Mirrors the package/schema/permission testing conventions used for |
| 5 | packages/app-forum in tests/test_app_forum_package.py. app-worker has no |
| 6 | 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_WORKER_DIR = PACKAGES_ROOT / "app-worker" |
| 19 | |
| 20 | |
| 21 | def _profiles_schema(): |
| 22 | return json.loads((APP_WORKER_DIR / "schemas" / "profiles.json").read_text()) |
| 23 | |
| 24 | |
| 25 | def _follows_schema(): |
| 26 | return json.loads((APP_WORKER_DIR / "schemas" / "follows.json").read_text()) |
| 27 | |
| 28 | |
| 29 | def _comments_schema(): |
| 30 | return json.loads((APP_WORKER_DIR / "schemas" / "profile_comments.json").read_text()) |
| 31 | |
| 32 | |
| 33 | def test_get_package_normalizes_app_worker_manifest(): |
| 34 | package = object_packages.get_package("app-worker", root=PACKAGES_ROOT) |
| 35 | |
| 36 | assert package["id"] == "app-worker" |
| 37 | assert package["name"] == "Worker" |
| 38 | assert {schema["collection"] for schema in package["schemas"]} == { |
| 39 | "profiles", |
| 40 | "follows", |
| 41 | "profile_comments", |
| 42 | } |
| 43 | assert {obj["id"] for obj in package["objects"]} == { |
| 44 | "site_profile", |
| 45 | "site_profile_edit", |
| 46 | } |
| 47 | assert package["permissions"] == [{"path": "permissions/rules.json"}] |
| 48 | assert {entry["collection"] for entry in package["seed"]} == { |
| 49 | "profiles", |
| 50 | "follows", |
| 51 | "profile_comments", |
| 52 | } |
| 53 | assert package["dependencies"] == [] |
| 54 | |
| 55 | |
| 56 | def test_dry_run_app_worker_package_is_safe(tmp_path): |
| 57 | object_root = tmp_path / "objects" |
| 58 | object_root.mkdir() |
| 59 | |
| 60 | plan = object_packages.dry_run_package( |
| 61 | "app-worker", |
| 62 | root=PACKAGES_ROOT, |
| 63 | base_dir=tmp_path / "data", |
| 64 | object_roots=[object_root], |
| 65 | ) |
| 66 | |
| 67 | assert plan["safe_to_install"] is True |
| 68 | assert plan["warnings"] == [] |
| 69 | assert {schema["collection"] for schema in plan["schemas"]} == { |
| 70 | "profiles", |
| 71 | "follows", |
| 72 | "profile_comments", |
| 73 | } |
| 74 | |
| 75 | |
| 76 | def test_install_app_worker_package_loads_schemas(tmp_path): |
| 77 | data_dir = tmp_path / "data" |
| 78 | object_root = tmp_path / "objects" |
| 79 | object_root.mkdir() |
| 80 | |
| 81 | object_packages.install_package( |
| 82 | "app-worker", |
| 83 | root=PACKAGES_ROOT, |
| 84 | base_dir=data_dir, |
| 85 | object_roots=[object_root], |
| 86 | ) |
| 87 | |
| 88 | profiles_schema = object_schemas.get_schema("profiles", base_dir=data_dir) |
| 89 | follows_schema = object_schemas.get_schema("follows", base_dir=data_dir) |
| 90 | comments_schema = object_schemas.get_schema("profile_comments", base_dir=data_dir) |
| 91 | |
| 92 | assert profiles_schema["name"] == "profiles" |
| 93 | assert follows_schema["name"] == "follows" |
| 94 | assert comments_schema["name"] == "profile_comments" |
| 95 | assert (object_root / "site" / "profile.py").is_file() |
| 96 | assert (object_root / "site" / "profile_edit.py").is_file() |
| 97 | |
| 98 | |
| 99 | def test_schema_json_files_are_valid_and_versioned(): |
| 100 | for name in ("profiles", "follows", "profile_comments"): |
| 101 | payload = json.loads((APP_WORKER_DIR / "schemas" / f"{name}.json").read_text()) |
| 102 | assert payload["name"] == name |
| 103 | assert payload["version"] == 1 |
| 104 | |
| 105 | |
| 106 | def test_three_collections_present_with_expected_field_names(): |
| 107 | profile_fields = [f["name"] for f in _profiles_schema()["fields"]] |
| 108 | assert profile_fields == [ |
| 109 | "id", "display_name", "bio", "skills", "experience", "location", |
| 110 | "education", "website", "social_links", "is_active", "owner_id", |
| 111 | "created_at", |
| 112 | ] |
| 113 | |
| 114 | follow_fields = [f["name"] for f in _follows_schema()["fields"]] |
| 115 | assert follow_fields == [ |
| 116 | "id", "follower_id", "following_id", "created_at", "owner_id", |
| 117 | ] |
| 118 | |
| 119 | comment_fields = [f["name"] for f in _comments_schema()["fields"]] |
| 120 | assert comment_fields == [ |
| 121 | "id", "profile_id", "author_name", "author_email", "author_url", |
| 122 | "content", "status", "ip_address", "created_at", "owner_id", |
| 123 | ] |
| 124 | |
| 125 | |
| 126 | def test_skills_is_free_text_not_a_relation(): |
| 127 | """SCOPE RULE: the source model's skills field is free text, not a |
| 128 | skills-matching taxonomy or relation. No skills collection, no |
| 129 | availability field, exists anywhere in this package. |
| 130 | """ |
| 131 | by_name = {f["name"]: f for f in _profiles_schema()["fields"]} |
| 132 | assert by_name["skills"]["type"] == "text" |
| 133 | assert "relation" not in by_name["skills"] |
| 134 | |
| 135 | field_names = {f["name"] for f in _profiles_schema()["fields"]} |
| 136 | assert "availability" not in field_names |
| 137 | assert "rating" not in field_names |
| 138 | assert "reviews" not in field_names |
| 139 | |
| 140 | |
| 141 | def test_profiles_forms_and_views_match_the_brief(): |
| 142 | schema = _profiles_schema() |
| 143 | assert schema["forms"]["default"]["fields"] == [ |
| 144 | "display_name", "bio", "skills", "experience", "location", |
| 145 | "education", "website", "social_links", "is_active", |
| 146 | ] |
| 147 | assert schema["search"]["fields"] == ["display_name", "skills", "bio"] |
| 148 | |
| 149 | |
| 150 | def test_profiles_is_active_defaults_true(): |
| 151 | by_name = {f["name"]: f for f in _profiles_schema()["fields"]} |
| 152 | assert by_name["is_active"]["type"] == "boolean" |
| 153 | assert by_name["is_active"]["default"] == "true" |
| 154 | |
| 155 | |
| 156 | def test_follows_storage_is_append_and_edge_is_directed(): |
| 157 | schema = _follows_schema() |
| 158 | assert schema["storage"] == "append" |
| 159 | |
| 160 | by_name = {f["name"]: f for f in schema["fields"]} |
| 161 | assert by_name["follower_id"]["type"] == "text" |
| 162 | assert by_name["follower_id"]["required"] is True |
| 163 | assert by_name["following_id"]["type"] == "text" |
| 164 | assert by_name["following_id"]["required"] is True |
| 165 | |
| 166 | |
| 167 | def test_follows_has_no_search_block(): |
| 168 | assert "search" not in _follows_schema() |
| 169 | |
| 170 | |
| 171 | def test_profile_comments_status_enum_and_defaults(): |
| 172 | by_name = {f["name"]: f for f in _comments_schema()["fields"]} |
| 173 | status_field = by_name["status"] |
| 174 | assert status_field["enum"] == ["pending", "approved", "rejected"] |
| 175 | assert status_field["default"] == "pending" |
| 176 | |
| 177 | |
| 178 | def test_profile_comments_guarded_transitions_match_the_brief(): |
| 179 | by_name = {f["name"]: f for f in _comments_schema()["fields"]} |
| 180 | transitions = by_name["status"]["transitions"] |
| 181 | owner_guard = {"owner_id": "$user_id"} |
| 182 | |
| 183 | pending_targets = {entry["to"]: entry["when"] for entry in transitions["pending"]} |
| 184 | assert pending_targets == {"approved": owner_guard, "rejected": owner_guard} |
| 185 | |
| 186 | approved_targets = {entry["to"]: entry["when"] for entry in transitions["approved"]} |
| 187 | assert approved_targets == {"rejected": owner_guard} |
| 188 | |
| 189 | # rejected is terminal: no entry in the transitions map at all. |
| 190 | assert "rejected" not in transitions |
| 191 | |
| 192 | |
| 193 | def test_profile_comments_hidden_fields(): |
| 194 | by_name = {f["name"]: f for f in _comments_schema()["fields"]} |
| 195 | assert by_name["author_email"]["permissions"] == {"public": "hidden"} |
| 196 | assert by_name["ip_address"]["permissions"] == {"public": "hidden"} |
| 197 | assert by_name["status"]["permissions"] == {"public": "hidden"} |
| 198 | |
| 199 | |
| 200 | def test_profile_comments_content_and_profile_id_required(): |
| 201 | by_name = {f["name"]: f for f in _comments_schema()["fields"]} |
| 202 | assert by_name["profile_id"]["required"] is True |
| 203 | assert by_name["profile_id"]["relation"]["collection"] == "profiles" |
| 204 | assert by_name["content"]["required"] is True |
| 205 | |
| 206 | |
| 207 | def test_profile_comments_has_no_search_block(): |
| 208 | assert "search" not in _comments_schema() |
| 209 | |
| 210 | |
| 211 | def _app_worker_policy(): |
| 212 | payload = json.loads((APP_WORKER_DIR / "permissions" / "rules.json").read_text()) |
| 213 | return object_permissions.policy_from_dict({"access_mode": "role_based", "rules": payload["rules"]}) |
| 214 | |
| 215 | |
| 216 | def test_anonymous_can_read_any_profile_and_follow_edge(): |
| 217 | policy = _app_worker_policy() |
| 218 | someone_elses_profile = {"owner_id": "7", "display_name": "Ada"} |
| 219 | someone_elses_follow = {"owner_id": "7", "follower_id": "7", "following_id": "8"} |
| 220 | |
| 221 | for collection, record in ( |
| 222 | ("profiles", someone_elses_profile), |
| 223 | ("follows", someone_elses_follow), |
| 224 | ): |
| 225 | decision = object_permissions.check_permission( |
| 226 | None, object_permissions.READ, policy=policy, collection=collection, record=record |
| 227 | ) |
| 228 | assert decision.allowed is True, f"anonymous read should be allowed on {collection}" |
| 229 | |
| 230 | |
| 231 | def test_anonymous_can_read_only_approved_profile_comments(): |
| 232 | policy = _app_worker_policy() |
| 233 | approved = {"owner_id": "9", "profile_id": "p1", "content": "hi", "status": "approved"} |
| 234 | pending = {"owner_id": "9", "profile_id": "p1", "content": "hi", "status": "pending"} |
| 235 | rejected = {"owner_id": "9", "profile_id": "p1", "content": "spam", "status": "rejected"} |
| 236 | |
| 237 | approved_decision = object_permissions.check_permission( |
| 238 | None, object_permissions.READ, policy=policy, collection="profile_comments", record=approved |
| 239 | ) |
| 240 | assert approved_decision.allowed is True |
| 241 | |
| 242 | for record in (pending, rejected): |
| 243 | decision = object_permissions.check_permission( |
| 244 | None, object_permissions.READ, policy=policy, collection="profile_comments", record=record |
| 245 | ) |
| 246 | assert decision.allowed is False |
| 247 | |
| 248 | |
| 249 | def test_owner_can_crud_own_profile_follow_and_comment(): |
| 250 | policy = _app_worker_policy() |
| 251 | subject = object_permissions.PermissionSubject(user_id="7") |
| 252 | records = { |
| 253 | "profiles": {"owner_id": "7", "display_name": "Ada"}, |
| 254 | "follows": {"owner_id": "7", "follower_id": "7", "following_id": "8"}, |
| 255 | "profile_comments": {"owner_id": "7", "profile_id": "p1", "content": "hi", "status": "pending"}, |
| 256 | } |
| 257 | |
| 258 | for collection, record in records.items(): |
| 259 | for action in ( |
| 260 | object_permissions.CREATE, |
| 261 | object_permissions.READ, |
| 262 | object_permissions.UPDATE, |
| 263 | object_permissions.DELETE, |
| 264 | ): |
| 265 | decision = object_permissions.check_permission( |
| 266 | subject, action, policy=policy, collection=collection, record=record |
| 267 | ) |
| 268 | assert decision.allowed is True, f"owner {action} should be allowed on {collection}" |
| 269 | |
| 270 | |
| 271 | def test_others_cannot_write_someone_elses_profile_follow_or_comment(): |
| 272 | policy = _app_worker_policy() |
| 273 | subject = object_permissions.PermissionSubject(user_id="8") |
| 274 | records = { |
| 275 | "profiles": {"owner_id": "7", "display_name": "Ada"}, |
| 276 | "follows": {"owner_id": "7", "follower_id": "7", "following_id": "9"}, |
| 277 | "profile_comments": {"owner_id": "7", "profile_id": "p1", "content": "hi", "status": "pending"}, |
| 278 | } |
| 279 | |
| 280 | for collection, record in records.items(): |
| 281 | for action in (object_permissions.UPDATE, object_permissions.DELETE): |
| 282 | decision = object_permissions.check_permission( |
| 283 | subject, action, policy=policy, collection=collection, record=record |
| 284 | ) |
| 285 | assert decision.allowed is False, f"non-owner {action} should be denied on {collection}" |
| 286 | |
| 287 | |
| 288 | def test_anonymous_cannot_create_a_profile_comment(): |
| 289 | """v1 guestbook posting is signed-in only -- no public create rule is |
| 290 | granted on profile_comments (see schemas/profile_comments.json and |
| 291 | dbbasic-package.json's deferred list for the anonymous path). |
| 292 | """ |
| 293 | policy = _app_worker_policy() |
| 294 | record = {"profile_id": "p1", "content": "hi", "status": "pending"} |
| 295 | |
| 296 | decision = object_permissions.check_permission( |
| 297 | None, object_permissions.CREATE, policy=policy, collection="profile_comments", record=record |
| 298 | ) |
| 299 | assert decision.allowed is False |
| 300 | |
| 301 | |
| 302 | def test_worker_pages_are_publicly_executable(): |
| 303 | policy = _app_worker_policy() |
| 304 | |
| 305 | for object_id in ("site_profile", "site_profile_edit"): |
| 306 | decision = object_permissions.check_permission( |
| 307 | None, object_permissions.EXECUTE, policy=policy, object_id=object_id |
| 308 | ) |
| 309 | assert decision.allowed is True |
| 310 | |
| 311 | |
| 312 | def test_seed_tsvs_have_no_data_rows_and_match_schema_field_order(): |
| 313 | """Header-only seeds, matching the established precedent (app-tasks, |
| 314 | app-notes, app-invoices, app-forum all ship header-only seeds). |
| 315 | """ |
| 316 | for name, schema in ( |
| 317 | ("profiles", _profiles_schema()), |
| 318 | ("follows", _follows_schema()), |
| 319 | ("profile_comments", _comments_schema()), |
| 320 | ): |
| 321 | path = APP_WORKER_DIR / "seed" / f"{name}.tsv" |
| 322 | lines = path.read_text().splitlines() |
| 323 | assert len(lines) == 1, f"{name}.tsv should be header-only" |
| 324 | header = lines[0].split("\t") |
| 325 | assert header == [f["name"] for f in schema["fields"]] |
| 326 | |
| 327 | |
| 328 | def test_no_disallowed_org_names_leak_into_the_package(): |
| 329 | """Public repo hygiene: no internal org/codename references anywhere |
| 330 | in this package's source, including in any source-doc path reference |
| 331 | (this package describes its source only as "a private |
| 332 | predecessor-system audit, not part of this repo"). |
| 333 | """ |
| 334 | # Built from fragments so this guard file itself stays clean of the |
| 335 | # very internal names it forbids. |
| 336 | banned = re.compile( |
| 337 | "|".join([r"\b" + "q" + "9" + r"\b", "ask" + "robots", r"\b" + "wo" + "ld" + r"\b"]), |
| 338 | re.IGNORECASE, |
| 339 | ) |
| 340 | for path in APP_WORKER_DIR.rglob("*"): |
| 341 | if path.is_dir(): |
| 342 | continue |
| 343 | text = path.read_text(encoding="utf-8", errors="ignore") |
| 344 | assert not banned.search(text), f"disallowed reference found in {path}" |
| 345 | |
| 346 | |
| 347 | def test_package_does_not_touch_app_shell_or_app_theme(): |
| 348 | """This package's brief explicitly forbids editing packages/app-shell |
| 349 | or packages/app-theme (nav/home wiring is the main loop's job). This |
| 350 | test only asserts app-worker's own manifest declares no dependency on |
| 351 | them; it cannot detect a stray edit elsewhere in the repo, but keeps |
| 352 | the intent on record. |
| 353 | """ |
| 354 | manifest = json.loads((APP_WORKER_DIR / "dbbasic-package.json").read_text()) |
| 355 | assert "app-shell" not in manifest.get("dependencies", []) |
| 356 | assert "app-theme" not in manifest.get("dependencies", []) |