feat: 增加管理后台的会话、配置和用户管理

This commit is contained in:
Nixevol
2026-10-28 03:53:52 +08:00
parent d42691aebb
commit ad52207a93
17 changed files with 1382 additions and 70 deletions
+23 -25
View File
@@ -1,11 +1,10 @@
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.db import SessionLocal
from euniv.models import Account
@@ -25,7 +24,7 @@ class FakeClient:
def close(self) -> None:
return None
def login(self, manual: bool = False) -> dict:
def login(self, manual: bool = False, auto_attempts: int = 3) -> dict:
if manual:
raise ManualJigsaw({
"big_img": "data:image/png;base64,YQ==",
@@ -83,26 +82,18 @@ class FakeClient:
}
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, manual=False: service_module.login_and_store(
db, employee_id, password, FakeClient, manual=manual
api_module.login_and_store = lambda db, employee_id, password, manual=False, auto_attempts=3: service_module.login_and_store(
db, employee_id, password, FakeClient, manual=manual, auto_attempts=auto_attempts
)
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"}
assert response.json() == {"role": "user", "employee_id": "6650000146"}
with SessionLocal() as db:
account = db.get(Account, "6650000146")
assert account is not None
@@ -110,7 +101,14 @@ def test_login_stores_password_and_credential_only() -> None:
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"}
assert set(account.__table__.columns.keys()) == {
"employee_id",
"password_cipher",
"credential_cipher",
"refresh_paused",
"last_refresh_at",
"credential_valid",
}
home = client.get("/api/home")
assert home.status_code == 200
@@ -147,7 +145,7 @@ def test_manual_jigsaw_keeps_image_pixels_and_retries() -> None:
def close(self) -> None:
return None
def login(self, manual: bool = False) -> dict:
def login(self, manual: bool = False, auto_attempts: int = 3) -> dict:
raise ManualJigsaw({
"big_img": "data:image/png;base64,YQ==",
"small_img": "data:image/png;base64,Yg==",
@@ -187,8 +185,8 @@ def test_manual_jigsaw_keeps_image_pixels_and_retries() -> None:
"yxt_token": "yxt-manual",
}
api_module.login_and_store = lambda db, employee_id, password, manual=False: service_module.login_and_store(
db, employee_id, password, ManualClient, manual=manual
api_module.login_and_store = lambda db, employee_id, password, manual=False, auto_attempts=3: service_module.login_and_store(
db, employee_id, password, ManualClient, manual=manual, auto_attempts=auto_attempts
)
api_module.submit_jigsaw = service_module.submit_jigsaw
client = TestClient(api_module.app)
@@ -208,7 +206,7 @@ def test_manual_jigsaw_keeps_image_pixels_and_retries() -> None:
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"}
assert done.json() == {"role": "user", "employee_id": "6650000146"}
with SessionLocal() as db:
account = db.get(Account, "6650000146")
assert account is not None
@@ -222,12 +220,12 @@ def test_browser_session_logout_and_revoke() -> None:
calls = {"login": 0}
class RememberClient(FakeClient):
def login(self, manual: bool = False) -> dict:
def login(self, manual: bool = False, auto_attempts: int = 3) -> dict:
calls["login"] += 1
return super().login(manual=manual)
return super().login(manual=manual, auto_attempts=auto_attempts)
api_module.login_and_store = lambda db, employee_id, password, manual=False: service_module.login_and_store(
db, employee_id, password, RememberClient, manual=manual
api_module.login_and_store = lambda db, employee_id, password, manual=False, auto_attempts=3: service_module.login_and_store(
db, employee_id, password, RememberClient, manual=manual, auto_attempts=auto_attempts
)
client = TestClient(api_module.app)
first = client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"})
@@ -237,13 +235,13 @@ def test_browser_session_logout_and_revoke() -> None:
assert account is not None
before = decrypt_secret(account.credential_cipher)
second = client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"})
assert second.json() == {"employee_id": "6650000146"}
assert second.json() == {"role": "user", "employee_id": "6650000146"}
assert calls["login"] == 1
with SessionLocal() as db:
account = db.get(Account, "6650000146")
assert account is not None
assert decrypt_secret(account.credential_cipher) == before
assert client.get("/api/session").json() == {"employee_id": "6650000146"}
assert client.get("/api/session").json() == {"role": "user", "employee_id": "6650000146"}
assert client.post("/api/logout").status_code == 200
assert client.get("/api/session").status_code == 401
with SessionLocal() as db: