Skip to content

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

fix(learner): restore profile reads

XZH7b65b9a35e84
diff --git a/deeptutor/multi_user/identity.py b/deeptutor/multi_user/identity.py
index 5e2a53f20..b7ac16e48 100644
--- a/deeptutor/multi_user/identity.py
+++ b/deeptutor/multi_user/identity.py
@@ -279,6 +279,15 @@ def get_user_by_id(user_id: str) -> tuple[str, dict[str, Any]] | None:
return None
+def get_learner_profile(username: str) -> dict[str, Any] | None:
+ """Return one ordinary user's structured learner profile."""
+ record = load_users().get(username)
+ if record is None or str(record.get("role") or "user") != "user":
+ return None
+ profile = record.get("learner_profile")
+ return profile if isinstance(profile, dict) else None
+
+
def set_learner_profile(username: str, profile: dict[str, Any] | None) -> dict[str, Any] | None:
"""Atomically replace one ordinary user's structured learner profile."""
with _USERS_WRITE_LOCK:
diff --git a/tests/multi_user/test_profile_router.py b/tests/multi_user/test_profile_router.py
index ae7fb10d0..d96aad452 100644
--- a/tests/multi_user/test_profile_router.py
+++ b/tests/multi_user/test_profile_router.py
@@ -99,6 +99,57 @@ def test_put_profile_sets_marker_on_own_record_only(profile_client):
assert users["alice"]["avatar"] == ""
+def test_learner_profile_endpoints_are_self_service(profile_client):
+ from deeptutor.multi_user.identity import load_users
+
+ client, _ = profile_client
+ url = "/api/v1/auth/profile/learner-profile"
+
+ assert client.get(url).status_code == 401
+ assert client.get(url, headers=_auth("user-token")).json() == {"learner_profile": None}
+ assert client.get(url, headers=_auth("admin-token")).status_code == 403
+
+ response = client.put(
+ url,
+ headers=_auth("user-token"),
+ json={"age": 9, "grade_level": " primary_4 ", "language": "zh-CN"},
+ )
+ assert response.status_code == 200
+ assert response.json() == {
+ "learner_profile": {
+ "schema_version": 1,
+ "age": 9,
+ "grade_level": "primary_4",
+ "language": "zh-CN",
+ }
+ }
+ assert client.get(url, headers=_auth("user-token")).json() == response.json()
+ assert load_users()["bob"]["learner_profile"]["grade_level"] == "primary_4"
+ assert load_users()["alice"]["learner_profile"] is None
+
+ invalid = client.put(url, headers=_auth("user-token"), json={"grade_level": " "})
+ assert invalid.status_code == 422
+
+
+def test_admin_learner_profile_endpoints_are_scoped_to_ordinary_users(profile_client):
+ client, _ = profile_client
+ url = "/api/v1/auth/users/bob/learner-profile"
+
+ assert client.get(url, headers=_auth("user-token")).status_code == 403
+ assert client.get(url, headers=_auth("admin-token")).json() == {"learner_profile": None}
+
+ response = client.put(
+ url, headers=_auth("admin-token"), json={"age": 10, "explanation_style": "concrete"}
+ )
+ assert response.status_code == 200
+ assert response.json()["learner_profile"]["age"] == 10
+ assert client.get(url, headers=_auth("admin-token")).json() == response.json()
+
+ admin_url = "/api/v1/auth/users/alice/learner-profile"
+ assert client.get(admin_url, headers=_auth("admin-token")).status_code == 404
+ assert client.put(admin_url, headers=_auth("admin-token"), json={"age": 10}).status_code == 404
+
+
def test_put_profile_rejects_img_and_malformed_markers(profile_client):
client, _ = profile_client
for bad in ("img:1", "icon:Leaf:teal", "icon:a:b:c", "../etc/passwd"):