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_catalog_package.py · 26.3 KB · 619 lines
| 1 | """Structural tests for packages/app-catalog (products + STOCK: locations, |
| 2 | stock_moves). |
| 3 | |
| 4 | Mirrors the package/schema/permission testing conventions used for |
| 5 | packages/app-invoices in tests/test_app_invoices_package.py. Behavior |
| 6 | tests for the derived-levels fold (object_stock.py) live in |
| 7 | tests/test_object_stock.py. |
| 8 | """ |
| 9 | |
| 10 | import json |
| 11 | import re |
| 12 | from pathlib import Path |
| 13 | |
| 14 | import object_packages |
| 15 | import object_permissions |
| 16 | import object_schemas |
| 17 | from package_pins import assert_includes_in_order |
| 18 | |
| 19 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 20 | APP_CATALOG_DIR = PACKAGES_ROOT / "app-catalog" |
| 21 | |
| 22 | # Field types that store money as a float/decimal rather than integer cents |
| 23 | # -- the doctrine this package must never violate (00-doctrine-and-contract.md). |
| 24 | _FLOAT_MONEY_TYPES = {"float", "number", "currency"} |
| 25 | |
| 26 | # quantity is the one deliberate exception: a count/measure (e.g. 2.5 kg), |
| 27 | # not currency -- same exception app-orders' order_lines.quantity documents. |
| 28 | _ALLOWED_FLOAT_FIELDS = {"quantity"} |
| 29 | |
| 30 | |
| 31 | def _products_schema(): |
| 32 | return json.loads((APP_CATALOG_DIR / "schemas" / "products.json").read_text()) |
| 33 | |
| 34 | |
| 35 | def _locations_schema(): |
| 36 | return json.loads((APP_CATALOG_DIR / "schemas" / "locations.json").read_text()) |
| 37 | |
| 38 | |
| 39 | def _stock_moves_schema(): |
| 40 | return json.loads((APP_CATALOG_DIR / "schemas" / "stock_moves.json").read_text()) |
| 41 | |
| 42 | |
| 43 | def test_get_package_normalizes_app_catalog_manifest(): |
| 44 | package = object_packages.get_package("app-catalog", root=PACKAGES_ROOT) |
| 45 | |
| 46 | assert package["id"] == "app-catalog" |
| 47 | assert package["name"] == "Catalog" |
| 48 | assert {schema["collection"] for schema in package["schemas"]} >= { |
| 49 | "products", "locations", "stock_moves", |
| 50 | "backorders", "reorder_suggestions", |
| 51 | } |
| 52 | # site_product_view was removed in the Stage-6 retrofit: the product |
| 53 | # permalink is now a seeded 59 detail view (site_view_render), not a |
| 54 | # bespoke page object -- see tests/test_app_catalog_detail_retrofit.py. |
| 55 | # 0.4.0 added the inventory-adjustments trio (hook gate/stamp, shrinkage |
| 56 | # composer, count reconciliation -- plan/inventory-adjustments-spec.md). |
| 57 | # 0.5.0 deletes the bespoke /products and /locations list page objects |
| 58 | # too: both held no business logic and are now seeded VIEW records over |
| 59 | # the generic `list` block. |
| 60 | # 0.7.0 adds the commerce-ops trio: the nightly reorder fold and the |
| 61 | # two counts (products to reorder, backorders waiting). |
| 62 | assert {obj["id"] for obj in package["objects"]} >= { |
| 63 | "site_stock", |
| 64 | "hook_stock_moves", |
| 65 | "system_stock_books", |
| 66 | "action_apply_count", |
| 67 | "system_reorder_check", |
| 68 | "system_reorder_attention", |
| 69 | # 0.10.0: backorders waiting is declared (collection + where), not code. |
| 70 | } |
| 71 | assert {"path": "permissions/rules.json"} in package["permissions"] |
| 72 | assert {entry["collection"] for entry in package["seed"]} >= { |
| 73 | "products", "locations", "stock_moves", "views", "site_routes", |
| 74 | "backorders", "reorder_suggestions", |
| 75 | } |
| 76 | |
| 77 | |
| 78 | def test_dry_run_app_catalog_package_is_safe(tmp_path): |
| 79 | object_root = tmp_path / "objects" |
| 80 | object_root.mkdir() |
| 81 | |
| 82 | plan = object_packages.dry_run_package( |
| 83 | "app-catalog", |
| 84 | root=PACKAGES_ROOT, |
| 85 | base_dir=tmp_path / "data", |
| 86 | object_roots=[object_root], |
| 87 | ) |
| 88 | |
| 89 | assert plan["safe_to_install"] is True |
| 90 | assert plan["warnings"] == [] |
| 91 | assert {schema["collection"] for schema in plan["schemas"]} >= { |
| 92 | "products", "locations", "stock_moves", |
| 93 | "backorders", "reorder_suggestions", |
| 94 | } |
| 95 | |
| 96 | |
| 97 | def test_install_app_catalog_package_loads_schema(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-catalog", |
| 104 | root=PACKAGES_ROOT, |
| 105 | base_dir=data_dir, |
| 106 | object_roots=[object_root], |
| 107 | ) |
| 108 | |
| 109 | products_schema = object_schemas.get_schema("products", base_dir=data_dir) |
| 110 | locations_schema = object_schemas.get_schema("locations", base_dir=data_dir) |
| 111 | stock_moves_schema = object_schemas.get_schema("stock_moves", base_dir=data_dir) |
| 112 | |
| 113 | assert products_schema["name"] == "products" |
| 114 | assert locations_schema["name"] == "locations" |
| 115 | assert stock_moves_schema["name"] == "stock_moves" |
| 116 | assert stock_moves_schema["storage"] == "append" |
| 117 | # product_view.py was removed in the Stage-6 retrofit (replaced by a |
| 118 | # seeded detail view); it must no longer be installed. |
| 119 | assert not (object_root / "site" / "product_view.py").exists() |
| 120 | # products.py/locations.py were deleted (0.5.0): both list pages are |
| 121 | # now seeded VIEW records too. |
| 122 | assert not (object_root / "site" / "products.py").exists() |
| 123 | assert not (object_root / "site" / "locations.py").exists() |
| 124 | assert (object_root / "site" / "stock.py").is_file() |
| 125 | |
| 126 | |
| 127 | def test_schema_json_file_is_valid_and_versioned(): |
| 128 | payload = _products_schema() |
| 129 | assert payload["name"] == "products" |
| 130 | # v1 -> v2: ASSET-only fields gained `visible_when` (Stage-6). v2 -> v3: |
| 131 | # entity_id scoping FK (65 multi-entity). v3 -> v4: merchandising -- |
| 132 | # category, parent_product_id, options, image_file_id. v4 -> v5: the |
| 133 | # commerce money layer -- is_gift_card, backorder_policy, reorder_point, |
| 134 | # reorder_quantity. All additive, and every one of them defaults to the |
| 135 | # behaviour the shop already had. |
| 136 | assert payload["version"] >= 5 |
| 137 | assert payload["views"]["list_mode"] == "table" |
| 138 | # entity_id is a relation into the entities collection (scoping FK). |
| 139 | by_name = {f["name"]: f for f in payload["fields"]} |
| 140 | assert by_name["entity_id"]["relation"]["collection"] == "entities" |
| 141 | |
| 142 | |
| 143 | def test_asset_fields_are_conditionally_visible_on_asset_products_only(): |
| 144 | payload = _products_schema() |
| 145 | by_name = {f["name"]: f for f in payload["fields"]} |
| 146 | for name in ("useful_life_months", "purchase_date", "salvage_value_cents", |
| 147 | "depreciation_method", "asset_status"): |
| 148 | assert by_name[name]["visible_when"] == {"field": "product_type", "equals": "asset"}, name |
| 149 | # Non-asset fields must NOT carry the condition (they always show). |
| 150 | assert "visible_when" not in by_name["name"] |
| 151 | assert "visible_when" not in by_name["price_cents"] |
| 152 | |
| 153 | |
| 154 | def test_no_money_field_uses_a_float_or_currency_type(): |
| 155 | """00-doctrine-and-contract.md's hard rule: money is *_cents integers, |
| 156 | never object_records.py's _FLOAT_TYPES = {"float", "number", "currency"}. |
| 157 | Products has no count/measure field like invoices' quantity, so there |
| 158 | is no allowed-float exception here at all. |
| 159 | """ |
| 160 | for field in _products_schema()["fields"]: |
| 161 | name = field["name"] |
| 162 | field_type = field.get("type") |
| 163 | if "_cents" in name: |
| 164 | assert field_type == "integer", f"products.{name} must be type integer, got {field_type!r}" |
| 165 | assert field_type not in _FLOAT_MONEY_TYPES, ( |
| 166 | f"products.{name} uses a float-shaped type {field_type!r}" |
| 167 | ) |
| 168 | |
| 169 | |
| 170 | def test_every_cents_field_is_present_and_integer(): |
| 171 | by_name = {f["name"]: f for f in _products_schema()["fields"]} |
| 172 | for name in ("price_cents", "cost_cents", "salvage_value_cents"): |
| 173 | assert by_name[name]["type"] == "integer" |
| 174 | assert by_name[name]["default"] == "0" |
| 175 | |
| 176 | |
| 177 | def test_product_type_enum_matches_the_source_model(): |
| 178 | """The reconciled predecessor system's Product model (private source |
| 179 | audit, not part of this repo): type choices |
| 180 | physical/digital/service/subscription/ASSET, carried verbatim. |
| 181 | """ |
| 182 | by_name = {f["name"]: f for f in _products_schema()["fields"]} |
| 183 | assert_includes_in_order(by_name["product_type"]["enum"], [ |
| 184 | "physical", "digital", "service", "subscription", "asset", |
| 185 | ]) |
| 186 | assert by_name["product_type"]["default"] == "physical" |
| 187 | |
| 188 | |
| 189 | def test_no_status_transitions_map_invented(): |
| 190 | """Products don't have a status FSM -- is_active is a plain boolean. |
| 191 | The task brief is explicit that no transitions map should be invented |
| 192 | here (contrast with app-invoices/app-tasks' guarded status enums). |
| 193 | """ |
| 194 | by_name = {f["name"]: f for f in _products_schema()["fields"]} |
| 195 | assert by_name["is_active"]["type"] == "boolean" |
| 196 | assert by_name["is_active"]["default"] == "true" |
| 197 | assert "transitions" not in by_name["is_active"] |
| 198 | for field in _products_schema()["fields"]: |
| 199 | assert "transitions" not in field, f"products.{field['name']} should not have a transitions map" |
| 200 | |
| 201 | |
| 202 | def test_asset_parity_fields_present(): |
| 203 | """ASSET parity fields carried from the predecessor system's Product |
| 204 | model (private source audit, not part of this repo: ASSET type carries |
| 205 | depreciation fields + helpers), not exercised in v1 (no depreciation |
| 206 | schedule is computed here) but kept so no future migration has to |
| 207 | backfill. |
| 208 | """ |
| 209 | by_name = {f["name"]: f for f in _products_schema()["fields"]} |
| 210 | |
| 211 | assert by_name["useful_life_months"]["type"] == "integer" |
| 212 | assert by_name["purchase_date"]["type"] == "date" |
| 213 | assert by_name["salvage_value_cents"]["type"] == "integer" |
| 214 | assert by_name["salvage_value_cents"]["default"] == "0" |
| 215 | assert by_name["depreciation_method"]["type"] == "enum" |
| 216 | assert_includes_in_order(by_name["depreciation_method"]["enum"], ["straight_line", "declining"]) |
| 217 | # nullable: no default, not required -- an asset whose method has not |
| 218 | # been set yet stays blank rather than forced to a guessed default. |
| 219 | assert "default" not in by_name["depreciation_method"] |
| 220 | assert not by_name["depreciation_method"].get("required") |
| 221 | assert by_name["asset_status"]["type"] == "text" |
| 222 | |
| 223 | |
| 224 | def test_finance_account_and_digital_file_pointers_carried_as_text(): |
| 225 | """The real predecessor links Product to finance Account rows and a |
| 226 | downloadable file. No finance or hard file-relation package exists |
| 227 | yet, so these stay plain text ids (see dbbasic-package.json) rather |
| 228 | than a schema `relation` this codebase cannot yet resolve. |
| 229 | """ |
| 230 | by_name = {f["name"]: f for f in _products_schema()["fields"]} |
| 231 | for name in ("income_account", "expense_account", "digital_file_id"): |
| 232 | assert by_name[name]["type"] == "text" |
| 233 | assert "relation" not in by_name[name] |
| 234 | |
| 235 | |
| 236 | def test_products_forms_and_views_match_the_brief(): |
| 237 | schema = _products_schema() |
| 238 | # v4 put `category` on the form and in the list, and made it the one |
| 239 | # filter: it is the field the shop page groups on, so somebody typing |
| 240 | # it in has to be able to see what they already used. |
| 241 | # v5 put the two stock-policy questions on the form beside is_active -- |
| 242 | # what to do when it runs out, and when to buy more -- because they are |
| 243 | # decisions about the same shelf. |
| 244 | assert_includes_in_order(schema["forms"]["default"]["fields"], [ |
| 245 | "name", "sku", "product_type", "is_gift_card", "category", |
| 246 | "description", "price_cents", "cost_cents", "currency", "unit", |
| 247 | "is_active", "backorder_policy", "reorder_point", "reorder_quantity", |
| 248 | ]) |
| 249 | assert_includes_in_order(schema["views"]["list_fields"], [ |
| 250 | "name", "sku", "product_type", "category", "price_cents", "is_active", |
| 251 | ]) |
| 252 | assert_includes_in_order(schema["views"]["filter_fields"], ["category"]) |
| 253 | |
| 254 | |
| 255 | def test_search_fields_cover_only_content_fields(): |
| 256 | """Products is content-searchable (name/sku/description) -- this is |
| 257 | the deliberate exception to the bookkeeping-field guard: no |
| 258 | price/cost/account/asset field belongs in search.fields. |
| 259 | """ |
| 260 | schema = _products_schema() |
| 261 | assert_includes_in_order(schema["search"]["fields"], ["name", "sku", "description"]) |
| 262 | bookkeeping_shaped = { |
| 263 | "price_cents", "cost_cents", "salvage_value_cents", "currency", |
| 264 | "income_account", "expense_account", "owner_id", "asset_status", |
| 265 | "depreciation_method", "useful_life_months", "purchase_date", |
| 266 | "digital_file_id", |
| 267 | } |
| 268 | assert not bookkeeping_shaped & set(schema["search"]["fields"]) |
| 269 | |
| 270 | |
| 271 | def test_products_schema_field_order_matches_the_brief(): |
| 272 | field_names = [f["name"] for f in _products_schema()["fields"]] |
| 273 | # The four merchandising fields (v4) sit together after description -- |
| 274 | # what a thing is filed under, what it is a variant of, which variant |
| 275 | # it is, and what it looks like -- rather than being appended past the |
| 276 | # asset block, so the schema still reads top to bottom as a product. |
| 277 | # v5's three stock-policy fields sit together after is_active -- what |
| 278 | # happens when the shelf is empty, and when to buy more -- because they |
| 279 | # are one conversation about one shelf. is_gift_card sits beside |
| 280 | # product_type instead, since it answers what the thing IS. |
| 281 | assert_includes_in_order(field_names, [ |
| 282 | "id", "name", "sku", "product_type", "is_gift_card", "description", |
| 283 | "category", "parent_product_id", "options", "image_file_id", |
| 284 | "price_cents", "cost_cents", "currency", "unit", "is_active", |
| 285 | "backorder_policy", "reorder_point", "reorder_quantity", |
| 286 | "income_account", "expense_account", "digital_file_id", |
| 287 | "useful_life_months", "purchase_date", "salvage_value_cents", |
| 288 | "depreciation_method", "asset_status", "owner_id", "entity_id", "created_at", |
| 289 | ]) |
| 290 | |
| 291 | |
| 292 | def test_locations_schema_field_order_and_types(): |
| 293 | schema = _locations_schema() |
| 294 | field_names = [f["name"] for f in schema["fields"]] |
| 295 | assert_includes_in_order(field_names, [ |
| 296 | "id", "name", "location_type", "parent_id", "code", "owner_id", "entity_id", "created_at", |
| 297 | ]) |
| 298 | by_name = {f["name"]: f for f in schema["fields"]} |
| 299 | assert by_name["name"]["type"] == "text" |
| 300 | assert by_name["name"]["required"] is True |
| 301 | assert by_name["code"]["type"] == "text" |
| 302 | assert by_name["created_at"]["read_only"] is True |
| 303 | |
| 304 | |
| 305 | def test_locations_type_enum_matches_the_source_model(): |
| 306 | """The reconciled predecessor system's Location model (private source |
| 307 | audit, not part of this repo): a real warehouse->bin hierarchy plus |
| 308 | virtual customer/supplier locations, carried verbatim -- with a plain |
| 309 | 'virtual' catch-all alongside the two named virtual counterparties. |
| 310 | """ |
| 311 | by_name = {f["name"]: f for f in _locations_schema()["fields"]} |
| 312 | assert_includes_in_order(by_name["location_type"]["enum"], [ |
| 313 | "warehouse", "zone", "aisle", "shelf", "bin", "customer", "supplier", "virtual", |
| 314 | ]) |
| 315 | assert by_name["location_type"]["default"] == "warehouse" |
| 316 | |
| 317 | |
| 318 | def test_locations_parent_id_is_a_self_relation(): |
| 319 | """Self-parent hierarchy: warehouse -> zone -> aisle -> shelf -> bin, |
| 320 | same shape app-thread's thread_comments.reply_to_id uses to relate a |
| 321 | collection to itself. Optional -- a top-level location has no parent. |
| 322 | """ |
| 323 | by_name = {f["name"]: f for f in _locations_schema()["fields"]} |
| 324 | assert by_name["parent_id"]["relation"]["collection"] == "locations" |
| 325 | assert "required" not in by_name["parent_id"] or not by_name["parent_id"]["required"] |
| 326 | |
| 327 | |
| 328 | def test_locations_search_covers_only_content_fields(): |
| 329 | schema = _locations_schema() |
| 330 | assert_includes_in_order(schema["search"]["fields"], ["name", "code"]) |
| 331 | |
| 332 | |
| 333 | def test_stock_moves_schema_is_append_storage(): |
| 334 | """stock_moves is the textbook append-mode collection |
| 335 | (docs/append-only-storage-design.md): immutable, write-heavy, never |
| 336 | updated. Declared via the schema's top-level "storage" key, the only |
| 337 | way a manual schema can opt in (object_schemas.py). |
| 338 | """ |
| 339 | schema = _stock_moves_schema() |
| 340 | assert schema["storage"] == "append" |
| 341 | |
| 342 | |
| 343 | def test_stock_moves_schema_field_order_and_relations(): |
| 344 | schema = _stock_moves_schema() |
| 345 | field_names = [f["name"] for f in schema["fields"]] |
| 346 | assert_includes_in_order(field_names, [ |
| 347 | "id", "product_id", "from_location_id", "to_location_id", "quantity", |
| 348 | "unit_cost_cents", "reason", "reference", "occurred_at", "owner_id", "entity_id", "created_at", |
| 349 | ]) |
| 350 | by_name = {f["name"]: f for f in schema["fields"]} |
| 351 | |
| 352 | assert by_name["product_id"]["relation"]["collection"] == "products" |
| 353 | assert by_name["product_id"]["required"] is True |
| 354 | |
| 355 | for name in ("from_location_id", "to_location_id"): |
| 356 | assert by_name[name]["relation"]["collection"] == "locations" |
| 357 | assert "required" not in by_name[name] or not by_name[name]["required"] |
| 358 | |
| 359 | |
| 360 | def test_stock_moves_reason_enum_matches_the_source_model(): |
| 361 | """The first six match the predecessor system's stock move reasons |
| 362 | (private source audit, not part of this repo), default transfer. v3 |
| 363 | extends the enum ADDITIVELY with the loss taxonomy |
| 364 | (plan/inventory-adjustments-spec.md) -- old rows stay valid. |
| 365 | """ |
| 366 | by_name = {f["name"]: f for f in _stock_moves_schema()["fields"]} |
| 367 | assert_includes_in_order(by_name["reason"]["enum"], [ |
| 368 | "purchase", "sale", "transfer", "adjustment", "return", "count", |
| 369 | "waste", "breakage", "theft", "expiry", "damage", "disaster", |
| 370 | ]) |
| 371 | assert by_name["reason"]["default"] == "transfer" |
| 372 | |
| 373 | |
| 374 | def test_stock_moves_quantity_and_cost_field_types(): |
| 375 | by_name = {f["name"]: f for f in _stock_moves_schema()["fields"]} |
| 376 | assert by_name["quantity"]["type"] == "number" |
| 377 | assert by_name["quantity"]["required"] is True |
| 378 | assert by_name["unit_cost_cents"]["type"] == "integer" |
| 379 | assert "required" not in by_name["unit_cost_cents"] or not by_name["unit_cost_cents"]["required"] |
| 380 | |
| 381 | |
| 382 | def test_stock_moves_has_no_search_config(): |
| 383 | """Moves are records, not content -- unlike products/locations, this |
| 384 | collection is deliberately not globally searchable. |
| 385 | """ |
| 386 | assert "search" not in _stock_moves_schema() |
| 387 | |
| 388 | |
| 389 | def test_no_money_field_in_locations_or_stock_moves_uses_a_float_or_currency_type(): |
| 390 | for schema in (_locations_schema(), _stock_moves_schema()): |
| 391 | for field in schema["fields"]: |
| 392 | name = field["name"] |
| 393 | if name in _ALLOWED_FLOAT_FIELDS: |
| 394 | continue |
| 395 | field_type = field.get("type") |
| 396 | if "_cents" in name: |
| 397 | assert field_type == "integer", f"{schema['name']}.{name} must be type integer, got {field_type!r}" |
| 398 | assert field_type not in _FLOAT_MONEY_TYPES, ( |
| 399 | f"{schema['name']}.{name} uses a float-shaped type {field_type!r}" |
| 400 | ) |
| 401 | |
| 402 | |
| 403 | def _app_catalog_policy(): |
| 404 | payload = json.loads((APP_CATALOG_DIR / "permissions" / "rules.json").read_text()) |
| 405 | return object_permissions.policy_from_dict({"access_mode": "role_based", "rules": payload["rules"]}) |
| 406 | |
| 407 | |
| 408 | def test_owner_can_crud_own_product(): |
| 409 | policy = _app_catalog_policy() |
| 410 | subject = object_permissions.PermissionSubject(user_id="7") |
| 411 | record = {"owner_id": "7", "name": "Widget", "product_type": "physical"} |
| 412 | |
| 413 | for action in ( |
| 414 | object_permissions.CREATE, |
| 415 | object_permissions.READ, |
| 416 | object_permissions.UPDATE, |
| 417 | object_permissions.DELETE, |
| 418 | ): |
| 419 | decision = object_permissions.check_permission( |
| 420 | subject, action, policy=policy, collection="products", record=record |
| 421 | ) |
| 422 | assert decision.allowed is True |
| 423 | |
| 424 | |
| 425 | def test_others_cannot_touch_someone_elses_product(): |
| 426 | policy = _app_catalog_policy() |
| 427 | subject = object_permissions.PermissionSubject(user_id="8") |
| 428 | record = {"owner_id": "7", "name": "Widget", "product_type": "physical"} |
| 429 | |
| 430 | for action in ( |
| 431 | object_permissions.READ, |
| 432 | object_permissions.UPDATE, |
| 433 | object_permissions.DELETE, |
| 434 | ): |
| 435 | decision = object_permissions.check_permission( |
| 436 | subject, action, policy=policy, collection="products", record=record |
| 437 | ) |
| 438 | assert decision.allowed is False |
| 439 | |
| 440 | |
| 441 | def test_anonymous_cannot_read_any_product(): |
| 442 | """No public read rule is granted on the products collection at all -- |
| 443 | v1 keeps the catalog owner-scoped like app-invoices; a public |
| 444 | storefront view is deferred (see dbbasic-package.json). |
| 445 | """ |
| 446 | policy = _app_catalog_policy() |
| 447 | record = {"owner_id": "7", "name": "Widget", "product_type": "physical"} |
| 448 | |
| 449 | decision = object_permissions.check_permission( |
| 450 | None, object_permissions.READ, policy=policy, collection="products", record=record |
| 451 | ) |
| 452 | assert decision.allowed is False |
| 453 | |
| 454 | |
| 455 | def test_no_public_execute_grant_survives_for_the_deleted_products_page(): |
| 456 | """/products and the product permalink are seeded views rendered by |
| 457 | site_view_render; no package ships site_products, so a public grant for |
| 458 | it would make any object later created under that id public without |
| 459 | anybody deciding so (bug-reports/2026-11-28-06-14-43). Never public |
| 460 | read on the collection either way.""" |
| 461 | policy = _app_catalog_policy() |
| 462 | |
| 463 | decision = object_permissions.check_permission( |
| 464 | None, object_permissions.EXECUTE, policy=policy, object_id="site_products" |
| 465 | ) |
| 466 | assert decision.allowed is False |
| 467 | |
| 468 | |
| 469 | def test_owner_can_crud_own_location(): |
| 470 | policy = _app_catalog_policy() |
| 471 | subject = object_permissions.PermissionSubject(user_id="7") |
| 472 | record = {"owner_id": "7", "name": "Main Warehouse", "location_type": "warehouse"} |
| 473 | |
| 474 | for action in ( |
| 475 | object_permissions.CREATE, |
| 476 | object_permissions.READ, |
| 477 | object_permissions.UPDATE, |
| 478 | object_permissions.DELETE, |
| 479 | ): |
| 480 | decision = object_permissions.check_permission( |
| 481 | subject, action, policy=policy, collection="locations", record=record |
| 482 | ) |
| 483 | assert decision.allowed is True |
| 484 | |
| 485 | |
| 486 | def test_others_cannot_touch_someone_elses_location(): |
| 487 | policy = _app_catalog_policy() |
| 488 | subject = object_permissions.PermissionSubject(user_id="8") |
| 489 | record = {"owner_id": "7", "name": "Main Warehouse", "location_type": "warehouse"} |
| 490 | |
| 491 | for action in ( |
| 492 | object_permissions.READ, |
| 493 | object_permissions.UPDATE, |
| 494 | object_permissions.DELETE, |
| 495 | ): |
| 496 | decision = object_permissions.check_permission( |
| 497 | subject, action, policy=policy, collection="locations", record=record |
| 498 | ) |
| 499 | assert decision.allowed is False |
| 500 | |
| 501 | |
| 502 | def test_owner_can_create_and_read_but_never_edit_or_delete_own_stock_move(): |
| 503 | """Stock levels ARE the moves, so a person's correction is a new |
| 504 | compensating move. The rules say so (no update or delete grant, and the |
| 505 | owner fallback denied); hook_stock_moves no longer carries that refusal.""" |
| 506 | policy = _app_catalog_policy() |
| 507 | subject = object_permissions.PermissionSubject(user_id="7") |
| 508 | record = {"owner_id": "7", "product_id": "p1", "quantity": "10", "reason": "purchase"} |
| 509 | |
| 510 | for action in (object_permissions.CREATE, object_permissions.READ): |
| 511 | decision = object_permissions.check_permission( |
| 512 | subject, action, policy=policy, collection="stock_moves", record=record |
| 513 | ) |
| 514 | assert decision.allowed is True |
| 515 | for action in (object_permissions.UPDATE, object_permissions.DELETE): |
| 516 | decision = object_permissions.check_permission( |
| 517 | subject, action, policy=policy, collection="stock_moves", record=record |
| 518 | ) |
| 519 | assert decision.allowed is False |
| 520 | |
| 521 | |
| 522 | def test_others_cannot_touch_someone_elses_stock_move(): |
| 523 | policy = _app_catalog_policy() |
| 524 | subject = object_permissions.PermissionSubject(user_id="8") |
| 525 | record = {"owner_id": "7", "product_id": "p1", "quantity": "10", "reason": "purchase"} |
| 526 | |
| 527 | for action in ( |
| 528 | object_permissions.READ, |
| 529 | object_permissions.UPDATE, |
| 530 | object_permissions.DELETE, |
| 531 | ): |
| 532 | decision = object_permissions.check_permission( |
| 533 | subject, action, policy=policy, collection="stock_moves", record=record |
| 534 | ) |
| 535 | assert decision.allowed is False |
| 536 | |
| 537 | |
| 538 | def test_anonymous_cannot_read_locations_or_stock_moves(): |
| 539 | policy = _app_catalog_policy() |
| 540 | |
| 541 | for collection, record in ( |
| 542 | ("locations", {"owner_id": "7", "name": "Main Warehouse"}), |
| 543 | ("stock_moves", {"owner_id": "7", "product_id": "p1", "quantity": "10"}), |
| 544 | ): |
| 545 | decision = object_permissions.check_permission( |
| 546 | None, object_permissions.READ, policy=policy, collection=collection, record=record |
| 547 | ) |
| 548 | assert decision.allowed is False |
| 549 | |
| 550 | |
| 551 | def test_the_stock_page_is_publicly_executable_and_the_deleted_locations_page_is_not(): |
| 552 | """site_stock is a real page object (it shows a sign-in prompt to |
| 553 | visitors): public execute on the page, never public read on the |
| 554 | collections. site_locations is no longer shipped, so it keeps no grant |
| 555 | (bug-reports/2026-11-28-06-14-43).""" |
| 556 | policy = _app_catalog_policy() |
| 557 | |
| 558 | def executable(object_id): |
| 559 | return object_permissions.check_permission( |
| 560 | None, object_permissions.EXECUTE, policy=policy, object_id=object_id |
| 561 | ).allowed |
| 562 | |
| 563 | assert executable("site_stock") is True |
| 564 | assert executable("site_locations") is False |
| 565 | |
| 566 | |
| 567 | def test_seed_tsvs_are_header_only_and_match_schema_field_order(): |
| 568 | """Header-only, matching the established precedent (app-tasks, |
| 569 | app-notes, app-invoices, app-contacts all ship header-only seeds). |
| 570 | The header order matches each schema's own field order for |
| 571 | readability. |
| 572 | """ |
| 573 | for schema_fn, filename in ( |
| 574 | (_products_schema, "products.tsv"), |
| 575 | (_locations_schema, "locations.tsv"), |
| 576 | (_stock_moves_schema, "stock_moves.tsv"), |
| 577 | ): |
| 578 | schema = schema_fn() |
| 579 | path = APP_CATALOG_DIR / "seed" / filename |
| 580 | lines = path.read_text().splitlines() |
| 581 | assert len(lines) == 1, f"{filename} should be header-only" |
| 582 | header = lines[0].split("\t") |
| 583 | assert header == [f["name"] for f in schema["fields"]] |
| 584 | |
| 585 | |
| 586 | def test_no_disallowed_org_names_leak_into_the_package(): |
| 587 | """Public repo hygiene: no internal org/codename references anywhere |
| 588 | in this package's source. |
| 589 | """ |
| 590 | # Built from fragments so this guard file itself stays clean of the very |
| 591 | # internal names it forbids (otherwise the test would flag its own source). |
| 592 | banned = re.compile( |
| 593 | "|".join([r"\b" + "q" + "9" + r"\b", "ask" + "robots", r"\b" + "wo" + "ld" + r"\b"]), |
| 594 | re.IGNORECASE, |
| 595 | ) |
| 596 | for path in APP_CATALOG_DIR.rglob("*"): |
| 597 | if "__pycache__" in path.parts: |
| 598 | continue # compiled bytes can match a name by chance |
| 599 | if path.is_dir(): |
| 600 | continue |
| 601 | text = path.read_text(encoding="utf-8", errors="ignore") |
| 602 | assert not banned.search(text), f"disallowed reference found in {path}" |
| 603 | |
| 604 | |
| 605 | def test_object_stock_module_has_no_disallowed_org_names(): |
| 606 | """object_stock.py lives at the repo root, not inside packages/ |
| 607 | app-catalog/ (see object_stock.py's own module docstring for why -- |
| 608 | same placement and same reason as object_finance.py for app-finance), |
| 609 | so the repo-hygiene sweep above never walks it -- covered here |
| 610 | explicitly instead, mirroring |
| 611 | test_app_finance_package.py::test_object_finance_module_has_no_disallowed_org_names. |
| 612 | """ |
| 613 | banned = re.compile( |
| 614 | "|".join([r"\b" + "q" + "9" + r"\b", "ask" + "robots", r"\b" + "wo" + "ld" + r"\b"]), |
| 615 | re.IGNORECASE, |
| 616 | ) |
| 617 | path = Path(__file__).resolve().parents[1] / "object_stock.py" |
| 618 | text = path.read_text(encoding="utf-8", errors="ignore") |
| 619 | assert not banned.search(text), f"disallowed reference found in {path}" |