fix: 凭据有效改为当场取到个人信息和本月学时
This commit is contained in:
+34
-23
@@ -116,23 +116,34 @@ def _require(db: Session, employee_id: str) -> Account:
|
||||
return account
|
||||
|
||||
|
||||
def _checked_hours(client) -> tuple[str, str]:
|
||||
def describe_credential(client) -> dict:
|
||||
"""用当前凭据现查个人信息和本月学时。两项都取到才算有效,取不到的字段留空。"""
|
||||
from admin.settings import source_for_employee
|
||||
from euniv.service import _month_hours
|
||||
|
||||
try:
|
||||
hours = _month_hours(client)
|
||||
except Exception:
|
||||
hours = ""
|
||||
info_ok = bool(client.credential_alive())
|
||||
profile = _checked_profile(client) if info_ok else {}
|
||||
if not str(profile.get("name") or "").strip():
|
||||
info_ok = False
|
||||
profile = {}
|
||||
hours = ""
|
||||
official = ""
|
||||
if hours and source_for_employee(str(getattr(client, "login_name", "") or "")) == "history":
|
||||
reader = getattr(client, "month_hours", None)
|
||||
if callable(reader):
|
||||
try:
|
||||
official = str(reader() or "").strip()
|
||||
except Exception:
|
||||
official = ""
|
||||
return hours, official
|
||||
if info_ok:
|
||||
try:
|
||||
hours = str(_month_hours(client) or "").strip()
|
||||
except Exception:
|
||||
hours = ""
|
||||
source = source_for_employee(str(getattr(client, "login_name", "") or ""))
|
||||
if source == "history":
|
||||
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:
|
||||
@@ -168,12 +179,12 @@ def check_employee(db: Session, employee_id: str) -> dict:
|
||||
client = _open_client(employee_id, password, proxy)
|
||||
try:
|
||||
client.restore(credential)
|
||||
alive = bool(client.credential_alive())
|
||||
result = "有效" if alive else "已失效"
|
||||
valid = alive
|
||||
profile = _checked_profile(client) if alive else {}
|
||||
if alive:
|
||||
hours, official_hours = _checked_hours(client)
|
||||
found = describe_credential(client)
|
||||
result = "有效" if found["valid"] else "已失效"
|
||||
valid = bool(found["valid"])
|
||||
profile = found["profile"]
|
||||
hours = found["hours"]
|
||||
official_hours = found["official"]
|
||||
break
|
||||
except Exception as exc:
|
||||
profile = {}
|
||||
@@ -199,16 +210,16 @@ def check_employee(db: Session, employee_id: str) -> dict:
|
||||
if account is None:
|
||||
raise HTTPException(status_code=404, detail="用户不存在")
|
||||
record_outcome(account, valid)
|
||||
if valid is True and profile:
|
||||
if profile:
|
||||
from euniv.service import _remember_profile
|
||||
|
||||
_remember_profile(db, employee_id, profile)
|
||||
if valid is True and hours:
|
||||
if hours:
|
||||
from euniv.study_control import stamp_hours
|
||||
|
||||
stamp_hours(account, hours)
|
||||
if official_hours:
|
||||
account.official_month_hours = official_hours[:32]
|
||||
if official_hours:
|
||||
account.official_month_hours = official_hours[:32]
|
||||
db.commit()
|
||||
payload = {"employee_id": employee_id, "result": result}
|
||||
if result == "检查失败":
|
||||
|
||||
@@ -665,6 +665,8 @@ def _refresh_one(employee_id: str, attempts: int, client_factory) -> None:
|
||||
valid = False
|
||||
checked_alive = False
|
||||
hours = ""
|
||||
official = ""
|
||||
profile: dict = {}
|
||||
try:
|
||||
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)
|
||||
valid = True
|
||||
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
|
||||
except Exception as exc:
|
||||
if _switch_proxy(account, client, exc):
|
||||
@@ -742,10 +751,14 @@ def _refresh_one(employee_id: str, attempts: int, client_factory) -> None:
|
||||
except Exception:
|
||||
pass
|
||||
record_outcome(account, valid)
|
||||
if profile:
|
||||
_remember_profile(db, employee_id, profile)
|
||||
if hours:
|
||||
from euniv.study_control import stamp_hours
|
||||
|
||||
stamp_hours(account, hours)
|
||||
if official:
|
||||
account.official_month_hours = official[:32]
|
||||
db.commit()
|
||||
if valid is True:
|
||||
from euniv.autostudy import request_wake
|
||||
|
||||
@@ -582,7 +582,7 @@ def test_check_all_reports_each_saved_account(monkeypatch) -> None:
|
||||
assert response.json() == [
|
||||
{"employee_id": "a-user", "result": "有效"},
|
||||
{"employee_id": "b-user", "result": "检查失败", "detail": "检查失败"},
|
||||
{"employee_id": "kept-user", "result": "有效"},
|
||||
{"employee_id": "kept-user", "result": "已失效"},
|
||||
]
|
||||
assert calls == ["a-user", "b-user", "kept-user"]
|
||||
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.month_hours == "660"
|
||||
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.user_name == "" and failed.company == ""
|
||||
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 == ""
|
||||
|
||||
|
||||
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:
|
||||
import admin.users as users
|
||||
|
||||
|
||||
Reference in New Issue
Block a user