Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

dbbasic-object-server/tests/test_app_activity_package.py
Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

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