import json from fastapi.testclient import TestClient from sqlalchemy import text from euniv.client import JigsawRejected, ManualJigsaw from euniv.crypto import decrypt_secret from euniv.db import SessionLocal, engine, init_db from euniv.models import Account class FakeClient: def __init__(self, login_name: str, password: str) -> None: self.login_name = login_name self.password = password self._token = "yxt-1" self._profile = { "token": "ucs-1", "accountId": login_name, "userId": "u1", "ssoinfo": "sso", "unionId": "", } def close(self) -> None: return None def login(self) -> dict: return dict(self._profile) def authorize(self, profile: dict) -> None: self._profile = dict(profile) def export_credential(self) -> dict: return { "ucs_token": self._profile["token"], "ucs_account_id": self._profile["accountId"], "ucs_user_id": self._profile["userId"], "ucs_ssoinfo": self._profile["ssoinfo"], "ucs_union_id": self._profile["unionId"], "yxt_token": self._token, } def restore(self, credential: dict) -> None: self._token = credential["yxt_token"] self._profile = { "token": credential["ucs_token"], "accountId": credential["ucs_account_id"], "userId": credential["ucs_user_id"], "ssoinfo": credential["ucs_ssoinfo"], "unionId": credential["ucs_union_id"], } def home_snapshot(self) -> dict: if self._token == "yxt-1": self._token = "yxt-2" return { "name": "测试用户", "avatar": "https://example.test/a.jpg", "department": "测试部门", "level": "Scientist", "year_score": "1.00", "month_score": "0.20", "total_hours": "10", "total_score": "3.00", "today_hours": "1", "today_score": "0.10", "pending_exam_count": 1, "pending_exams": [{"name": "安全考试", "deadline": ""}], } def setup_function() -> None: with engine.begin() as connection: connection.execute(text("DROP TABLE IF EXISTS accounts")) connection.execute(text("DROP TABLE IF EXISTS schedules")) connection.execute(text("DROP TABLE IF EXISTS alembic_version")) init_db() def test_login_stores_password_and_credential_only() -> None: import euniv.api as api_module import euniv.service as service_module api_module.login_and_store = lambda db, employee_id, password: service_module.login_and_store( db, employee_id, password, FakeClient ) api_module.fetch_home = lambda db, employee_id: service_module.fetch_home(db, employee_id, FakeClient) client = TestClient(api_module.app) response = client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"}) assert response.status_code == 200 assert response.json() == {"employee_id": "6650000146"} with SessionLocal() as db: account = db.get(Account, "6650000146") assert account is not None assert decrypt_secret(account.password_cipher) == "secret-pass" stored = json.loads(decrypt_secret(account.credential_cipher)) assert stored["yxt_token"] == "yxt-1" assert stored["ucs_token"] == "ucs-1" assert set(account.__table__.columns.keys()) == {"employee_id", "password_cipher", "credential_cipher"} home = client.get("/api/home", params={"employee_id": "6650000146"}) assert home.status_code == 200 body = home.json() assert body["name"] == "测试用户" assert body["pending_exams"] == [{"name": "安全考试", "deadline": ""}] with SessionLocal() as db: account = db.get(Account, "6650000146") assert account is not None updated = json.loads(decrypt_secret(account.credential_cipher)) assert updated["yxt_token"] == "yxt-2" assert "测试用户" not in decrypt_secret(account.credential_cipher) assert "测试用户" not in decrypt_secret(account.password_cipher) def test_manual_jigsaw_keeps_image_pixels_and_retries() -> None: import euniv.api as api_module import euniv.service as service_module service_module._pending.clear() class ManualClient: def __init__(self, login_name: str, password: str) -> None: self.login_name = login_name self.password = password self._profile = { "token": "ucs-1", "accountId": login_name, "userId": "u1", "ssoinfo": "sso", "unionId": "", } def close(self) -> None: return None def login(self) -> dict: raise ManualJigsaw({ "big_img": "data:image/png;base64,YQ==", "small_img": "data:image/png;base64,Yg==", "y": 56, "width": 280, "height": 172, "piece_width": 48, "piece_height": 48, }) def next_jigsaw(self) -> dict: return { "big_img": "data:image/png;base64,Yw==", "small_img": "data:image/png;base64,ZA==", "y": 60, "width": 280, "height": 172, "piece_width": 48, "piece_height": 48, } def submit_jigsaw(self, offset: int) -> dict: if offset != 157: raise JigsawRejected("拼图未通过") return dict(self._profile) def authorize(self, profile: dict) -> None: self._profile = dict(profile) def export_credential(self) -> dict: return { "ucs_token": "ucs-1", "ucs_account_id": self.login_name, "ucs_user_id": "u1", "ucs_ssoinfo": "sso", "ucs_union_id": "", "yxt_token": "yxt-manual", } api_module.login_and_store = lambda db, employee_id, password: service_module.login_and_store( db, employee_id, password, ManualClient ) api_module.submit_jigsaw = service_module.submit_jigsaw client = TestClient(api_module.app) started = client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"}) assert started.status_code == 200 body = started.json() assert body["jigsaw"]["width"] == 280 assert body["jigsaw"]["height"] == 172 assert body["jigsaw"]["y"] == 56 assert "password" not in body["jigsaw"] with SessionLocal() as db: assert db.get(Account, "6650000146") is None missed = client.post("/api/login/jigsaw", json={"attempt_id": body["attempt_id"], "offset": 10}) assert missed.status_code == 200 assert missed.json()["jigsaw"]["y"] == 60 done = client.post("/api/login/jigsaw", json={"attempt_id": body["attempt_id"], "offset": 157}) assert done.status_code == 200 assert done.json() == {"employee_id": "6650000146"} with SessionLocal() as db: account = db.get(Account, "6650000146") assert account is not None assert decrypt_secret(account.password_cipher) == "secret-pass"