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_dashboard_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, 19 minutes ago History
test_app_dashboard_package.py · 5.2 KB · 143 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
21from package_pins import assert_includes_in_order
22
23PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages"
24APP_DASHBOARD_DIR = PACKAGES_ROOT / "app-dashboard"
25TEST_FILE = Path(__file__).resolve()
26
27
28def _app_dashboard_policy():
29 payload = json.loads((APP_DASHBOARD_DIR / "permissions" / "rules.json").read_text())
30 return object_permissions.policy_from_dict({"access_mode": "role_based", "rules": payload["rules"]})
31
32
33def test_get_package_normalizes_app_dashboard_manifest():
34 package = object_packages.get_package("app-dashboard", root=PACKAGES_ROOT)
35
36 assert package["id"] == "app-dashboard"
37 assert package["name"] == "Dashboard"
38 assert_includes_in_order(package["objects"], [{"id": "site_dashboard", "path": "objects/site/dashboard.py"}])
39 assert package["schemas"] == []
40 assert package["seed"] == []
41 assert {"path": "permissions/rules.json"} in package["permissions"]
42
43
44def test_dry_run_app_dashboard_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-dashboard",
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
59def test_site_dashboard_execute_is_public():
60 """Same split as site_notes/site_tasks/site_activity: public EXECUTE on
61 the *page* object (it shows a sign-in prompt to visitors), never public
62 read on a collection -- app-dashboard owns no collection at all.
63 """
64 policy = _app_dashboard_policy()
65
66 decision = object_permissions.check_permission(
67 None, object_permissions.EXECUTE, policy=policy, object_id="site_dashboard"
68 )
69
70 assert decision.allowed is True
71
72
73def test_dashboard_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-dashboard", 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_dashboard", 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 result.result.get("status", 200) == 200
93 assert "Sign in" in body
94 assert 'id="ai-usage"' not in body
95
96
97def test_dashboard_page_serves_stats_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-dashboard", 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_dashboard", payload={"_identity": {"user_id": "alice"}}
111 ),
112 roots=[object_root],
113 )
114
115 assert result.ok is True
116 body = result.result["body"]
117 # The page holds no data access of its own -- it fetches with the
118 # caller's session cookie, so the scaffolding should reference the
119 # source endpoints/collections it folds, not embed any data.
120 assert 'id="ai-usage"' in body
121 assert 'id="task-stats"' in body
122 assert 'id="activity"' in body
123 assert "/collections/ai_usage/records" in body
124 assert "/collections/tasks/records" in body
125 assert "/api/activity" in body
126 assert '<link rel="stylesheet" href="/style">' in body
127 assert '<script src="/nav">' in body
128
129
130def test_no_disallowed_org_names_leak_into_the_package_or_this_test_file():
131 """Public repo hygiene: no internal org/codename references anywhere in
132 this package's source or in this test file.
133 """
134 # Built from fragments so this guard file itself stays clean of the very
135 # internal names it forbids (otherwise the test would flag its own source).
136 banned = re.compile(
137 "|".join([r"\b" + "q" + "9" + r"\b", "ask" + "robots", r"\b" + "wo" + "ld" + r"\b"]),
138 re.IGNORECASE,
139 )
140 paths = [p for p in APP_DASHBOARD_DIR.rglob("*") if p.is_file()] + [TEST_FILE]
141 for path in paths:
142 text = path.read_text(encoding="utf-8", errors="ignore")
143 assert not banned.search(text), f"disallowed reference found in {path}"