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_money_gates_fulfilment_lines.py · 8.5 KB · 174 lines
| 1 | """The lines of signed-for receipts and shipped shipments: a settled |
| 2 | parent keeps its lines, and the ordinary moves around it still work. |
| 3 | |
| 4 | tests/test_findings_money_gates.py holds the reproductions (a line moved |
| 5 | out of a received receipt, or a shipped shipment, and then edited). This |
| 6 | file is the rest of the change: hook_receipt_lines and hook_shipment_lines |
| 7 | check the parent a line is ON as well as the one it is going to, run on |
| 8 | delete, and refuse when a parent that exists cannot be read. |
| 9 | """ |
| 10 | |
| 11 | import pytest |
| 12 | |
| 13 | import object_records |
| 14 | from test_findings_money_gates import ( # noqa: F401 (fresh_handler_index is autouse) |
| 15 | fresh_handler_index, fulfilment_site, must, ok, received_delivery, shipped_parcel, |
| 16 | ) |
| 17 | |
| 18 | |
| 19 | def refused(result, *, code, status=409): |
| 20 | got, body = result |
| 21 | assert got == status and body.get("code") == code, (got, body) |
| 22 | return body |
| 23 | |
| 24 | |
| 25 | # --- receipt lines ------------------------------------------------------------------- |
| 26 | |
| 27 | |
| 28 | def test_a_line_moves_freely_between_two_open_receipts(tmp_path, monkeypatch): |
| 29 | """The both-parents check must not have frozen the ordinary case: the |
| 30 | clerk put the line on the wrong (still open) receipt.""" |
| 31 | site = fulfilment_site(tmp_path, monkeypatch) |
| 32 | ok(site.post("orders", "ann", {"id": "po1", "doc_type": "purchase", "number": "PO-1", |
| 33 | "customer_name": "Supplier"}), "purchase order") |
| 34 | ok(site.post("order_lines", "ann", {"id": "pol1", "order_id": "po1", "description": "mugs", |
| 35 | "quantity": "10", "unit_price_cents": "500"}), "po line") |
| 36 | ok(site.post("receipts", "ann", {"id": "rc1", "order_id": "po1"}), "receipt") |
| 37 | ok(site.post("receipts", "ann", {"id": "rc2", "order_id": "po1"}), "second receipt") |
| 38 | ok(site.post("receipt_lines", "ann", {"id": "rl1", "receipt_id": "rc1", |
| 39 | "order_line_id": "pol1", "quantity_received": "4"}), |
| 40 | "line") |
| 41 | |
| 42 | ok(site.put("receipt_lines", "rl1", "ann", {"receipt_id": "rc2"}), "move between open receipts") |
| 43 | ok(site.put("receipt_lines", "rl1", "ann", {"quantity_received": "6"}), "and edit it there") |
| 44 | |
| 45 | stored = site.get("receipt_lines", "rl1") |
| 46 | assert (stored["receipt_id"], stored["quantity_received"]) == ("rc2", "6") |
| 47 | |
| 48 | |
| 49 | def test_a_line_cannot_be_moved_into_a_received_receipt(tmp_path, monkeypatch): |
| 50 | """The direction the hook always checked, still checked.""" |
| 51 | site = fulfilment_site(tmp_path, monkeypatch) |
| 52 | received_delivery(site) |
| 53 | ok(site.post("order_lines", "ann", {"id": "pol2", "order_id": "po1", "description": "lids", |
| 54 | "quantity": "5", "unit_price_cents": "100"}), "po line") |
| 55 | ok(site.post("receipt_lines", "ann", {"id": "rl2", "receipt_id": "rc2", |
| 56 | "order_line_id": "pol2", "quantity_received": "5"}), |
| 57 | "line on the open receipt") |
| 58 | |
| 59 | body = refused(site.put("receipt_lines", "rl2", "ann", {"receipt_id": "rc1"}), |
| 60 | code="hook_rejected") |
| 61 | |
| 62 | assert "Receipt rc1 is already received" in body["error"] |
| 63 | assert site.get("receipt_lines", "rl2")["receipt_id"] == "rc2" |
| 64 | |
| 65 | |
| 66 | def test_the_refusal_names_the_receipt_the_line_is_leaving(tmp_path, monkeypatch): |
| 67 | """Somebody told "no" needs to know which of the two receipts is the |
| 68 | settled one.""" |
| 69 | site = fulfilment_site(tmp_path, monkeypatch) |
| 70 | received_delivery(site) |
| 71 | |
| 72 | body = refused(site.put("receipt_lines", "rl1", "ann", {"receipt_id": "rc2"}), |
| 73 | code="hook_rejected") |
| 74 | |
| 75 | assert "Receipt rc1 is already received" in body["error"] |
| 76 | |
| 77 | |
| 78 | def test_the_admin_token_cannot_edit_or_delete_a_signed_for_line(tmp_path, monkeypatch): |
| 79 | """No rule grants a signed-in user delete on receipt_lines, which is |
| 80 | what protected them before the hook ran on delete. The admin token is |
| 81 | not bound by rules, and a settled delivery is settled for it too.""" |
| 82 | site = fulfilment_site(tmp_path, monkeypatch) |
| 83 | received_delivery(site) |
| 84 | ok(site.post("receipt_lines", "ann", {"id": "rl-open", "receipt_id": "rc2", |
| 85 | "order_line_id": "pol1", "quantity_received": "0", |
| 86 | "discrepancy_note": "nothing in this carton"}), |
| 87 | "a line on the open receipt") |
| 88 | |
| 89 | refused(site.put("receipt_lines", "rl1", "admin", {"quantity_received": "1"}), |
| 90 | code="hook_rejected") |
| 91 | refused(site.delete("receipt_lines", "rl1", "admin"), code="hook_rejected") |
| 92 | assert site.get("receipt_lines", "rl1")["quantity_received"] == "10" |
| 93 | |
| 94 | ok(site.delete("receipt_lines", "rl-open", "admin"), "a line of an open receipt") |
| 95 | assert not site.exists("receipt_lines", "rl-open") |
| 96 | |
| 97 | |
| 98 | def test_the_receiving_writer_still_adds_lines_at_the_storage_level(tmp_path, monkeypatch): |
| 99 | """action_receive_goods writes receipt_lines through object_records and |
| 100 | carries its own checks; the hook does not gate it.""" |
| 101 | site = fulfilment_site(tmp_path, monkeypatch) |
| 102 | received_delivery(site) |
| 103 | |
| 104 | object_records.create_collection_record( |
| 105 | "receipt_lines", {"id": "rl-sys", "receipt_id": "rc2", "order_line_id": "pol1", |
| 106 | "quantity_received": "0", "discrepancy_note": "short", "owner_id": "ann"}, |
| 107 | base_dir=site.data, actor="action_receive_goods") |
| 108 | assert site.exists("receipt_lines", "rl-sys") |
| 109 | |
| 110 | |
| 111 | # --- shipment lines ------------------------------------------------------------------ |
| 112 | |
| 113 | |
| 114 | def test_a_line_moves_freely_between_two_open_shipments(tmp_path, monkeypatch): |
| 115 | site = fulfilment_site(tmp_path, monkeypatch) |
| 116 | ok(site.post("orders", "ann", {"id": "so1", "doc_type": "sale", "number": "SO-1", |
| 117 | "customer_name": "Customer"}), "sales order") |
| 118 | ok(site.post("order_lines", "ann", {"id": "sol1", "order_id": "so1", "description": "mugs", |
| 119 | "quantity": "3", "unit_price_cents": "900"}), "so line") |
| 120 | ok(site.post("shipments", "ann", {"id": "sh1", "order_id": "so1"}), "shipment") |
| 121 | ok(site.post("shipments", "ann", {"id": "sh2", "order_id": "so1"}), "second shipment") |
| 122 | ok(site.post("shipment_lines", "ann", {"id": "sl1", "shipment_id": "sh1", |
| 123 | "order_line_id": "sol1", "quantity": "2"}), "line") |
| 124 | |
| 125 | ok(site.put("shipment_lines", "sl1", "ann", {"shipment_id": "sh2"}), "move between open boxes") |
| 126 | ok(site.put("shipment_lines", "sl1", "ann", {"quantity": "3"}), "and edit it there") |
| 127 | |
| 128 | stored = site.get("shipment_lines", "sl1") |
| 129 | assert (stored["shipment_id"], stored["quantity"]) == ("sh2", "3") |
| 130 | |
| 131 | |
| 132 | def test_the_refusal_names_the_shipment_the_line_is_leaving_and_admin_is_bound_too( |
| 133 | tmp_path, monkeypatch): |
| 134 | site = fulfilment_site(tmp_path, monkeypatch) |
| 135 | shipped_parcel(site) |
| 136 | |
| 137 | body = refused(site.put("shipment_lines", "sl1", "ann", {"shipment_id": "sh2"}), |
| 138 | code="hook_rejected") |
| 139 | assert "Shipment sh1 is already shipped" in body["error"] |
| 140 | |
| 141 | refused(site.put("shipment_lines", "sl1", "admin", {"shipment_id": "sh2"}), |
| 142 | code="hook_rejected") |
| 143 | refused(site.delete("shipment_lines", "sl1", "admin"), code="hook_rejected") |
| 144 | stored = site.get("shipment_lines", "sl1") |
| 145 | assert (stored["shipment_id"], stored["quantity"]) == ("sh1", "3") |
| 146 | |
| 147 | |
| 148 | @pytest.mark.parametrize("collection,parent,record_id,change,build", [ |
| 149 | ("receipt_lines", "receipts", "rl1", {"quantity_received": "1"}, received_delivery), |
| 150 | ("shipment_lines", "shipments", "sl1", {"quantity": "1"}, shipped_parcel), |
| 151 | ]) |
| 152 | def test_a_line_whose_parent_cannot_be_read_is_refused(tmp_path, monkeypatch, collection, parent, |
| 153 | record_id, change, build): |
| 154 | """Both hooks answered None -- allow -- for ANY failure to load the |
| 155 | parent, meaning "relation validation owns a missing parent". A parent |
| 156 | that exists and cannot be read is not missing: the settled gate must |
| 157 | not open because it could not look.""" |
| 158 | site = fulfilment_site(tmp_path, monkeypatch) |
| 159 | build(site) |
| 160 | before = site.get(collection, record_id) |
| 161 | real = object_records.get_collection_record |
| 162 | |
| 163 | def get(name, *args, **kwargs): |
| 164 | if name == parent: |
| 165 | raise OSError(f"simulated: {parent} is unreadable") |
| 166 | return real(name, *args, **kwargs) |
| 167 | |
| 168 | monkeypatch.setattr(object_records, "get_collection_record", get) |
| 169 | status, body = site.put(collection, record_id, "ann", change) |
| 170 | monkeypatch.setattr(object_records, "get_collection_record", real) |
| 171 | |
| 172 | must(body.get("code") == "hook_rejected", f"not refused by the hook: {status} {body}") |
| 173 | assert "could not be read" in body["error"] |
| 174 | assert site.get(collection, record_id) == before |