Skip to content

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

Block 9: backend endpoint tests (21 new + 3 list_all), fix stale local HTTPException imports

whf97496d651199
diff --git a/deeptutor/api/routers/guided_learning.py b/deeptutor/api/routers/guided_learning.py
index c8a2ff8c8..a71ee7dcf 100644
--- a/deeptutor/api/routers/guided_learning.py
+++ b/deeptutor/api/routers/guided_learning.py
@@ -89,7 +89,6 @@ async def list_all_progress():
@router.get("/progress/{book_id}")
async def get_progress(book_id: str):
if not book_id or ".." in book_id or "/" in book_id or "\\" in book_id or ":" in book_id:
- from fastapi import HTTPException
raise HTTPException(status_code=400, detail="Invalid book_id")
service = get_learning_service()
progress = service.get_or_create(book_id)
@@ -99,7 +98,6 @@ async def get_progress(book_id: str):
@router.post("/progress/{book_id}/answer")
async def submit_answer(book_id: str, body: AnswerRequest):
if not book_id or ".." in book_id or "/" in book_id or "\\" in book_id or ":" in book_id:
- from fastapi import HTTPException
raise HTTPException(status_code=400, detail="Invalid book_id")
service = get_learning_service()
scheduler = get_scheduler()
@@ -154,7 +152,6 @@ async def submit_answer(book_id: str, body: AnswerRequest):
@router.get("/progress/{book_id}/reviews")
async def get_reviews(book_id: str):
if not book_id or ".." in book_id or "/" in book_id or "\\" in book_id or ":" in book_id:
- from fastapi import HTTPException
raise HTTPException(status_code=400, detail="Invalid book_id")
service = get_learning_service()
scheduler = get_scheduler()
@@ -167,7 +164,6 @@ async def get_reviews(book_id: str):
@router.post("/progress/{book_id}/init-modules")
async def init_modules(book_id: str, body: InitModulesRequest):
if not book_id or ".." in book_id or "/" in book_id or "\\" in book_id or ":" in book_id:
- from fastapi import HTTPException
raise HTTPException(status_code=400, detail="Invalid book_id")
service = get_learning_service()
progress = service.get_or_create(book_id)
diff --git a/deeptutor/learning/tests/test_api_endpoints.py b/deeptutor/learning/tests/test_api_endpoints.py
new file mode 100644
index 000000000..be8a5257d
--- /dev/null
+++ b/deeptutor/learning/tests/test_api_endpoints.py
@@ -0,0 +1,196 @@
+"""API endpoint tests for guided_learning router."""
+
+import pytest
+from fastapi import FastAPI
+from fastapi.testclient import TestClient
+from deeptutor.api.routers.guided_learning import router
+from deeptutor.learning.storage import LearningStore
+
+
+def app(tmp_path, monkeypatch):
+ """Create a minimal FastAPI app with only the guided_learning router.
+ Monkeypatch LearningStore to use tmp_path for test isolation."""
+ def _make_store_with_tmp(root=None):
+ return LearningStore(root=tmp_path)
+ monkeypatch.setattr(
+ "deeptutor.api.routers.guided_learning.LearningStore",
+ _make_store_with_tmp,
+ )
+ app = FastAPI()
+ app.include_router(router, prefix="/api/v1/learning")
+ return app
+
+
+def client(app):
+ return TestClient(app)
+
+
+# -- GET /progress (list_all) --------------------------------------------
+
+class TestListProgress:
+ def test_list_empty(self, client):
+ resp = client.get("/api/v1/learning/progress")
+ assert resp.status_code == 200
+ assert resp.json() == []
+
+ def test_list_with_data(self, client):
+ client.post("/api/v1/learning/progress/testbook/init-modules",
+ json={"modules": [{"id": "m1", "name": "M1", "order": 0,
+ "knowledge_points": [{"id": "kp1", "name": "KP1",
+ "type": "concept", "module_id": "m1"}]}]})
+ resp = client.get("/api/v1/learning/progress")
+ assert resp.status_code == 200
+ book_ids = [p["book_id"] for p in resp.json()]
+ assert "testbook" in book_ids
+
+
+# -- POST /progress/{book_id}/init-modules --------------------------------
+
+class TestInitModules:
+ def test_init_basic(self, client):
+ resp = client.post("/api/v1/learning/progress/init1/init-modules",
+ json={"modules": [
+ {"id": "m1", "name": "Module 1", "order": 0,
+ "knowledge_points": [{"id": "kp1", "name": "KP1",
+ "type": "concept", "module_id": "m1"}]}
+ ]})
+ assert resp.status_code == 200
+ assert resp.json()["module_count"] == 1
+
+ def test_init_empty_modules(self, client):
+ resp = client.post("/api/v1/learning/progress/init2/init-modules",
+ json={"modules": []})
+ assert resp.status_code == 200
+ assert resp.json()["module_count"] == 0
+
+ def test_init_invalid_kp_returns_422(self, client):
+ resp = client.post("/api/v1/learning/progress/init3/init-modules",
+ json={"modules": [
+ {"id": "m1", "name": "M1", "order": 0,
+ "knowledge_points": [{"bad_key": "no_name"}]}
+ ]})
+ assert resp.status_code == 422
+
+
+# -- GET /progress/{book_id} ----------------------------------------------
+
+class TestGetProgress:
+ def test_get_progress_creates_on_fly(self, client):
+ resp = client.get("/api/v1/learning/progress/newbook")
+ assert resp.status_code == 200
+ assert resp.json()["book_id"] == "newbook"
+
+ def test_get_progress_invalid_id_returns_400(self, client):
+ resp = client.get("/api/v1/learning/progress/a\\b")
+ assert resp.status_code == 400
+
+
+# -- DELETE /progress/{book_id} -------------------------------------------
+
+class TestDeleteProgress:
+ def test_delete_success(self, client):
+ client.post("/api/v1/learning/progress/del1/init-modules",
+ json={"modules": []})
+ resp = client.delete("/api/v1/learning/progress/del1")
+ assert resp.status_code == 200
+ assert resp.json()["status"] == "ok"
+
+ def test_delete_nonexistent_returns_404(self, client):
+ resp = client.delete("/api/v1/learning/progress/nonexistent42")
+ assert resp.status_code == 404
+
+ def test_delete_twice_returns_404(self, client):
+ client.post("/api/v1/learning/progress/del2/init-modules",
+ json={"modules": []})
+ client.delete("/api/v1/learning/progress/del2")
+ resp = client.delete("/api/v1/learning/progress/del2")
+ assert resp.status_code == 404
+
+ def test_delete_invalid_book_id_returns_400(self, client):
+ resp = client.delete("/api/v1/learning/progress/a\\b")
+ assert resp.status_code == 400
+
+
+# -- POST /progress/{book_id}/redo ----------------------------------------
+
+class TestRedoProgress:
+ def test_redo_resets_stage(self, client):
+ client.post("/api/v1/learning/progress/redo1/init-modules",
+ json={"modules": [{"id": "m1", "name": "M1", "order": 0,
+ "knowledge_points": []}]})
+ resp = client.post("/api/v1/learning/progress/redo1/redo")
+ assert resp.status_code == 200
+ prog = client.get("/api/v1/learning/progress/redo1").json()
+ assert prog["current_stage"] == "diagnostic_phase1"
+
+ def test_redo_nonexistent_returns_404(self, client):
+ resp = client.post("/api/v1/learning/progress/nope42/redo")
+ assert resp.status_code == 404
+
+
+# -- POST /progress/{book_id}/import-from-book ----------------------------
+
+class TestImportFromBook:
+ def test_import_two_chapters(self, client):
+ resp = client.post("/api/v1/learning/progress/import1/import-from-book",
+ json={"chapters": [
+ {"title": "Ch1", "knowledge_points": ["KP1", "KP2"]},
+ {"title": "Ch2", "knowledge_points": ["KP3"]},
+ ]})
+ assert resp.status_code == 200
+ data = resp.json()
+ assert data["module_count"] == 2
+ assert data["status"] == "ok"
+
+ prog = client.get("/api/v1/learning/progress/import1").json()
+ assert len(prog["modules"]) == 2
+
+ def test_import_empty_chapters(self, client):
+ resp = client.post("/api/v1/learning/progress/import2/import-from-book",
+ json={"chapters": []})
+ assert resp.status_code == 200
+ assert resp.json()["module_count"] == 0
+
+
+# -- POST /progress/{book_id}/generate-from-notebook ----------------------
+
+class TestGenerateFromNotebook:
+ def test_missing_records_returns_400(self, client):
+ resp = client.post("/api/v1/learning/progress/nb1/generate-from-notebook",
+ json={"notebook_id": "nb", "records": []})
+ assert resp.status_code == 400
+
+ def test_invalid_book_id_returns_400(self, client):
+ resp = client.post("/api/v1/learning/progress/a\\b/generate-from-notebook",
+ json={"notebook_id": "nb",
+ "records": [{"id": "r1", "type": "note", "title": "T", "output": "O"}]})
+ assert resp.status_code == 400
+
+
+# -- book_id validation consistency ----------------------------------------
+
+class TestBookIdValidation:
+ """Verify all endpoints reject dangerous book_id characters."""
+
+ # NOTE: `..` and `/` are normalized by HTTP clients before reaching the
+ # handler, so they cannot be tested at the HTTP level. Storage-level
+ # path-traversal rejection is covered in test_storage.py.
+ # Here we test `\` and `:` which survive URL transport.
+
+ @pytest.mark.parametrize("method,path,body", [
+ ("GET", "/api/v1/learning/progress/a\\b", None),
+ ("DELETE", "/api/v1/learning/progress/a\\b", None),
+ ("POST", "/api/v1/learning/progress/D:foo/init-modules", {"modules": []}),
+ ("POST", "/api/v1/learning/progress/foo:bar/import-from-book", {"chapters": []}),
+ ])
+ def test_evil_book_id_rejected(self, client, method, path, body):
+ kwargs = {"json": body} if body is not None else {}
+ if method == "GET":
+ resp = client.get(path, **kwargs)
+ elif method == "POST":
+ resp = client.post(path, **kwargs)
+ elif method == "DELETE":
+ resp = client.delete(path, **kwargs)
+ assert resp.status_code == 400, f"{method} {path} should return 400, got {resp.status_code}"
diff --git a/deeptutor/learning/tests/test_storage.py b/deeptutor/learning/tests/test_storage.py
index b3d9140d6..bcda63acd 100644
--- a/deeptutor/learning/tests/test_storage.py
+++ b/deeptutor/learning/tests/test_storage.py
@@ -101,3 +101,22 @@ class TestPathTraversal:
def test_rejects_in_save(self, store):
with pytest.raises(ValueError, match="Invalid book_id"):
store.save(LearningProgress(book_id="../evil"))
+
+
+# ── list_all ──────────────────────────────────────────────────────────────
+
+class TestListAll:
+ def test_list_all_empty(self, store):
+ assert store.list_all() == []
+
+ def test_list_all_multiple(self, store):
+ store.save(LearningProgress(book_id="a"))
+ store.save(LearningProgress(book_id="b"))
+ ids = store.list_all()
+ assert sorted(ids) == ["a", "b"]
+
+ def test_list_all_after_delete(self, store):
+ store.save(LearningProgress(book_id="x"))
+ store.save(LearningProgress(book_id="y"))
+ store.delete("x")
+ assert store.list_all() == ["y"]