"""Sign-in (plan 6F.7): one session per waiter asks before signing the other phone out (the default), only phones count; usernames on paired screens (venue setting + the device's own).""" import pytest from conftest import TEST_PIN from tests.test_live import err, owner_h, pair # noqa: F401 (fixtures) ANDROID = "Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0 Mobile Safari/537.36" def _phone(client, user, **extra): return client.post("/api/auth/login", headers={"user-agent": ANDROID}, json={"client": "waiter", "user_id": user.id, "pin": TEST_PIN, **extra}) def _h(r): assert r.status_code == 200, r.text return {"Authorization": f"Bearer {r.json()['access_token']}"} @pytest.fixture def ended(monkeypatch): from services import ws_bus got = [] real = ws_bus.end_session monkeypatch.setattr(ws_bus, "end_session", lambda sid, reason: (got.append(reason), real(sid, reason))[1]) return got def test_ask_is_the_default_and_says_where(client, make_user, ended): w = make_user(role="waiter") first = _h(_phone(client, w)) r = _phone(client, w) assert r.status_code == 409 and err(r) == "auth.session_elsewhere" params = r.json()["error"]["params"] assert params["device"] == "Android · Chrome" and params["since"].endswith("Z") assert client.get("/api/auth/me", headers=first).status_code == 200 # asking ends nothing second = _h(_phone(client, w, replace=True)) gone = client.get("/api/auth/me", headers=first) assert gone.status_code == 401 and err(gone) == "auth.session_ended" assert gone.json()["error"]["params"]["reason"] == "replaced" # the old phone can say why assert ended == ["replaced"] # its socket was told at once assert client.get("/api/auth/me", headers=second).status_code == 200 def test_replace_does_not_bypass_refuse_new(client, make_user, owner_h): assert client.put("/api/settings/auth.single_session_waiter", headers=owner_h, json={"value": "refuse_new"}).status_code == 200 w = make_user(role="waiter") _h(_phone(client, w)) r = _phone(client, w, replace=True) assert r.status_code == 409 and err(r) == "auth.session_exists" def test_only_phones_count(client, make_user, pair): owner = make_user(role="owner") token, dev = pair("pos_counter", "Till") till = client.post("/api/auth/login", headers={"X-Xenia-Device": token}, json={"client": "terminal", "user_id": owner.id, "pin": TEST_PIN}) assert till.status_code == 200, till.text _h(_phone(client, owner)) # the counter's session isn't a phone: no question till_h = {"Authorization": f"Bearer {till.json()['access_token']}", "X-Xenia-Device": token} assert client.get("/api/auth/me", headers=till_h).status_code == 200 def test_usernames_on_terminals(client, make_user, owner_h, pair): cashier = make_user(role="owner") token, dev = pair("pos_counter", "Till") dh = {"X-Xenia-Device": token} assert len(client.get("/api/devices/me/users", headers=dh).json()) > 0 assert client.put("/api/settings/auth.terminal_require_username", headers=owner_h, json={"value": "true"}).status_code == 200 assert client.get("/api/devices/me/users", headers=dh).json() == [] assert client.get("/api/devices/me", headers=dh).json()["require_username"] is True by_id = client.post("/api/auth/login", headers=dh, json={"client": "terminal", "user_id": cashier.id, "pin": TEST_PIN}) assert by_id.status_code == 422 and err(by_id) == "auth.username_required" wrong = client.post("/api/auth/login", headers=dh, json={"client": "terminal", "username": "nobody_here", "pin": TEST_PIN}) assert wrong.status_code == 401 and err(wrong) == "auth.invalid_credentials" ok = client.post("/api/auth/login", headers=dh, json={"client": "terminal", "username": cashier.username, "pin": TEST_PIN}) assert ok.status_code == 200, ok.text # "tap" can't go with usernames: the screen asks for the PIN instead dev_id = dev.json()["id"] assert client.patch(f"/api/devices/{dev_id}", headers=owner_h, json={"login_mode": "tap"}).status_code == 200 assert client.get("/api/devices/me", headers=dh).json()["login_mode"] == "pin" tap = client.post("/api/auth/login", headers=dh, json={"client": "terminal", "username": cashier.username}) assert tap.status_code == 403 and err(tap) == "auth.login_mode_not_allowed" # the device's own choice wins over the venue's out = client.patch(f"/api/devices/{dev_id}", headers=owner_h, json={"require_username": False}).json() assert out["require_username"] is False and out["effective_require_username"] is False assert client.get("/api/devices/me", headers=dh).json()["login_mode"] == "tap" assert len(client.get("/api/devices/me/users", headers=dh).json()) > 0 out = client.patch(f"/api/devices/{dev_id}", headers=owner_h, json={"require_username": None}).json() assert out["require_username"] is None and out["effective_require_username"] is True def test_device_label(): from core.sessions import device_label assert device_label(ANDROID) == "Android · Chrome" assert device_label("Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 Version/17.0 Mobile/15E148 Safari/604.1") == "iPhone · Safari" assert device_label("Mozilla/5.0 (Linux; Android 14; wv) AppleWebKit/537.36 Chrome/130.0 Mobile Safari/537.36".replace("wv)", "Pixel; wv)")) == "Android · app" assert device_label(None) == ""