|
8 | 8 | from unittest.mock import patch, MagicMock, AsyncMock |
9 | 9 | import os |
10 | 10 | import sys |
11 | | -from fastapi import FastAPI, HTTPException, Form, Body |
12 | | -from typing import Dict, Any |
| 11 | +from fastapi import FastAPI, HTTPException, Form, Body, Header |
| 12 | +from typing import Dict, Any, Optional |
13 | 13 | from fastapi.testclient import TestClient |
14 | 14 | from PIL import Image |
15 | 15 | import pytest |
@@ -127,9 +127,16 @@ def setup_mock_routes(self): |
127 | 127 | """ |
128 | 128 | @self.app.post("/tasks") |
129 | 129 | @pytest.mark.asyncio |
130 | | - async def create_task(request: Dict[str, Any] = Body(None)): |
131 | | - # Simulate task creation |
132 | | - task_result = self.process_and_forward.delay.return_value |
| 130 | + async def create_task(request: Dict[str, Any] = Body(None), authorization: Optional[str] = Header(None)): |
| 131 | + # Simulate task creation by actually calling the mock |
| 132 | + task_result = self.process_and_forward.delay( |
| 133 | + source=request.get("source"), |
| 134 | + source_type=request.get("source_type"), |
| 135 | + chunking_strategy=request.get("chunking_strategy"), |
| 136 | + index_name=request.get("index_name"), |
| 137 | + original_filename=request.get("original_filename"), |
| 138 | + authorization=authorization |
| 139 | + ) |
133 | 140 | return {"task_id": task_result.id} |
134 | 141 |
|
135 | 142 | @self.app.post("/tasks/process") |
@@ -302,16 +309,67 @@ def test_create_task(self): |
302 | 309 | "source_type": "file", |
303 | 310 | "chunking_strategy": "basic", |
304 | 311 | "index_name": self.index_name, |
| 312 | + "original_filename": "test-file.pdf", |
305 | 313 | "additional_params": {"param1": "value1"} |
306 | 314 | } |
307 | 315 |
|
308 | | - # Execute request |
| 316 | + # Execute request with authorization header |
| 317 | + response = self.client.post( |
| 318 | + "/tasks", |
| 319 | + json=request_data, |
| 320 | + headers={"Authorization": "Bearer test-token"} |
| 321 | + ) |
| 322 | + |
| 323 | + # Assert expectations |
| 324 | + self.assertEqual(response.status_code, 200) # Mock route defaults to 200 |
| 325 | + self.assertEqual(response.json(), {"task_id": self.task_id}) |
| 326 | + |
| 327 | + # Verify that process_and_forward.delay was called with authorization parameter |
| 328 | + self.process_and_forward.delay.assert_called_once_with( |
| 329 | + source=self.source, |
| 330 | + source_type="file", |
| 331 | + chunking_strategy="basic", |
| 332 | + index_name=self.index_name, |
| 333 | + original_filename="test-file.pdf", |
| 334 | + authorization="Bearer test-token" |
| 335 | + ) |
| 336 | + |
| 337 | + def test_create_task_without_authorization(self): |
| 338 | + """ |
| 339 | + Test creating a new task without authorization header. |
| 340 | + Verifies that the endpoint works when authorization is not provided. |
| 341 | + """ |
| 342 | + # Set up mock |
| 343 | + mock_task = MagicMock() |
| 344 | + mock_task.id = self.task_id |
| 345 | + self.process_and_forward.delay.return_value = mock_task |
| 346 | + |
| 347 | + # Test data |
| 348 | + request_data = { |
| 349 | + "source": self.source, |
| 350 | + "source_type": "file", |
| 351 | + "chunking_strategy": "basic", |
| 352 | + "index_name": self.index_name, |
| 353 | + "original_filename": "test-file.pdf" |
| 354 | + } |
| 355 | + |
| 356 | + # Execute request without authorization header |
309 | 357 | response = self.client.post("/tasks", json=request_data) |
310 | 358 |
|
311 | 359 | # Assert expectations |
312 | | - self.assertEqual(response.status_code, 200) # FastAPI test client defaults to 200 |
| 360 | + self.assertEqual(response.status_code, 200) # Mock route defaults to 200 |
313 | 361 | self.assertEqual(response.json(), {"task_id": self.task_id}) |
314 | 362 |
|
| 363 | + # Verify that process_and_forward.delay was called with None authorization |
| 364 | + self.process_and_forward.delay.assert_called_once_with( |
| 365 | + source=self.source, |
| 366 | + source_type="file", |
| 367 | + chunking_strategy="basic", |
| 368 | + index_name=self.index_name, |
| 369 | + original_filename="test-file.pdf", |
| 370 | + authorization=None |
| 371 | + ) |
| 372 | + |
315 | 373 | def test_process_sync_endpoint_success(self): |
316 | 374 | """ |
317 | 375 | Test synchronous processing endpoint success case. |
|
0 commit comments