Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

test(api): adapt knowledge_router/file_preview tests to lazy route inclusion

polaris-smart664f8a61ab81
diff --git a/tests/api/test_file_preview.py b/tests/api/test_file_preview.py
index 5c783a2a2..1c5771082 100644
--- a/tests/api/test_file_preview.py
+++ b/tests/api/test_file_preview.py
@@ -21,16 +21,45 @@ def _request(query: str = "") -> SimpleNamespace:
return SimpleNamespace(query_params=QueryParams(query), headers={})
+def _file_preview_routes(app):
+ """Materialize lazily-included routers (new-fastapi ``_IncludedRouter``).
+
+ Recent FastAPI turns ``include_router`` into lazy wrappers carrying the
+ prefix and router-level dependencies in ``include_context``; flatten them
+ the same way ``tests/video_learning/test_router.py`` does.
+ """
+ flattened = []
+ for route in app.routes:
+ nested = getattr(route, "original_router", None)
+ if nested is None:
+ if getattr(route, "path", "") == "/api/file-preview/pdf":
+ flattened.append(SimpleNamespace(dependant=route.dependant))
+ continue
+ ctx = getattr(route, "include_context", None)
+ prefix = str(getattr(ctx, "prefix", "") or "")
+ router_deps = list(getattr(ctx, "dependencies", None) or [])
+ for inner in nested.routes:
+ if prefix + str(getattr(inner, "path", "")) != "/api/file-preview/pdf":
+ continue
+ deps = list(inner.dependant.dependencies) + router_deps
+ flattened.append(SimpleNamespace(dependencies=deps))
+ return flattened
+
+
def test_preview_route_requires_authentication(monkeypatch) -> None:
from deeptutor.api.main import app
from deeptutor.api.routers import auth
- routes = [
- route for route in app.routes if getattr(route, "path", "") == "/api/file-preview/pdf"
- ]
+ routes = _file_preview_routes(app)
assert len(routes) == 2 # GET source and POST uploaded bytes
+
+ def _dep_call(dependency):
+ # Route-level deps are Dependant objects (.call); router-level deps
+ # coming from include_context are raw Depends (.dependency).
+ return getattr(dependency, "call", None) or getattr(dependency, "dependency", None)
+
assert all(
- any(dependency.call is auth.require_auth for dependency in route.dependant.dependencies)
+ any(_dep_call(dependency) is auth.require_auth for dependency in route.dependencies)
for route in routes
)
monkeypatch.setattr(auth, "AUTH_ENABLED", True)
diff --git a/tests/api/test_knowledge_router.py b/tests/api/test_knowledge_router.py
index 14977e421..2ddf5e776 100644
--- a/tests/api/test_knowledge_router.py
+++ b/tests/api/test_knowledge_router.py
@@ -53,6 +53,36 @@ def _build_app() -> FastAPI:
return app
+def _materialized_routes(app: "FastAPI"):
+ """Flatten app routes, materializing new-fastapi lazy ``_IncludedRouter``.
+
+ Recent FastAPI (>=0.130) turns ``include_router`` into lazy wrappers whose
+ ``original_router``/``include_context`` carry the prefix; ``app.routes``
+ only exposes real routes after mounting. Same flatten pattern as
+ ``tests/video_learning/test_router.py``. Method matching is not modelled —
+ every surface checked here is a GET reading route.
+ """
+ from starlette.routing import compile_path
+
+ for route in app.router.routes:
+ nested = getattr(route, "original_router", None)
+ if nested is None:
+ yield route
+ continue
+ ctx = getattr(route, "include_context", None)
+ prefix = str(getattr(ctx, "prefix", "") or "")
+ for inner in nested.routes:
+ template = prefix + str(getattr(inner, "path", ""))
+ path_regex, _, _ = compile_path(template)
+
+ def _matches(scope, _rx=path_regex):
+ if _rx.match(scope.get("path", "")):
+ return (Match.FULL, {})
+ return (Match.NONE, None)
+
+ yield SimpleNamespace(path=template, matches=_matches)
+
+
@pytest.mark.parametrize(
("path", "route_path", "surface"),
[
@@ -94,7 +124,9 @@ def test_learner_surface_uses_actual_kb_route_template(
) -> None:
app = _build_app()
scope = {"type": "http", "method": "GET", "path": path, "root_path": ""}
- matched = next(route for route in app.router.routes if route.matches(scope)[0] is Match.FULL)
+ matched = next(
+ route for route in _materialized_routes(app) if route.matches(scope)[0] is Match.FULL
+ )
assert matched.path == route_path
assert _learning_surface_for_path(path, "GET", route_path=matched.path) == surface