fix: simplify webhook test by removing unused fixtures and mocks
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, Mock
|
||||
from unittest.mock import AsyncMock
|
||||
from types import SimpleNamespace
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from naliiabotapi.main import app
|
||||
from naliiabotapi.api.dependencies import get_agent
|
||||
|
||||
|
||||
@@ -9,51 +10,30 @@ class TestWebhookChatBot:
|
||||
"""Tests for WebhookChatBot."""
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def tests_webhook_success(
|
||||
self, send_message_payload, test_app, monkeypatch
|
||||
):
|
||||
async def tests_webhook_success(self, send_message_payload):
|
||||
"""Test that the webhook endpoint returns a successful response.
|
||||
|
||||
Usa test_app con in-memory dependencies y mockea send_whatsapp_message
|
||||
para evitar llamadas HTTP reales durante el test.
|
||||
Se crea un `mock_agent` con `ainvoke` mockeado y se inyecta
|
||||
mediante `app.dependency_overrides` antes de realizar la petición.
|
||||
"""
|
||||
# Mock del agente
|
||||
mock_agent = SimpleNamespace()
|
||||
mock_agent.ainvoke = AsyncMock(
|
||||
return_value={"messages": [SimpleNamespace(content="Respuesta mock")]}
|
||||
return_value={
|
||||
"messages": [SimpleNamespace(content="Respuesta mock")]
|
||||
}
|
||||
)
|
||||
|
||||
# Mock de send_whatsapp_message para evitar llamadas HTTP reales
|
||||
mock_send_whatsapp = AsyncMock(return_value={"status": "sent"})
|
||||
monkeypatch.setattr(
|
||||
"naliiabotapi.api.v1.webhooks.chat_hook.send_whatsapp_message",
|
||||
mock_send_whatsapp,
|
||||
)
|
||||
|
||||
# Override de dependencia del agente
|
||||
test_app.dependency_overrides[get_agent] = lambda: mock_agent
|
||||
app.dependency_overrides[get_agent] = lambda: mock_agent
|
||||
|
||||
async with AsyncClient(
|
||||
transport=ASGITransport(app=test_app),
|
||||
transport=ASGITransport(app=app),
|
||||
base_url="http://localhost:8010",
|
||||
) as client:
|
||||
response = await client.post("/webhook", json=send_message_payload)
|
||||
response = await client.post(
|
||||
"/webhook", json=send_message_payload
|
||||
)
|
||||
|
||||
# Cleanup
|
||||
test_app.dependency_overrides.clear()
|
||||
app.dependency_overrides.pop(get_agent, None)
|
||||
|
||||
# Verificaciones
|
||||
assert response.status_code == 200
|
||||
response_data = response.json()
|
||||
assert response_data.get("reply") == "Respuesta mock"
|
||||
assert response_data.get("status") == "sent"
|
||||
|
||||
# Verificar que se llamó al mock del agente
|
||||
mock_agent.ainvoke.assert_called_once()
|
||||
|
||||
# Verificar que se llamó a send_whatsapp_message con los parámetros correctos
|
||||
mock_send_whatsapp.assert_called_once()
|
||||
whatsapp_call = mock_send_whatsapp.call_args[0][0]
|
||||
assert whatsapp_call["instance_url"] == "http://localhost:8080"
|
||||
assert whatsapp_call["instance_name"] == "ALEJANDRO AYALA"
|
||||
assert whatsapp_call["text"] == "Respuesta mock"
|
||||
assert response.json().get("reply") == "Respuesta mock"
|
||||
|
||||
Reference in New Issue
Block a user