feat(cloud): Alembic, license token v2 with modules, plan presets, module editor

Platform v2 step 1.7 (APPS_PLATFORM D-3, D-20, D-21, D-22, §5.2).
- Alembic replaces create_all + _run_migrations. 0001_cloud_baseline is
  exactly the v1 production schema (incl. the dead menu_hours_en/_gr), checked
  against a frozen fixture; schema.py refuses an unstamped v1 DB, so prod is
  stamped once at cutover (DEPLOYMENT_GUIDE Part 6)
- 0002_v2_licensing: sites.modules (JSON), sites.module_catalog(_at),
  plan_presets; existing sites get every module v1 had (KDS unlimited); the
  same map is seeded as preset "Everything (v1 sites)"; dead columns dropped
- license token v: 2 with a normalized `modules` map
- heartbeat v2: request {version, local_ip, module_catalog} (catalogue stored
  per site); response {license_token, latest_version, waiter_domain,
  site_numeric_id} - the unsigned v1 license fields are gone
- sites API: create with a preset, PUT /{id}/modules (validated),
  POST /{id}/apply-preset/{preset}; /api/presets CRUD
- sysadmin panel: Licensed modules card per site (built from the reported
  catalogue, fallback list), preset picker on register, Presets page
- .dockerignore: local *.db, .env and tests no longer baked into the image
- tests (new): baseline = v1, unstamped refused, stamp+upgrade of a v1 DB,
  drift, token v2 shape/signature, module edits, presets; CL-2/CL-7 rules

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-12 19:27:47 +02:00
co-authored by Claude Opus 5.5
parent 048b46af41
commit 48fd10df77
30 changed files with 1306 additions and 61 deletions
+101
View File
@@ -0,0 +1,101 @@
"""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"}
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