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_rollup_package.py · 12.8 KB · 315 lines
| 1 | """Structural + permission tests for packages/app-rollup |
| 2 | (plan/vocabulary/14-rollup-spec.md). |
| 3 | |
| 4 | Mirrors tests/test_app_settings_package.py's package/schema/permission |
| 5 | conventions. The worked end-to-end example (orders-per-day) and the |
| 6 | "a rollup target can be public while its source stays owner-scoped" |
| 7 | payoff test live here rather than in tests/test_object_rollups.py because |
| 8 | they exercise the real package manifest + real permission policy, not |
| 9 | just object_rollups.py in isolation. |
| 10 | """ |
| 11 | |
| 12 | import json |
| 13 | import re |
| 14 | from pathlib import Path |
| 15 | |
| 16 | import object_packages |
| 17 | import object_permissions |
| 18 | import object_records |
| 19 | import object_rollups |
| 20 | import object_schemas |
| 21 | |
| 22 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 23 | APP_ROLLUP_DIR = PACKAGES_ROOT / "app-rollup" |
| 24 | |
| 25 | |
| 26 | def _write_schema(data_dir, collection, fields): |
| 27 | path = data_dir / "schemas" / f"{collection}.json" |
| 28 | path.parent.mkdir(parents=True, exist_ok=True) |
| 29 | path.write_text(json.dumps({"name": collection, "fields": fields})) |
| 30 | return path |
| 31 | |
| 32 | |
| 33 | # --- Package manifest / install -------------------------------------------- |
| 34 | |
| 35 | def test_get_package_normalizes_app_rollup_manifest(): |
| 36 | package = object_packages.get_package("app-rollup", root=PACKAGES_ROOT) |
| 37 | |
| 38 | assert package["id"] == "app-rollup" |
| 39 | assert package["name"] == "Rollups" |
| 40 | assert package["objects"] == [] |
| 41 | assert package["seed"] == [] |
| 42 | assert package["permissions"] == [{"path": "permissions/rules.json"}] |
| 43 | assert {schema["collection"] for schema in package["schemas"]} == {"rollup_definitions"} |
| 44 | |
| 45 | |
| 46 | def test_dry_run_app_rollup_package_is_safe(tmp_path): |
| 47 | object_root = tmp_path / "objects" |
| 48 | object_root.mkdir() |
| 49 | |
| 50 | plan = object_packages.dry_run_package( |
| 51 | "app-rollup", root=PACKAGES_ROOT, base_dir=tmp_path / "data", object_roots=[object_root], |
| 52 | ) |
| 53 | |
| 54 | assert plan["safe_to_install"] is True |
| 55 | assert plan["warnings"] == [] |
| 56 | assert {schema["collection"] for schema in plan["schemas"]} == {"rollup_definitions"} |
| 57 | |
| 58 | |
| 59 | def test_install_app_rollup_package_loads_schema(tmp_path): |
| 60 | data_dir = tmp_path / "data" |
| 61 | object_root = tmp_path / "objects" |
| 62 | object_root.mkdir() |
| 63 | |
| 64 | object_packages.install_package( |
| 65 | "app-rollup", root=PACKAGES_ROOT, base_dir=data_dir, object_roots=[object_root], |
| 66 | ) |
| 67 | |
| 68 | schema = object_schemas.get_schema("rollup_definitions", base_dir=data_dir) |
| 69 | assert schema["name"] == "rollup_definitions" |
| 70 | field_names = [f["name"] for f in schema["fields"]] |
| 71 | assert field_names == [ |
| 72 | "id", "name", "source_collection", "filter", "group_by", "time_bucket", |
| 73 | "metrics", "target_collection", "min_group_size", "refresh_mode", |
| 74 | "refresh_interval_seconds", "last_computed_at", "enabled", |
| 75 | ] |
| 76 | by_name = {f["name"]: f for f in schema["fields"]} |
| 77 | assert by_name["last_computed_at"].get("read_only") is True |
| 78 | assert schema["views"]["list_mode"] == "table" |
| 79 | |
| 80 | |
| 81 | def test_schema_json_file_is_valid_and_versioned(): |
| 82 | payload = json.loads((APP_ROLLUP_DIR / "schemas" / "rollup_definitions.json").read_text()) |
| 83 | assert payload["name"] == "rollup_definitions" |
| 84 | assert payload["version"] == 1 |
| 85 | assert payload["views"]["list_mode"] == "table" |
| 86 | |
| 87 | |
| 88 | def test_no_disallowed_org_names_leak_into_the_package(): |
| 89 | """Public repo hygiene: no internal org/codename references anywhere |
| 90 | in this package's source (same guard as tests/test_app_orders_package.py). |
| 91 | """ |
| 92 | banned = re.compile( |
| 93 | "|".join([r"\b" + "q" + "9" + r"\b", "ask" + "robots", r"\b" + "wo" + "ld" + r"\b"]), |
| 94 | re.IGNORECASE, |
| 95 | ) |
| 96 | for path in APP_ROLLUP_DIR.rglob("*"): |
| 97 | if path.is_dir(): |
| 98 | continue |
| 99 | text = path.read_text(encoding="utf-8", errors="ignore") |
| 100 | assert not banned.search(text), f"disallowed reference found in {path}" |
| 101 | |
| 102 | |
| 103 | # --- Permissions: rollup_definitions is admin-owned, full stop ------------- |
| 104 | |
| 105 | def _app_rollup_policy(extra_rules=()): |
| 106 | payload = json.loads((APP_ROLLUP_DIR / "permissions" / "rules.json").read_text()) |
| 107 | rules = list(payload["rules"]) + list(extra_rules) |
| 108 | return object_permissions.policy_from_dict({"access_mode": "role_based", "rules": rules}) |
| 109 | |
| 110 | |
| 111 | def test_rollup_definitions_denied_to_registered_non_admin_for_every_action(): |
| 112 | policy = _app_rollup_policy() |
| 113 | subject = object_permissions.PermissionSubject(user_id="7") |
| 114 | record = {"id": "r1", "name": "Orders per day", "source_collection": "orders"} |
| 115 | |
| 116 | for action in ( |
| 117 | object_permissions.CREATE, object_permissions.READ, |
| 118 | object_permissions.UPDATE, object_permissions.DELETE, |
| 119 | ): |
| 120 | decision = object_permissions.check_permission( |
| 121 | subject, action, policy=policy, collection="rollup_definitions", record=record, |
| 122 | ) |
| 123 | assert decision.allowed is False, action |
| 124 | |
| 125 | |
| 126 | def test_rollup_definitions_unreachable_by_anonymous_reads(): |
| 127 | policy = _app_rollup_policy() |
| 128 | |
| 129 | decision = object_permissions.check_permission( |
| 130 | None, object_permissions.READ, policy=policy, collection="rollup_definitions", |
| 131 | ) |
| 132 | |
| 133 | assert decision.allowed is False |
| 134 | |
| 135 | |
| 136 | def test_rollup_definitions_allowed_for_admin_role(): |
| 137 | policy = _app_rollup_policy() |
| 138 | admin = object_permissions.PermissionSubject(user_id="1", roles=("admin",)) |
| 139 | |
| 140 | for action in ( |
| 141 | object_permissions.CREATE, object_permissions.READ, |
| 142 | object_permissions.UPDATE, object_permissions.DELETE, |
| 143 | ): |
| 144 | decision = object_permissions.check_permission( |
| 145 | admin, action, policy=policy, collection="rollup_definitions", |
| 146 | ) |
| 147 | assert decision.allowed is True, action |
| 148 | |
| 149 | |
| 150 | # --- Worked example: orders-per-day, end-to-end ---------------------------- |
| 151 | |
| 152 | ORDERS_FIELDS = [ |
| 153 | {"name": "id"}, |
| 154 | {"name": "owner_id", "type": "text"}, |
| 155 | {"name": "channel", "type": "text"}, |
| 156 | {"name": "status", "type": "text"}, |
| 157 | {"name": "total_cents", "type": "integer"}, |
| 158 | {"name": "created_at", "type": "datetime"}, |
| 159 | ] |
| 160 | |
| 161 | |
| 162 | def _install_worked_example(data_dir, object_root): |
| 163 | object_packages.install_package( |
| 164 | "app-rollup", root=PACKAGES_ROOT, base_dir=data_dir, object_roots=[object_root], |
| 165 | ) |
| 166 | _write_schema(data_dir, "orders", ORDERS_FIELDS) |
| 167 | |
| 168 | # Two customers' paid orders (owner-scoped source data) plus one draft |
| 169 | # order that the rollup's filter must exclude. |
| 170 | for row in ( |
| 171 | {"id": "o1", "owner_id": "alice", "channel": "web", "status": "paid", |
| 172 | "total_cents": "1000", "created_at": "2026-07-01T09:00:00Z"}, |
| 173 | {"id": "o2", "owner_id": "bob", "channel": "web", "status": "paid", |
| 174 | "total_cents": "3000", "created_at": "2026-07-01T15:00:00Z"}, |
| 175 | {"id": "o3", "owner_id": "alice", "channel": "retail", "status": "paid", |
| 176 | "total_cents": "500", "created_at": "2026-07-01T10:00:00Z"}, |
| 177 | {"id": "o4", "owner_id": "bob", "channel": "web", "status": "draft", |
| 178 | "total_cents": "999999", "created_at": "2026-07-01T09:00:00Z"}, |
| 179 | ): |
| 180 | object_records.create_collection_record("orders", row, base_dir=data_dir, roots=[object_root]) |
| 181 | |
| 182 | definition = { |
| 183 | "id": "rollup_orders_by_day", |
| 184 | "name": "Orders per day", |
| 185 | "source_collection": "orders", |
| 186 | "filter": json.dumps({"status": "paid"}), |
| 187 | "group_by": json.dumps(["channel"]), |
| 188 | "time_bucket": json.dumps({"field": "created_at", "granularity": "day"}), |
| 189 | "metrics": json.dumps([ |
| 190 | {"op": "count", "as": "order_count"}, |
| 191 | {"op": "sum", "field": "total_cents", "as": "revenue_cents"}, |
| 192 | {"op": "avg", "field": "total_cents", "as": "avg_order_cents"}, |
| 193 | ]), |
| 194 | "target_collection": "rollup_orders_by_day", |
| 195 | "min_group_size": "", |
| 196 | "refresh_mode": "scheduled", |
| 197 | "refresh_interval_seconds": "3600", |
| 198 | "enabled": "true", |
| 199 | } |
| 200 | object_records.create_collection_record( |
| 201 | "rollup_definitions", definition, base_dir=data_dir, roots=[object_root], actor="admin", |
| 202 | ) |
| 203 | return definition |
| 204 | |
| 205 | |
| 206 | def test_worked_example_orders_per_day_computes_expected_rows(tmp_path): |
| 207 | data_dir = tmp_path / "data" |
| 208 | object_root = tmp_path / "objects" |
| 209 | object_root.mkdir() |
| 210 | definition = _install_worked_example(data_dir, object_root) |
| 211 | |
| 212 | result = object_rollups.compute_rollup(definition, base_dir=data_dir, roots=[object_root]) |
| 213 | assert result["groups"] == 2 # web, retail -- the draft order never counts |
| 214 | |
| 215 | rows = object_records.read_collection_records("rollup_orders_by_day", base_dir=data_dir, roots=[object_root]) |
| 216 | by_channel = {r["channel"]: r for r in rows} |
| 217 | |
| 218 | assert by_channel["web"]["bucket_start"] == "2026-07-01" |
| 219 | assert by_channel["web"]["order_count"] == "2" |
| 220 | assert by_channel["web"]["revenue_cents"] == "4000" # alice's 1000 + bob's 3000 |
| 221 | assert by_channel["web"]["avg_order_cents"] == "2000.0" |
| 222 | assert by_channel["retail"]["order_count"] == "1" |
| 223 | assert by_channel["retail"]["revenue_cents"] == "500" |
| 224 | |
| 225 | # No customer identity anywhere in the target -- owner_id was never a |
| 226 | # group_by field or a metric, so it never made it into the derived |
| 227 | # schema at all. |
| 228 | schema = object_schemas.get_schema("rollup_orders_by_day", base_dir=data_dir) |
| 229 | assert "owner_id" not in [f["name"] for f in schema["fields"]] |
| 230 | |
| 231 | |
| 232 | def test_worked_example_rollup_target_can_be_public_while_source_stays_owner_scoped(tmp_path): |
| 233 | """The payoff named in 14's Permissions Posture: orders is owner- |
| 234 | scoped, but rollup_orders_by_day (aggregate counts/revenue, no |
| 235 | customer identity in any row) can carry a public-read rule of its |
| 236 | own, granted independently of the source's rule. |
| 237 | """ |
| 238 | data_dir = tmp_path / "data" |
| 239 | object_root = tmp_path / "objects" |
| 240 | object_root.mkdir() |
| 241 | definition = _install_worked_example(data_dir, object_root) |
| 242 | object_rollups.compute_rollup(definition, base_dir=data_dir, roots=[object_root]) |
| 243 | |
| 244 | target_row = object_records.read_collection_records( |
| 245 | "rollup_orders_by_day", base_dir=data_dir, roots=[object_root] |
| 246 | )[0] |
| 247 | |
| 248 | policy = _app_rollup_policy([ |
| 249 | { |
| 250 | "effect": "allow", |
| 251 | "principal": "registered", |
| 252 | "actions": ["create", "read", "update", "delete"], |
| 253 | "collection": "orders", |
| 254 | "row_filter": {"owner_id": "$user_id"}, |
| 255 | "reason": "signed-in users manage their own orders", |
| 256 | }, |
| 257 | { |
| 258 | "effect": "allow", |
| 259 | "principal": "public", |
| 260 | "actions": ["read"], |
| 261 | "collection": "rollup_orders_by_day", |
| 262 | "reason": "aggregate revenue by day and channel; no individual order or customer identity", |
| 263 | }, |
| 264 | ]) |
| 265 | |
| 266 | order_row = {"id": "o1", "owner_id": "alice", "channel": "web", "status": "paid", "total_cents": "1000"} |
| 267 | |
| 268 | # The source stays exactly as private as it always was: an anonymous |
| 269 | # visitor, and even a signed-in user who isn't the owner, are denied. |
| 270 | anon_on_orders = object_permissions.check_permission( |
| 271 | None, object_permissions.READ, policy=policy, collection="orders", record=order_row, |
| 272 | ) |
| 273 | other_user_on_orders = object_permissions.check_permission( |
| 274 | object_permissions.PermissionSubject(user_id="bob"), |
| 275 | object_permissions.READ, policy=policy, collection="orders", record=order_row, |
| 276 | ) |
| 277 | assert anon_on_orders.allowed is False |
| 278 | assert other_user_on_orders.allowed is False |
| 279 | |
| 280 | # The rollup target, carrying its OWN rule, is readable by anyone -- |
| 281 | # including someone with no account at all -- despite resting entirely |
| 282 | # on data drawn from the owner-scoped orders collection above. |
| 283 | anon_on_rollup = object_permissions.check_permission( |
| 284 | None, object_permissions.READ, policy=policy, collection="rollup_orders_by_day", record=target_row, |
| 285 | ) |
| 286 | assert anon_on_rollup.allowed is True |
| 287 | |
| 288 | # And the rollup target still isn't writable by that same public reader |
| 289 | # -- only the daemon pass (via allow_computed_submission, never wired |
| 290 | # to a request) writes its rows. |
| 291 | anon_write_on_rollup = object_permissions.check_permission( |
| 292 | None, object_permissions.UPDATE, policy=policy, collection="rollup_orders_by_day", record=target_row, |
| 293 | ) |
| 294 | assert anon_write_on_rollup.allowed is False |
| 295 | |
| 296 | |
| 297 | def test_worked_example_min_group_size_suppresses_a_single_customer_day(tmp_path): |
| 298 | """14's Permissions Posture disclosure-threat worked case: a channel |
| 299 | with exactly one paid order on a given day, grouped finely enough, |
| 300 | would otherwise disclose that one customer's presence (and revenue) |
| 301 | to anyone who can read a public target. min_group_size=2 drops it. |
| 302 | """ |
| 303 | data_dir = tmp_path / "data" |
| 304 | object_root = tmp_path / "objects" |
| 305 | object_root.mkdir() |
| 306 | definition = _install_worked_example(data_dir, object_root) |
| 307 | definition["min_group_size"] = "2" |
| 308 | |
| 309 | result = object_rollups.compute_rollup(definition, base_dir=data_dir, roots=[object_root]) |
| 310 | |
| 311 | # "retail" has exactly one paid order (alice's) -- suppressed. "web" |
| 312 | # has two (alice's and bob's) -- kept. |
| 313 | assert result["suppressed"] == 1 |
| 314 | rows = object_records.read_collection_records("rollup_orders_by_day", base_dir=data_dir, roots=[object_root]) |
| 315 | assert {r["channel"] for r in rows} == {"web"} |