import json from fastapi.testclient import TestClient from sqlalchemy import text 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)