mockapi/tests/test_route_manager_fix.py
2026-03-16 05:47:01 +00:00

45 lines
No EOL
1.7 KiB
Python

"""
Test that route_manager is attached to app.state before first request.
"""
import pytest
from fastapi.testclient import TestClient
from app import create_app
def test_route_manager_attached():
"""Ensure route_manager is attached after app creation."""
app = create_app()
assert hasattr(app.state, 'route_manager')
assert hasattr(app.state, 'session_factory')
assert app.state.route_manager is not None
# Ensure route_manager has app reference
assert app.state.route_manager.app is app
def test_admin_dashboard_with_route_manager():
"""Test that admin dashboard can access route_manager dependency."""
app = create_app()
client = TestClient(app)
# Login first
resp = client.post("/admin/login", data={"username": "admin", "password": "admin123"})
assert resp.status_code in (200, 302, 307)
# Request dashboard with trailing slash (correct route)
resp = client.get("/admin/", follow_redirects=True)
# Should return 200, not 500 AttributeError
assert resp.status_code == 200
# Ensure route_manager stats are present (optional)
# The dashboard template includes stats; we can check for some text
assert "Dashboard" in resp.text
def test_route_manager_dependency():
"""Test get_route_manager dependency returns the attached route_manager."""
from controllers.admin_controller import get_route_manager
from fastapi import Request
from unittest.mock import Mock
# Create mock request with app.state.route_manager
app = create_app()
request = Mock(spec=Request)
request.app = app
route_manager = get_route_manager(request)
assert route_manager is app.state.route_manager
if __name__ == "__main__":
pytest.main([__file__, "-v"])