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_order_invoice_link.py · 6.3 KB · 117 lines
| 1 | """An order's invoice_id is a pointer at money, and nothing checked it. |
| 2 | |
| 3 | `orders.invoice_id` is a plain text field: no relation, no existence check, and |
| 4 | a registered user may create and update their own orders. The reactions that |
| 5 | run when an invoice is paid find "the order for this invoice" by scanning orders |
| 6 | for that invoice_id and taking the first. So mallory pointed an order of her own |
| 7 | at the shop's invoice; when the shop's customer paid it, system_gift_card_issue |
| 8 | read HER order's lines, and opened a gift card for her own 1,000,000 "gift card" |
| 9 | line, funded by a 5,000 payment she had nothing to do with. The same join is how |
| 10 | action_resolve_dispute and action_disposition_return found a payment to refund |
| 11 | (tests/test_disputes.py, tests/test_returns.py), how system_shop_fulfillment |
| 12 | picked the order to ship, and how the order tracking page found the invoice |
| 13 | whose pay link (its portal capability) it shows. |
| 14 | |
| 15 | Each reader now takes only an order and invoice with one owner: checkout writes |
| 16 | both under the shop's owner, so anything else is no part of the sale. Making |
| 17 | invoice_id a validated relation (so relation.requires could refuse it at the |
| 18 | API) was weighed and not done: object_records re-validates every field on every |
| 19 | write, so an order whose invoice was later deleted could no longer change |
| 20 | status, and orders are routinely written before their invoice exists. |
| 21 | """ |
| 22 | |
| 23 | import importlib.util |
| 24 | import json |
| 25 | from pathlib import Path |
| 26 | |
| 27 | import object_records |
| 28 | from conftest import stage_collection |
| 29 | from test_findings_money_gates import run_object |
| 30 | |
| 31 | ROOT = Path(__file__).resolve().parents[1] |
| 32 | |
| 33 | |
| 34 | def _env(tmp_path, monkeypatch): |
| 35 | data = tmp_path / "data" |
| 36 | for package, name in (("app-billing", "wallets"), ("app-billing", "wallet_entries"), |
| 37 | ("app-settings", "app_settings"), ("app-catalog", "products"), |
| 38 | ("app-orders", "orders"), ("app-orders", "order_lines"), |
| 39 | ("app-invoices", "invoices"), ("app-payments", "payments"), |
| 40 | ("app-payments", "refunds")): |
| 41 | stage_collection(data, package, name) |
| 42 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data)) |
| 43 | return data |
| 44 | |
| 45 | |
| 46 | def _hijacked_invoice(data): |
| 47 | make = lambda c, r: object_records.create_collection_record(c, r, base_dir=data) |
| 48 | # Mallory's order exists first, as any older order would. |
| 49 | make("products", {"id": "gcm", "name": "Her card", "price_cents": "1000000", |
| 50 | "is_gift_card": "true", "product_type": "digital", "owner_id": "mallory"}) |
| 51 | make("orders", {"id": "om", "number": "M-1", "customer_name": "M", "owner_id": "mallory"}) |
| 52 | make("order_lines", {"id": "olm", "order_id": "om", "product_id": "gcm", "description": "card", |
| 53 | "quantity": "1", "unit_price_cents": "1000000", |
| 54 | "line_total_cents": "1000000", "owner_id": "mallory"}) |
| 55 | # The shop's real sale. |
| 56 | make("products", {"id": "gc", "name": "Gift Card $50", "price_cents": "5000", |
| 57 | "is_gift_card": "true", "product_type": "digital", "owner_id": "shop"}) |
| 58 | make("invoices", {"id": "inv1", "number": "INV-1", "customer_name": "Ada", |
| 59 | "total_cents": "5000", "status": "sent", "owner_id": "shop"}) |
| 60 | make("orders", {"id": "o1", "number": "SO-1", "customer_name": "Ada", "invoice_id": "inv1", |
| 61 | "owner_id": "shop"}) |
| 62 | make("order_lines", {"id": "ol1", "order_id": "o1", "product_id": "gc", |
| 63 | "description": "Gift Card $50", "quantity": "1", |
| 64 | "unit_price_cents": "5000", "line_total_cents": "5000", |
| 65 | "owner_id": "shop"}) |
| 66 | # Her own order re-pointed at the shop's invoice: a PUT the old schema took. |
| 67 | object_records.update_collection_record("orders", "om", {"invoice_id": "inv1"}, |
| 68 | base_dir=data, actor="mallory") |
| 69 | payment = make("payments", {"id": "pay1", "invoice_id": "inv1", "amount_cents": "5000", |
| 70 | "method": "card", "received_on": "2026-07-24", |
| 71 | "status": "received", "owner_id": "shop"}) |
| 72 | return {"event": "payments.record.created", "collection": "payments", |
| 73 | "record_id": "pay1", "action": "create", "record": payment} |
| 74 | |
| 75 | |
| 76 | def test_paying_the_shops_invoice_issues_the_shops_gift_card_not_one_for_an_order_pointing_at_it( |
| 77 | tmp_path, monkeypatch): |
| 78 | data = _env(tmp_path, monkeypatch) |
| 79 | event = _hijacked_invoice(data) |
| 80 | result = run_object("system_gift_card_issue", dict(event), "app-billing", method="EVENT") |
| 81 | assert result.ok, result |
| 82 | wallets = object_records.read_collection_records("wallets", base_dir=data) |
| 83 | assert [w["owner_id"] for w in wallets] == ["shop"] |
| 84 | credits = [int(row["amount_minor"]) |
| 85 | for row in object_records.read_collection_records("wallet_entries", base_dir=data)] |
| 86 | assert credits == [5000] |
| 87 | |
| 88 | |
| 89 | def _module(path): |
| 90 | spec = importlib.util.spec_from_file_location(path.stem, path) |
| 91 | module = importlib.util.module_from_spec(spec) |
| 92 | spec.loader.exec_module(module) |
| 93 | return module |
| 94 | |
| 95 | |
| 96 | def test_the_order_an_invoice_settles_is_one_its_owner_owns(tmp_path, monkeypatch): |
| 97 | """system_shop_fulfillment confirms and ships "the order for this |
| 98 | invoice"; it took mallory's, so the shop's customer's payment shipped her |
| 99 | order. Both reactions use the same join.""" |
| 100 | data = _env(tmp_path, monkeypatch) |
| 101 | _hijacked_invoice(data) |
| 102 | for path in ("packages/app-billing/objects/system/gift_card_issue.py", |
| 103 | "packages/app-shop/objects/system/shop_fulfillment.py"): |
| 104 | order = _module(ROOT / path)._order_for_invoice(data, "inv1") |
| 105 | assert order is not None and order["id"] == "o1", (path, order) |
| 106 | |
| 107 | |
| 108 | def test_an_orders_tracking_page_does_not_show_somebody_elses_invoice(tmp_path, monkeypatch): |
| 109 | """site_order_status puts the invoice's pay link -- its portal token, the |
| 110 | capability to the customer's bill -- on the order's own page. Mallory's |
| 111 | order pointed at the shop's invoice showed her the shop customer's.""" |
| 112 | data = _env(tmp_path, monkeypatch) |
| 113 | _hijacked_invoice(data) |
| 114 | status = _module(ROOT / "packages/app-orders/objects/site/order_status.py") |
| 115 | orders = {row["id"]: row for row in object_records.read_collection_records("orders", base_dir=data)} |
| 116 | assert status._invoice_for(data, orders["om"]) is None |
| 117 | assert status._invoice_for(data, orders["o1"])["id"] == "inv1" |