"""Caller ID as an integration (plan step 3.5): the phone system's webhook logs the call, the server matches the customer (Greek number rules) and publishes `call_incoming` on the main live connection to the users with caller_id.use and to the counter POS screens; the old public socket is gone; an optional shared secret; the call log; the lookup.""" import pytest from services.caller_id import normalise from tests.test_live import err, owner_h, pair # noqa: F401 (fixtures) @pytest.mark.parametrize("raw", ["2106001234", "+30 210 600 1234", "0030-210-600-1234", "302106001234", "(210) 600-1234"]) def test_greek_numbers_normalise(raw): assert normalise(raw) == "2106001234" def test_short_numbers_keep_their_30(): assert normalise("3012345") == "3012345" and normalise(None) == "" @pytest.fixture def eleni(db, make_user): from models.customers import Customer from models.order import Order, OrderItem owner = make_user(role="owner") c = Customer(name="Eleni Papa", nickname="Lena", phone="+30 697 111 2222", notes="Likes the corner table", created_by_id=owner.id) db.add(c) db.flush() o = Order(opened_by=owner.id, status="closed", customer_id=c.id) db.add(o) db.flush() from models.product import Product p = Product(name="Souvlaki", base_price=4.0) db.add(p) db.flush() db.add(OrderItem(order_id=o.id, product_id=p.id, added_by=owner.id, quantity=3, unit_price=4.0, status="paid", printed=True)) db.commit() return c @pytest.fixture def sent(monkeypatch): import services.ws_bus as bus got = [] real = bus.broadcast_sync monkeypatch.setattr(bus, "broadcast_sync", lambda kind, data, **kw: (got.append((kind, data, kw)), real(kind, data, **kw))) return got def test_a_call_is_logged_matched_and_published_to_who_may_see_it(client, db, owner_h, pair, make_user, eleni, sent): from core.access.queries import users_with_key pos_token, pos = pair("pos_counter", "Till") make_user(role="waiter") # no caller_id.use: doesn't hear it r = client.get("/api/phone/call-event", params={"caller": "6971112222", "ext": "100"}) assert r.status_code == 200, r.text assert r.json()["matched"] is True (kind, data, kw), = [x for x in sent if x[0] == "call_incoming"] assert data["number"] == "6971112222" and data["ext"] == "100" card = data["customer"] assert (card["name"], card["nickname"], card["visit_count"], card["total_spent"]) == ("Eleni Papa", "Lena", 1, 12.0) assert card["recent_orders"][0]["item_count"] == 3 assert sorted(kw["user_ids"]) == sorted(u.id for u in users_with_key(db, "caller_id.use")) assert kw["device_ids"] == [pos.json()["id"]] # an unknown number: still logged and published, no customer client.get("/api/phone/call-event", params={"caller": "2100000000"}) assert sent[-1][1]["customer"] is None log = client.get("/api/phone/calls", headers=owner_h).json()["calls"] assert [(c["number"], c["customer"]["name"] if c["customer"] else None) for c in log] == [("2100000000", None), ("6971112222", "Eleni Papa")] def test_nothing_can_listen_without_signing_in(client, owner_h, make_user): from starlette.websockets import WebSocketDisconnect with pytest.raises(Exception): with client.websocket_connect("/api/phone/ws") as ws: ws.receive_text() assert client.get("/api/phone/calls").status_code in (401, 403) from tests.conftest import TEST_PASSWORD make_user(role="waiter", username="w_calls") tok = client.post("/api/auth/login", json={"client": "manager", "username": "w_calls", "password": TEST_PASSWORD}).json().get("access_token") assert client.get("/api/phone/calls", headers={"Authorization": f"Bearer {tok}"} if tok else {}).status_code in (401, 403) assert WebSocketDisconnect def test_the_optional_shared_secret(client, db, sent, owner_h): from models.settings import PosSettings db.add(PosSettings(key="caller_id.secret", value="s3cret-ucm")) db.commit() assert err(client.get("/api/phone/call-event", params={"caller": "2101234567"})) == "caller_id.key_invalid" assert err(client.get("/api/phone/call-event", params={"caller": "2101234567", "key": "wrong"})) == "caller_id.key_invalid" assert client.get("/api/phone/call-event", params={"caller": "2101234567", "key": "s3cret-ucm"}).status_code == 200 assert len([x for x in sent if x[0] == "call_incoming"]) == 1 def test_lookup_by_phone(client, owner_h, eleni): r = client.get("/api/customers/lookup", params={"phone": "0030 697 111 2222"}, headers=owner_h) assert r.status_code == 200, r.text assert r.json()["customer"]["id"] == eleni.id assert client.get("/api/customers/lookup", params={"phone": "123"}, headers=owner_h).json()["customer"] is None