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