"""The customer display (plan step 3.4): a POS pushes what its customer sees, only its own displays hear it; the idle screen is the venue's design (name, logo, both, blank, an image, a slideshow, a QR code), with images the venue uploads.""" import io import pytest from tests.test_live import err, owner_h, pair # noqa: F401 (fixtures) PNG = (b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89" b"\x00\x00\x00\rIDATx\x9cc\xf8\x0f\x00\x00\x01\x01\x00\x05\x18\xd8N\x00\x00\x00\x00IEND\xaeB`\x82") @pytest.fixture(autouse=True) def media_dir(tmp_path, monkeypatch): import routers.customer_display as cd monkeypatch.setattr(cd, "MEDIA_DIR", str(tmp_path / "media")) cd._last.clear() return tmp_path / "media" @pytest.fixture def counters(client, owner_h, pair): """Two counters, each with its own customer display.""" out = {} for n in (1, 2): pos_token, pos = pair("pos_counter", f"Till {n}") disp_token, disp = pair("customer_display", f"Display {n}", linked_device_id=pos.json()["id"]) assert disp.status_code == 201, disp.text out[n] = {"pos": {"X-Xenia-Device": pos_token}, "display": {"X-Xenia-Device": disp_token}, "pos_id": pos.json()["id"], "display_id": disp.json()["id"]} return out @pytest.fixture def sent(monkeypatch): import routers.customer_display as cd got = [] real = cd.broadcast_sync monkeypatch.setattr(cd, "broadcast_sync", lambda kind, data, **kw: (got.append((kind, data, kw)), real(kind, data, **kw))) return got CART = {"mode": "cart", "lines": [{"name": "Freddo", "quantity": 2, "total": 6.0}, {"name": "Toast", "quantity": 1, "total": 3.5, "options": ["no ham"]}], "total": 9.5} def test_a_sale_shows_only_on_its_own_display(client, counters, sent): one, two = counters[1], counters[2] assert client.post("/api/display/state", headers=one["pos"], json=CART).status_code == 204 assert [(k, kw["device_ids"]) for k, _, kw in sent] == [("display.state", [one["display_id"]])] s1 = client.get("/api/display/state", headers=one["display"]).json() assert s1["linked"] is True and s1["state"]["mode"] == "cart" and s1["state"]["total"] == 9.5 assert [line["name"] for line in s1["state"]["lines"]] == ["Freddo", "Toast"] assert client.get("/api/display/state", headers=two["display"]).json()["state"]["mode"] == "idle" # the same state again: nothing new to send client.post("/api/display/state", headers=one["pos"], json=CART) assert len(sent) == 1 client.post("/api/display/state", headers=one["pos"], json={"mode": "paid", "total": 9.5, "paid": 20, "change": 10.5}) assert sent[-1][1]["change"] == 10.5 # only a POS pushes, only a display reads assert err(client.post("/api/display/state", headers=one["display"], json=CART)) == "device.out_of_scope" assert err(client.get("/api/display/state", headers=one["pos"])) == "device.out_of_scope" assert err(client.post("/api/display/state", json=CART)) == "device.not_paired" assert client.post("/api/display/state", headers=one["pos"], json={"mode": "dance"}).status_code == 422 def test_the_idle_screen_is_the_venues_design(client, owner_h, counters, sent, media_dir, monkeypatch): from services import cloud_sync monkeypatch.setitem(cloud_sync.license_state, "apps_url", "https://menu.example.com") monkeypatch.setattr(cloud_sync.settings, "SITE_ID", "taverna-1") s = client.get("/api/display/settings", headers=owner_h).json() assert s["idle"]["style"] == "name" and s["media"] == [] up = lambda data, ctype="image/png": client.post("/api/display/media", headers=owner_h, # noqa: E731 files={"file": ("x.png", io.BytesIO(data), ctype)}) logo, photo = up(PNG).json(), up(PNG).json() assert logo["url"].startswith("/api/display/media/") and len(list(media_dir.iterdir())) == 2 assert err(up(b"hello", "text/plain")) == "display.media_type" assert client.get(logo["url"]).content == PNG # the displays load it with assert client.get("/api/display/media/../../secret.png").status_code == 404 # a slideshow of the two photos, with a line of text body = {"style": "slideshow", "slides": [logo["name"], photo["name"]], "slide_seconds": 5, "message": "Kalimera!"} r = client.put("/api/display/settings", headers=owner_h, json=body) assert r.status_code == 200, r.text assert sorted(kw["device_ids"] for k, _, kw in sent if k == "display.settings")[0] == sorted([counters[1]["display_id"], counters[2]["display_id"]]) idle = client.get("/api/display/state", headers=counters[1]["display"]).json()["idle"] assert idle["style"] == "slideshow" and idle["slide_urls"] == [logo["url"], photo["url"]] and idle["message"] == "Kalimera!" # a QR code: the venue's ordering link, or any link client.put("/api/display/settings", headers=owner_h, json={"style": "qr", "qr_target": "ordering", "qr_caption": "Order online"}) assert client.get("/api/display/state", headers=counters[1]["display"]).json()["idle"]["qr_link"] == "https://menu.example.com/order/taverna-1" assert err(client.put("/api/display/settings", headers=owner_h, json={"style": "qr", "qr_target": "custom"})) == "display.qr_url_missing" assert client.put("/api/display/settings", headers=owner_h, json={"style": "qr", "qr_target": "custom", "qr_url": "javascript:x"}).status_code == 422 # an image that isn't there; deleting one takes it off the screen assert err(client.put("/api/display/settings", headers=owner_h, json={"style": "logo", "logo": "0" * 32 + ".png"})) == "display.media_unknown" client.put("/api/display/settings", headers=owner_h, json={"style": "name_logo", "logo": logo["name"], "theme": "light"}) assert client.delete(f"/api/display/media/{logo['name']}", headers=owner_h).status_code == 204 after = client.get("/api/display/settings", headers=owner_h).json() assert after["idle"]["logo"] is None and [m["name"] for m in after["media"]] == [photo["name"]] and after["idle"]["theme"] == "light" def test_settings_need_the_key(client, make_user): from tests.conftest import TEST_PASSWORD waiter = make_user(role="waiter", username="w_display") tok = client.post("/api/auth/login", json={"client": "manager", "username": "w_display", "password": TEST_PASSWORD}).json().get("access_token") h = {"Authorization": f"Bearer {tok}"} if tok else {} assert client.get("/api/display/settings", headers=h).status_code in (401, 403) assert waiter.id