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_settings_package.py · 9.3 KB · 281 lines
| 1 | """Structural tests for packages/app-settings (Phase 6: user_prefs + feature_flags). |
| 2 | |
| 3 | Mirrors the package/schema/permission testing conventions used for |
| 4 | packages/app-notes in tests/test_object_packages.py and |
| 5 | tests/test_object_permissions.py. |
| 6 | """ |
| 7 | |
| 8 | import json |
| 9 | import re |
| 10 | from pathlib import Path |
| 11 | |
| 12 | import object_packages |
| 13 | import object_permissions |
| 14 | import object_schemas |
| 15 | |
| 16 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 17 | APP_SETTINGS_DIR = PACKAGES_ROOT / "app-settings" |
| 18 | |
| 19 | |
| 20 | def test_get_package_normalizes_app_settings_manifest(): |
| 21 | package = object_packages.get_package("app-settings", root=PACKAGES_ROOT) |
| 22 | |
| 23 | assert package["id"] == "app-settings" |
| 24 | assert package["name"] == "Settings" |
| 25 | assert package["version"] == "0.4.0" |
| 26 | # 0.3.0's only object: the gate that makes one-row-per-key true rather |
| 27 | # than merely intended (see tests/test_settings_uniqueness.py). |
| 28 | assert package["objects"] == [ |
| 29 | {"id": "hook_app_settings", "path": "objects/hook/app_settings.py"}] |
| 30 | assert package["seed"] == [ |
| 31 | {"collection": "ai_prices", "path": "seed/ai_prices.tsv"}, |
| 32 | # Media cannot be priced as a token rate; see object_unit_prices. |
| 33 | {"collection": "unit_prices", "path": "seed/unit_prices.tsv"}] |
| 34 | assert package["permissions"] == [{"path": "permissions/rules.json"}] |
| 35 | assert {schema["collection"] for schema in package["schemas"]} == { |
| 36 | "user_prefs", |
| 37 | "feature_flags", |
| 38 | "ai_prices", |
| 39 | "app_settings", # server-wide per-app config (payments.* first) |
| 40 | "unit_prices", # per-image / per-second media prices |
| 41 | } |
| 42 | |
| 43 | |
| 44 | def test_dry_run_app_settings_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-settings", |
| 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 | assert {schema["collection"] for schema in plan["schemas"]} == { |
| 58 | "user_prefs", |
| 59 | "feature_flags", |
| 60 | "ai_prices", |
| 61 | "app_settings", # server-wide per-app config (payments.* first) |
| 62 | "unit_prices", # per-image / per-second media prices |
| 63 | } |
| 64 | |
| 65 | |
| 66 | def test_install_app_settings_package_loads_schemas(tmp_path): |
| 67 | data_dir = tmp_path / "data" |
| 68 | object_root = tmp_path / "objects" |
| 69 | object_root.mkdir() |
| 70 | |
| 71 | object_packages.install_package( |
| 72 | "app-settings", |
| 73 | root=PACKAGES_ROOT, |
| 74 | base_dir=data_dir, |
| 75 | object_roots=[object_root], |
| 76 | ) |
| 77 | |
| 78 | prefs_schema = object_schemas.get_schema("user_prefs", base_dir=data_dir) |
| 79 | flags_schema = object_schemas.get_schema("feature_flags", base_dir=data_dir) |
| 80 | |
| 81 | assert prefs_schema["name"] == "user_prefs" |
| 82 | assert [field["name"] for field in prefs_schema["fields"]] == [ |
| 83 | "id", |
| 84 | "owner_id", |
| 85 | "key", |
| 86 | "value", |
| 87 | ] |
| 88 | assert prefs_schema["forms"]["default"]["fields"] == ["key", "value"] |
| 89 | |
| 90 | assert flags_schema["name"] == "feature_flags" |
| 91 | assert [field["name"] for field in flags_schema["fields"]] == [ |
| 92 | "id", |
| 93 | "flag", |
| 94 | "value", |
| 95 | "description", |
| 96 | ] |
| 97 | |
| 98 | prices_schema = object_schemas.get_schema("ai_prices", base_dir=data_dir) |
| 99 | assert prices_schema["name"] == "ai_prices" |
| 100 | assert [field["name"] for field in prices_schema["fields"]] == [ |
| 101 | "id", |
| 102 | "provider", |
| 103 | "model", |
| 104 | "input_per_million_cents", |
| 105 | "output_per_million_cents", |
| 106 | "effective_date", |
| 107 | ] |
| 108 | |
| 109 | |
| 110 | def test_schema_json_files_are_valid_and_versioned(): |
| 111 | for name in ("user_prefs", "feature_flags", "ai_prices"): |
| 112 | payload = json.loads((APP_SETTINGS_DIR / "schemas" / f"{name}.json").read_text()) |
| 113 | assert payload["name"] == name |
| 114 | assert payload["version"] == 1 |
| 115 | assert payload["views"]["list_mode"] == "table" |
| 116 | |
| 117 | |
| 118 | def test_install_app_settings_package_seeds_ai_prices(tmp_path): |
| 119 | data_dir = tmp_path / "data" |
| 120 | object_root = tmp_path / "objects" |
| 121 | object_root.mkdir() |
| 122 | |
| 123 | object_packages.install_package( |
| 124 | "app-settings", |
| 125 | root=PACKAGES_ROOT, |
| 126 | base_dir=data_dir, |
| 127 | object_roots=[object_root], |
| 128 | ) |
| 129 | |
| 130 | import object_records |
| 131 | |
| 132 | rows = object_records.read_collection_records("ai_prices", base_dir=data_dir) |
| 133 | assert rows # seeded with at least one current entry |
| 134 | for row in rows: |
| 135 | # Integer cents only -- never a float for money. |
| 136 | assert re.fullmatch(r"-?\d+", row["input_per_million_cents"]) |
| 137 | assert re.fullmatch(r"-?\d+", row["output_per_million_cents"]) |
| 138 | |
| 139 | |
| 140 | def _app_settings_policy(): |
| 141 | payload = json.loads((APP_SETTINGS_DIR / "permissions" / "rules.json").read_text()) |
| 142 | return object_permissions.policy_from_dict({"access_mode": "role_based", "rules": payload["rules"]}) |
| 143 | |
| 144 | |
| 145 | def test_user_prefs_row_filter_scopes_reads_to_owner(): |
| 146 | policy = _app_settings_policy() |
| 147 | subject = object_permissions.PermissionSubject(user_id="7") |
| 148 | |
| 149 | own_row = object_permissions.check_permission( |
| 150 | subject, |
| 151 | object_permissions.READ, |
| 152 | policy=policy, |
| 153 | collection="user_prefs", |
| 154 | record={"owner_id": "7", "key": "theme", "value": "dark"}, |
| 155 | ) |
| 156 | other_row = object_permissions.check_permission( |
| 157 | subject, |
| 158 | object_permissions.READ, |
| 159 | policy=policy, |
| 160 | collection="user_prefs", |
| 161 | record={"owner_id": "8", "key": "theme", "value": "light"}, |
| 162 | ) |
| 163 | |
| 164 | assert own_row.allowed is True |
| 165 | assert other_row.allowed is False |
| 166 | |
| 167 | |
| 168 | def test_user_prefs_row_filter_scopes_writes_to_owner(): |
| 169 | policy = _app_settings_policy() |
| 170 | subject = object_permissions.PermissionSubject(user_id="7") |
| 171 | |
| 172 | own_update = object_permissions.check_permission( |
| 173 | subject, |
| 174 | object_permissions.UPDATE, |
| 175 | policy=policy, |
| 176 | collection="user_prefs", |
| 177 | record={"owner_id": "7", "key": "theme", "value": "dark"}, |
| 178 | ) |
| 179 | other_delete = object_permissions.check_permission( |
| 180 | subject, |
| 181 | object_permissions.DELETE, |
| 182 | policy=policy, |
| 183 | collection="user_prefs", |
| 184 | record={"owner_id": "8", "key": "theme", "value": "light"}, |
| 185 | ) |
| 186 | |
| 187 | assert own_update.allowed is True |
| 188 | assert other_delete.allowed is False |
| 189 | |
| 190 | |
| 191 | def test_feature_flags_readable_by_registered_users(): |
| 192 | policy = _app_settings_policy() |
| 193 | subject = object_permissions.PermissionSubject(user_id="7") |
| 194 | |
| 195 | decision = object_permissions.check_permission( |
| 196 | subject, object_permissions.READ, policy=policy, collection="feature_flags" |
| 197 | ) |
| 198 | |
| 199 | assert decision.allowed is True |
| 200 | |
| 201 | |
| 202 | def test_feature_flags_not_writable_by_registered_users(): |
| 203 | policy = _app_settings_policy() |
| 204 | subject = object_permissions.PermissionSubject(user_id="7") |
| 205 | record = {"flag": "kanban_view", "value": "on", "description": "Kanban board"} |
| 206 | |
| 207 | create = object_permissions.check_permission( |
| 208 | subject, object_permissions.CREATE, policy=policy, collection="feature_flags", record=record |
| 209 | ) |
| 210 | update = object_permissions.check_permission( |
| 211 | subject, object_permissions.UPDATE, policy=policy, collection="feature_flags", record=record |
| 212 | ) |
| 213 | delete = object_permissions.check_permission( |
| 214 | subject, object_permissions.DELETE, policy=policy, collection="feature_flags", record=record |
| 215 | ) |
| 216 | |
| 217 | assert create.allowed is False |
| 218 | assert update.allowed is False |
| 219 | assert delete.allowed is False |
| 220 | |
| 221 | |
| 222 | def test_feature_flags_unreachable_by_anonymous_reads(): |
| 223 | policy = _app_settings_policy() |
| 224 | |
| 225 | decision = object_permissions.check_permission( |
| 226 | None, object_permissions.READ, policy=policy, collection="feature_flags" |
| 227 | ) |
| 228 | |
| 229 | assert decision.allowed is False |
| 230 | |
| 231 | |
| 232 | def test_ai_prices_readable_by_registered_users(): |
| 233 | policy = _app_settings_policy() |
| 234 | subject = object_permissions.PermissionSubject(user_id="7") |
| 235 | |
| 236 | decision = object_permissions.check_permission( |
| 237 | subject, object_permissions.READ, policy=policy, collection="ai_prices" |
| 238 | ) |
| 239 | |
| 240 | assert decision.allowed is True |
| 241 | |
| 242 | |
| 243 | def test_ai_prices_not_writable_by_registered_users(): |
| 244 | """Prices are live-editable data, but only for admins (who bypass the |
| 245 | policy engine entirely) -- an ordinary registered user must not be able |
| 246 | to create, update, or delete a price row, or cost recording could be |
| 247 | corrupted the same way the predecessor's hardcoded table went stale. |
| 248 | """ |
| 249 | policy = _app_settings_policy() |
| 250 | subject = object_permissions.PermissionSubject(user_id="7") |
| 251 | record = { |
| 252 | "provider": "anthropic", |
| 253 | "model": "claude-haiku-4-5", |
| 254 | "input_per_million_cents": "1", |
| 255 | "output_per_million_cents": "1", |
| 256 | "effective_date": "2026-01-01", |
| 257 | } |
| 258 | |
| 259 | create = object_permissions.check_permission( |
| 260 | subject, object_permissions.CREATE, policy=policy, collection="ai_prices", record=record |
| 261 | ) |
| 262 | update = object_permissions.check_permission( |
| 263 | subject, object_permissions.UPDATE, policy=policy, collection="ai_prices", record=record |
| 264 | ) |
| 265 | delete = object_permissions.check_permission( |
| 266 | subject, object_permissions.DELETE, policy=policy, collection="ai_prices", record=record |
| 267 | ) |
| 268 | |
| 269 | assert create.allowed is False |
| 270 | assert update.allowed is False |
| 271 | assert delete.allowed is False |
| 272 | |
| 273 | |
| 274 | def test_ai_prices_unreachable_by_anonymous_reads(): |
| 275 | policy = _app_settings_policy() |
| 276 | |
| 277 | decision = object_permissions.check_permission( |
| 278 | None, object_permissions.READ, policy=policy, collection="ai_prices" |
| 279 | ) |
| 280 | |
| 281 | assert decision.allowed is False |