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