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_credit_wallet.py · 11.9 KB · 255 lines
| 1 | """Whose wallet a dispute's store credit lands in. |
| 2 | |
| 3 | action_resolve_dispute took a body wallet_id and wrote the credit into |
| 4 | whatever wallet that named: no owner, kind or active check. Whoever was |
| 5 | resolving a real complaint could send the customer's credit to their own |
| 6 | wallet, a friend's, or a gift card, and an unknown id answered |
| 7 | `no_such_wallet` where somebody else's wallet answered nothing of the |
| 8 | kind, so the door also said which wallet ids exist. |
| 9 | |
| 10 | The rule now: the credit lands in a wallet of the ORDER's customer (the |
| 11 | dispute's customer_email only when there is no order). A wallet_id picks |
| 12 | among that customer's active balance / store_credit wallets; anything |
| 13 | else gets the same answer as a customer with no wallet, byte for byte, |
| 14 | and writes nothing. Only an admin may name another wallet. |
| 15 | """ |
| 16 | |
| 17 | import json |
| 18 | |
| 19 | import object_identity |
| 20 | import object_permission_store |
| 21 | import object_permissions |
| 22 | import object_records |
| 23 | |
| 24 | import test_disputes as td |
| 25 | from conftest import stage_collection |
| 26 | |
| 27 | ADMIN = {"user_id": "boss", "roles": ["admin"]} |
| 28 | |
| 29 | |
| 30 | def _wallet(data_dir, wallet_id, owner, **fields): |
| 31 | record = {"id": wallet_id, "owner_id": owner, "is_active": "true"} |
| 32 | record.update(fields) |
| 33 | return object_records.create_collection_record("wallets", record, |
| 34 | base_dir=data_dir) |
| 35 | |
| 36 | |
| 37 | def _credit(root, **payload): |
| 38 | payload.setdefault("dispute_id", "dis-1") |
| 39 | payload.setdefault("resolution_kind", "credit") |
| 40 | payload.setdefault("amount_cents", "500") |
| 41 | return td.resolve(root, **payload) |
| 42 | |
| 43 | |
| 44 | def _still_open(data_dir): |
| 45 | row = object_records.get_collection_record("disputes", "dis-1", |
| 46 | base_dir=data_dir) |
| 47 | return row["status"] == "open" and row["resolution_claim"] == "" |
| 48 | |
| 49 | |
| 50 | def test_a_wallet_id_naming_somebody_elses_wallet_is_refused_and_writes_nothing( |
| 51 | tmp_path, monkeypatch): |
| 52 | data_dir, root = td.shop_with_a_dispute(tmp_path, monkeypatch) |
| 53 | td.wallet(data_dir) # ada's own |
| 54 | _wallet(data_dir, "wal-clerk", "clerk@example.test") |
| 55 | refused = _credit(root, wallet_id="wal-clerk") |
| 56 | assert refused["status"] == 409 |
| 57 | assert refused["missing_wallet"] == "no_wallet_for_customer" |
| 58 | assert td.entries(data_dir) == [] |
| 59 | assert _still_open(data_dir) |
| 60 | |
| 61 | |
| 62 | def test_an_unknown_wallet_id_gets_the_byte_identical_answer(tmp_path, monkeypatch): |
| 63 | data_dir, root = td.shop_with_a_dispute(tmp_path, monkeypatch) |
| 64 | td.wallet(data_dir) |
| 65 | _wallet(data_dir, "wal-clerk", "clerk@example.test") |
| 66 | somebody_elses = _credit(root, wallet_id="wal-clerk") |
| 67 | unknown = _credit(root, wallet_id="wal-does-not-exist") |
| 68 | assert repr(unknown) == repr(somebody_elses) |
| 69 | assert td.entries(data_dir) == [] |
| 70 | |
| 71 | |
| 72 | def test_a_gift_card_is_not_the_customers_wallet(tmp_path, monkeypatch): |
| 73 | """A gift card's owner_id is the shop and it is spent by whoever holds |
| 74 | the code: crediting one hands the money to a bearer, not the customer. |
| 75 | Refused even when the card's owner field carries the customer's own |
| 76 | email.""" |
| 77 | data_dir, root = td.shop_with_a_dispute(tmp_path, monkeypatch) |
| 78 | td.wallet(data_dir) |
| 79 | _wallet(data_dir, "gc-shop", "shop", kind="gift_card", code="GC-1") |
| 80 | _wallet(data_dir, "gc-ada", "ada@example.test", kind="gift_card", code="GC-2") |
| 81 | for card in ("gc-shop", "gc-ada"): |
| 82 | refused = _credit(root, wallet_id=card) |
| 83 | assert refused["missing_wallet"] == "no_wallet_for_customer", card |
| 84 | assert td.entries(data_dir) == [] |
| 85 | |
| 86 | |
| 87 | def test_no_gift_card_or_inactive_wallet_is_picked_by_default(tmp_path, monkeypatch): |
| 88 | data_dir, root = td.shop_with_a_dispute(tmp_path, monkeypatch) |
| 89 | _wallet(data_dir, "gc-ada", "ada@example.test", kind="gift_card", code="GC-2") |
| 90 | _wallet(data_dir, "wal-off", "ada@example.test", is_active="false") |
| 91 | refused = _credit(root) |
| 92 | assert refused["missing_wallet"] == "no_wallet_for_customer" |
| 93 | refused = _credit(root, wallet_id="wal-off") |
| 94 | assert refused["missing_wallet"] == "no_wallet_for_customer" |
| 95 | assert td.entries(data_dir) == [] |
| 96 | |
| 97 | |
| 98 | def test_the_customers_second_wallet_is_selectable(tmp_path, monkeypatch): |
| 99 | data_dir, root = td.shop_with_a_dispute(tmp_path, monkeypatch) |
| 100 | td.wallet(data_dir) |
| 101 | _wallet(data_dir, "wal-2", "ada@example.test", kind="store_credit") |
| 102 | result = _credit(root, wallet_id="wal-2") |
| 103 | assert result["ok"] is True, result |
| 104 | assert [row["wallet_id"] for row in td.entries(data_dir)] == ["wal-2"] |
| 105 | |
| 106 | |
| 107 | def test_a_wallet_keyed_by_the_customers_user_id_is_found(tmp_path, monkeypatch): |
| 108 | """Wallets are owned by a user id; checkout stamps an order with the |
| 109 | email only. The email resolves to the one ACTIVE user carrying it.""" |
| 110 | data_dir, root = td.shop_with_a_dispute(tmp_path, monkeypatch) |
| 111 | object_identity.create_user({"user_id": "usr_ada", "email": "Ada@Example.test", |
| 112 | "status": "active"}, base_dir=data_dir) |
| 113 | _wallet(data_dir, "wal-u", "usr_ada") |
| 114 | result = _credit(root) |
| 115 | assert result["ok"] is True, result |
| 116 | assert [row["wallet_id"] for row in td.entries(data_dir)] == ["wal-u"] |
| 117 | |
| 118 | |
| 119 | def test_an_unverified_or_ambiguous_email_resolves_to_nobody(tmp_path, monkeypatch): |
| 120 | data_dir, root = td.shop_with_a_dispute(tmp_path, monkeypatch) |
| 121 | object_identity.create_user({"user_id": "usr_pending", "email": "ada@example.test", |
| 122 | "status": "pending"}, base_dir=data_dir) |
| 123 | _wallet(data_dir, "wal-p", "usr_pending") |
| 124 | assert _credit(root)["missing_wallet"] == "no_wallet_for_customer" |
| 125 | object_identity.create_user({"user_id": "usr_a1", "email": "ada@example.test", |
| 126 | "status": "active"}, base_dir=data_dir) |
| 127 | object_identity.create_user({"user_id": "usr_a2", "email": "ada@example.test", |
| 128 | "status": "active"}, base_dir=data_dir) |
| 129 | _wallet(data_dir, "wal-a1", "usr_a1") |
| 130 | assert _credit(root)["missing_wallet"] == "no_wallet_for_customer" |
| 131 | assert td.entries(data_dir) == [] |
| 132 | |
| 133 | |
| 134 | def test_an_admin_may_name_another_wallet(tmp_path, monkeypatch): |
| 135 | data_dir, root = td.shop_with_a_dispute(tmp_path, monkeypatch) |
| 136 | _wallet(data_dir, "wal-other", "someone@example.test") |
| 137 | result = _credit(root, wallet_id="wal-other", _identity=ADMIN) |
| 138 | assert result["ok"] is True, result |
| 139 | assert [row["wallet_id"] for row in td.entries(data_dir)] == ["wal-other"] |
| 140 | |
| 141 | |
| 142 | def test_editing_the_disputes_email_does_not_redirect_when_there_is_an_order( |
| 143 | tmp_path, monkeypatch): |
| 144 | data_dir, root = td.shop_with_a_dispute(tmp_path, monkeypatch) |
| 145 | td.wallet(data_dir) # ada's, the order's customer |
| 146 | _wallet(data_dir, "wal-clerk", "clerk@example.test") |
| 147 | object_records.update_collection_record( |
| 148 | "disputes", "dis-1", {"customer_email": "clerk@example.test"}, |
| 149 | base_dir=data_dir, actor="test") |
| 150 | refused = _credit(root, wallet_id="wal-clerk") |
| 151 | assert refused["missing_wallet"] == "no_wallet_for_customer" |
| 152 | result = _credit(root) |
| 153 | assert result["ok"] is True, result |
| 154 | assert [row["wallet_id"] for row in td.entries(data_dir)] == ["wal-1"] |
| 155 | |
| 156 | |
| 157 | def test_without_an_order_the_disputes_email_names_the_customer(tmp_path, monkeypatch): |
| 158 | data_dir, root = td.shop_with_a_dispute(tmp_path, monkeypatch, |
| 159 | order_id="", payment_id="pay-1") |
| 160 | td.wallet(data_dir) |
| 161 | _wallet(data_dir, "wal-clerk", "clerk@example.test") |
| 162 | assert _credit(root, wallet_id="wal-clerk")["missing_wallet"] == \ |
| 163 | "no_wallet_for_customer" |
| 164 | result = _credit(root) |
| 165 | assert result["ok"] is True, result |
| 166 | assert [row["wallet_id"] for row in td.entries(data_dir)] == ["wal-1"] |
| 167 | |
| 168 | |
| 169 | def test_a_signed_in_customer_cannot_credit_themselves_from_their_own_payment( |
| 170 | tmp_path, monkeypatch): |
| 171 | """A registered customer may write their own dispute, and may own the |
| 172 | payment row, but owning the dispute is not a payout grant: even with a |
| 173 | cashier's role they are refused before any ceiling is measured.""" |
| 174 | data_dir, root = td.setup_env(tmp_path, monkeypatch) |
| 175 | # The rules these packages ship, installed as a real server has them: |
| 176 | # the opt-out that matters is app-payments' owner_fallback deny. |
| 177 | payload = object_permissions.policy_to_dict( |
| 178 | object_permission_store.load_policy(data_dir)) |
| 179 | for package in ("app-payments", "app-disputes", "app-billing"): |
| 180 | rules = json.loads((td.PACKAGES / package / "permissions" / "rules.json") |
| 181 | .read_text())["rules"] |
| 182 | payload["rules"].extend(rules) |
| 183 | object_permission_store.replace_policy(payload, data_dir) |
| 184 | object_records.create_collection_record( |
| 185 | "invoices", |
| 186 | {"id": "inv-c", "number": "C-1", "customer_name": "Cust", |
| 187 | "customer_email": "cust", "status": "sent", "issue_date": "2026-07-01", |
| 188 | "due_date": "2026-07-15", "subtotal_cents": "3600", |
| 189 | "total_cents": "3600", "owner_id": "cust"}, |
| 190 | base_dir=data_dir) |
| 191 | object_records.create_collection_record( |
| 192 | "payments", |
| 193 | {"id": "pay-c", "invoice_id": "inv-c", "amount_cents": "3600", "method": "card", |
| 194 | "received_on": "2026-07-02", "status": "received", "owner_id": "cust"}, |
| 195 | base_dir=data_dir) |
| 196 | td.dispute(data_dir, "dis-1", order_id="", payment_id="pay-c", kind="payment", |
| 197 | customer_email="cust", opened_by="cust", owner_id="cust") |
| 198 | _wallet(data_dir, "wal-c", "cust") |
| 199 | refused = _credit(root, amount_cents="3600", _identity={"user_id": "cust", "roles": ["cashier"]}) |
| 200 | assert refused["status"] == 403 and refused["code"] == "payout_needs_manager", refused |
| 201 | assert td.entries(data_dir) == [] |
| 202 | |
| 203 | |
| 204 | # --- the wallet must hold the dispute's currency ------------------------------- |
| 205 | # wallet_entries.amount_minor is in the wallet's own denomination, and a |
| 206 | # wallet's denomination is its own field (blank is USD). The credit was |
| 207 | # written without comparing it to the order's currency, so 5.00 USD of |
| 208 | # dispute landed as 500 satoshis, or as 500 of a currency a hundred times |
| 209 | # smaller. |
| 210 | |
| 211 | def test_a_credit_into_a_wallet_of_another_denomination_is_refused(tmp_path, monkeypatch): |
| 212 | data_dir, root = td.shop_with_a_dispute(tmp_path, monkeypatch) |
| 213 | stage_collection(data_dir, "app-finance", "denominations", seed=True) |
| 214 | _wallet(data_dir, "wal-sats", "ada@example.test", denomination_id="den-btc") |
| 215 | refused = _credit(root, wallet_id="wal-sats") |
| 216 | assert refused["status"] == 409 and refused["code"] == "currency_mismatch", refused |
| 217 | assert "USD" in refused["error"] and "BTC" in refused["error"] |
| 218 | assert td.entries(data_dir) == [] |
| 219 | assert _still_open(data_dir) |
| 220 | |
| 221 | |
| 222 | def test_a_blank_denomination_is_usd_and_matches_a_usd_order(tmp_path, monkeypatch): |
| 223 | data_dir, root = td.shop_with_a_dispute(tmp_path, monkeypatch) |
| 224 | td.wallet(data_dir) # blank denomination |
| 225 | assert _credit(root)["ok"] is True |
| 226 | assert len(td.entries(data_dir)) == 1 |
| 227 | |
| 228 | |
| 229 | def test_an_order_in_another_currency_is_not_credited_to_a_blank_usd_wallet( |
| 230 | tmp_path, monkeypatch): |
| 231 | data_dir, root = td.shop_with_a_dispute(tmp_path, monkeypatch) |
| 232 | object_records.update_collection_record( |
| 233 | "orders", "ord-1", {"currency": "EUR"}, base_dir=data_dir, |
| 234 | preserve_read_only=True) |
| 235 | td.wallet(data_dir) |
| 236 | refused = _credit(root) |
| 237 | assert refused["status"] == 409 and refused["code"] == "currency_mismatch", refused |
| 238 | assert td.entries(data_dir) == [] |
| 239 | |
| 240 | |
| 241 | def test_a_wallet_whose_denomination_cannot_be_read_is_not_credited(tmp_path, monkeypatch): |
| 242 | data_dir, root = td.shop_with_a_dispute(tmp_path, monkeypatch) |
| 243 | wallet = td.wallet(data_dir) |
| 244 | # Below the record API (which refuses a dangling pointer), as a restored |
| 245 | # or hand-edited file would have it. |
| 246 | path = data_dir / "collections" / "wallets" / "records.tsv" |
| 247 | lines = path.read_text().split("\n") |
| 248 | header = lines[0].split("\t") |
| 249 | row = lines[1].split("\t") |
| 250 | row[header.index("denomination_id")] = "den-gone" |
| 251 | lines[1] = "\t".join(row) |
| 252 | path.write_text("\n".join(lines)) |
| 253 | refused = _credit(root) |
| 254 | assert refused["status"] == 409 and refused["code"] == "currency_mismatch", refused |
| 255 | assert td.entries(data_dir) == [] |