40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
import pytest
|
|
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
|
|
|
|
|
|
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 `ainvoke` mockeado y se inyecta
|
|
mediante `app.dependency_overrides` antes de realizar la petición.
|
|
"""
|
|
mock_agent = SimpleNamespace()
|
|
mock_agent.ainvoke = AsyncMock(
|
|
return_value={
|
|
"messages": [SimpleNamespace(content="Respuesta mock")]
|
|
}
|
|
)
|
|
|
|
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
|
|
)
|
|
|
|
app.dependency_overrides.pop(get_agent, None)
|
|
|
|
assert response.status_code == 200
|
|
assert response.json().get("reply") == "Respuesta mock"
|