| """ |
| Chat Endpoint Integration Tests |
| |
| Tests the chat endpoint functionality including: |
| - JWT authentication verification |
| - User_id match validation |
| - Message persistence before and after agent execution |
| - Timeout handling |
| """ |
|
|
| import pytest |
| from fastapi.testclient import TestClient |
|
|
| from src.main import app |
| from tests.utils.task_helpers import count_tasks |
|
|
|
|
| @pytest.mark.integration |
| def test_chat_endpoint_verifies_jwt_authentication(test_user): |
| """ |
| Test: Chat endpoint verifies JWT authentication |
| |
| Verifies that chat endpoint rejects requests without valid JWT. |
| """ |
| client = TestClient(app) |
|
|
| |
| response = client.post( |
| f"/api/{test_user.id}/chat", |
| json={"message": "Show my tasks"} |
| ) |
|
|
| |
| assert response.status_code == 401 |
|
|
|
|
| @pytest.mark.integration |
| def test_chat_endpoint_validates_user_id_match(test_user, test_user2, test_jwt_token): |
| """ |
| Test: Chat endpoint validates user_id match |
| |
| Verifies that chat endpoint rejects requests where URL user_id doesn't match JWT. |
| """ |
| client = TestClient(app) |
|
|
| |
| response = client.post( |
| f"/api/{test_user2.id}/chat", |
| headers={"Authorization": f"Bearer {test_jwt_token}"}, |
| json={"message": "Show my tasks"} |
| ) |
|
|
| |
| assert response.status_code == 403 |
|
|
|
|
| @pytest.mark.integration |
| def test_chat_endpoint_persists_messages_before_and_after_agent_execution(test_user, auth_headers, test_session): |
| """ |
| Test: Chat endpoint persists messages before and after agent execution |
| |
| Verifies that both user and assistant messages are saved to database. |
| """ |
| client = TestClient(app) |
|
|
| |
| response = client.post( |
| f"/api/{test_user.id}/chat", |
| headers=auth_headers, |
| json={"message": "Add a task to test persistence"} |
| ) |
|
|
| |
| assert response.status_code == 200 |
| data = response.json() |
|
|
| |
| assert "conversation_id" in data |
| assert "message_id" in data |
|
|
| |
| from sqlmodel import select |
| from src.models.message import Message |
|
|
| statement = select(Message).where(Message.conversation_id == data["conversation_id"]) |
| messages = test_session.exec(statement).all() |
|
|
| |
| assert len(messages) >= 2 |
|
|
|
|
| @pytest.mark.integration |
| @pytest.mark.asyncio |
| async def test_chat_endpoint_handles_timeout_gracefully(test_user, auth_headers): |
| """ |
| Test: Chat endpoint handles timeout gracefully |
| |
| Verifies that chat endpoint returns appropriate error for agent timeout. |
| """ |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
|
|
| |
| client = TestClient(app) |
|
|
| response = client.post( |
| f"/api/{test_user.id}/chat", |
| headers=auth_headers, |
| json={"message": "Quick test"} |
| ) |
|
|
| |
| assert response.status_code in [200, 500] |
|
|