|
| 1 | +import json |
| 2 | + |
| 3 | +import httpx |
| 4 | +import pytest |
| 5 | +from pydantic import SecretStr |
| 6 | + |
| 7 | +from app.clients.whatsapp_client import WhatsAppClient |
| 8 | +from app.config import settings |
| 9 | +from app.utils.whatsapp_utils import generate_payload |
| 10 | + |
| 11 | + |
| 12 | +def use_mock(monkeypatch, enabled=True): |
| 13 | + monkeypatch.setattr(settings, "mock_whatsapp", enabled) |
| 14 | + monkeypatch.setattr( |
| 15 | + settings, "mock_whatsapp_api_url", "http://127.0.0.1:3100/api/whatsapp" |
| 16 | + ) |
| 17 | + monkeypatch.setattr(settings, "mock_whatsapp_token", SecretStr("local-token")) |
| 18 | + |
| 19 | + |
| 20 | +async def send_with(client, receive, *args): |
| 21 | + await client.client.aclose() |
| 22 | + async with httpx.AsyncClient( |
| 23 | + base_url=client.url, transport=httpx.MockTransport(receive) |
| 24 | + ) as transport: |
| 25 | + client.client = transport |
| 26 | + return await client.send_message(*args) |
| 27 | + |
| 28 | + |
| 29 | +async def test_production_keeps_meta_destination_and_form_encoding(monkeypatch): |
| 30 | + use_mock(monkeypatch, enabled=False) |
| 31 | + client = WhatsAppClient() |
| 32 | + requests = [] |
| 33 | + |
| 34 | + def receive(request): |
| 35 | + requests.append(request) |
| 36 | + return httpx.Response(200) |
| 37 | + |
| 38 | + assert await send_with(client, receive, "255700000001", "Hello 🦒") |
| 39 | + request = requests[0] |
| 40 | + expected = httpx.Request( |
| 41 | + "POST", "https://x", data=generate_payload("255700000001", "Hello 🦒") |
| 42 | + ) |
| 43 | + assert request.content == expected.content |
| 44 | + assert request.url.host == "graph.facebook.com" |
| 45 | + assert ( |
| 46 | + request.url.path |
| 47 | + == f"/{settings.meta_api_version}/{settings.whatsapp_cloud_number_id}/messages" |
| 48 | + ) |
| 49 | + assert ( |
| 50 | + request.headers["authorization"] |
| 51 | + == f"Bearer {settings.whatsapp_api_token.get_secret_value()}" |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +async def test_mock_sends_json_to_the_local_receiver(monkeypatch): |
| 56 | + use_mock(monkeypatch) |
| 57 | + client = WhatsAppClient() |
| 58 | + requests = [] |
| 59 | + |
| 60 | + def receive(request): |
| 61 | + requests.append(request) |
| 62 | + return httpx.Response(200, json={"messages": [{"id": "wamid.test"}]}) |
| 63 | + |
| 64 | + assert await send_with(client, receive, "255700000001", "Hello 🦒") |
| 65 | + assert await send_with(client, receive, "1", "Select", ["a", "b", "c", "d"]) |
| 66 | + request = requests[0] |
| 67 | + assert str(request.url) == "http://127.0.0.1:3100/api/whatsapp/messages" |
| 68 | + assert request.headers["authorization"] == "Bearer local-token" |
| 69 | + assert json.loads(request.content) == generate_payload("255700000001", "Hello 🦒") |
| 70 | + assert json.loads(requests[1].content)["type"] == "interactive" |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize("reject_second", [False, True]) |
| 74 | +async def test_long_text_preserves_chunks_and_stops_on_rejection( |
| 75 | + monkeypatch, reject_second |
| 76 | +): |
| 77 | + use_mock(monkeypatch) |
| 78 | + client = WhatsAppClient() |
| 79 | + chunks = [] |
| 80 | + |
| 81 | + def receive(request): |
| 82 | + chunks.append(json.loads(request.content)["text"]["body"]) |
| 83 | + return httpx.Response(429 if reject_second and len(chunks) == 2 else 200) |
| 84 | + |
| 85 | + sent = await send_with(client, receive, "255700000001", "x" * 9001) |
| 86 | + assert sent is not reject_second |
| 87 | + assert [len(chunk) for chunk in chunks] == ( |
| 88 | + [4000, 4000] if reject_second else [4000, 4000, 1001] |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +async def test_mock_read_receipt_and_typing_use_the_same_transport(monkeypatch): |
| 93 | + use_mock(monkeypatch) |
| 94 | + client = WhatsAppClient() |
| 95 | + await client.client.aclose() |
| 96 | + requests = [] |
| 97 | + |
| 98 | + def receive(request): |
| 99 | + requests.append(request) |
| 100 | + return httpx.Response(200) |
| 101 | + |
| 102 | + async with httpx.AsyncClient( |
| 103 | + base_url=client.url, transport=httpx.MockTransport(receive) |
| 104 | + ) as transport: |
| 105 | + client.client = transport |
| 106 | + await client.send_read_receipt_with_typing_indicator("wamid.inbound") |
| 107 | + await client.send_read_receipt_with_typing_indicator("") |
| 108 | + assert len(requests) == 1 |
| 109 | + assert json.loads(requests[0].content) == { |
| 110 | + "messaging_product": "whatsapp", |
| 111 | + "status": "read", |
| 112 | + "message_id": "wamid.inbound", |
| 113 | + "typing_indicator": {"type": "text"}, |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | +@pytest.mark.parametrize("failure", [httpx.ConnectError, httpx.ReadTimeout]) |
| 118 | +async def test_transport_errors_return_false_without_retry(monkeypatch, failure): |
| 119 | + use_mock(monkeypatch) |
| 120 | + client = WhatsAppClient() |
| 121 | + attempts = 0 |
| 122 | + |
| 123 | + def receive(request): |
| 124 | + nonlocal attempts |
| 125 | + attempts += 1 |
| 126 | + raise failure("Simulated local failure", request=request) |
| 127 | + |
| 128 | + assert not await send_with(client, receive, "255700000001", "Hello") |
| 129 | + assert attempts == 1 |
0 commit comments