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_commerce_action_owner_checks.py · 9.3 KB · 194 lines
| 1 | """Do five commerce actions check whose records they change? |
| 2 | |
| 3 | A reviewer suspected, without running anything, that receive_goods, |
| 4 | create_shipment, disposition_return, dropship_order and buy_label let any |
| 5 | signed-in user act on anybody's records. Each of them reads its record |
| 6 | straight from storage and writes back through trusted server-side writes, |
| 7 | so the collection API's row filters never run: the action is the only gate. |
| 8 | |
| 9 | Each test here drives the real ASGI app with enforcement ON (the harness |
| 10 | from tests/test_findings_access_control.py): alice owns the records, bob is |
| 11 | a registered stranger, and the package's own execute rule lets both of them |
| 12 | run the action. The controls first prove that the collection API refuses |
| 13 | bob the same record and that alice can still do the thing herself, so a |
| 14 | pass cannot come from locking everybody out. |
| 15 | |
| 16 | All five were seen to FAIL before the fix (bob's request succeeded and |
| 17 | wrote: a receipt shelving alice's purchase order, a shipment of alice's |
| 18 | order, alice's return dispositioned and closed, a purchase order committing |
| 19 | alice to buy from bob's choice of vendor, and a tracking number stamped on |
| 20 | alice's parcel). They now answer as a missing record does. |
| 21 | """ |
| 22 | |
| 23 | from __future__ import annotations |
| 24 | |
| 25 | import pytest |
| 26 | |
| 27 | from test_findings_access_control import ( # noqa: F401 (make_site is a fixture) |
| 28 | _json, |
| 29 | _text, |
| 30 | make_site, |
| 31 | precondition, |
| 32 | ) |
| 33 | |
| 34 | |
| 35 | def _api_refuses(site, bob, collection, record_id): |
| 36 | direct = site.call(f"/collections/{collection}/records/{record_id}", headers=bob) |
| 37 | precondition(direct.status == 404, # hidden like a missing row |
| 38 | f"control: the API lets bob read {collection}/{record_id}: {direct.status}") |
| 39 | |
| 40 | |
| 41 | def _purchase_order(site, order_id="po-1", owner="alice"): |
| 42 | site.create("orders", { |
| 43 | "id": order_id, "doc_type": "purchase", "number": f"PO-{order_id}", |
| 44 | "customer_name": "Alice Supplier", "status": "confirmed", "owner_id": owner}) |
| 45 | site.create("order_lines", { |
| 46 | "id": f"{order_id}-l1", "order_id": order_id, "description": "Pallet", |
| 47 | "quantity": "2", "unit_price_cents": "400", "line_total_cents": "800", |
| 48 | "owner_id": owner}) |
| 49 | |
| 50 | |
| 51 | def _sale_order(site, order_id="so-1", owner="alice", status="confirmed"): |
| 52 | site.create("orders", { |
| 53 | "id": order_id, "doc_type": "sale", "number": f"SO-{order_id}", |
| 54 | "customer_name": "Carol Customer", "status": status, "owner_id": owner}) |
| 55 | site.create("order_lines", { |
| 56 | "id": f"{order_id}-l1", "order_id": order_id, "description": "Widget", |
| 57 | "quantity": "2", "unit_price_cents": "1000", "line_total_cents": "2000", |
| 58 | "owner_id": owner}) |
| 59 | |
| 60 | |
| 61 | # --- receive_goods ------------------------------------------------------------ |
| 62 | |
| 63 | def test_a_stranger_cannot_receive_goods_against_someone_elses_purchase_order(make_site): |
| 64 | site = make_site("app-receiving") |
| 65 | alice, bob = site.user("alice"), site.user("bob") |
| 66 | _purchase_order(site, "po-1") |
| 67 | _purchase_order(site, "po-mine") |
| 68 | _api_refuses(site, bob, "orders", "po-1") |
| 69 | mine = site.execute("action_receive_goods", {"order_id": "po-mine"}, alice) |
| 70 | precondition(mine.status == 200 and _json(mine).get("receipt_id"), |
| 71 | f"control: alice cannot receive her own PO: {mine.status} {_text(mine)[:200]}") |
| 72 | before = len(site.rows("receipts")) |
| 73 | |
| 74 | response = site.execute("action_receive_goods", {"order_id": "po-1"}, bob) |
| 75 | assert response.status in (403, 404) and len(site.rows("receipts")) == before, ( |
| 76 | f"bob got HTTP {response.status} and {len(site.rows('receipts')) - before} " |
| 77 | f"receipt(s) were booked against alice's purchase order") |
| 78 | |
| 79 | |
| 80 | # --- create_shipment ---------------------------------------------------------- |
| 81 | |
| 82 | def test_a_stranger_cannot_raise_a_shipment_of_someone_elses_order(make_site): |
| 83 | site = make_site("app-shipping") |
| 84 | alice, bob = site.user("alice"), site.user("bob") |
| 85 | _sale_order(site, "so-1") |
| 86 | _sale_order(site, "so-mine") |
| 87 | _api_refuses(site, bob, "orders", "so-1") |
| 88 | mine = site.execute("action_create_shipment", {"order_id": "so-mine"}, alice) |
| 89 | precondition(mine.status == 200 and _json(mine).get("shipment_id"), |
| 90 | f"control: alice cannot ship her own order: {mine.status} {_text(mine)[:200]}") |
| 91 | before = len(site.rows("shipments")) |
| 92 | |
| 93 | response = site.execute("action_create_shipment", {"order_id": "so-1"}, bob) |
| 94 | assert response.status in (403, 404) and len(site.rows("shipments")) == before, ( |
| 95 | f"bob got HTTP {response.status} and raised " |
| 96 | f"{len(site.rows('shipments')) - before} shipment(s) of alice's order") |
| 97 | |
| 98 | |
| 99 | # --- dropship_order ----------------------------------------------------------- |
| 100 | |
| 101 | def test_a_stranger_cannot_drop_ship_someone_elses_order(make_site): |
| 102 | site = make_site("app-orders") |
| 103 | alice, bob = site.user("alice"), site.user("bob") |
| 104 | _sale_order(site, "so-1") |
| 105 | _sale_order(site, "so-mine") |
| 106 | _api_refuses(site, bob, "orders", "so-1") |
| 107 | mine = site.execute("action_dropship_order", |
| 108 | {"order_id": "so-mine", "vendor_id": "v-1", |
| 109 | "vendor_name": "Kiln"}, alice) |
| 110 | precondition(mine.status == 200 and _json(mine).get("purchase_order_id"), |
| 111 | f"control: alice cannot drop-ship her own order: {mine.status} {_text(mine)[:200]}") |
| 112 | before = len(site.rows("orders")) |
| 113 | |
| 114 | response = site.execute("action_dropship_order", |
| 115 | {"order_id": "so-1", "vendor_id": "v-bob", |
| 116 | "vendor_name": "Bob's Cousin"}, bob) |
| 117 | after = site.row("orders", "so-1") |
| 118 | assert response.status in (403, 404) and len(site.rows("orders")) == before \ |
| 119 | and not after.get("linked_order_id"), ( |
| 120 | f"bob got HTTP {response.status}; alice's order is now linked to " |
| 121 | f"{after.get('linked_order_id')!r} from vendor {after.get('vendor_id')!r}") |
| 122 | |
| 123 | |
| 124 | # --- buy_label ---------------------------------------------------------------- |
| 125 | |
| 126 | def _shipment(site, shipment_id, owner="alice", direction="outbound", status="packed"): |
| 127 | site.create("shipments", { |
| 128 | "id": shipment_id, "order_id": "so-1", "direction": direction, |
| 129 | "status": status, "carrier": "Royal Mail", "owner_id": owner}) |
| 130 | |
| 131 | |
| 132 | def test_a_stranger_cannot_put_a_label_on_someone_elses_shipment(make_site): |
| 133 | site = make_site("app-shipping", "app-settings") |
| 134 | alice, bob = site.user("alice"), site.user("bob") |
| 135 | site.create("app_settings", {"id": "s-carrier", "key": "carrier.provider", |
| 136 | "value": "manual"}) |
| 137 | _sale_order(site, "so-1") |
| 138 | _shipment(site, "sh-1") |
| 139 | _shipment(site, "sh-mine") |
| 140 | _api_refuses(site, bob, "shipments", "sh-1") |
| 141 | mine = site.execute("action_buy_label", |
| 142 | {"shipment_id": "sh-mine", "tracking_number": "AL123"}, alice) |
| 143 | precondition(mine.status == 200 and site.row("shipments", "sh-mine")["tracking_number"] == "AL123", |
| 144 | f"control: alice cannot label her own parcel: {mine.status} {_text(mine)[:200]}") |
| 145 | |
| 146 | response = site.execute("action_buy_label", |
| 147 | {"shipment_id": "sh-1", "tracking_number": "BOB-FAKE"}, bob) |
| 148 | after = site.row("shipments", "sh-1") |
| 149 | assert response.status in (403, 404) and after.get("tracking_number", "") == "", ( |
| 150 | f"bob got HTTP {response.status}; alice's parcel now carries tracking " |
| 151 | f"number {after.get('tracking_number')!r}") |
| 152 | |
| 153 | |
| 154 | # --- disposition_return ------------------------------------------------------- |
| 155 | |
| 156 | def _received_return(site, suffix, owner="alice"): |
| 157 | """An authorized RMA whose parcel has arrived. Its line names no product, |
| 158 | so nothing needs a stock location: the action can complete.""" |
| 159 | shipment_id = f"in-{suffix}" |
| 160 | _shipment(site, shipment_id, owner=owner, direction="inbound", status="received") |
| 161 | site.create("shipment_lines", { |
| 162 | "id": f"{shipment_id}-l1", "shipment_id": shipment_id, |
| 163 | "order_line_id": "so-1-l1", "description": "Widget", "quantity": "1", |
| 164 | "owner_id": owner}) |
| 165 | site.create("return_authorizations", { |
| 166 | "id": f"rma-{suffix}", "order_id": "so-1", "shipment_id": shipment_id, |
| 167 | "reason": "no_longer_wanted", "status": "authorized", "owner_id": owner}) |
| 168 | return shipment_id |
| 169 | |
| 170 | |
| 171 | def test_a_stranger_cannot_disposition_someone_elses_return(make_site): |
| 172 | site = make_site("app-returns") |
| 173 | alice, bob = site.user("alice"), site.user("bob") |
| 174 | _sale_order(site, "so-1", status="delivered") |
| 175 | theirs = _received_return(site, "a") |
| 176 | own = _received_return(site, "mine") |
| 177 | _api_refuses(site, bob, "shipments", theirs) |
| 178 | mine = site.execute("action_disposition_return", { |
| 179 | "shipment_id": own, "refund": "none", |
| 180 | "lines": [{"shipment_line_id": f"{own}-l1", "disposition": "restock", |
| 181 | "quantity": "1"}]}, alice) |
| 182 | precondition(mine.status == 200 and site.row("shipments", own)["status"] == "dispositioned", |
| 183 | f"control: alice cannot disposition her own return: {mine.status} {_text(mine)[:300]}") |
| 184 | |
| 185 | response = site.execute("action_disposition_return", { |
| 186 | "shipment_id": theirs, "refund": "none", |
| 187 | "lines": [{"shipment_line_id": f"{theirs}-l1", "disposition": "dispose", |
| 188 | "quantity": "1"}]}, bob) |
| 189 | shipment = site.row("shipments", theirs) |
| 190 | rma = site.row("return_authorizations", "rma-a") |
| 191 | assert response.status in (403, 404) and shipment["status"] == "received" \ |
| 192 | and rma["status"] == "authorized", ( |
| 193 | f"bob got HTTP {response.status}; alice's return is now " |
| 194 | f"shipment={shipment['status']!r} rma={rma['status']!r}") |