40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
import pytest
|
|
from types import SimpleNamespace
|
|
from httpx import ASGITransport, AsyncClient
|
|
from naliiabotapi.main import app
|
|
from naliiabotapi.api.dependencies import get_agent
|
|
|
|
|
|
class TestWebhookChatBot:
|
|
"""Tests for WebhookChatBot."""
|
|
|
|
@pytest.mark.anyio
|
|
async def tests_webhook_success(self, send_message_payload):
|
|
"""Test that the webhook endpoint returns a successful response.
|
|
|
|
Se crea un `mock_agent` con `invoke` mockeado y se inyecta
|
|
mediante `app.dependency_overrides` antes de realizar la petición.
|
|
"""
|
|
# Crear mock del agente y su invoke
|
|
mock_agent = SimpleNamespace()
|
|
|
|
def mock_invoke(state, config=None):
|
|
return {"messages": [SimpleNamespace(content="Respuesta mock")]}
|
|
|
|
mock_agent.invoke = mock_invoke
|
|
|
|
# Sobrescribir la dependencia del agente en la app
|
|
app.dependency_overrides[get_agent] = lambda: mock_agent
|
|
|
|
async with AsyncClient(
|
|
transport=ASGITransport(app=app),
|
|
base_url="http://localhost:8010"
|
|
) as client:
|
|
response = await client.post("/webhook", json=send_message_payload)
|
|
|
|
# Limpiar override para no afectar a otros tests
|
|
app.dependency_overrides.pop(get_agent, None)
|
|
|
|
assert response.status_code == 200
|
|
assert response.json().get("reply") == "Respuesta mock"
|