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_exchange_protocol.py · 21.1 KB · 436 lines
| 1 | """Box-to-box exchange, the protocol: profile, partners, inbound checks, delivery. |
| 2 | |
| 3 | Two companies' boxes run in this process (tests/exchange_harness.py), each a |
| 4 | real ASGI app over its own data directory, wired by an in-process transport; |
| 5 | no socket is opened. Every refusal named in docs/box-to-box-exchange.md is |
| 6 | provoked with a request built the way an attacker or a broken partner would |
| 7 | build it, and the answer and what was (not) stored are both checked, because |
| 8 | a check that answers 403 and stores the document anyway is no check. |
| 9 | """ |
| 10 | |
| 11 | from __future__ import annotations |
| 12 | |
| 13 | import builtins |
| 14 | import json |
| 15 | import sys |
| 16 | from datetime import datetime, timedelta, timezone |
| 17 | |
| 18 | import pytest |
| 19 | |
| 20 | import object_exchange as ex |
| 21 | import object_exchange_box as xbox |
| 22 | from exchange_harness import connect, two_companies |
| 23 | |
| 24 | BASE = ["app-orders"] |
| 25 | |
| 26 | |
| 27 | def create(box, collection, record, user="owner"): |
| 28 | status, body = box.call(user, "POST", f"/collections/{collection}/records", record) |
| 29 | assert status == 201, (collection, status, body) |
| 30 | return body["record"] |
| 31 | |
| 32 | |
| 33 | def purchase_order(box, number="PO-1042", lines=((12, 1850, "Widget"),)): |
| 34 | order = create(box, "orders", { |
| 35 | "doc_type": "purchase", "number": number, "customer_name": "Supplier Ltd", |
| 36 | "currency": "EUR", "order_date": "2026-12-01", "status": "draft"}) |
| 37 | total = 0 |
| 38 | for quantity, price, description in lines: |
| 39 | create(box, "order_lines", {"order_id": order["id"], "description": description, |
| 40 | "quantity": quantity, "unit_price_cents": price}) |
| 41 | total += quantity * price |
| 42 | # The totals handler is a reaction (off in this sandbox); stamp what it would. |
| 43 | status, body = box.call("owner", "PUT", f"/collections/orders/records/{order['id']}", |
| 44 | {"subtotal_cents": total, "total_cents": total}) |
| 45 | assert status == 200, body |
| 46 | return order |
| 47 | |
| 48 | |
| 49 | def wire_from(box, envelope, signature=None, *, sent_at=None): |
| 50 | """A delivery built by `box` with its own keys, as its exchange would.""" |
| 51 | with box.active(): |
| 52 | base = str(box.data_dir) |
| 53 | signature = signature or xbox._sign(base, ex.canonical(envelope)) |
| 54 | wire = xbox._wire(base, envelope, signature) |
| 55 | if sent_at: |
| 56 | wire["delivery"]["sent_at"] = sent_at |
| 57 | wire["delivery_signature"] = xbox._sign(base, ex.delivery_message(wire["delivery"])) |
| 58 | return wire |
| 59 | |
| 60 | |
| 61 | def envelope_from(box, recipient, *, doc_id="doc-crafted-1", doc_type="purchase_order", body=None): |
| 62 | return ex.new_envelope(doc_id=doc_id, doc_type=doc_type, sender=box.domain, |
| 63 | recipient=recipient, body=body or { |
| 64 | "number": "PO-9", "issue_date": "2026-12-01", "currency": "EUR", |
| 65 | "lines": [{"line_id": "1", "description": "Widget", "quantity": "1", |
| 66 | "unit_price_minor": 100, "line_extension_minor": 100}], |
| 67 | "totals": {"line_extension_minor": 100, "tax_minor": 0, |
| 68 | "payable_minor": 100}}) |
| 69 | |
| 70 | |
| 71 | def post(network, box, wire): |
| 72 | status, raw = network.transport("POST", f"https://{box.domain}/webhooks/exchange", |
| 73 | json.dumps(wire).encode(), {"content-type": "application/json"}) |
| 74 | return status, json.loads(raw) |
| 75 | |
| 76 | |
| 77 | @pytest.fixture |
| 78 | def pair(): |
| 79 | with two_companies(BASE) as (network, buyer, seller): |
| 80 | yield network, buyer, seller |
| 81 | |
| 82 | |
| 83 | @pytest.fixture |
| 84 | def connected(pair): |
| 85 | network, buyer, seller = pair |
| 86 | connect(buyer, seller) |
| 87 | return pair |
| 88 | |
| 89 | |
| 90 | # --- profile and keys ------------------------------------------------------------ |
| 91 | |
| 92 | def test_profile_is_public_signed_and_carries_no_private_key(pair): |
| 93 | network, buyer, _ = pair |
| 94 | status, profile = buyer.call("anon", "GET", "/.well-known/dbbasic") |
| 95 | assert status == 200 |
| 96 | assert profile["domain"] == "shop.example" |
| 97 | assert profile["exchange_endpoint"] == "https://shop.example/webhooks/exchange" |
| 98 | assert profile["document_types"] == list(ex.DOCUMENT_TYPES) |
| 99 | [key] = profile["keys"] |
| 100 | assert key["status"] == "active" and key["key_id"] == ex.key_id_for(ex.unb64u(key["public_key"])) |
| 101 | assert xbox.check_profile(profile, "shop.example") == profile |
| 102 | pem = next((buyer.data_dir / "exchange" / "private").iterdir()).read_bytes() |
| 103 | assert b"PRIVATE" not in json.dumps(profile).encode() and pem.decode() not in json.dumps(profile) |
| 104 | |
| 105 | |
| 106 | def test_profile_is_404_until_the_box_is_named(): |
| 107 | from scripts.try_package import Sandbox |
| 108 | with Sandbox(["app-exchange"]) as box: |
| 109 | status, body = box.call("anon", "GET", "/.well-known/dbbasic") |
| 110 | assert status == 404, body |
| 111 | with Sandbox(["app-notes"]) as box: |
| 112 | status, _ = box.call("anon", "GET", "/.well-known/dbbasic") |
| 113 | assert status == 404, "no package, no profile" |
| 114 | |
| 115 | |
| 116 | def test_only_an_admin_or_the_owner_manages_partners(pair): |
| 117 | network, buyer, seller = pair |
| 118 | status, body = buyer.admin("connect", user="clerk", domain=seller.domain) |
| 119 | assert status == 403, body |
| 120 | status, body = buyer.call("clerk", "GET", "/collections/trading_partners/records") |
| 121 | assert status == 403, body |
| 122 | status, body = buyer.call("owner", "POST", "/collections/trading_partners/records", |
| 123 | {"domain": "x.example", "status": "connected"}) |
| 124 | assert status == 403, "partners are made by the protocol, not typed in" |
| 125 | |
| 126 | |
| 127 | def test_the_exchange_refuses_without_the_extra_naming_it(pair, monkeypatch): |
| 128 | network, buyer, _ = pair |
| 129 | real_import = builtins.__import__ |
| 130 | |
| 131 | def no_crypto(name, *args, **kwargs): |
| 132 | if name.startswith("cryptography"): |
| 133 | raise ImportError(name) |
| 134 | return real_import(name, *args, **kwargs) |
| 135 | |
| 136 | for name in [n for n in sys.modules if n.startswith("cryptography")]: |
| 137 | monkeypatch.delitem(sys.modules, name) |
| 138 | monkeypatch.setattr(builtins, "__import__", no_crypto) |
| 139 | status, body = buyer.call("anon", "GET", "/.well-known/dbbasic") |
| 140 | assert status == 409 and "dbbasic-object-server[exchange]" in body["error"] |
| 141 | status, body = buyer.call("anon", "POST", "/webhooks/exchange", {"dron_delivery": "0.1"}) |
| 142 | assert status == 409 and "dbbasic-object-server[exchange]" in body["error"] |
| 143 | |
| 144 | |
| 145 | # --- connecting ---------------------------------------------------------------------- |
| 146 | |
| 147 | def test_connecting_is_explicit_and_mutual(pair): |
| 148 | network, buyer, seller = pair |
| 149 | status, body = buyer.admin("connect", domain=seller.domain) |
| 150 | assert status == 200, body |
| 151 | [asked] = buyer.records("trading_partners") |
| 152 | [pending] = seller.records("trading_partners") |
| 153 | assert asked["status"] == "requested" and pending["status"] == "pending" |
| 154 | assert json.loads(pending["pinned_keys"]) == [], "nothing is trusted before the owner accepts" |
| 155 | # Before the seller's owner accepts, nothing the buyer sends is accepted. |
| 156 | status, answer = post(network, seller, wire_from(buyer, envelope_from(buyer, seller.domain))) |
| 157 | assert status == 403 and answer["code"] == "not_a_partner" |
| 158 | status, body = seller.admin("accept", partner_id=pending["id"]) |
| 159 | assert status == 200, body |
| 160 | assert buyer.records("trading_partners")[0]["status"] == "connected" |
| 161 | assert seller.records("trading_partners")[0]["status"] == "connected" |
| 162 | # The seller pinned exactly the keys the buyer's domain publishes. |
| 163 | _, profile = buyer.call("anon", "GET", "/.well-known/dbbasic") |
| 164 | assert [k["key_id"] for k in json.loads(seller.records("trading_partners")[0]["pinned_keys"])] \ |
| 165 | == [k["key_id"] for k in profile["keys"]] |
| 166 | |
| 167 | |
| 168 | def test_a_request_signed_with_a_key_the_domain_does_not_publish_cannot_be_accepted(pair): |
| 169 | """Somebody claiming to be shop.example asks the supplier to connect, |
| 170 | with their own keys in the profile they carry. The request verifies |
| 171 | against those keys, so it is stored as pending; accepting it fetches |
| 172 | shop.example's real profile, finds a different key, and refuses.""" |
| 173 | network, buyer, seller = pair |
| 174 | from scripts.try_package import Sandbox |
| 175 | from exchange_harness import Box |
| 176 | with Sandbox(["app-exchange"]) as evil_sandbox: |
| 177 | evil = Box(evil_sandbox, "shop.example", "impostor") |
| 178 | evil.add_user("owner", roles=["owner"]) |
| 179 | assert evil.admin("setup", domain="shop.example", company_name="Not The Shop")[0] == 200 |
| 180 | with evil.active(): |
| 181 | base = str(evil.data_dir) |
| 182 | env, sig = xbox._signed_envelope( |
| 183 | base, doc_id="conn-x", doc_type="connection_request", sender="shop.example", |
| 184 | recipient=seller.domain, body={"profile": xbox.profile(base)}) |
| 185 | status, answer = post(network, seller, wire_from(evil, env, sig)) |
| 186 | assert status == 202, answer |
| 187 | [pending] = seller.records("trading_partners") |
| 188 | status, body = seller.admin("accept", partner_id=pending["id"]) |
| 189 | assert status == 409 and body["code"] == "key_mismatch", body |
| 190 | assert seller.records("trading_partners")[0]["status"] == "pending" |
| 191 | |
| 192 | |
| 193 | def test_unanswered_requests_are_bounded(pair, monkeypatch): |
| 194 | network, buyer, seller = pair |
| 195 | monkeypatch.setattr(xbox, "MAX_PENDING_REQUESTS", 0) |
| 196 | status, body = buyer.admin("connect", domain=seller.domain) |
| 197 | assert status == 502 and "429" in body["error"], body |
| 198 | |
| 199 | |
| 200 | # --- a document crossing ----------------------------------------------------------- |
| 201 | |
| 202 | def test_a_purchase_order_is_stored_verbatim_and_answered_with_a_verified_receipt(connected): |
| 203 | network, buyer, seller = connected |
| 204 | order = purchase_order(buyer) |
| 205 | status, sent = buyer.send("orders", order["id"], partner=seller.domain) |
| 206 | assert status == 200 and sent["document_type"] == "purchase_order", sent |
| 207 | [queued] = buyer.records("exchange_outbox") |
| 208 | assert queued["status"] == "queued" |
| 209 | result = buyer.deliver() |
| 210 | assert result["delivered"] == 1, result |
| 211 | [delivered] = buyer.records("exchange_outbox") |
| 212 | assert delivered["status"] == "delivered" and delivered["receipt_verified"] == "true" |
| 213 | [stored] = seller.records("exchange_inbox") |
| 214 | assert stored["document_id"] == sent["document_id"] and stored["integrity"] == "verified" |
| 215 | wire = json.loads(stored["raw_body"]) |
| 216 | assert wire["envelope"]["body"]["lines"][0]["unit_price_minor"] == 1850 |
| 217 | assert wire["envelope"]["body"]["totals"]["payable_minor"] == 22200 |
| 218 | # Both sides hold the same receipt, and it verifies against the seller's key. |
| 219 | receipt = json.loads(delivered["receipt"]) |
| 220 | assert receipt["receipt"] == json.loads(stored["receipt"])["receipt"] |
| 221 | _, profile = seller.call("anon", "GET", "/.well-known/dbbasic") |
| 222 | key = {k["key_id"]: k["public_key"] for k in profile["keys"]}[receipt["signature"]["key_id"]] |
| 223 | assert ex.verify(key, ex.receipt_message(receipt["receipt"]), receipt["signature"]) |
| 224 | # The stored body is the signed document: its signature still verifies. |
| 225 | _, buyer_profile = buyer.call("anon", "GET", "/.well-known/dbbasic") |
| 226 | bkey = buyer_profile["keys"][0]["public_key"] |
| 227 | assert ex.verify(bkey, ex.canonical(wire["envelope"]), wire["signature"]) |
| 228 | |
| 229 | |
| 230 | def test_a_received_document_cannot_be_edited_or_deleted_even_by_an_admin(connected): |
| 231 | network, buyer, seller = connected |
| 232 | buyer.send("orders", purchase_order(buyer)["id"], partner=seller.domain) |
| 233 | buyer.deliver() |
| 234 | [stored] = seller.records("exchange_inbox") |
| 235 | status, body = seller.call("admin", "PUT", f"/collections/exchange_inbox/records/{stored['id']}", |
| 236 | {"raw_body": "{}"}) |
| 237 | assert status == 409, body |
| 238 | status, body = seller.call("admin", "DELETE", f"/collections/exchange_inbox/records/{stored['id']}") |
| 239 | assert status == 409, body |
| 240 | |
| 241 | |
| 242 | def test_a_resend_stores_nothing_and_gets_the_same_receipt(connected): |
| 243 | network, buyer, seller = connected |
| 244 | buyer.send("orders", purchase_order(buyer)["id"], partner=seller.domain) |
| 245 | buyer.deliver() |
| 246 | [row] = buyer.records("exchange_outbox") |
| 247 | stored = json.loads(row["envelope"]) |
| 248 | status, again = post(network, seller, wire_from(buyer, stored["envelope"], stored["signature"])) |
| 249 | assert status == 200 and again["duplicate"] is True |
| 250 | assert again["receipt"] == json.loads(row["receipt"])["receipt"] |
| 251 | assert again["signature"] == json.loads(row["receipt"])["signature"] |
| 252 | assert len(seller.records("exchange_inbox")) == 1 |
| 253 | assert len(seller.records("orders")) == 1, "one purchase order, one draft sale order" |
| 254 | assert len(seller.records("order_lines")) == 1 |
| 255 | |
| 256 | |
| 257 | def test_the_same_id_on_a_different_document_is_refused(connected): |
| 258 | network, buyer, seller = connected |
| 259 | first = envelope_from(buyer, seller.domain, doc_id="doc-same") |
| 260 | assert post(network, seller, wire_from(buyer, first))[0] == 200 |
| 261 | second = envelope_from(buyer, seller.domain, doc_id="doc-same", |
| 262 | body={**first["body"], "number": "PO-OTHER"}) |
| 263 | status, answer = post(network, seller, wire_from(buyer, second)) |
| 264 | assert status == 409 and answer["code"] == "id_reused" |
| 265 | assert len(seller.records("exchange_inbox")) == 1 |
| 266 | |
| 267 | |
| 268 | def test_a_forged_signature_is_refused_and_nothing_is_stored(connected): |
| 269 | network, buyer, seller = connected |
| 270 | wire = wire_from(buyer, envelope_from(buyer, seller.domain)) |
| 271 | value = wire["signature"]["value"] |
| 272 | wire["signature"]["value"] = value[:10] + ("A" if value[10] != "A" else "B") + value[11:] |
| 273 | status, answer = post(network, seller, wire) |
| 274 | assert status == 401 and answer["code"] == "bad_signature" |
| 275 | assert seller.records("exchange_inbox") == [] |
| 276 | |
| 277 | |
| 278 | def test_a_tampered_body_is_refused(connected): |
| 279 | """Somebody between the boxes raises the quantity; the signature was |
| 280 | over the original bytes.""" |
| 281 | network, buyer, seller = connected |
| 282 | order = purchase_order(buyer) |
| 283 | buyer.send("orders", order["id"], partner=seller.domain) |
| 284 | |
| 285 | def tamper(body: bytes) -> bytes: |
| 286 | wire = json.loads(body) |
| 287 | if wire.get("envelope", {}).get("type") == "purchase_order": |
| 288 | wire["envelope"]["body"]["lines"][0]["quantity"] = "1200" |
| 289 | return json.dumps(wire).encode() |
| 290 | |
| 291 | network.tamper = tamper |
| 292 | result = buyer.deliver() |
| 293 | network.tamper = None |
| 294 | assert result["dead"] == 1, result |
| 295 | [row] = buyer.records("exchange_outbox") |
| 296 | assert row["status"] == "dead" and "401" in row["last_error"] |
| 297 | assert seller.records("exchange_inbox") == [] |
| 298 | |
| 299 | |
| 300 | def test_a_captured_delivery_replayed_later_is_refused(connected): |
| 301 | network, buyer, seller = connected |
| 302 | old = ex.utc_stamp(datetime.now(timezone.utc) - timedelta(minutes=30)) |
| 303 | status, answer = post(network, seller, |
| 304 | wire_from(buyer, envelope_from(buyer, seller.domain), sent_at=old)) |
| 305 | assert status == 400 and answer["code"] == "stale_delivery" |
| 306 | assert seller.records("exchange_inbox") == [] |
| 307 | |
| 308 | |
| 309 | def test_a_stranger_is_refused(connected): |
| 310 | network, buyer, seller = connected |
| 311 | from scripts.try_package import Sandbox |
| 312 | from exchange_harness import Box |
| 313 | with Sandbox(["app-exchange"]) as stranger_sandbox: |
| 314 | stranger = network.add(Box(stranger_sandbox, "stranger.example", "stranger")) |
| 315 | stranger.add_user("owner", roles=["owner"]) |
| 316 | assert stranger.admin("setup", domain="stranger.example", company_name="X")[0] == 200 |
| 317 | status, answer = post(network, seller, |
| 318 | wire_from(stranger, envelope_from(stranger, seller.domain))) |
| 319 | assert status == 403 and answer["code"] == "not_a_partner" |
| 320 | assert seller.records("exchange_inbox") == [] |
| 321 | |
| 322 | |
| 323 | def test_a_disconnected_partner_is_refused_immediately(connected): |
| 324 | """Even when the notice never reached it: the buyer still thinks it is |
| 325 | connected and sends, and the seller refuses.""" |
| 326 | network, buyer, seller = connected |
| 327 | [partner] = seller.records("trading_partners") |
| 328 | network.down.add(buyer.domain) |
| 329 | status, body = seller.admin("disconnect", partner_id=partner["id"]) |
| 330 | network.down.discard(buyer.domain) |
| 331 | assert status == 200 and body["notified"] is False, body |
| 332 | buyer.send("orders", purchase_order(buyer)["id"], partner=seller.domain) |
| 333 | result = buyer.deliver() |
| 334 | assert result["dead"] == 1 |
| 335 | [row] = buyer.records("exchange_outbox") |
| 336 | assert "403" in row["last_error"] and "not a connected trading partner" in row["last_error"] |
| 337 | assert seller.records("exchange_inbox") == [] |
| 338 | |
| 339 | |
| 340 | def test_a_document_for_another_box_is_refused(connected): |
| 341 | network, buyer, seller = connected |
| 342 | status, answer = post(network, seller, |
| 343 | wire_from(buyer, envelope_from(buyer, "elsewhere.example"))) |
| 344 | assert status == 400 and answer["code"] == "wrong_recipient" |
| 345 | assert seller.records("exchange_inbox") == [] |
| 346 | |
| 347 | |
| 348 | def test_a_type_the_partner_may_not_send_is_refused(connected): |
| 349 | network, buyer, seller = connected |
| 350 | [partner] = seller.records("trading_partners") |
| 351 | status, body = seller.admin("set_types", partner_id=partner["id"], |
| 352 | receive_types=["remittance", "invoice"]) |
| 353 | assert status == 200, body |
| 354 | buyer.send("orders", purchase_order(buyer)["id"], partner=seller.domain) |
| 355 | buyer.deliver() |
| 356 | [row] = buyer.records("exchange_outbox") |
| 357 | # Refused after the seller verified who sent it: a signed refusal, kept. |
| 358 | assert row["status"] == "refused" and "does not accept purchase_order" in row["last_error"] |
| 359 | assert seller.records("exchange_inbox") == [] |
| 360 | |
| 361 | |
| 362 | def test_key_rotation_keeps_old_signatures_verifying_and_refuses_an_unvouched_key(connected): |
| 363 | network, buyer, seller = connected |
| 364 | buyer.send("orders", purchase_order(buyer, "PO-1")["id"], partner=seller.domain) |
| 365 | buyer.deliver() |
| 366 | status, body = buyer.admin("rotate_key") |
| 367 | assert status == 200, body |
| 368 | _, profile = buyer.call("anon", "GET", "/.well-known/dbbasic") |
| 369 | states = {k["key_id"]: k["status"] for k in profile["keys"]} |
| 370 | assert states == {body["retired"]: "retired", body["active"]: "active"} |
| 371 | assert not ex.has_private_key(buyer.data_dir, body["retired"]) |
| 372 | # A document signed with the new key is accepted: the old key vouched for it. |
| 373 | buyer.send("orders", purchase_order(buyer, "PO-2")["id"], partner=seller.domain) |
| 374 | assert buyer.deliver()["delivered"] == 1 |
| 375 | pinned = {k["key_id"] for k in json.loads(seller.records("trading_partners")[0]["pinned_keys"])} |
| 376 | assert pinned == {body["retired"], body["active"]} |
| 377 | # What the old key signed still verifies against the retired key it lists. |
| 378 | first = [r for r in seller.records("exchange_inbox") if r["key_id"] == body["retired"]][0] |
| 379 | wire = json.loads(first["raw_body"]) |
| 380 | old_public = [k["public_key"] for k in profile["keys"] if k["key_id"] == body["retired"]][0] |
| 381 | assert ex.verify(old_public, ex.canonical(wire["envelope"]), wire["signature"]) |
| 382 | # A key nobody pinned vouched for is refused. |
| 383 | with buyer.active(): |
| 384 | rogue_id, _ = ex.generate_key(buyer.data_dir) |
| 385 | envelope = envelope_from(buyer, seller.domain, doc_id="doc-rogue") |
| 386 | rogue_sig = ex.sign(buyer.data_dir, rogue_id, ex.canonical(envelope)) |
| 387 | status, answer = post(network, seller, wire_from(buyer, envelope, rogue_sig)) |
| 388 | assert status == 403 and answer["code"] == "key_not_pinned" |
| 389 | |
| 390 | |
| 391 | def test_a_partner_that_does_not_answer_is_retried_with_backoff(connected): |
| 392 | network, buyer, seller = connected |
| 393 | buyer.send("orders", purchase_order(buyer)["id"], partner=seller.domain) |
| 394 | network.down.add(seller.domain) |
| 395 | result = buyer.deliver() |
| 396 | assert result["retrying"] == 1, result |
| 397 | [row] = buyer.records("exchange_outbox") |
| 398 | assert row["status"] == "queued" and row["attempts"] == "1" |
| 399 | first_retry = ex.parse_stamp(row["next_attempt_at"]) |
| 400 | assert first_retry > datetime.now(timezone.utc) + timedelta(seconds=50) |
| 401 | assert buyer.deliver()["attempted"] == 0, "not due yet" |
| 402 | with buyer.active(): |
| 403 | xbox.deliver_due(str(buyer.data_dir), now=first_retry + timedelta(seconds=1)) |
| 404 | [row] = buyer.records("exchange_outbox") |
| 405 | assert row["attempts"] == "2" |
| 406 | assert ex.parse_stamp(row["next_attempt_at"]) - first_retry >= timedelta(seconds=119) |
| 407 | network.down.clear() |
| 408 | with buyer.active(): |
| 409 | xbox.deliver_due(str(buyer.data_dir), now=datetime.now(timezone.utc) + timedelta(hours=1)) |
| 410 | [row] = buyer.records("exchange_outbox") |
| 411 | assert row["status"] == "delivered" and len(seller.records("exchange_inbox")) == 1 |
| 412 | |
| 413 | |
| 414 | def test_a_record_is_sent_once_and_only_by_staff_who_may_change_it(connected): |
| 415 | """Sending commits the business, so it is staff's (admins, the owner |
| 416 | role, role:manager); and staff send only what they may update.""" |
| 417 | network, buyer, seller = connected |
| 418 | order = purchase_order(buyer) |
| 419 | status, body = buyer.send("orders", order["id"], user="clerk", partner=seller.domain) |
| 420 | assert status == 403, "a plain registered user is a customer, not staff" |
| 421 | mine = create(buyer, "orders", {"doc_type": "purchase", "number": "PO-C", |
| 422 | "customer_name": "S"}, user="clerk") |
| 423 | status, body = buyer.send("orders", mine["id"], user="clerk", partner=seller.domain) |
| 424 | assert status == 403, "not even an order the customer owns" |
| 425 | assert buyer.records("exchange_outbox") == [] |
| 426 | assert buyer.send("orders", order["id"], partner=seller.domain)[0] == 200 |
| 427 | status, body = buyer.send("orders", order["id"], partner=seller.domain) |
| 428 | assert status == 409 and body["code"] == "already_sent" |
| 429 | |
| 430 | |
| 431 | def test_the_requests_went_over_https_to_the_partners_own_domains(connected): |
| 432 | network, buyer, seller = connected |
| 433 | buyer.send("orders", purchase_order(buyer)["id"], partner=seller.domain) |
| 434 | buyer.deliver() |
| 435 | assert network.requests and all(url.startswith(("https://shop.example/", "https://supplier.example/")) |
| 436 | for _, url in network.requests) |