Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/tests/test_app_catalog_detail_retrofit.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_app_catalog_detail_retrofit.py · 5.4 KB · 130 lines
1"""Structural tests for packages/app-catalog after the Stage-6 retrofit: the
2bespoke ~165-line site_product_view.py permalink page was replaced by a
3seeded 59 detail view (site_view_render) whose `detail` block is
4owner-aware (editable/deletable), so there is no per-product page object
5anymore. Products have no public read path -- the row-filtered products
6collection has no public read rule -- so this page only ever served (and
7still only ever serves) the owner; non-owners and anonymous visitors are
8permission-gated at the collection API the same as before.
9"""
10
11import json
12from pathlib import Path
13
14import object_packages
15import object_permissions
16
17PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages"
18APP_CATALOG_DIR = PACKAGES_ROOT / "app-catalog"
19
20
21def _seed_rows(name):
22 import csv
23
24 with open(APP_CATALOG_DIR / "seed" / f"{name}.tsv", newline="") as fh:
25 return list(csv.DictReader(fh, delimiter="\t"))
26
27
28def test_manifest_drops_the_bespoke_view_object_and_seeds_the_detail_view():
29 package = object_packages.get_package("app-catalog", root=PACKAGES_ROOT)
30 # The permalink page object is gone; 0.4.0 joined it with the
31 # inventory-adjustments trio (hook/composer/count action). 0.5.0 then
32 # deleted the bespoke products/locations list page objects too -- see
33 # tests/test_app_catalog_package.py. 0.7.0 added the reorder pass and
34 # the two attention providers, none of which is a page.
35 assert {obj["id"] for obj in package["objects"]} == {
36 "site_stock",
37 "hook_stock_moves", "system_stock_books", "action_apply_count",
38 "system_reorder_check", "system_reorder_attention",
39 "system_backorder_attention",
40 }
41 # The detail view + its route are seeded (into app-views'/site-routing's
42 # own shared collections), alongside the existing products/locations/
43 # stock_moves seeds and 0.7.0's backorders/reorder_suggestions.
44 assert {entry["collection"] for entry in package["seed"]} == {
45 "products", "locations", "stock_moves", "views", "site_routes",
46 "backorders", "reorder_suggestions",
47 }
48
49
50def test_product_view_object_file_is_deleted():
51 assert not (APP_CATALOG_DIR / "objects" / "site" / "product_view.py").exists()
52
53
54def test_seeded_detail_view_uses_an_owner_aware_editable_detail_block():
55 rows = _seed_rows("views")
56 # 0.5.0 added the /products and /locations index views alongside this
57 # detail view, so assert on the detail row rather than a total that
58 # keeps growing.
59 rows = [r for r in rows if r["id"] == "view_products_detail"]
60 assert len(rows) == 1
61 view = rows[0]
62 assert view["id"] == "view_products_detail"
63 assert view["route"] == "/products/{product_id}"
64 blocks = json.loads(view["blocks"])
65 assert len(blocks) == 1
66 block = blocks[0]
67 assert block["kind"] == "detail"
68 assert block["collection"] == "products"
69 assert block["record_id"] == "$record_id"
70 # The retrofit's whole point: owner-editable + deletable in place --
71 # preserving what the bespoke page did. Non-owners never reach the
72 # record at all (no public read rule on products), so the detail
73 # block's read-only view is moot for them; the owner affordances are
74 # what this page actually existed for.
75 assert block["editable"] is True
76 assert block["deletable"] is True
77 assert block["delete_redirect"] == "/products"
78
79
80def test_permalink_route_is_seeded_to_the_view_render_generator():
81 rows = _seed_rows("site_routes")
82 # 0.5.0 appended the /products and /locations index routes, so assert
83 # on the detail row rather than a total that keeps growing.
84 rows = [r for r in rows if r["id"] == "route_products_detail"]
85 assert len(rows) == 1
86 route = rows[0]
87 assert route["id"] == "route_products_detail"
88 assert route["pattern"] == "/products/{product_id}"
89 assert route["object_id"] == "site_view_render"
90 assert route["priority"] == "10"
91
92
93def test_permissions_no_longer_reference_the_removed_object():
94 payload = json.loads((APP_CATALOG_DIR / "permissions" / "rules.json").read_text())
95 object_ids = {rule.get("object_id") for rule in payload["rules"]}
96 assert "site_product_view" not in object_ids
97 # The other page objects (list pages) are untouched.
98 assert "site_products" in object_ids
99 assert "site_locations" in object_ids
100 assert "site_stock" in object_ids
101
102
103def test_no_public_read_rule_was_added_to_products():
104 """The retrofit must not loosen the owner-only posture: products still
105 has no public read rule -- the seeded view's detail block is
106 read-only-for-everyone in principle, but nobody except the owner can
107 actually fetch the record.
108 """
109 payload = json.loads((APP_CATALOG_DIR / "permissions" / "rules.json").read_text())
110 policy = object_permissions.policy_from_dict(
111 {"access_mode": "role_based", "rules": payload["rules"]}
112 )
113 record = {"owner_id": "7", "name": "Widget", "product_type": "physical"}
114 decision = object_permissions.check_permission(
115 None, object_permissions.READ, policy=policy, collection="products", record=record
116 )
117 assert decision.allowed is False
118
119
120def test_dry_run_is_safe(tmp_path):
121 object_root = tmp_path / "objects"
122 object_root.mkdir()
123 plan = object_packages.dry_run_package(
124 "app-catalog",
125 root=PACKAGES_ROOT,
126 base_dir=tmp_path / "data",
127 object_roots=[object_root],
128 )
129 assert plan["safe_to_install"] is True
130 assert plan["warnings"] == []