This commit is contained in:
1 parent
67dcea21d0
commit
64e7cc5d8b
14 files changed
+689
-50
No files matched your search
@@ -0,0 +1,155 @@
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi.security import HTTPAuthorizationCredentials
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
||||
|
||||
from backend import db, main
|
||||
from backend.auth import hash_password
|
||||
|
||||
|
||||
class EnvPatch:
|
||||
def __init__(self):
|
||||
self.attributes = []
|
||||
|
||||
def setattr(self, target, name, value):
|
||||
self.attributes.append((target, name, getattr(target, name)))
|
||||
setattr(target, name, value)
|
||||
|
||||
def restore(self):
|
||||
while self.attributes:
|
||||
target, name, original = self.attributes.pop()
|
||||
setattr(target, name, original)
|
||||
|
||||
|
||||
class TempDatabase:
|
||||
def __enter__(self):
|
||||
self.temp_dir = tempfile.TemporaryDirectory(ignore_cleanup_errors=True)
|
||||
self.original = db.DATABASE_PATH
|
||||
db.DATABASE_PATH = Path(self.temp_dir.name) / "test.sqlite"
|
||||
db.initialize_database()
|
||||
return db.DATABASE_PATH
|
||||
|
||||
def __exit__(self, exc_type, exc, tb):
|
||||
db.DATABASE_PATH = self.original
|
||||
self.temp_dir.cleanup()
|
||||
|
||||
|
||||
def configure_auth(monkeypatch):
|
||||
monkeypatch.setattr(main, "get_config_value", lambda key, default="": {
|
||||
"ADMIN_USERNAME": "admin",
|
||||
"ADMIN_PASSWORD": "admin123",
|
||||
"AUTH_SECRET": "test-secret",
|
||||
}.get(key, default))
|
||||
|
||||
|
||||
def create_student_session():
|
||||
user = db.register_user_with_activation(
|
||||
username="student",
|
||||
password_hash=hash_password("secret123"),
|
||||
activation_code="SL-ABC123-DEF456-GHI789",
|
||||
)
|
||||
login = main.api_auth_login(main.LoginRequest(username="student", password="secret123"))
|
||||
return main.require_session(
|
||||
HTTPAuthorizationCredentials(scheme="Bearer", credentials=login["token"])
|
||||
), user
|
||||
|
||||
|
||||
def create_video_record():
|
||||
return db.create_video(
|
||||
title="Lesson One",
|
||||
speaker="Teacher",
|
||||
description="",
|
||||
tags=["口语"],
|
||||
video_url="https://example.com/lesson-one.mp4",
|
||||
subtitles=[],
|
||||
)
|
||||
|
||||
|
||||
def test_learning_progress_round_trips_for_logged_in_user(monkeypatch):
|
||||
configure_auth(monkeypatch)
|
||||
|
||||
with TempDatabase():
|
||||
db.create_activation_codes(["SL-ABC123-DEF456-GHI789"], "admin")
|
||||
session, _user = create_student_session()
|
||||
video = create_video_record()
|
||||
|
||||
saved = main.api_save_my_progress(
|
||||
main.LearningStateRequest(
|
||||
favorites=[str(video["id"])],
|
||||
progress={
|
||||
str(video["id"]): main.VideoProgressPayload(
|
||||
currentTime=42.6,
|
||||
activeLineIndex=3,
|
||||
practicedLineIndexes=[0, 2, 2],
|
||||
difficultLineIndexes=[1],
|
||||
updatedAt="2026-06-14T10:00:00.000Z",
|
||||
)
|
||||
},
|
||||
),
|
||||
session=session,
|
||||
)
|
||||
loaded = main.api_get_my_progress(session=session)
|
||||
|
||||
assert saved == loaded
|
||||
assert loaded["favorites"] == [str(video["id"])]
|
||||
progress = loaded["progress"][str(video["id"])]
|
||||
assert progress["currentTime"] == 42.6
|
||||
assert progress["activeLineIndex"] == 3
|
||||
assert progress["practicedLineIndexes"] == [0, 2]
|
||||
assert progress["difficultLineIndexes"] == [1]
|
||||
|
||||
|
||||
def test_learning_progress_save_replaces_removed_favorite_and_merges_progress(monkeypatch):
|
||||
configure_auth(monkeypatch)
|
||||
|
||||
with TempDatabase():
|
||||
db.create_activation_codes(["SL-ABC123-DEF456-GHI789"], "admin")
|
||||
session, _user = create_student_session()
|
||||
video = create_video_record()
|
||||
|
||||
main.api_save_my_progress(
|
||||
main.LearningStateRequest(
|
||||
favorites=[str(video["id"])],
|
||||
progress={
|
||||
str(video["id"]): main.VideoProgressPayload(
|
||||
currentTime=10,
|
||||
practicedLineIndexes=[0],
|
||||
difficultLineIndexes=[2],
|
||||
)
|
||||
},
|
||||
),
|
||||
session=session,
|
||||
)
|
||||
loaded = main.api_save_my_progress(
|
||||
main.LearningStateRequest(
|
||||
favorites=[],
|
||||
progress={
|
||||
str(video["id"]): main.VideoProgressPayload(
|
||||
currentTime=12,
|
||||
practicedLineIndexes=[0, 1],
|
||||
difficultLineIndexes=[],
|
||||
)
|
||||
},
|
||||
),
|
||||
session=session,
|
||||
)
|
||||
|
||||
assert loaded["favorites"] == []
|
||||
assert loaded["progress"][str(video["id"])]["currentTime"] == 12
|
||||
assert loaded["progress"][str(video["id"])]["practicedLineIndexes"] == [0, 1]
|
||||
assert loaded["progress"][str(video["id"])]["difficultLineIndexes"] == []
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
patch = EnvPatch()
|
||||
try:
|
||||
configure_auth(patch)
|
||||
test_learning_progress_round_trips_for_logged_in_user(patch)
|
||||
patch.restore()
|
||||
configure_auth(patch)
|
||||
test_learning_progress_save_replaces_removed_favorite_and_merges_progress(patch)
|
||||
finally:
|
||||
patch.restore()
|
||||
Reference in new issue
Block a user