Files
xenia-pos-cloud/cloud_backend/tests/test_checkout.py
T
bonaminandClaude Opus 5.5 e0158b2c72 test(cloud): the public menu carries each product's allergens (contract C-2, platform-v2 step 6.3)
The snapshot is stored as the site sends it, so `allergens` {key: contains | may_contain}
reaches GET /api/menu/{site} for the QR menu and the order app without a cloud change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 10:04:20 +02:00

143 lines
8.7 KiB
Python

"""The cloud prices online orders itself; sites only touch their own data (plan phase 3S, S.2).
Done when: a tampered price or total is refused, a correct order is stored with the cloud's
prices, and one site can't read or overwrite another's data."""
import json
from test_licensing_v2 import _heartbeat, _new_site
COLA, PIE, SOUP = 11, 12, 13
# online ordering on, any time (no hours), as soon as possible (S.4)
ORDERING = {"services": ["takeaway", "delivery"], "hours": {}, "prep_minutes": 20, "slot_minutes": 15, "days_ahead": 0, "asap": True}
def _h(site):
return {"X-Site-ID": site["site_id"], "X-Site-Key": site["secret_key"]}
def _menu(client, site, numeric):
"""A snapshot as the site pushes it after S.2: prices per sales point, where it's sold."""
snap = {"categories": [{"id": 1, "name": "All", "products": [
{"id": COLA, "name": "Cola 1.5L", "base_price": 3.5, "digital_price": 3.0, "digital_available": True,
"sold_at": ["qr_table", "online_takeaway", "online_delivery"],
"prices": {"qr_table": 3.0, "online_takeaway": 2.5, "online_delivery": 2.8},
"quick_options": [{"id": 7, "name": "Ice", "price": 0.2}]},
{"id": PIE, "name": "Pie", "base_price": 5.0, "digital_price": 5.0, "digital_available": True,
"sold_at": ["qr_table"], "prices": {"qr_table": 5.0}, "quick_options": []},
{"id": SOUP, "name": "Soup", "base_price": 6.0, "digital_price": 6.0, "digital_available": False,
"sold_at": ["qr_table", "online_delivery"], "prices": {"qr_table": 6.0, "online_delivery": 6.0}, "quick_options": []},
]}], "ordering": ORDERING, "timezone": "Europe/Athens"}
r = client.post("/api/menu/sync", headers=_h(site), json={"site_id": numeric, "snapshot_json": json.dumps(snap)})
assert r.status_code == 200, r.text
def _venue(client, admin_h):
site = _new_site(client, admin_h)
numeric = _heartbeat(client, site)["site_numeric_id"]
_menu(client, site, numeric)
return site, numeric
def _order(client, site, order_type="takeaway", **extra):
body = {"order_type": order_type, "customer_name": "Maria", "customer_phone": "6900000000",
"items": [{"product_id": COLA, "quantity": 2}], **extra}
return client.post(f"/api/orders/{site['site_id']}", json=body)
def _code(r):
return r.json()["detail"]["code"]
def test_the_cloud_prices_by_sales_point(client, admin_h):
site, _ = _venue(client, admin_h)
q = lambda t, items: client.post(f"/api/orders/{site['site_id']}/quote", json={"order_type": t, "items": items}) # noqa: E731
take = q("takeaway", [{"product_id": COLA, "quantity": 2, "options": [7]}]).json()
assert (take["sales_point"], take["lines"][0]["unit_price"], take["total"]) == ("online_takeaway", 2.7, 5.4)
assert q("dine_in", [{"product_id": COLA, "quantity": 1}]).json()["total"] == 3.0
assert q("delivery", [{"product_id": COLA, "quantity": 1}]).json()["total"] == 2.8
assert _code(q("takeaway", [{"product_id": PIE, "quantity": 1}])) == "order.item_unavailable" # QR menu only
assert _code(q("delivery", [{"product_id": SOUP, "quantity": 1}])) == "order.item_unavailable" # out of stock now
assert _code(q("takeaway", [{"product_id": 999, "quantity": 1}])) == "order.item_unavailable"
assert _code(q("takeaway", [{"product_id": COLA, "quantity": 0}])) == "order.quantity_invalid"
assert _code(q("takeaway", [{"product_id": COLA, "quantity": 1, "options": [8]}])) == "order.option_unavailable"
assert _code(q("teleport", [{"product_id": COLA, "quantity": 1}])) == "order.type_invalid"
assert _code(q("takeaway", [])) == "order.empty"
def test_a_tampered_order_is_refused_and_a_correct_one_stored_at_the_clouds_prices(client, admin_h):
"""The done-when, first part."""
site, numeric = _venue(client, admin_h)
r = _order(client, site, items=[{"product_id": COLA, "quantity": 2, "unit_price": 0.01}], subtotal=0.02, total=0.02)
assert r.status_code == 409 and _code(r) == "order.price_changed"
assert r.json()["detail"]["params"]["total"] == 5.0 # the current prices come back
assert _code(_order(client, site, total=1.0)) == "order.price_changed"
ok = _order(client, site, total=5.0, delivery_fee=-3, customer_table=" ")
assert ok.status_code == 201, ok.text
pending = client.get(f"/api/orders/pending/{numeric}", headers=_h(site)).json()
stored = next(o for o in pending if o["public_ref"] == ok.json()["public_ref"])
assert (stored["subtotal"], stored["delivery_fee"], stored["total"], stored["sales_point"]) == (5.0, 0.0, 5.0, "online_takeaway")
assert json.loads(stored["items_json"]) == [{"product_id": COLA, "name": "Cola 1.5L", "quantity": 2, "unit_price": 2.5, "options": []}]
# no total sent at all: priced by the cloud anyway (the old menu app's fields are optional)
assert _order(client, site).status_code == 201
def test_delivery_details(client, admin_h):
site, numeric = _venue(client, admin_h) # table orders: tests/test_qr.py (S.7)
assert _code(_order(client, site, "delivery")) == "order.details_missing"
assert _order(client, site, "delivery", customer_address="Main square 3").status_code == 201
def test_a_site_only_touches_its_own_data(client, admin_h):
"""The done-when, second part: a site's key can't read or overwrite another site's data."""
a, a_num = _venue(client, admin_h)
b, b_num = _venue(client, admin_h)
assert _order(client, a).status_code == 201
assert client.get(f"/api/orders/pending/{a_num}", headers=_h(b)).status_code == 403
assert client.post("/api/menu/sync", headers=_h(b), json={"site_id": a_num, "snapshot_json": "{}"}).status_code == 403
assert client.post("/api/remote/snapshot", headers=_h(b), json={"site_id": a_num, "snapshot_json": "{}"}).status_code == 403
# a's menu is intact
assert _order(client, a, total=5.0).status_code == 201
assert client.post("/api/menu/sync", headers=_h(a), json={"site_id": a_num, "snapshot_json": "not json"}).status_code == 422
def test_a_snapshot_from_before_s2_still_prices(client, admin_h):
"""A site that hasn't updated yet pushes digital_price / digital_discount only."""
site = _new_site(client, admin_h)
numeric = _heartbeat(client, site)["site_numeric_id"]
old = {"categories": [{"id": 1, "name": "All", "products": [
{"id": COLA, "name": "Cola", "base_price": 4.0, "digital_price": None, "digital_discount": 25, "digital_available": True,
"quick_options": []}]}]}
client.post("/api/menu/sync", headers=_h(site), json={"site_id": numeric, "snapshot_json": json.dumps(old)})
r = client.post(f"/api/orders/{site['site_id']}/quote", json={"order_type": "dine_in", "items": [{"product_id": COLA, "quantity": 1}]})
assert r.json()["total"] == 3.0
def test_order_numbers_per_site_and_tracking_by_token(client, admin_h):
"""Two venues both have an ORD-0001 (the second used to fail); the customer tracks their
order by its random token, and the bare number no longer identifies it."""
a, _ = _venue(client, admin_h)
b, _ = _venue(client, admin_h)
ra, rb = _order(client, a).json(), _order(client, b).json()
assert ra["public_ref"] == rb["public_ref"] == "ORD-0001"
assert ra["tracking_token"] != rb["tracking_token"] and ra["total"] == 5.0
t = client.get(f"/api/orders/track/{ra['tracking_token']}")
tj = t.json()
assert t.status_code == 200 and (tj["public_ref"], tj["status"], tj["rejection_reason"], tj["total"]) == ("ORD-0001", "pending_acceptance", None, 5.0)
assert tj["lines"] == [{"name": "Cola 1.5L", "quantity": 2, "unit_price": 2.5, "options": []}] and tj["order_type"] == "takeaway"
assert client.get("/api/orders/status/ORD-0001").status_code == 404 # ambiguous now
assert client.get("/api/orders/track/nope").status_code == 404
def test_the_public_menu_carries_each_products_allergens(client, admin_h):
"""Plan step 6.3 (contract C-2): the site sends `allergens` {key: contains | may_contain} per
product; the public menu passes them to the QR menu and the order app as they are."""
site = _new_site(client, admin_h)
numeric = _heartbeat(client, site)["site_numeric_id"]
snap = {"categories": [{"id": 1, "name": "All", "products": [
{"id": PIE, "name": "Pie", "base_price": 5.0, "digital_price": 5.0, "digital_available": True,
"sold_at": ["qr_table"], "prices": {"qr_table": 5.0}, "quick_options": [],
"allergens": {"gluten": "contains", "sesame": "may_contain"}}]}], "ordering": ORDERING, "timezone": "Europe/Athens"}
assert client.post("/api/menu/sync", headers=_h(site), json={"site_id": numeric, "snapshot_json": json.dumps(snap)}).status_code == 200
menu = client.get(f"/api/menu/{site['site_id']}").json()
assert menu["categories"][0]["products"][0]["allergens"] == {"gluten": "contains", "sesame": "may_contain"}