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_activity_package.py · 7.4 KB · 215 lines
| 1 | """Structural + behavioral tests for packages/app-activity. |
| 2 | |
| 3 | Mirrors the package/schema/permission testing conventions used for |
| 4 | packages/app-notes and packages/app-tasks (see tests/test_app_settings_package.py, |
| 5 | tests/test_app_invoices_package.py) and the direct-execution pattern |
| 6 | tests/test_app_views_package.py uses for a renderer-style site object. |
| 7 | Route-level /api/activity behavior is tested through the ASGI harness in |
| 8 | tests/test_object_server.py, the same way tests/test_object_server_prefs.py |
| 9 | and tests/test_object_tts.py test their own signed-in-only GET routes. |
| 10 | """ |
| 11 | |
| 12 | import json |
| 13 | import re |
| 14 | from pathlib import Path |
| 15 | |
| 16 | import object_execution |
| 17 | import object_packages |
| 18 | import object_permissions |
| 19 | import object_server |
| 20 | import python_object_runtime |
| 21 | |
| 22 | from test_object_server import create_identity_session, enable_admin_token, request, write_records |
| 23 | |
| 24 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 25 | APP_ACTIVITY_DIR = PACKAGES_ROOT / "app-activity" |
| 26 | |
| 27 | |
| 28 | def _app_activity_policy(): |
| 29 | payload = json.loads((APP_ACTIVITY_DIR / "permissions" / "rules.json").read_text()) |
| 30 | return object_permissions.policy_from_dict({"access_mode": "role_based", "rules": payload["rules"]}) |
| 31 | |
| 32 | |
| 33 | def test_get_package_normalizes_app_activity_manifest(): |
| 34 | package = object_packages.get_package("app-activity", root=PACKAGES_ROOT) |
| 35 | |
| 36 | assert package["id"] == "app-activity" |
| 37 | assert package["name"] == "Activity" |
| 38 | assert package["objects"] == [{"id": "site_activity", "path": "objects/site/activity.py"}] |
| 39 | assert package["schemas"] == [] |
| 40 | assert package["seed"] == [] |
| 41 | assert package["permissions"] == [{"path": "permissions/rules.json"}] |
| 42 | |
| 43 | |
| 44 | def test_dry_run_app_activity_package_is_safe(tmp_path): |
| 45 | object_root = tmp_path / "objects" |
| 46 | object_root.mkdir() |
| 47 | |
| 48 | plan = object_packages.dry_run_package( |
| 49 | "app-activity", |
| 50 | root=PACKAGES_ROOT, |
| 51 | base_dir=tmp_path / "data", |
| 52 | object_roots=[object_root], |
| 53 | ) |
| 54 | |
| 55 | assert plan["safe_to_install"] is True |
| 56 | assert plan["warnings"] == [] |
| 57 | |
| 58 | |
| 59 | def test_site_activity_execute_is_public(): |
| 60 | """Same split as site_notes/site_tasks: public EXECUTE on the *page* |
| 61 | object (it shows a sign-in prompt to visitors), never public read on a |
| 62 | collection -- app-activity owns no collection at all. |
| 63 | """ |
| 64 | policy = _app_activity_policy() |
| 65 | |
| 66 | decision = object_permissions.check_permission( |
| 67 | None, object_permissions.EXECUTE, policy=policy, object_id="site_activity" |
| 68 | ) |
| 69 | |
| 70 | assert decision.allowed is True |
| 71 | |
| 72 | |
| 73 | def test_activity_page_prompts_anonymous_visitors_to_sign_in(tmp_path): |
| 74 | data_dir = tmp_path / "data" |
| 75 | object_root = tmp_path / "objects" |
| 76 | object_root.mkdir() |
| 77 | |
| 78 | object_packages.install_package( |
| 79 | "app-activity", root=PACKAGES_ROOT, base_dir=data_dir, object_roots=[object_root] |
| 80 | ) |
| 81 | |
| 82 | runtime = python_object_runtime.PythonObjectRuntime(base_dir=data_dir) |
| 83 | result = object_execution.execute_object( |
| 84 | runtime, |
| 85 | object_execution.ObjectExecutionRequest("site_activity", payload={"_identity": {}}), |
| 86 | roots=[object_root], |
| 87 | ) |
| 88 | |
| 89 | assert result.ok is True |
| 90 | body = result.result["body"] |
| 91 | assert result.result["content_type"] == "text/html; charset=utf-8" |
| 92 | assert "Sign in" in body |
| 93 | assert 'id="feed"' not in body |
| 94 | |
| 95 | |
| 96 | def test_activity_page_serves_feed_scaffolding_for_signed_in_users(tmp_path): |
| 97 | data_dir = tmp_path / "data" |
| 98 | object_root = tmp_path / "objects" |
| 99 | object_root.mkdir() |
| 100 | |
| 101 | object_packages.install_package( |
| 102 | "app-activity", root=PACKAGES_ROOT, base_dir=data_dir, object_roots=[object_root] |
| 103 | ) |
| 104 | |
| 105 | runtime = python_object_runtime.PythonObjectRuntime(base_dir=data_dir) |
| 106 | result = object_execution.execute_object( |
| 107 | runtime, |
| 108 | object_execution.ObjectExecutionRequest( |
| 109 | "site_activity", payload={"_identity": {"user_id": "alice"}} |
| 110 | ), |
| 111 | roots=[object_root], |
| 112 | ) |
| 113 | |
| 114 | assert result.ok is True |
| 115 | body = result.result["body"] |
| 116 | assert 'id="feed"' in body |
| 117 | assert "/api/activity" in body |
| 118 | assert '<script src="/nav">' in body |
| 119 | |
| 120 | |
| 121 | def test_no_disallowed_org_names_leak_into_the_package(): |
| 122 | """Public repo hygiene: no internal org/codename references anywhere |
| 123 | in this package's source. |
| 124 | """ |
| 125 | # Built from fragments so this guard file itself stays clean of the very |
| 126 | # internal names it forbids (otherwise the test would flag its own source). |
| 127 | banned = re.compile( |
| 128 | "|".join([r"\b" + "q" + "9" + r"\b", "ask" + "robots", r"\b" + "wo" + "ld" + r"\b"]), |
| 129 | re.IGNORECASE, |
| 130 | ) |
| 131 | for path in APP_ACTIVITY_DIR.rglob("*"): |
| 132 | if path.is_dir(): |
| 133 | continue |
| 134 | text = path.read_text(encoding="utf-8", errors="ignore") |
| 135 | assert not banned.search(text), f"disallowed reference found in {path}" |
| 136 | |
| 137 | |
| 138 | # --------------------------------------------------------------------------- |
| 139 | # GET /api/activity route |
| 140 | # --------------------------------------------------------------------------- |
| 141 | |
| 142 | |
| 143 | def signed_in_bearer(user_id): |
| 144 | token, _ = create_identity_session({"user_id": user_id}) |
| 145 | return [("authorization", f"Bearer {token}")] |
| 146 | |
| 147 | |
| 148 | def test_get_api_activity_requires_a_signed_in_session(tmp_path, monkeypatch): |
| 149 | data_dir = tmp_path / "data" |
| 150 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 151 | write_records(data_dir, "notes", "id\ttitle\n") |
| 152 | |
| 153 | status, _, payload = request("/api/activity") |
| 154 | |
| 155 | assert status == 401 |
| 156 | assert payload["status"] == "error" |
| 157 | |
| 158 | |
| 159 | def test_get_api_activity_rejects_unsupported_methods(tmp_path, monkeypatch): |
| 160 | data_dir = tmp_path / "data" |
| 161 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 162 | enable_admin_token(monkeypatch) |
| 163 | headers = signed_in_bearer("alice") |
| 164 | |
| 165 | status, _, _ = request("/api/activity", method="POST", headers=headers) |
| 166 | |
| 167 | assert status == 405 |
| 168 | |
| 169 | |
| 170 | def test_get_api_activity_returns_only_the_signed_in_users_feed(tmp_path, monkeypatch): |
| 171 | data_dir = tmp_path / "data" |
| 172 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 173 | enable_admin_token(monkeypatch) |
| 174 | write_records(data_dir, "notes", "id\ttitle\n") |
| 175 | |
| 176 | import object_records |
| 177 | |
| 178 | object_records.create_collection_record( |
| 179 | "notes", {"id": "n1", "title": "Alice's note"}, base_dir=data_dir, roots=[], actor="alice", |
| 180 | ) |
| 181 | object_records.create_collection_record( |
| 182 | "notes", {"id": "n2", "title": "Bob's note"}, base_dir=data_dir, roots=[], actor="bob", |
| 183 | ) |
| 184 | |
| 185 | status, _, payload = request("/api/activity", headers=signed_in_bearer("alice")) |
| 186 | |
| 187 | assert status == 200 |
| 188 | assert payload["status"] == "ok" |
| 189 | assert [entry["record_id"] for entry in payload["activity"]] == ["n1"] |
| 190 | assert payload["activity"][0]["title"] == "Alice's note" |
| 191 | assert payload["activity"][0]["action"] == "create" |
| 192 | assert "before" not in payload["activity"][0] |
| 193 | |
| 194 | |
| 195 | def test_get_api_activity_respects_limit_query_param(tmp_path, monkeypatch): |
| 196 | data_dir = tmp_path / "data" |
| 197 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 198 | enable_admin_token(monkeypatch) |
| 199 | write_records(data_dir, "notes", "id\ttitle\n") |
| 200 | |
| 201 | import object_records |
| 202 | |
| 203 | for i in range(3): |
| 204 | object_records.create_collection_record( |
| 205 | "notes", {"id": f"n{i}", "title": f"Note {i}"}, |
| 206 | base_dir=data_dir, roots=[], actor="alice", |
| 207 | ) |
| 208 | |
| 209 | status, _, payload = request( |
| 210 | "/api/activity", query_string="limit=1", headers=signed_in_bearer("alice") |
| 211 | ) |
| 212 | |
| 213 | assert status == 200 |
| 214 | assert len(payload["activity"]) == 1 |
| 215 | assert payload["activity"][0]["record_id"] == "n2" |