generated from john/python-template
ui redirects
This commit is contained in:
@@ -6,6 +6,8 @@ from contextlib import AsyncExitStack
|
|||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
from fastapi import status
|
||||||
|
from fastapi.responses import RedirectResponse
|
||||||
|
|
||||||
from .api.errors import register_error_handlers
|
from .api.errors import register_error_handlers
|
||||||
from .api.health import router as health_router
|
from .api.health import router as health_router
|
||||||
@@ -50,6 +52,15 @@ async def _lifespan(app: FastAPI):
|
|||||||
def create_app() -> FastAPI:
|
def create_app() -> FastAPI:
|
||||||
"""Create and configure the FastAPI application."""
|
"""Create and configure the FastAPI application."""
|
||||||
app = FastAPI(title="Transcription", lifespan=_lifespan)
|
app = FastAPI(title="Transcription", lifespan=_lifespan)
|
||||||
|
|
||||||
|
@app.get("/", include_in_schema=False)
|
||||||
|
async def root_redirect() -> RedirectResponse:
|
||||||
|
return RedirectResponse(url="/ui", status_code=status.HTTP_307_TEMPORARY_REDIRECT)
|
||||||
|
|
||||||
|
@app.get("/ui", include_in_schema=False)
|
||||||
|
async def ui_redirect() -> RedirectResponse:
|
||||||
|
return RedirectResponse(url="/ui/upload", status_code=status.HTTP_307_TEMPORARY_REDIRECT)
|
||||||
|
|
||||||
register_error_handlers(app)
|
register_error_handlers(app)
|
||||||
register_pages(app)
|
register_pages(app)
|
||||||
app.include_router(health_router)
|
app.include_router(health_router)
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
"""Tests for UI page registration wiring."""
|
"""Tests for UI page registration wiring."""
|
||||||
|
|
||||||
from fastapi import FastAPI
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from fastapi import FastAPI
|
||||||
|
|
||||||
from transcription.ui import register_pages
|
from transcription.ui import register_pages
|
||||||
|
|
||||||
@@ -11,16 +10,29 @@ from transcription.ui import register_pages
|
|||||||
class TestPageRegistration:
|
class TestPageRegistration:
|
||||||
"""Verify page registration and route wiring."""
|
"""Verify page registration and route wiring."""
|
||||||
|
|
||||||
def test_register_pages_adds_expected_routes(self):
|
def test_register_pages_wires_upload_jobs_and_mount(self, monkeypatch):
|
||||||
"""register_pages wires upload and jobs routes into the app."""
|
"""register_pages registers pages and mounts NiceGUI at /ui."""
|
||||||
|
calls: list[str] = []
|
||||||
|
|
||||||
|
def _record_upload() -> None:
|
||||||
|
calls.append("upload")
|
||||||
|
|
||||||
|
def _record_jobs() -> None:
|
||||||
|
calls.append("jobs")
|
||||||
|
|
||||||
|
def _record_run_with(
|
||||||
|
_app: FastAPI,
|
||||||
|
*,
|
||||||
|
mount_path: str,
|
||||||
|
show_welcome_message: bool,
|
||||||
|
) -> None:
|
||||||
|
calls.append(f"run_with:{mount_path}:{show_welcome_message}")
|
||||||
|
|
||||||
|
monkeypatch.setattr("transcription.ui.register_upload_page", _record_upload)
|
||||||
|
monkeypatch.setattr("transcription.ui.register_jobs_page", _record_jobs)
|
||||||
|
monkeypatch.setattr("transcription.ui.ui.run_with", _record_run_with)
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
register_pages(app)
|
register_pages(app)
|
||||||
app.add_api_route("/healthz", lambda: {"status": "ok"}, methods=["GET"])
|
|
||||||
|
|
||||||
client = TestClient(app)
|
assert calls == ["upload", "jobs", "run_with:/ui:False"]
|
||||||
ui_response = client.get("/ui")
|
|
||||||
health_response = client.get("/healthz")
|
|
||||||
|
|
||||||
assert ui_response.status_code == 200
|
|
||||||
assert health_response.status_code == 200
|
|
||||||
assert health_response.json() == {"status": "ok"}
|
|
||||||
|
|||||||
@@ -31,6 +31,20 @@ def client(tmp_path: Path):
|
|||||||
class TestPageRendering:
|
class TestPageRendering:
|
||||||
"""Verify the upload page is available and includes the main controls."""
|
"""Verify the upload page is available and includes the main controls."""
|
||||||
|
|
||||||
|
def test_root_redirects_to_ui(self, client):
|
||||||
|
"""GET / redirects to the UI mount point."""
|
||||||
|
response = client.get("/", follow_redirects=False)
|
||||||
|
|
||||||
|
assert response.status_code == 307
|
||||||
|
assert response.headers["location"] == "/ui"
|
||||||
|
|
||||||
|
def test_ui_redirects_to_upload(self, client):
|
||||||
|
"""GET /ui redirects to the upload page."""
|
||||||
|
response = client.get("/ui", follow_redirects=False)
|
||||||
|
|
||||||
|
assert response.status_code == 307
|
||||||
|
assert response.headers["location"] == "/ui/upload"
|
||||||
|
|
||||||
def test_upload_page_renders_expected_controls(self, client):
|
def test_upload_page_renders_expected_controls(self, client):
|
||||||
"""GET /ui/upload returns the page shell and upload controls."""
|
"""GET /ui/upload returns the page shell and upload controls."""
|
||||||
response = client.get("/ui/upload")
|
response = client.get("/ui/upload")
|
||||||
|
|||||||
Reference in New Issue
Block a user