Files
xenia-pos-cloud/cloud_backend/tests/test_fees.py
T
bonaminandClaude Opus 5.5 b25bfcda3e feat(cloud): the online ordering app (takeaway, delivery) with hours, time slots and rate limits (platform-v2 3S, S.4)
- connect_frontend/order-app at /order/{site}: menu per service at that service's prices,
  extras, basket (kept per tab), checkout priced by the cloud (fee, area by list or
  postcode, minimum), name/phone/address/notes (remembered), as soon as possible or a time
  slot, tracking page with steps, lines and totals; en/el; tokens in index.css. Logic in
  src/logic.js, unit-tested; built and served by the connect image (npm test in the build).
- services/ordering.py: the venue's services, hours (past midnight), preparation, slot grid,
  days ahead, ASAP; takeaway/delivery orders checked (order.service_unavailable,
  order.closed, order.time_invalid) and need a phone; GET /api/orders/{site}/slots; the
  public menu carries the ordering summary.
- services/ratelimit.py: per-caller limits on quote, order and track (429 rate.limited).
- online_orders.requested_for (0003, unreleased) stored as UTC; every datetime sent with its
  offset (the database keeps no zone: a 12:00 Athens slot came back as 12:00 unzoned).
- Tracking returns what was ordered, the totals, the type and the time.
- Tests: test_ordering.py (5), checkout/fees updated; order-app unit (4).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:39:00 +02:00

87 lines
4.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Delivery fees at checkout (plan phase 3S, step S.3).
The engine (services/fees.py) is the site's file; both repos run the same cases
(tests/fixtures/fee_cases.json, identical in both; the site's tests check the copies match).
Done when (cloud half): the cloud charges the venue's fee and refuses an order below the minimum."""
import json
from datetime import datetime, timezone
from pathlib import Path
import pytest
from test_checkout import COLA, ORDERING, _code, _h, _menu, _order
from test_licensing_v2 import _heartbeat, _new_site
CASES = json.loads((Path(__file__).resolve().parent / "fixtures" / "fee_cases.json").read_text(encoding="utf-8"))
@pytest.mark.parametrize("case", CASES["cases"], ids=[c["name"] for c in CASES["cases"]])
def test_shared_fee_cases(case):
from services.fees import compute
got = compute(case["rules"], CASES["areas"], **case["ctx"])
for key, value in case["expect"].items():
assert got[key] == value, (key, got)
FEES = {"areas": [{"id": 1, "name": "Centre", "postcodes": ["24100"], "is_active": True},
{"id": 2, "name": "Far", "postcodes": ["24131"], "is_active": True}],
"rules": [{"id": 1, "name": "Minimum", "action": "minimum", "value": 5, "sales_points": ["online_delivery"]},
{"id": 2, "name": "Base", "action": "set", "value": 2, "sales_points": ["online_delivery"]},
{"id": 3, "name": "Far", "area_ids": [2], "action": "add", "value": 1.5, "sales_points": ["online_delivery"]}]}
def _venue_with_fees(client, admin_h):
site = _new_site(client, admin_h)
numeric = _heartbeat(client, site)["site_numeric_id"]
_menu(client, site, numeric)
# the same menu plus the venue's fees, as the site pushes them since S.3
menu = client.get(f"/api/menu/{site['site_id']}").json()
snap = {"categories": menu["categories"], "fees": FEES, "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
return site, numeric
def _quote(client, site, **body):
return client.post(f"/api/orders/{site['site_id']}/quote", json={"order_type": "delivery", **body})
def test_the_cloud_charges_the_venues_fee(client, admin_h):
site, numeric = _venue_with_fees(client, admin_h)
q = _quote(client, site, items=[{"product_id": COLA, "quantity": 2}], area_id=1).json() # 2 × 2.8 delivery price
assert (q["subtotal"], q["delivery_fee"], q["total"], q["area_name"], q["fee_rules"]) == (5.6, 2.0, 7.6, "Centre", ["Base"])
q = _quote(client, site, items=[{"product_id": COLA, "quantity": 2}], postcode="241 31").json()
assert (q["delivery_fee"], q["area_name"]) == (3.5, "Far")
# the order: the fee is the cloud's, whatever the page sends; the area is stored
assert _code(_order(client, site, "delivery", customer_address="Main sq. 3", area_id=1, delivery_fee=0, total=5.6)) == "order.price_changed"
ok = _order(client, site, "delivery", customer_address="Main sq. 3", area_id=1, total=7.6)
assert ok.status_code == 201, ok.text
stored = next(o for o in client.get(f"/api/orders/pending/{numeric}", headers=_h(site)).json() if o["public_ref"] == ok.json()["public_ref"])
assert (stored["delivery_fee"], stored["total"], stored["delivery_area"]) == (2.0, 7.6, "Centre")
# takeaway: the delivery rules don't apply
assert client.post(f"/api/orders/{site['site_id']}/quote", json={"order_type": "takeaway", "items": [{"product_id": COLA, "quantity": 1}]}).json()["delivery_fee"] == 0
def test_minimum_and_area_are_refused(client, admin_h):
site, _ = _venue_with_fees(client, admin_h)
r = _quote(client, site, items=[{"product_id": COLA, "quantity": 1}], area_id=1)
assert r.status_code == 409 and _code(r) == "order.minimum_not_met"
assert r.json()["detail"]["params"] == {"minimum": 5.0, "subtotal": 2.8}
assert _code(_order(client, site, "delivery", customer_address="x", area_id=1, items=[{"product_id": COLA, "quantity": 1}])) == "order.minimum_not_met"
assert _code(_quote(client, site, items=[{"product_id": COLA, "quantity": 2}], postcode="10431")) == "order.area_not_served"
assert _code(_quote(client, site, items=[{"product_id": COLA, "quantity": 2}])) == "order.area_not_served"
def test_the_public_menu_lists_areas_but_not_the_rules(client, admin_h):
site, _ = _venue_with_fees(client, admin_h)
menu = client.get(f"/api/menu/{site['site_id']}").json()
assert menu["delivery_areas"] == [{"id": 1, "name": "Centre"}, {"id": 2, "name": "Far"}]
assert "fees" not in menu
def test_the_venue_clock():
from services.checkout import venue_clock
at = datetime(2026, 11, 15, 21, 30, tzinfo=timezone.utc) # Sunday 23:30 in Athens (UTC+2)
assert venue_clock({"timezone": "Europe/Athens"}, at) == (6, "23:30")
assert venue_clock({"timezone": "Nowhere/Bad"}, at) == (6, "23:30")