Plan phase 3S, step S.7.
- services/qr.py: the snapshot's `qr` {ordering, tables: [{id, label, token_hash}]} (sha256
of each table's token; the cloud never holds the tokens). A table order (dine_in) needs
the venue's ordering on, the site's menu_mode "order" and a valid table token: otherwise
order.service_unavailable / 403 order.table_invalid (a forged, old or typed-in table is
refused). The order keeps the table's id and label (online_orders.table_id, migration 0006).
- Public menu: ?t=<token> → `table` {id, label} or `table_invalid`; `table_ordering`; `qr`
never leaves the cloud.
- Heartbeat returns `apps_url` (new setting PUBLIC_APPS_URL: where /menu/ and /order/ are
served) so sites can print table QR codes that open the right address.
- Menu app: the table from its QR code (kept for the visit), a "Table 5" banner, ordering
only at a real table (otherwise read-only, with a notice for an invalid code); checkout
without a typed table number; prices are the QR menu's (sales point qr_table — it showed
in-store prices since S.1 removed digital_price) and only products sold there; the
confirmation page for table orders (in the kitchen); the delivery/takeaway leftovers
removed (they have their own app since S.4); test ids for e2e.
- Tests: tests/test_qr.py (3); test_checkout's typed-table case moved there.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
102 lines
4.9 KiB
Python
102 lines
4.9 KiB
Python
"""License token v2, site modules, presets, heartbeat catalogue (plan step 1.7, D-3, D-20, D-21)."""
|
|
import base64
|
|
import json
|
|
|
|
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
|
|
|
|
from conftest import TEST_PUB
|
|
|
|
|
|
def _decode(token):
|
|
body, sig = token.split(".")
|
|
pad = lambda s: s + "=" * (-len(s) % 4) # noqa: E731
|
|
Ed25519PublicKey.from_public_bytes(base64.b64decode(TEST_PUB)).verify(
|
|
base64.urlsafe_b64decode(pad(sig)), body.encode()) # raises if the signature is wrong
|
|
return json.loads(base64.urlsafe_b64decode(pad(body)))
|
|
|
|
|
|
def _new_site(client, admin_h, **extra):
|
|
r = client.post("/api/sites/", headers=admin_h, json={
|
|
"name": "Test", "owner_name": "O", "contact_email": "o@example.com",
|
|
"license_expires_at": "2027-06-01T00:00:00+00:00", **extra})
|
|
assert r.status_code == 201, r.text
|
|
return r.json()
|
|
|
|
|
|
def _heartbeat(client, site, catalog=()):
|
|
r = client.post("/api/heartbeat/", headers={"X-Site-ID": site["site_id"], "X-Site-Key": site["secret_key"]},
|
|
json={"version": "2.0.0", "local_ip": "192.168.1.10", "module_catalog": list(catalog)})
|
|
assert r.status_code == 200, r.text
|
|
return r.json()
|
|
|
|
|
|
def test_normalize_modules():
|
|
from services.license_token import normalize_modules
|
|
assert normalize_modules({"kds": {"limits": {"devices": "2"}, "junk": 1}, "tabs": None}) == \
|
|
{"kds": {"limits": {"devices": 2}}, "tabs": {}}
|
|
|
|
|
|
def test_new_site_gets_default_preset_and_v2_token(client, admin_h):
|
|
site = _new_site(client, admin_h)
|
|
assert "ordering" in site["modules"]
|
|
hb = _heartbeat(client, site)
|
|
assert set(hb) == {"license_token", "latest_version", "waiter_domain", "site_numeric_id", "apps_url"}
|
|
p = _decode(hb["license_token"])
|
|
assert p["v"] == 2 and p["site_id"] == site["site_id"] and p["modules"]["kds"] == {"limits": {"devices": None}}
|
|
assert {"active", "locked", "expires_at", "issued_at"} <= set(p)
|
|
|
|
|
|
def test_site_without_preset_licenses_nothing(client, admin_h):
|
|
site = _new_site(client, admin_h, preset=None)
|
|
assert _decode(_heartbeat(client, site)["license_token"])["modules"] == {}
|
|
|
|
|
|
def test_editing_modules_changes_the_next_token(client, admin_h):
|
|
site = _new_site(client, admin_h)
|
|
r = client.put(f"/api/sites/{site['site_id']}/modules", headers=admin_h,
|
|
json={"modules": {"ordering": {}, "kds": {"limits": {"devices": 2}},
|
|
"caller_id": {"exp": "2027-01-31T00:00:00+00:00"}}})
|
|
assert r.status_code == 200, r.text
|
|
mods = _decode(_heartbeat(client, site)["license_token"])["modules"]
|
|
assert mods == {"ordering": {}, "kds": {"limits": {"devices": 2}},
|
|
"caller_id": {"exp": "2027-01-31T00:00:00+00:00"}}
|
|
|
|
|
|
def test_bad_module_maps_are_refused(client, admin_h):
|
|
site = _new_site(client, admin_h)
|
|
for bad in ({"kds": {"limits": {"devices": -1}}}, {"kds": {"limits": {"devices": "x"}}},
|
|
{"caller_id": {"exp": "tomorrow"}}):
|
|
assert client.put(f"/api/sites/{site['site_id']}/modules", headers=admin_h, json={"modules": bad}).status_code == 422
|
|
|
|
|
|
def test_heartbeat_stores_the_module_catalogue(client, admin_h):
|
|
site = _new_site(client, admin_h)
|
|
catalog = [{"key": "kds", "kind": "app", "name": "Kitchen display", "depends": ["ordering"],
|
|
"limits": [{"key": "devices", "free": 0, "licensed_default": 1}]}]
|
|
_heartbeat(client, site, catalog)
|
|
got = client.get(f"/api/sites/{site['site_id']}", headers=admin_h).json()
|
|
assert got["module_catalog"] == catalog and got["module_catalog_at"]
|
|
|
|
|
|
def test_presets_crud_and_apply(client, admin_h):
|
|
r = client.post("/api/presets/", headers=admin_h, json={"name": "Cafe basic", "description": "POS + KDS",
|
|
"modules": {"ordering": {}, "kds": {"limits": {"devices": 1}}}})
|
|
assert r.status_code == 201, r.text
|
|
preset = r.json()
|
|
names = [p["name"] for p in client.get("/api/presets/", headers=admin_h).json()]
|
|
assert "Cafe basic" in names and "Everything (v1 sites)" in names
|
|
site = _new_site(client, admin_h, preset="Cafe basic")
|
|
assert set(site["modules"]) == {"ordering", "kds"}
|
|
site2 = _new_site(client, admin_h)
|
|
applied = client.post(f"/api/sites/{site2['site_id']}/apply-preset/{preset['id']}", headers=admin_h).json()
|
|
assert applied["modules"] == {"ordering": {}, "kds": {"limits": {"devices": 1}}}
|
|
assert client.post("/api/presets/", headers=admin_h, json={"name": "Cafe basic", "modules": {}}).status_code == 409
|
|
assert client.delete(f"/api/presets/{preset['id']}", headers=admin_h).status_code == 204
|
|
|
|
|
|
def test_unknown_preset_on_create(client, admin_h):
|
|
r = client.post("/api/sites/", headers=admin_h, json={
|
|
"name": "T", "owner_name": "O", "contact_email": "o@x", "license_expires_at": "2027-06-01T00:00:00+00:00",
|
|
"preset": "Nope"})
|
|
assert r.status_code == 400
|