Aniworld/tests/conftest.py
Lukas 731fd56768 feat: implement setup redirect middleware and fix test suite
- Created SetupRedirectMiddleware to redirect unconfigured apps to /setup
- Enhanced /api/auth/setup endpoint to save anime_directory to config
- Updated SetupRequest model to accept optional anime_directory parameter
- Modified setup.html to send anime_directory in setup API call
- Added @pytest.mark.requires_clean_auth marker for tests needing unconfigured state
- Modified conftest.py to conditionally setup auth based on test marker
- Fixed all test failures (846/846 tests now passing)
- Updated instructions.md to mark setup tasks as complete

This implementation ensures users are guided through initial setup
before accessing the application, while maintaining test isolation
and preventing auth state leakage between tests.
2025-10-24 19:55:26 +02:00

86 lines
2.9 KiB
Python

"""Pytest configuration and shared fixtures for all tests."""
import pytest
from src.server.services.auth_service import auth_service
def pytest_configure(config):
"""Register custom pytest marks."""
config.addinivalue_line(
"markers",
"performance: mark test as a performance test"
)
config.addinivalue_line(
"markers",
"security: mark test as a security test"
)
config.addinivalue_line(
"markers",
"requires_clean_auth: test requires auth to NOT be configured initially"
)
@pytest.fixture(autouse=True)
def reset_auth_and_rate_limits(request):
"""Reset authentication state and rate limits before each test.
This ensures:
1. Auth service state doesn't leak between tests
2. Rate limit window is reset for test client IP
3. Auth is configured with a default test password UNLESS the test
is marked with @pytest.mark.requires_clean_auth
Applied to all tests automatically via autouse=True.
"""
# Reset auth service state
auth_service._hash = None # noqa: SLF001
auth_service._failed.clear() # noqa: SLF001
# Check if test requires clean (unconfigured) auth state
requires_clean_auth = request.node.get_closest_marker("requires_clean_auth")
# Configure auth with a default test password so middleware allows requests
# This prevents the SetupRedirectMiddleware from blocking all test requests
# Skip this if the test explicitly needs clean auth state
if not requires_clean_auth:
try:
auth_service.setup_master_password("TestPass123!")
except Exception:
# If setup fails (e.g., already set), that's okay
pass
# Reset rate limiter - clear rate limit dict if middleware exists
# This prevents tests from hitting rate limits on auth endpoints
try:
from src.server.fastapi_app import app
# Try to find and clear the rate limiter dict
# Middleware is stored in app.middleware_stack or accessible
# through app's internal structure
if hasattr(app, 'middleware_stack'):
# Try to find AuthMiddleware in the stack
stack = app.middleware_stack
while stack is not None:
if hasattr(stack, 'cls'):
# This is a middleware class
pass
if hasattr(stack, 'app') and hasattr(
stack, '_rate'
): # noqa: SLF001
# Found a potential AuthMiddleware instance
stack._rate.clear() # noqa: SLF001
stack = getattr(stack, 'app', None)
except BaseException:
# If middleware reset fails, tests might hit rate limits
# but we continue anyway - they're not critical
pass
yield
# Clean up after test
auth_service._hash = None # noqa: SLF001
auth_service._failed.clear() # noqa: SLF001