From a27def119cedc6c5954b79eff4dc23accd350107 Mon Sep 17 00:00:00 2001 From: lebaudantoine Date: Tue, 17 Mar 2026 22:24:53 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85(summary)=20add=20tests=20for=20the=20?= =?UTF-8?q?task=20endpoint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ensure the API correctly validates and accepts the parameters required to register a task in the queue. --- src/summary/tests/api/test_api_tasks.py | 88 +++++++++++++++++++++++++ src/summary/tests/conftest.py | 16 ++++- 2 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 src/summary/tests/api/test_api_tasks.py diff --git a/src/summary/tests/api/test_api_tasks.py b/src/summary/tests/api/test_api_tasks.py new file mode 100644 index 00000000..9492bf95 --- /dev/null +++ b/src/summary/tests/api/test_api_tasks.py @@ -0,0 +1,88 @@ +"""Integration tests for the API tasks endpoints.""" + +# tests/unit/test_api_tasks.py +from unittest.mock import MagicMock, patch + + +class TestTasks: + """Tests for the /tasks endpoint.""" + + @patch( + "summary.api.route.tasks.process_audio_transcribe_summarize_v2.apply_async", + return_value=MagicMock(id="task-id-abc"), + ) + @patch("summary.api.route.tasks.time.time", return_value=1735725600.0) + def test_create_task_returns_task_id(self, mock_time, mock_apply_async, client): + """POST /tasks/ with valid payload returns id and dispatches Celery task.""" + response = client.post( + "api/v1/tasks/", + headers={"Authorization": "Bearer test-api-token"}, + json={ + "owner_id": "owner-123", + "filename": "recording.mp4", + "email": "user@example.com", + "sub": "sub-123", + "room": "room-abc", + "recording_date": "2026-01-01", + "recording_time": "10:00:00", + "language": None, + "download_link": "http://example.com/file.mp4", + }, + ) + + assert response.status_code == 200 + assert response.json() == {"id": "task-id-abc", "message": "Task created"} + + args = mock_apply_async.call_args.kwargs["args"] + assert args == [ + "owner-123", # owner_id + "recording.mp4", # filename + "user@example.com", # email + "sub-123", # sub + 1735725600.0, # frozen time + "room-abc", # room + "2026-01-01", # recording_date + "10:00:00", # recording_time + None, # language + "http://example.com/file.mp4", # download_link + None, # context_language + ] + + def test_create_task_invalid_language(self, client): + """POST /tasks/ with an unsupported language returns 422.""" + payload = {"language": "klingon"} + response = client.post( + "/api/v1/tasks/", + headers={"Authorization": "Bearer test-api-token"}, + json=payload, + ) + + assert response.status_code == 422 + + @patch( + "summary.api.route.tasks.AsyncResult", + return_value=MagicMock(status="PENDING"), + ) + def test_get_task_status_pending(self, mock_result, client): + """GET /tasks/{id} returns PENDING status when the task has not started yet.""" + response = client.get( + "/api/v1/tasks/task-id-abc", + headers={"Authorization": "Bearer test-api-token"}, + ) + + assert response.status_code == 200 + assert response.json() == {"id": "task-id-abc", "status": "PENDING"} + + @patch( + "summary.api.route.tasks.AsyncResult", + return_value=MagicMock(status="SUCCESS"), + ) + def test_get_task_status_success(self, mock_result, client): + """GET /tasks/{id} returns SUCCESS status when the task has completed.""" + response = client.get( + "/api/v1/tasks/task-id-abc", + headers={"Authorization": "Bearer test-api-token"}, + ) + + assert response.status_code == 200 + assert response.json()["status"] == "SUCCESS" diff --git a/src/summary/tests/conftest.py b/src/summary/tests/conftest.py index cbce00b9..e002c46e 100644 --- a/src/summary/tests/conftest.py +++ b/src/summary/tests/conftest.py @@ -2,11 +2,23 @@ import pytest from fastapi.testclient import TestClient +from pydantic import SecretStr +from summary.core.config import Settings, get_settings from summary.main import app +def get_settings_override(): + """Return settings for tests.""" + return Settings( + app_api_token=SecretStr("test-api-token"), + ) + + @pytest.fixture() def client(): - """Provide a FastAPI TestClient for integration tests.""" - return TestClient(app) + """Provide a FastAPI TestClient for tests.""" + client = TestClient(app) + app.dependency_overrides[get_settings] = get_settings_override + + return client