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

0 downloads

dbbasic-object-server/tests/test_app_dashboard_package.py
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.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_app_dashboard_package.py · 5.1 KB · 142 lines
1"""Structural + behavioral tests for packages/app-dashboard.
2
3Mirrors the package/permission testing conventions used for
4packages/app-activity (see tests/test_app_activity_package.py) and the
5direct-execution pattern tests/test_app_views_package.py uses for a
6renderer-style site object. app-dashboard owns no collection and no
7schema -- it only folds existing collections (ai_usage, tasks, /api/
8activity, notes/contacts/invoices) into a page, so there is nothing here
9to test at the data layer; the page-execution and permission tests below
10cover the whole surface.
11"""
12
13import json
14import re
15from pathlib import Path
16
17import object_execution
18import object_packages
19import object_permissions
20import python_object_runtime
21
22PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages"
23APP_DASHBOARD_DIR = PACKAGES_ROOT / "app-dashboard"
24TEST_FILE = Path(__file__).resolve()
25
26
27def _app_dashboard_policy():
28 payload = json.loads((APP_DASHBOARD_DIR / "permissions" / "rules.json").read_text())
29 return object_permissions.policy_from_dict({"access_mode": "role_based", "rules": payload["rules"]})
30
31
32def test_get_package_normalizes_app_dashboard_manifest():
33 package = object_packages.get_package("app-dashboard", root=PACKAGES_ROOT)
34
35 assert package["id"] == "app-dashboard"
36 assert package["name"] == "Dashboard"
37 assert package["objects"] == [{"id": "site_dashboard", "path": "objects/site/dashboard.py"}]
38 assert package["schemas"] == []
39 assert package["seed"] == []
40 assert package["permissions"] == [{"path": "permissions/rules.json"}]
41
42
43def test_dry_run_app_dashboard_package_is_safe(tmp_path):
44 object_root = tmp_path / "objects"
45 object_root.mkdir()
46
47 plan = object_packages.dry_run_package(
48 "app-dashboard",
49 root=PACKAGES_ROOT,
50 base_dir=tmp_path / "data",
51 object_roots=[object_root],
52 )
53
54 assert plan["safe_to_install"] is True
55 assert plan["warnings"] == []
56
57
58def test_site_dashboard_execute_is_public():
59 """Same split as site_notes/site_tasks/site_activity: public EXECUTE on
60 the *page* object (it shows a sign-in prompt to visitors), never public
61 read on a collection -- app-dashboard owns no collection at all.
62 """
63 policy = _app_dashboard_policy()
64
65 decision = object_permissions.check_permission(
66 None, object_permissions.EXECUTE, policy=policy, object_id="site_dashboard"
67 )
68
69 assert decision.allowed is True
70
71
72def test_dashboard_page_prompts_anonymous_visitors_to_sign_in(tmp_path):
73 data_dir = tmp_path / "data"
74 object_root = tmp_path / "objects"
75 object_root.mkdir()
76
77 object_packages.install_package(
78 "app-dashboard", root=PACKAGES_ROOT, base_dir=data_dir, object_roots=[object_root]
79 )
80
81 runtime = python_object_runtime.PythonObjectRuntime(base_dir=data_dir)
82 result = object_execution.execute_object(
83 runtime,
84 object_execution.ObjectExecutionRequest("site_dashboard", payload={"_identity": {}}),
85 roots=[object_root],
86 )
87
88 assert result.ok is True
89 body = result.result["body"]
90 assert result.result["content_type"] == "text/html; charset=utf-8"
91 assert result.result.get("status", 200) == 200
92 assert "Sign in" in body
93 assert 'id="ai-usage"' not in body
94
95
96def test_dashboard_page_serves_stats_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-dashboard", 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_dashboard", payload={"_identity": {"user_id": "alice"}}
110 ),
111 roots=[object_root],
112 )
113
114 assert result.ok is True
115 body = result.result["body"]
116 # The page holds no data access of its own -- it fetches with the
117 # caller's session cookie, so the scaffolding should reference the
118 # source endpoints/collections it folds, not embed any data.
119 assert 'id="ai-usage"' in body
120 assert 'id="task-stats"' in body
121 assert 'id="activity"' in body
122 assert "/collections/ai_usage/records" in body
123 assert "/collections/tasks/records" in body
124 assert "/api/activity" in body
125 assert '<link rel="stylesheet" href="/style">' in body
126 assert '<script src="/nav">' in body
127
128
129def test_no_disallowed_org_names_leak_into_the_package_or_this_test_file():
130 """Public repo hygiene: no internal org/codename references anywhere in
131 this package's source or in this test file.
132 """
133 # Built from fragments so this guard file itself stays clean of the very
134 # internal names it forbids (otherwise the test would flag its own source).
135 banned = re.compile(
136 "|".join([r"\b" + "q" + "9" + r"\b", "ask" + "robots", r"\b" + "wo" + "ld" + r"\b"]),
137 re.IGNORECASE,
138 )
139 paths = [p for p in APP_DASHBOARD_DIR.rglob("*") if p.is_file()] + [TEST_FILE]
140 for path in paths:
141 text = path.read_text(encoding="utf-8", errors="ignore")
142 assert not banned.search(text), f"disallowed reference found in {path}"