fix tests
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
"""Unit tests for setup redirect middleware."""
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
from starlette.responses import JSONResponse
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
|
||||
from src.server.middleware.setup_redirect import SetupRedirectMiddleware
|
||||
from src.server.services.auth_service import auth_service
|
||||
@@ -46,9 +45,11 @@ def app():
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(app):
|
||||
"""Create a test client."""
|
||||
return TestClient(app)
|
||||
async def client(app):
|
||||
"""Create an async test client."""
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
||||
yield ac
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
@@ -95,10 +96,11 @@ def reset_config_service():
|
||||
class TestSetupRedirectMiddleware:
|
||||
"""Test cases for setup redirect middleware."""
|
||||
|
||||
def test_redirect_to_setup_when_not_configured(self, client):
|
||||
"""Test that HTML requests are redirected to /setup when not configured."""
|
||||
@pytest.mark.asyncio
|
||||
async def test_redirect_to_setup_when_not_configured(self, client):
|
||||
"""Test that HTML requests redirect to /setup when not configured."""
|
||||
# Request home page with HTML accept header (don't follow redirects)
|
||||
response = client.get(
|
||||
response = await client.get(
|
||||
"/", headers={"Accept": "text/html"}, follow_redirects=False
|
||||
)
|
||||
|
||||
@@ -106,36 +108,40 @@ class TestSetupRedirectMiddleware:
|
||||
assert response.status_code == 302
|
||||
assert response.headers["location"] == "/setup"
|
||||
|
||||
def test_setup_page_accessible_without_config(self, client):
|
||||
"""Test that /setup page is accessible even when not configured."""
|
||||
response = client.get("/setup")
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_page_accessible_without_config(self, client):
|
||||
"""Test that /setup page is accessible when not configured."""
|
||||
response = await client.get("/setup")
|
||||
|
||||
# Should not redirect
|
||||
assert response.status_code == 200
|
||||
assert response.json()["message"] == "Setup page"
|
||||
|
||||
def test_api_returns_503_when_not_configured(self, client):
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_returns_503_when_not_configured(self, client):
|
||||
"""Test that API requests return 503 when not configured."""
|
||||
response = client.get("/api/data")
|
||||
response = await client.get("/api/data")
|
||||
|
||||
# Should return 503 Service Unavailable
|
||||
assert response.status_code == 503
|
||||
assert "setup_url" in response.json()
|
||||
assert response.json()["setup_url"] == "/setup"
|
||||
|
||||
def test_exempt_api_endpoints_accessible(self, client):
|
||||
@pytest.mark.asyncio
|
||||
async def test_exempt_api_endpoints_accessible(self, client):
|
||||
"""Test that exempt API endpoints are accessible without setup."""
|
||||
# Health endpoint should be accessible
|
||||
response = client.get("/api/health")
|
||||
response = await client.get("/api/health")
|
||||
assert response.status_code == 200
|
||||
assert response.json()["status"] == "ok"
|
||||
|
||||
# Auth status endpoint should be accessible
|
||||
response = client.get("/api/auth/status")
|
||||
response = await client.get("/api/auth/status")
|
||||
assert response.status_code == 200
|
||||
assert response.json()["configured"] is False
|
||||
|
||||
def test_no_redirect_when_configured(self, client):
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_redirect_when_configured(self, client):
|
||||
"""Test that no redirect happens when auth and config are set up."""
|
||||
# Configure auth service
|
||||
auth_service.setup_master_password("Test@Password123")
|
||||
@@ -147,13 +153,14 @@ class TestSetupRedirectMiddleware:
|
||||
config_service.save_config(config, create_backup=False)
|
||||
|
||||
# Request home page
|
||||
response = client.get("/", headers={"Accept": "text/html"})
|
||||
response = await client.get("/", headers={"Accept": "text/html"})
|
||||
|
||||
# Should not redirect
|
||||
assert response.status_code == 200
|
||||
assert response.json()["message"] == "Home page"
|
||||
|
||||
def test_api_works_when_configured(self, client):
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_works_when_configured(self, client):
|
||||
"""Test that API requests work normally when configured."""
|
||||
# Configure auth service
|
||||
auth_service.setup_master_password("Test@Password123")
|
||||
@@ -165,44 +172,44 @@ class TestSetupRedirectMiddleware:
|
||||
config_service.save_config(config, create_backup=False)
|
||||
|
||||
# Request API endpoint
|
||||
response = client.get("/api/data")
|
||||
response = await client.get("/api/data")
|
||||
|
||||
# Should work normally
|
||||
assert response.status_code == 200
|
||||
assert response.json()["data"] == "some data"
|
||||
|
||||
def test_static_files_always_accessible(self, client):
|
||||
@pytest.mark.asyncio
|
||||
async def test_static_files_always_accessible(self, client, app):
|
||||
"""Test that static file paths are always accessible."""
|
||||
# Create a route that mimics static file serving
|
||||
from fastapi import FastAPI
|
||||
app = client.app
|
||||
|
||||
@app.get("/static/css/style.css")
|
||||
async def static_css():
|
||||
return {"content": "css"}
|
||||
|
||||
# Request static file
|
||||
response = client.get("/static/css/style.css")
|
||||
response = await client.get("/static/css/style.css")
|
||||
|
||||
# Should be accessible even without setup
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_redirect_when_only_auth_configured(self, client):
|
||||
@pytest.mark.asyncio
|
||||
async def test_redirect_when_only_auth_configured(self, client):
|
||||
"""Test redirect when auth is configured but config is invalid."""
|
||||
# Configure auth but don't create config file
|
||||
auth_service.setup_master_password("Test@Password123")
|
||||
|
||||
# Request home page
|
||||
response = client.get("/", headers={"Accept": "text/html"})
|
||||
response = await client.get("/", headers={"Accept": "text/html"})
|
||||
|
||||
# Should still work because load_config creates default config
|
||||
# This is the current behavior - may need to adjust if we want
|
||||
# stricter setup requirements
|
||||
assert response.status_code in [200, 302]
|
||||
|
||||
def test_root_path_redirect(self, client):
|
||||
@pytest.mark.asyncio
|
||||
async def test_root_path_redirect(self, client):
|
||||
"""Test that root path redirects to setup when not configured."""
|
||||
response = client.get(
|
||||
response = await client.get(
|
||||
"/", headers={"Accept": "text/html"}, follow_redirects=False
|
||||
)
|
||||
|
||||
@@ -210,8 +217,8 @@ class TestSetupRedirectMiddleware:
|
||||
assert response.status_code == 302
|
||||
assert response.headers["location"] == "/setup"
|
||||
|
||||
def test_path_matching_exact_and_prefix(self, client):
|
||||
"""Test that path matching works for both exact and prefix matches."""
|
||||
def test_path_matching_exact_and_prefix(self):
|
||||
"""Test that path matching works for both exact and prefix."""
|
||||
middleware = SetupRedirectMiddleware(app=FastAPI())
|
||||
|
||||
# Exact matches
|
||||
|
||||
Reference in New Issue
Block a user