Imago/tests/test_middleware_extended.py
Bruno Charest cc99fea20a
Some checks failed
CI / Lint & Format (push) Has been cancelled
CI / Tests (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / Docker Build (push) Has been cancelled
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.
2026-02-24 11:22:10 -05:00

35 lines
1.2 KiB
Python

import pytest
from unittest.mock import MagicMock
from app.middleware import (
_get_client_id_from_request, get_upload_rate_limit,
get_ai_rate_limit, upload_rate_limit_key, ai_rate_limit_key
)
def test_get_client_id_from_request_with_id():
request = MagicMock()
request.state.client_id = "test-client"
assert _get_client_id_from_request(request) == "test-client"
def test_get_client_id_from_request_fallback():
request = MagicMock()
del request.state.client_id # Ensure it's not there
request.client.host = "1.2.3.4"
request.headers = {}
# slowapi's get_remote_address uses request.client.host or headers
with patch("app.middleware.get_remote_address", return_value="1.2.3.4"):
assert _get_client_id_from_request(request) == "1.2.3.4"
from unittest.mock import patch
def test_rate_limit_helpers():
assert "hour" in get_upload_rate_limit("free")
assert "hour" in get_ai_rate_limit("premium")
assert get_upload_rate_limit("invalid") == get_upload_rate_limit("free")
def test_rate_limit_keys():
request = MagicMock()
request.state.client_id = "abc"
assert upload_rate_limit_key(request) == "abc"
assert ai_rate_limit_key(request) == "abc"