Refactor backend: fix geo cache cleanup, scheduler heartbeat, correlation middleware; update docs
This commit is contained in:
@@ -9,21 +9,14 @@ from starlette.testclient import TestClient
|
||||
from app.config import Settings
|
||||
from app.main import create_app
|
||||
from app.middleware.correlation import CORRELATION_ID_CONTEXT_KEY
|
||||
from app.models.server import ServerStatus
|
||||
|
||||
|
||||
def test_correlation_middleware_generates_uuid_when_header_absent() -> None:
|
||||
def test_correlation_middleware_generates_uuid_when_header_absent(
|
||||
test_settings: Settings,
|
||||
) -> None:
|
||||
"""Correlation middleware generates a UUID4 when X-Correlation-ID header is missing."""
|
||||
settings = Settings(
|
||||
database_path="/tmp/test.db",
|
||||
fail2ban_socket="/tmp/fake_fail2ban.sock",
|
||||
fail2ban_config_dir="/tmp/fail2ban",
|
||||
session_secret="test-secret-key-do-not-use-in-production",
|
||||
session_duration_minutes=60,
|
||||
timezone="UTC",
|
||||
log_level="debug",
|
||||
)
|
||||
|
||||
app = create_app(settings=settings)
|
||||
app = create_app(settings=test_settings)
|
||||
|
||||
# Test with TestClient (synchronous)
|
||||
client = TestClient(app)
|
||||
@@ -37,19 +30,11 @@ def test_correlation_middleware_generates_uuid_when_header_absent() -> None:
|
||||
assert correlation_id.count("-") == 4
|
||||
|
||||
|
||||
def test_correlation_middleware_preserves_header_from_request() -> None:
|
||||
def test_correlation_middleware_preserves_header_from_request(
|
||||
test_settings: Settings,
|
||||
) -> None:
|
||||
"""Correlation middleware preserves X-Correlation-ID header from client request."""
|
||||
settings = Settings(
|
||||
database_path="/tmp/test.db",
|
||||
fail2ban_socket="/tmp/fake_fail2ban.sock",
|
||||
fail2ban_config_dir="/tmp/fail2ban",
|
||||
session_secret="test-secret-key-do-not-use-in-production",
|
||||
session_duration_minutes=60,
|
||||
timezone="UTC",
|
||||
log_level="debug",
|
||||
)
|
||||
|
||||
app = create_app(settings=settings)
|
||||
app = create_app(settings=test_settings)
|
||||
|
||||
client = TestClient(app)
|
||||
test_correlation_id = "550e8400-e29b-41d4-a716-446655440000"
|
||||
@@ -59,19 +44,18 @@ def test_correlation_middleware_preserves_header_from_request() -> None:
|
||||
assert response.headers["X-Correlation-ID"] == test_correlation_id
|
||||
|
||||
|
||||
def test_correlation_middleware_stores_in_request_state() -> None:
|
||||
def test_correlation_middleware_stores_in_request_state(
|
||||
test_settings: Settings,
|
||||
) -> None:
|
||||
"""Correlation middleware stores correlation ID in request.state for handlers."""
|
||||
settings = Settings(
|
||||
database_path="/tmp/test.db",
|
||||
fail2ban_socket="/tmp/fake_fail2ban.sock",
|
||||
fail2ban_config_dir="/tmp/fail2ban",
|
||||
session_secret="test-secret-key-do-not-use-in-production",
|
||||
session_duration_minutes=60,
|
||||
timezone="UTC",
|
||||
log_level="debug",
|
||||
)
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
app = create_app(settings=test_settings)
|
||||
app.state.server_status = ServerStatus(online=True)
|
||||
mock_scheduler = MagicMock()
|
||||
mock_scheduler.running = True
|
||||
app.state.scheduler = mock_scheduler
|
||||
|
||||
app = create_app(settings=settings)
|
||||
client = TestClient(app)
|
||||
|
||||
# Make a request and verify correlation ID is available to handlers
|
||||
@@ -84,19 +68,11 @@ def test_correlation_middleware_stores_in_request_state() -> None:
|
||||
assert response.headers["X-Correlation-ID"] == test_correlation_id
|
||||
|
||||
|
||||
def test_correlation_id_in_response_headers() -> None:
|
||||
def test_correlation_id_in_response_headers(
|
||||
test_settings: Settings,
|
||||
) -> None:
|
||||
"""Correlation ID is included in all response headers."""
|
||||
settings = Settings(
|
||||
database_path="/tmp/test.db",
|
||||
fail2ban_socket="/tmp/fake_fail2ban.sock",
|
||||
fail2ban_config_dir="/tmp/fail2ban",
|
||||
session_secret="test-secret-key-do-not-use-in-production",
|
||||
session_duration_minutes=60,
|
||||
timezone="UTC",
|
||||
log_level="debug",
|
||||
)
|
||||
|
||||
app = create_app(settings=settings)
|
||||
app = create_app(settings=test_settings)
|
||||
client = TestClient(app)
|
||||
|
||||
# Test without providing header (should generate one)
|
||||
|
||||
Reference in New Issue
Block a user