feat: 登录后进入实时首页,并加密保存可刷新的登录凭证

This commit is contained in:
2026-10-26 18:12:00 +08:00
parent eaad01d5a8
commit 91707f959f
14 changed files with 748 additions and 136 deletions
+71 -5
View File
@@ -1,3 +1,5 @@
import json
from fastapi.testclient import TestClient
from sqlalchemy import text
@@ -10,12 +12,61 @@ 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 {"accountId": self.login_name}
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:
@@ -26,21 +77,36 @@ def setup_function() -> None:
init_db()
def test_login_stores_only_employee_id_and_password() -> None:
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"}
listed = client.get("/api/accounts").json()
assert listed == [{"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"
assert set(account.__table__.columns.keys()) == {"employee_id", "password_cipher"}
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)