fix: 凭据有效改为当场取到个人信息和本月学时

This commit is contained in:
Nixevol
2026-11-21 15:01:54 +08:00
parent a6306e8339
commit a5539405ca
3 changed files with 85 additions and 26 deletions
+34 -23
View File
@@ -116,23 +116,34 @@ def _require(db: Session, employee_id: str) -> Account:
return account return account
def _checked_hours(client) -> tuple[str, str]: def describe_credential(client) -> dict:
"""用当前凭据现查个人信息和本月学时。两项都取到才算有效,取不到的字段留空。"""
from admin.settings import source_for_employee from admin.settings import source_for_employee
from euniv.service import _month_hours from euniv.service import _month_hours
try: info_ok = bool(client.credential_alive())
hours = _month_hours(client) profile = _checked_profile(client) if info_ok else {}
except Exception: if not str(profile.get("name") or "").strip():
hours = "" info_ok = False
profile = {}
hours = ""
official = "" official = ""
if hours and source_for_employee(str(getattr(client, "login_name", "") or "")) == "history": if info_ok:
reader = getattr(client, "month_hours", None) try:
if callable(reader): hours = str(_month_hours(client) or "").strip()
try: except Exception:
official = str(reader() or "").strip() hours = ""
except Exception: source = source_for_employee(str(getattr(client, "login_name", "") or ""))
official = "" if source == "history":
return hours, official reader = getattr(client, "month_hours", None)
if callable(reader):
try:
official = str(reader() or "").strip()
except Exception:
official = ""
else:
official = hours
return {"valid": info_ok and bool(hours), "profile": profile, "hours": hours, "official": official}
def _checked_profile(client) -> dict: def _checked_profile(client) -> dict:
@@ -168,12 +179,12 @@ def check_employee(db: Session, employee_id: str) -> dict:
client = _open_client(employee_id, password, proxy) client = _open_client(employee_id, password, proxy)
try: try:
client.restore(credential) client.restore(credential)
alive = bool(client.credential_alive()) found = describe_credential(client)
result = "有效" if alive else "已失效" result = "有效" if found["valid"] else "已失效"
valid = alive valid = bool(found["valid"])
profile = _checked_profile(client) if alive else {} profile = found["profile"]
if alive: hours = found["hours"]
hours, official_hours = _checked_hours(client) official_hours = found["official"]
break break
except Exception as exc: except Exception as exc:
profile = {} profile = {}
@@ -199,16 +210,16 @@ def check_employee(db: Session, employee_id: str) -> dict:
if account is None: if account is None:
raise HTTPException(status_code=404, detail="用户不存在") raise HTTPException(status_code=404, detail="用户不存在")
record_outcome(account, valid) record_outcome(account, valid)
if valid is True and profile: if profile:
from euniv.service import _remember_profile from euniv.service import _remember_profile
_remember_profile(db, employee_id, profile) _remember_profile(db, employee_id, profile)
if valid is True and hours: if hours:
from euniv.study_control import stamp_hours from euniv.study_control import stamp_hours
stamp_hours(account, hours) stamp_hours(account, hours)
if official_hours: if official_hours:
account.official_month_hours = official_hours[:32] account.official_month_hours = official_hours[:32]
db.commit() db.commit()
payload = {"employee_id": employee_id, "result": result} payload = {"employee_id": employee_id, "result": result}
if result == "检查失败": if result == "检查失败":
+14 -1
View File
@@ -665,6 +665,8 @@ def _refresh_one(employee_id: str, attempts: int, client_factory) -> None:
valid = False valid = False
checked_alive = False checked_alive = False
hours = "" hours = ""
official = ""
profile: dict = {}
try: try:
from euniv.proxy_pool import MAX_SWITCHES from euniv.proxy_pool import MAX_SWITCHES
@@ -709,7 +711,14 @@ def _refresh_one(employee_id: str, attempts: int, client_factory) -> None:
_persist(db, employee_id, password, client, profile) _persist(db, employee_id, password, client, profile)
valid = True valid = True
if valid: if valid:
hours = _month_hours(client) from admin.users import describe_credential
found = describe_credential(client)
hours = found["hours"]
official = found["official"]
profile = found["profile"]
valid = bool(found["valid"])
checked_alive = valid
break break
except Exception as exc: except Exception as exc:
if _switch_proxy(account, client, exc): if _switch_proxy(account, client, exc):
@@ -742,10 +751,14 @@ def _refresh_one(employee_id: str, attempts: int, client_factory) -> None:
except Exception: except Exception:
pass pass
record_outcome(account, valid) record_outcome(account, valid)
if profile:
_remember_profile(db, employee_id, profile)
if hours: if hours:
from euniv.study_control import stamp_hours from euniv.study_control import stamp_hours
stamp_hours(account, hours) stamp_hours(account, hours)
if official:
account.official_month_hours = official[:32]
db.commit() db.commit()
if valid is True: if valid is True:
from euniv.autostudy import request_wake from euniv.autostudy import request_wake
+37 -2
View File
@@ -582,7 +582,7 @@ def test_check_all_reports_each_saved_account(monkeypatch) -> None:
assert response.json() == [ assert response.json() == [
{"employee_id": "a-user", "result": "有效"}, {"employee_id": "a-user", "result": "有效"},
{"employee_id": "b-user", "result": "检查失败", "detail": "检查失败"}, {"employee_id": "b-user", "result": "检查失败", "detail": "检查失败"},
{"employee_id": "kept-user", "result": "有效"}, {"employee_id": "kept-user", "result": "已失效"},
] ]
assert calls == ["a-user", "b-user", "kept-user"] assert calls == ["a-user", "b-user", "kept-user"]
from euniv.study_window import month_key from euniv.study_window import month_key
@@ -595,7 +595,9 @@ def test_check_all_reports_each_saved_account(monkeypatch) -> None:
assert checked is not None and checked.user_name == "莫英达" and checked.company == "广州瀚信广州处" assert checked is not None and checked.user_name == "莫英达" and checked.company == "广州瀚信广州处"
assert checked.month_hours == "660" assert checked.month_hours == "660"
assert checked.hours_month == month_key() assert checked.hours_month == month_key()
assert kept is not None and kept.month_hours == "12" and kept.hours_month == "2026-10" assert kept is not None and kept.credential_valid is False
assert kept.user_name == "莫英达" and kept.company == "广州瀚信广州处"
assert kept.month_hours == "12" and kept.hours_month == "2026-10"
assert failed is not None and failed.credential_valid is None assert failed is not None and failed.credential_valid is None
assert failed.user_name == "" and failed.company == "" assert failed.user_name == "" and failed.company == ""
assert failed.month_hours == "" assert failed.month_hours == ""
@@ -604,6 +606,39 @@ def test_check_all_reports_each_saved_account(monkeypatch) -> None:
assert untouched.user_name == "" and untouched.company == "" assert untouched.user_name == "" and untouched.company == ""
def test_check_replaces_previous_valid_when_hours_cannot_be_read(monkeypatch) -> None:
import admin.users as users
_add_account("stale-user", valid=True)
with SessionLocal() as db:
account = db.get(Account, "stale-user")
assert account is not None
account.month_hours = "616"
account.official_month_hours = "321"
account.user_name = "旧名字"
db.commit()
class _InfoOnly(_Alive):
def month_hours(self) -> str:
raise RuntimeError("学时页面打不开")
monkeypatch.setattr(users, "build_client", lambda login_name, password, proxy=None: _InfoOnly(login_name, password))
api = _app()
client = TestClient(api.app)
assert client.post("/api/login", json={"employee_id": "admin", "password": INITIAL_PASSWORD}).status_code == 200
checked = client.post("/api/admin/users/stale-user/check")
assert checked.status_code == 200
assert checked.json()["result"] == "已失效"
with SessionLocal() as db:
account = db.get(Account, "stale-user")
assert account is not None
assert account.credential_valid is False
assert account.user_name == "莫英达"
assert account.company == "广州瀚信广州处"
assert account.month_hours == "616"
assert account.official_month_hours == "321"
def test_credentials_and_delete_user(monkeypatch) -> None: def test_credentials_and_delete_user(monkeypatch) -> None:
import admin.users as users import admin.users as users