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_dispute_manager_access.py · 16.2 KB · 338 lines
| 1 | """Who may settle a dispute, under the rules the packages really ship. |
| 2 | |
| 3 | Under app-disputes 0.4.5 no ordinary staff member could pay out a dispute |
| 4 | somebody else had opened: updating a dispute was owner-row only, and the |
| 5 | resolve action also asked for update on the order, which no shipped role |
| 6 | holds. The fix is not a right to edit orders. role:manager and role:owner |
| 7 | may update every dispute, and that is the payout grant; the action then |
| 8 | reads the order, its bill and its payments under its own authority and |
| 9 | checks for itself what an order-update right used to stand in for. |
| 10 | |
| 11 | Every test here installs the shipped rules of app-disputes, app-orders, |
| 12 | app-invoices, app-payments and app-billing -- no permissive fixture. |
| 13 | """ |
| 14 | |
| 15 | import json |
| 16 | |
| 17 | import object_access |
| 18 | import object_corrections |
| 19 | import object_records |
| 20 | |
| 21 | import test_disputes as td |
| 22 | |
| 23 | CASHIER = {"user_id": "cara", "roles": ["cashier"]} |
| 24 | OTHER_CASHIER = {"user_id": "carl", "roles": ["cashier"]} |
| 25 | SHIFT_LEAD = {"user_id": "sam", "roles": ["shift_lead"]} |
| 26 | MANAGER = {"user_id": "mo", "roles": ["manager"]} |
| 27 | OWNER = {"user_id": "olive", "roles": ["owner"]} |
| 28 | CUSTOMER = {"user_id": "ada", "roles": ["registered"]} |
| 29 | |
| 30 | SHIPPED = ("app-orders", "app-invoices", "app-payments", "app-billing") |
| 31 | |
| 32 | |
| 33 | def shop(tmp_path, monkeypatch, *, cents=3600, total=None, before=None, |
| 34 | **dispute_fields): |
| 35 | """A shop sale paid at checkout, a dispute about it opened by cashier |
| 36 | cara, and the customer's wallet.""" |
| 37 | data_dir, root = td.setup_env(tmp_path, monkeypatch) |
| 38 | td.install_rules(data_dir, *SHIPPED) |
| 39 | td.product(data_dir) |
| 40 | extra = {"total_cents": total} if total is not None else {} |
| 41 | td.order(data_dir, **extra) |
| 42 | td.order_line(data_dir, "line-1") |
| 43 | td.paid(data_dir, cents=cents) |
| 44 | if before is not None: |
| 45 | before(data_dir) |
| 46 | fields = {"owner_id": "cara", "opened_by": "cara"} |
| 47 | fields.update(dispute_fields) |
| 48 | td.dispute(data_dir, **fields) |
| 49 | td.wallet(data_dir) |
| 50 | return data_dir, root |
| 51 | |
| 52 | |
| 53 | def resolve(root, identity, **payload): |
| 54 | payload.setdefault("dispute_id", "dis-1") |
| 55 | return td.resolve(root, _identity=identity, **payload) |
| 56 | |
| 57 | |
| 58 | def status(data_dir, dispute_id="dis-1"): |
| 59 | return object_records.get_collection_record( |
| 60 | "disputes", dispute_id, base_dir=data_dir)["status"] |
| 61 | |
| 62 | |
| 63 | # --- a manager settles a dispute a cashier opened ------------------------------ |
| 64 | |
| 65 | def test_a_manager_credits_a_dispute_a_cashier_opened_once(tmp_path, monkeypatch): |
| 66 | data_dir, root = shop(tmp_path, monkeypatch) |
| 67 | done = resolve(root, MANAGER, resolution_kind="credit", amount_cents="1200", |
| 68 | note="Parcel lost; credit instead of a resend") |
| 69 | assert done.get("ok") is True, done |
| 70 | assert status(data_dir) == "resolved" |
| 71 | [entry] = td.entries(data_dir) |
| 72 | assert entry["wallet_id"] == "wal-1" and entry["amount_minor"] == "1200" |
| 73 | row = object_records.get_collection_record("disputes", "dis-1", base_dir=data_dir) |
| 74 | assert row["resolved_by"] == "mo" |
| 75 | |
| 76 | again = resolve(root, MANAGER, resolution_kind="credit", amount_cents="1200") |
| 77 | assert again["ok"] is True and "already resolved" in again["note"] |
| 78 | assert len(td.entries(data_dir)) == 1 |
| 79 | |
| 80 | |
| 81 | def test_a_manager_refunds_a_dispute_a_cashier_opened(tmp_path, monkeypatch): |
| 82 | data_dir, root = shop(tmp_path, monkeypatch) |
| 83 | done = resolve(root, MANAGER, resolution_kind="refund", amount_cents="1200") |
| 84 | assert done.get("ok") is True, done |
| 85 | [refund] = td.refunds(data_dir) |
| 86 | # The refund is the money's account's (the shop's), not the cashier's |
| 87 | # who typed the dispute. |
| 88 | assert refund["payment_id"] == "pay-1" and refund["owner_id"] == "shop" |
| 89 | assert resolve(root, MANAGER, resolution_kind="refund", |
| 90 | amount_cents="1200")["ok"] is True |
| 91 | assert len(td.refunds(data_dir)) == 1 |
| 92 | |
| 93 | |
| 94 | def test_the_owner_role_may_settle_too(tmp_path, monkeypatch): |
| 95 | data_dir, root = shop(tmp_path, monkeypatch) |
| 96 | done = resolve(root, OWNER, resolution_kind="credit", amount_cents="500") |
| 97 | assert done.get("ok") is True, done |
| 98 | assert len(td.entries(data_dir)) == 1 |
| 99 | |
| 100 | |
| 101 | def test_a_manager_holds_no_right_to_edit_orders(tmp_path, monkeypatch): |
| 102 | """The payout works without one, and none was added: the shipped rules |
| 103 | still refuse a manager an update of the order.""" |
| 104 | data_dir, _ = shop(tmp_path, monkeypatch) |
| 105 | order = object_records.get_collection_record("orders", "ord-1", base_dir=data_dir) |
| 106 | for identity in (MANAGER, OWNER, CASHIER, SHIFT_LEAD): |
| 107 | access = object_access.Access(identity, base_dir=data_dir) |
| 108 | assert access.can("read", "orders", order), identity |
| 109 | assert not access.can("update", "orders", order), identity |
| 110 | rules = json.loads((td.APP_DISPUTES_DIR / "permissions" / "rules.json").read_text()) |
| 111 | assert not [r for r in rules["rules"] |
| 112 | if r.get("collection") in ("orders", "order_lines", "invoices", "payments")] |
| 113 | |
| 114 | |
| 115 | # --- who may not ------------------------------------------------------------------ |
| 116 | |
| 117 | def test_a_cashier_cannot_resolve_somebody_elses_dispute(tmp_path, monkeypatch): |
| 118 | data_dir, root = shop(tmp_path, monkeypatch) |
| 119 | for kind, extra in (("credit", {"amount_cents": "500"}), |
| 120 | ("refund", {"amount_cents": "500"}), |
| 121 | ("no_action", {"note": "not ours"})): |
| 122 | refused = resolve(root, OTHER_CASHIER, resolution_kind=kind, **extra) |
| 123 | assert refused["status"] == 404, (kind, refused) |
| 124 | assert status(data_dir) == "open" |
| 125 | assert td.entries(data_dir) == [] and td.refunds(data_dir) == [] |
| 126 | |
| 127 | |
| 128 | def test_a_shift_lead_cannot_pay_out_even_their_own_dispute(tmp_path, monkeypatch): |
| 129 | data_dir, root = shop(tmp_path, monkeypatch, owner_id="sam", opened_by="sam") |
| 130 | refused = resolve(root, SHIFT_LEAD, resolution_kind="credit", amount_cents="500") |
| 131 | assert refused["status"] == 403 and refused["code"] == "payout_needs_manager" |
| 132 | assert td.entries(data_dir) == [] |
| 133 | closed = resolve(root, SHIFT_LEAD, resolution_kind="no_action", note="found it") |
| 134 | assert closed["ok"] is True |
| 135 | |
| 136 | |
| 137 | def test_a_customer_cannot_run_the_action(tmp_path, monkeypatch): |
| 138 | data_dir, root = shop(tmp_path, monkeypatch, owner_id="ada", opened_by="ada") |
| 139 | for identity in (CUSTOMER, {"user_id": "ada"}, None): |
| 140 | refused = resolve(root, identity, resolution_kind="credit", amount_cents="500") |
| 141 | assert refused["status"] in (401, 403), (identity, refused) |
| 142 | assert td.entries(data_dir) == [] and status(data_dir) == "open" |
| 143 | |
| 144 | |
| 145 | def test_role_grants_does_not_count_owning_the_row(tmp_path, monkeypatch): |
| 146 | data_dir, _ = shop(tmp_path, monkeypatch) |
| 147 | row = object_records.get_collection_record("disputes", "dis-1", base_dir=data_dir) |
| 148 | cara = object_access.Access(CASHIER, base_dir=data_dir) |
| 149 | assert cara.can("update", "disputes", row) # her own row |
| 150 | assert not object_corrections.role_grants(cara, "update", collection="disputes", record=row) |
| 151 | mo = object_access.Access(MANAGER, base_dir=data_dir) |
| 152 | assert object_corrections.role_grants(mo, "update", collection="disputes", record=row) |
| 153 | |
| 154 | |
| 155 | # --- what a payout may come out of ------------------------------------------------ |
| 156 | |
| 157 | def test_an_order_naming_another_accounts_bill_pays_nothing(tmp_path, monkeypatch): |
| 158 | """orders.invoice_id is not a validated pointer: mallory's own order names |
| 159 | the shop's bill. A manager settling a dispute about it must not measure a |
| 160 | credit in, or refund from, the shop's money.""" |
| 161 | def mallorys_order(data_dir): |
| 162 | td.order(data_dir, "ord-m", owner="mallory", number="SO-M", invoice_id="inv-1", |
| 163 | customer_email="mallory@example.test") |
| 164 | data_dir, root = shop(tmp_path, monkeypatch, order_id="ord-m", before=mallorys_order) |
| 165 | td.wallet(data_dir, "wal-m", owner="mallory@example.test") |
| 166 | for kind in ("credit", "refund", "replacement"): |
| 167 | refused = resolve(root, MANAGER, resolution_kind=kind, amount_cents="1200") |
| 168 | assert refused["status"] == 409 and refused["code"] == "order_mismatch", (kind, refused) |
| 169 | assert td.entries(data_dir) == [] and td.refunds(data_dir) == [] |
| 170 | assert [row["id"] for row in td.orders(data_dir)] == ["ord-1", "ord-m"] |
| 171 | assert status(data_dir) == "open" |
| 172 | |
| 173 | |
| 174 | def test_a_payment_that_is_not_on_the_disputes_order_pays_nothing(tmp_path, monkeypatch): |
| 175 | def another_sale(data_dir): |
| 176 | object_records.create_collection_record( |
| 177 | "invoices", |
| 178 | {"id": "inv-x", "number": "X-1", "customer_name": "Someone", |
| 179 | "customer_email": "x@example.test", "status": "sent", |
| 180 | "issue_date": "2026-07-01", "due_date": "2026-07-15", |
| 181 | "subtotal_cents": "9900", "total_cents": "9900", "owner_id": "shop"}, |
| 182 | base_dir=data_dir) |
| 183 | object_records.create_collection_record( |
| 184 | "payments", |
| 185 | {"id": "pay-x", "invoice_id": "inv-x", "amount_cents": "9900", |
| 186 | "method": "card", "received_on": "2026-07-02", "status": "received", |
| 187 | "owner_id": "shop"}, |
| 188 | base_dir=data_dir) |
| 189 | data_dir, root = shop(tmp_path, monkeypatch, payment_id="pay-x", before=another_sale) |
| 190 | for kind in ("credit", "refund"): |
| 191 | refused = resolve(root, MANAGER, resolution_kind=kind, amount_cents="5000") |
| 192 | assert refused["status"] == 409 and refused["code"] == "order_mismatch", (kind, refused) |
| 193 | assert td.entries(data_dir) == [] and td.refunds(data_dir) == [] |
| 194 | |
| 195 | |
| 196 | def test_a_bill_of_another_order_pays_nothing(tmp_path, monkeypatch): |
| 197 | """The bill says which order it bills (source_order_id); an order naming |
| 198 | a bill raised for a different order of the same account is refused.""" |
| 199 | data_dir, root = shop(tmp_path, monkeypatch) |
| 200 | object_records.update_collection_record( |
| 201 | "invoices", "inv-1", {"source_order_id": "ord-other"}, base_dir=data_dir, |
| 202 | actor="test") |
| 203 | refused = resolve(root, MANAGER, resolution_kind="credit", amount_cents="500") |
| 204 | assert refused["status"] == 409 and refused["code"] == "order_mismatch", refused |
| 205 | assert td.entries(data_dir) == [] |
| 206 | |
| 207 | |
| 208 | def test_a_credit_over_what_was_paid_is_refused(tmp_path, monkeypatch): |
| 209 | data_dir, root = shop(tmp_path, monkeypatch) |
| 210 | refused = resolve(root, MANAGER, resolution_kind="credit", amount_cents="3601") |
| 211 | assert refused["status"] == 409 and refused["code"] == "credit_over_ceiling", refused |
| 212 | assert refused["ceiling_cents"] == "3600" |
| 213 | assert td.entries(data_dir) == [] and status(data_dir) == "open" |
| 214 | assert resolve(root, MANAGER, resolution_kind="credit", |
| 215 | amount_cents="3600")["ok"] is True |
| 216 | |
| 217 | |
| 218 | def test_a_credit_never_exceeds_the_orders_total(tmp_path, monkeypatch): |
| 219 | """Paid 36.00 against a 30.00 order (a double tap at the till): the |
| 220 | excess is the payment's to refund, not the dispute's to credit.""" |
| 221 | data_dir, root = shop(tmp_path, monkeypatch, total=3000) |
| 222 | refused = resolve(root, MANAGER, resolution_kind="credit", amount_cents="3001") |
| 223 | assert refused["status"] == 409 and refused["ceiling_cents"] == "3000", refused |
| 224 | assert td.entries(data_dir) == [] |
| 225 | |
| 226 | |
| 227 | def test_earlier_credits_on_the_order_come_off_the_ceiling(tmp_path, monkeypatch): |
| 228 | data_dir, root = shop(tmp_path, monkeypatch) |
| 229 | td.dispute(data_dir, "dis-2", owner_id="carl", opened_by="carl", |
| 230 | summary="Mug chipped") |
| 231 | assert resolve(root, MANAGER, dispute_id="dis-2", resolution_kind="credit", |
| 232 | amount_cents="2000")["ok"] is True |
| 233 | refused = resolve(root, MANAGER, resolution_kind="credit", amount_cents="1601") |
| 234 | assert refused["status"] == 409 and refused["ceiling_cents"] == "1600", refused |
| 235 | assert len(td.entries(data_dir)) == 1 |
| 236 | |
| 237 | |
| 238 | def test_a_currency_mismatch_is_still_refused_for_a_manager(tmp_path, monkeypatch): |
| 239 | data_dir, root = shop(tmp_path, monkeypatch) |
| 240 | object_records.update_collection_record( |
| 241 | "orders", "ord-1", {"currency": "EUR"}, base_dir=data_dir, |
| 242 | preserve_read_only=True) |
| 243 | refused = resolve(root, MANAGER, resolution_kind="credit", amount_cents="500") |
| 244 | assert refused["status"] == 409 and refused["code"] == "currency_mismatch", refused |
| 245 | assert td.entries(data_dir) == [] |
| 246 | |
| 247 | |
| 248 | # --- staff open a dispute about a shop sale --------------------------------------- |
| 249 | # The record API lets a dispute point only at an order its writer may update, |
| 250 | # which no staff role holds on the shop's orders, so a cashier taking the call |
| 251 | # got 403. action_open_dispute is the staff door; the relation stays as it is. |
| 252 | |
| 253 | def open_dispute(root, identity, **payload): |
| 254 | payload.setdefault("kind", "delivery") |
| 255 | payload.setdefault("summary", "Parcel never arrived") |
| 256 | return td.run(root, "action_open_dispute", "POST", {**payload, "_identity": identity}) |
| 257 | |
| 258 | |
| 259 | def counter(tmp_path, monkeypatch, before=None): |
| 260 | data_dir, root = td.setup_env(tmp_path, monkeypatch) |
| 261 | td.install_rules(data_dir, *SHIPPED, "app-shipping") |
| 262 | td.product(data_dir) |
| 263 | td.order(data_dir) |
| 264 | td.order_line(data_dir, "line-1") |
| 265 | td.paid(data_dir) |
| 266 | td.wallet(data_dir) |
| 267 | if before is not None: |
| 268 | before(data_dir) |
| 269 | return data_dir, root |
| 270 | |
| 271 | |
| 272 | def test_the_record_api_still_refuses_a_cashiers_pointer_at_a_shop_order( |
| 273 | tmp_path, monkeypatch): |
| 274 | """The relation is unchanged: the action is the staff door, not a looser |
| 275 | pointer rule every registered user would also get.""" |
| 276 | data_dir, _ = counter(tmp_path, monkeypatch) |
| 277 | cara = object_access.Access(CASHIER, base_dir=data_dir) |
| 278 | refusal = cara.relation_refusal("disputes", {"order_id": "ord-1"}) |
| 279 | assert refusal is not None and refusal[0] == 403 |
| 280 | |
| 281 | |
| 282 | def test_a_cashier_opens_a_dispute_about_a_shop_order(tmp_path, monkeypatch): |
| 283 | data_dir, root = counter(tmp_path, monkeypatch) |
| 284 | opened = open_dispute(root, CASHIER, order_id="ord-1", payment_id="pay-1", |
| 285 | customer_email="ada@example.test") |
| 286 | assert opened.get("ok") is True, opened |
| 287 | row = object_records.get_collection_record("disputes", opened["dispute_id"], |
| 288 | base_dir=data_dir) |
| 289 | assert row["status"] == "open" and row["owner_id"] == "cara" |
| 290 | assert row["opened_by"] == "cara" and row["order_id"] == "ord-1" |
| 291 | assert row["resolution_kind"] == "" and row["resolution_ref"] == "" |
| 292 | |
| 293 | # ...and a manager settles it, once. |
| 294 | done = resolve(root, MANAGER, dispute_id=opened["dispute_id"], |
| 295 | resolution_kind="credit", amount_cents="1000") |
| 296 | assert done.get("ok") is True, done |
| 297 | assert len(td.entries(data_dir)) == 1 |
| 298 | |
| 299 | |
| 300 | def test_a_cashier_names_the_orders_own_shipment(tmp_path, monkeypatch): |
| 301 | def shipments(data_dir): |
| 302 | for sid, oid in (("shp-1", "ord-1"), ("shp-x", "ord-x")): |
| 303 | if oid == "ord-x": |
| 304 | td.order(data_dir, "ord-x", number="SO-X") |
| 305 | object_records.create_collection_record( |
| 306 | "shipments", {"id": sid, "order_id": oid, "owner_id": "shop"}, |
| 307 | base_dir=data_dir) |
| 308 | data_dir, root = counter(tmp_path, monkeypatch, before=shipments) |
| 309 | assert open_dispute(root, CASHIER, order_id="ord-1", |
| 310 | shipment_id="shp-1").get("ok") is True |
| 311 | wrong = open_dispute(root, CASHIER, order_id="ord-1", shipment_id="shp-x") |
| 312 | assert wrong["status"] == 409 and wrong["code"] == "order_mismatch", wrong |
| 313 | # Without an order, a shipment staff may not read is answered as missing. |
| 314 | alone = open_dispute(root, CASHIER, shipment_id="shp-1") |
| 315 | assert alone["status"] == 404 and "shp-1" not in alone["error"], alone |
| 316 | |
| 317 | |
| 318 | def test_a_customer_cannot_use_the_staff_door(tmp_path, monkeypatch): |
| 319 | data_dir, root = counter(tmp_path, monkeypatch) |
| 320 | for identity in (CUSTOMER, {"user_id": "ada"}, None): |
| 321 | refused = open_dispute(root, identity, order_id="ord-1") |
| 322 | assert refused["status"] in (401, 403), (identity, refused) |
| 323 | assert td.disputes(data_dir) == [] |
| 324 | |
| 325 | |
| 326 | def test_opening_refuses_what_does_not_belong_together(tmp_path, monkeypatch): |
| 327 | def mallorys_order(data_dir): |
| 328 | td.order(data_dir, "ord-m", owner="mallory", number="SO-M", invoice_id="inv-1") |
| 329 | data_dir, root = counter(tmp_path, monkeypatch, before=mallorys_order) |
| 330 | # A payment named beside an order must be on that order's bill and account. |
| 331 | refused = open_dispute(root, CASHIER, order_id="ord-m", payment_id="pay-1") |
| 332 | assert refused["status"] == 409 and refused["code"] == "order_mismatch", refused |
| 333 | # An unknown order is a 404 that does not echo the id. |
| 334 | missing = open_dispute(root, CASHIER, order_id="ord-nope") |
| 335 | assert missing["status"] == 404 and "ord-nope" not in missing["error"] |
| 336 | bad = open_dispute(root, CASHIER, order_id="ord-1", kind="vibes") |
| 337 | assert bad["status"] == 400 |
| 338 | assert td.disputes(data_dir) == [] |