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_stock_moves.py · 7.9 KB · 176 lines
| 1 | """stock_moves after they are written: the ledger is append-only for the |
| 2 | people who use it, repairable by an admin, and untouched for the trusted |
| 3 | writers that feed it. |
| 4 | |
| 5 | tests/test_findings_money_gates.py holds the reproductions (a purchase |
| 6 | edited to -1000, a write-off turned into a receipt, a move deleted). This |
| 7 | file states the position hook_stock_moves now takes: |
| 8 | |
| 9 | * somebody who is not an admin can change nothing about a move and |
| 10 | cannot delete one -- the correction is a compensating move; |
| 11 | * an admin can do both, and an admin's edit still passes the shape gates |
| 12 | (positive quantity, a loss leaves a location and goes nowhere); |
| 13 | * receipt posting, order fulfilment, counts and returns write through |
| 14 | object_records and are not gated by the hook. |
| 15 | """ |
| 16 | |
| 17 | from decimal import Decimal |
| 18 | |
| 19 | import pytest |
| 20 | |
| 21 | import object_records |
| 22 | from test_findings_money_gates import ( # noqa: F401 (fresh_handler_index is autouse) |
| 23 | fresh_handler_index, move, ok, on_hand, stock_site, |
| 24 | ) |
| 25 | |
| 26 | |
| 27 | def hook_refused(result, status=409): |
| 28 | got, body = result |
| 29 | assert got == status and body.get("code") == "hook_rejected", (got, body) |
| 30 | return body["error"] |
| 31 | |
| 32 | |
| 33 | def validation_refused(result): |
| 34 | """The schema's own field rules (`exclusive_min`, `required_when`): the |
| 35 | ordinary validation 400, which carries no code, and the same for every |
| 36 | writer, not only the ones that pass a hook.""" |
| 37 | got, body = result |
| 38 | assert got == 400 and body.get("code") is None, (got, body) |
| 39 | return body["error"] |
| 40 | |
| 41 | |
| 42 | def forbidden(result): |
| 43 | """The ledger is append-only for the person who recorded a move: the |
| 44 | rules grant create and read only and deny the owner fallback for update |
| 45 | and delete. That is a permission refusal now, not a hook one.""" |
| 46 | got, body = result |
| 47 | assert got == 403 and body.get("code") == "forbidden", (got, body) |
| 48 | |
| 49 | |
| 50 | @pytest.mark.parametrize("change", [ |
| 51 | {"quantity": "9"}, |
| 52 | {"quantity": "11"}, |
| 53 | {"product_id": "other"}, |
| 54 | {"to_location_id": "back"}, |
| 55 | {"from_location_id": "shelf"}, |
| 56 | {"unit_cost_cents": "1"}, |
| 57 | {"reason": "adjustment"}, |
| 58 | {"reference": "orders/somebody-elses"}, |
| 59 | {"occurred_at": "2020-01-01T00:00:00Z"}, |
| 60 | ], ids=lambda change: next(iter(change))) |
| 61 | def test_the_owner_cannot_edit_any_field_of_a_stock_move(tmp_path, monkeypatch, change): |
| 62 | site = stock_site(tmp_path, monkeypatch) |
| 63 | ok(site.post("products", "ann", {"id": "other", "name": "Other"}), "second product") |
| 64 | ok(site.post("locations", "ann", {"id": "back", "name": "Back room"}), "second location") |
| 65 | ok(move(site, "m1", to_location_id="shelf", quantity="10", unit_cost_cents="800", |
| 66 | reason="purchase"), "purchase") |
| 67 | before = site.get("stock_moves", "m1") |
| 68 | |
| 69 | forbidden(site.put("stock_moves", "m1", "ann", change)) |
| 70 | |
| 71 | assert site.get("stock_moves", "m1") == before |
| 72 | assert on_hand(site) == 10 |
| 73 | |
| 74 | |
| 75 | def test_the_owner_cannot_delete_a_move(tmp_path, monkeypatch): |
| 76 | site = stock_site(tmp_path, monkeypatch) |
| 77 | ok(move(site, "m1", to_location_id="shelf", quantity="10", reason="purchase"), "purchase") |
| 78 | |
| 79 | forbidden(site.delete("stock_moves", "m1", "ann")) |
| 80 | |
| 81 | assert site.exists("stock_moves", "m1") and on_hand(site) == 10 |
| 82 | |
| 83 | |
| 84 | def test_the_correction_for_a_wrong_move_is_a_compensating_move(tmp_path, monkeypatch): |
| 85 | """What the refusal tells the owner to do, done: ten were booked in and |
| 86 | only eight arrived, so two go back out as an adjustment.""" |
| 87 | site = stock_site(tmp_path, monkeypatch) |
| 88 | ok(move(site, "m1", to_location_id="shelf", quantity="10", reason="purchase"), "purchase") |
| 89 | forbidden(site.put("stock_moves", "m1", "ann", {"quantity": "8"})) |
| 90 | |
| 91 | ok(move(site, "m2", from_location_id="shelf", quantity="2", reason="adjustment"), "correction") |
| 92 | |
| 93 | assert on_hand(site) == 8 |
| 94 | assert [row["id"] for row in site.rows("stock_moves")] == ["m1", "m2"] |
| 95 | |
| 96 | |
| 97 | def test_an_admin_can_repair_a_move_but_not_into_a_shape_a_create_would_refuse(tmp_path, monkeypatch): |
| 98 | site = stock_site(tmp_path, monkeypatch) |
| 99 | ok(move(site, "m1", to_location_id="shelf", quantity="10", unit_cost_cents="800", |
| 100 | reason="purchase"), "purchase") |
| 101 | ok(move(site, "m2", from_location_id="shelf", quantity="1", reason="waste"), "waste") |
| 102 | |
| 103 | ok(site.put("stock_moves", "m1", "admin", {"quantity": "8"}), "admin corrects the count") |
| 104 | assert on_hand(site) == 7 |
| 105 | |
| 106 | error = validation_refused(site.put("stock_moves", "m1", "admin", {"quantity": "-1000"})) |
| 107 | assert "'quantity' must be greater than 0" in error |
| 108 | error = hook_refused(site.put("stock_moves", "m2", "admin", {"to_location_id": "shelf"}), |
| 109 | status=400) |
| 110 | assert "cannot have a destination" in error |
| 111 | error = validation_refused(site.put("stock_moves", "m2", "admin", {"from_location_id": ""})) |
| 112 | assert "'from_location_id' is required when reason is one of waste" in error |
| 113 | assert Decimal(site.get("stock_moves", "m1")["quantity"]) == 8 |
| 114 | assert site.get("stock_moves", "m2")["to_location_id"] == "" |
| 115 | |
| 116 | |
| 117 | def test_an_admin_edit_never_restamps_the_cost(tmp_path, monkeypatch): |
| 118 | """The cost is a point-in-time fact stamped at the moment of movement. |
| 119 | An admin correcting a quantity later must not re-price the move at |
| 120 | today's product cost.""" |
| 121 | site = stock_site(tmp_path, monkeypatch) |
| 122 | ok(move(site, "m1", to_location_id="shelf", quantity="10", reason="purchase"), "purchase") |
| 123 | assert site.get("stock_moves", "m1")["unit_cost_cents"] == "500" |
| 124 | ok(site.put("products", "mug", "ann", {"cost_cents": "900"}), "the product is repriced") |
| 125 | |
| 126 | ok(site.put("stock_moves", "m1", "admin", {"quantity": "8"}), "admin corrects the count") |
| 127 | |
| 128 | assert site.get("stock_moves", "m1")["unit_cost_cents"] == "500" |
| 129 | |
| 130 | |
| 131 | def test_an_admin_can_delete_a_move(tmp_path, monkeypatch): |
| 132 | site = stock_site(tmp_path, monkeypatch) |
| 133 | ok(move(site, "m1", to_location_id="shelf", quantity="10", reason="purchase"), "purchase") |
| 134 | |
| 135 | ok(site.delete("stock_moves", "m1", "admin"), "admin delete") |
| 136 | |
| 137 | assert not site.exists("stock_moves", "m1") and on_hand(site) == 0 |
| 138 | |
| 139 | |
| 140 | def test_storage_level_writers_still_write_moves(tmp_path, monkeypatch): |
| 141 | """receipt_posting, order_fulfillment, apply_count and |
| 142 | disposition_return create moves through object_records. No hook runs |
| 143 | for them, so the freeze cannot have reached them.""" |
| 144 | site = stock_site(tmp_path, monkeypatch) |
| 145 | object_records.create_collection_record( |
| 146 | "stock_moves", {"id": "m-recv", "product_id": "mug", "to_location_id": "shelf", |
| 147 | "quantity": "6", "unit_cost_cents": "800", "reason": "purchase", |
| 148 | "reference": "receipts/rc1", "owner_id": "ann"}, |
| 149 | base_dir=site.data, actor="receipt_posting") |
| 150 | object_records.create_collection_record( |
| 151 | "stock_moves", {"id": "m-sale", "product_id": "mug", "from_location_id": "shelf", |
| 152 | "quantity": "2", "unit_cost_cents": "800", "reason": "sale", |
| 153 | "reference": "shipments/sh1", "owner_id": "ann"}, |
| 154 | base_dir=site.data, actor="order_fulfillment") |
| 155 | |
| 156 | assert on_hand(site) == 4 |
| 157 | |
| 158 | |
| 159 | def test_a_move_whose_product_cannot_be_read_is_refused_not_left_uncosted(tmp_path, monkeypatch): |
| 160 | """The other fallback in the stamp: products could not be READ (as |
| 161 | opposed to: no such product, which relation validation reports). It |
| 162 | used to return None and let the move land with no cost at all.""" |
| 163 | site = stock_site(tmp_path, monkeypatch) |
| 164 | real = object_records.get_collection_record |
| 165 | |
| 166 | def get(collection, record_id, *args, **kwargs): |
| 167 | if collection == "products": |
| 168 | raise OSError("simulated: products is unreadable") |
| 169 | return real(collection, record_id, *args, **kwargs) |
| 170 | |
| 171 | monkeypatch.setattr(object_records, "get_collection_record", get) |
| 172 | status, body = move(site, "m1", to_location_id="shelf", quantity="10", reason="purchase") |
| 173 | monkeypatch.setattr(object_records, "get_collection_record", real) |
| 174 | |
| 175 | assert status >= 400 and "could not be read" in body.get("error", ""), (status, body) |
| 176 | assert not site.exists("stock_moves", "m1") |