Add comprehensive test suite for image processing and related services
- Implement tests for database generator to ensure proper session handling. - Create tests for EXIF extraction and conversion functions. - Add tests for image-related endpoints, ensuring proper data retrieval and isolation between clients. - Develop tests for OCR functionality, including language detection and text extraction. - Introduce tests for the image processing pipeline, covering success and failure scenarios. - Validate rate limiting functionality and ensure independent counters for different clients. - Implement scraper tests to verify HTML content fetching and error handling. - Add unit tests for various services, including storage and filename generation. - Establish worker entry point for ARQ to handle background image processing tasks.
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
"""
|
||||
Tests de rate limiting — vérification des limites par client.
|
||||
"""
|
||||
import io
|
||||
import pytest
|
||||
from unittest.mock import patch, AsyncMock
|
||||
from httpx import AsyncClient
|
||||
|
||||
from app.models.client import APIClient, ClientPlan
|
||||
from app.dependencies.auth import hash_api_key
|
||||
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────
|
||||
# Helper : upload rapide
|
||||
# ─────────────────────────────────────────────────────────────
|
||||
|
||||
async def _quick_upload(async_client: AsyncClient, headers: dict) -> int:
|
||||
"""Upload rapide et retourne le status code."""
|
||||
# Image JPEG minimale
|
||||
jpeg_bytes = (
|
||||
b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00"
|
||||
b"\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t"
|
||||
b"\x08\n\x0c\x14\r\x0c\x0b\x0b\x0c\x19\x12\x13\x0f\x14\x1d\x1a"
|
||||
b"\x1f\x1e\x1d\x1a\x1c\x1c $.\' \",#\x1c\x1c(7),01444\x1f\'9=82<.342"
|
||||
b"\xff\xc0\x00\x0b\x08\x00\x01\x00\x01\x01\x01\x11\x00"
|
||||
b"\xff\xc4\x00\x1f\x00\x00\x01\x05\x01\x01\x01\x01\x01\x01\x00\x00"
|
||||
b"\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b"
|
||||
b"\xff\xc4\x00\xb5\x10\x00\x02\x01\x03\x03\x02\x04\x03\x05\x05\x04"
|
||||
b"\x04\x00\x00\x01}\x01\x02\x03\x00\x04\x11\x05\x12!1A\x06\x13Qa\x07"
|
||||
b"\x22q\x142\x81\x91\xa1\x08#B\xb1\xc1\x15R\xd1\xf0$3br\x82\t\n\x16"
|
||||
b"\x17\x18\x19\x1a%&\'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\x83"
|
||||
b"\x84\x85\x86\x87\x88\x89\x8a\x92\x93\x94\x95\x96\x97\x98\x99\x9a"
|
||||
b"\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6\xb7\xb8"
|
||||
b"\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xd2\xd3\xd4\xd5\xd6"
|
||||
b"\xd7\xd8\xd9\xda\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xf1\xf2"
|
||||
b"\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa"
|
||||
b"\xff\xda\x00\x08\x01\x01\x00\x00?\x00T\xdb\xae\x8a(\x03\xff\xd9"
|
||||
)
|
||||
|
||||
# Le pipeline ARQ est mocké globalement dans conftest.py
|
||||
response = await async_client.post(
|
||||
"/images/upload",
|
||||
files={"file": ("test.jpg", io.BytesIO(jpeg_bytes), "image/jpeg")},
|
||||
headers=headers,
|
||||
)
|
||||
return response.status_code
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────
|
||||
# Rate limit headers sont présents dans les réponses
|
||||
# ─────────────────────────────────────────────────────────────
|
||||
|
||||
async def test_rate_limit_headers_present(
|
||||
async_client: AsyncClient,
|
||||
client_a: APIClient,
|
||||
auth_headers_a: dict,
|
||||
):
|
||||
# Le pipeline ARQ est mocké globalement dans conftest.py
|
||||
jpeg_bytes = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xd9"
|
||||
response = await async_client.post(
|
||||
"/images/upload",
|
||||
files={"file": ("test.jpg", io.BytesIO(jpeg_bytes), "image/jpeg")},
|
||||
headers=auth_headers_a,
|
||||
)
|
||||
# slowapi met des headers rate limit dans la réponse
|
||||
# Vérifier que la réponse est soit 201 (succès) soit contient des headers rate limit
|
||||
assert response.status_code in (201, 429)
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────
|
||||
# Compteurs distincts par client
|
||||
# ─────────────────────────────────────────────────────────────
|
||||
|
||||
async def test_rate_limit_per_client_independent(
|
||||
async_client: AsyncClient,
|
||||
client_a: APIClient,
|
||||
client_b: APIClient,
|
||||
auth_headers_a: dict,
|
||||
auth_headers_b: dict,
|
||||
):
|
||||
"""Les compteurs de rate limit sont indépendants par client."""
|
||||
# Uploads pour A
|
||||
status_a = await _quick_upload(async_client, auth_headers_a)
|
||||
assert status_a == 201
|
||||
|
||||
# Uploads pour B (ne doit pas être affecté par les requêtes de A)
|
||||
status_b = await _quick_upload(async_client, auth_headers_b)
|
||||
assert status_b == 201
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────
|
||||
# Vérification fonctionnelle du rate limiter
|
||||
# ─────────────────────────────────────────────────────────────
|
||||
|
||||
async def test_rate_limiter_is_configured(
|
||||
async_client: AsyncClient,
|
||||
client_a: APIClient,
|
||||
auth_headers_a: dict,
|
||||
):
|
||||
"""Le rate limiter est bien actif sur le endpoint upload."""
|
||||
from app.middleware import limiter
|
||||
# Le limiter doit être configuré et actif
|
||||
assert limiter is not None
|
||||
assert limiter.enabled
|
||||
Reference in New Issue
Block a user