fix: 学时读不到时先重新登录再判断凭据失效

This commit is contained in:
Nixevol
2026-11-21 15:28:59 +08:00
parent a5539405ca
commit d5226fc1a1
3 changed files with 60 additions and 3 deletions
+15
View File
@@ -180,6 +180,21 @@ def check_employee(db: Session, employee_id: str) -> dict:
try:
client.restore(credential)
found = describe_credential(client)
from euniv.client import is_qr_password
if not found["valid"] and password and not is_qr_password(password):
try:
from admin.settings import auto_attempt_count
from euniv.client import LoginRejected
from euniv.service import _persist
logged = client.login(manual=False, auto_attempts=auto_attempt_count(db))
_persist(db, employee_id, password, client, logged)
found = describe_credential(client)
except LoginRejected:
found = {"valid": False, "profile": {}, "hours": "", "official": ""}
except Exception:
pass
result = "有效" if found["valid"] else "已失效"
valid = bool(found["valid"])
profile = found["profile"]
+11 -3
View File
@@ -659,8 +659,6 @@ def _refresh_one(employee_id: str, attempts: int, client_factory) -> None:
if is_studying(employee_id):
return
if account.credential_valid is False:
return
client = None
valid = False
checked_alive = False
@@ -714,11 +712,21 @@ def _refresh_one(employee_id: str, attempts: int, client_factory) -> None:
from admin.users import describe_credential
found = describe_credential(client)
if not found["valid"] and password and not is_qr_password(password):
logged = client.login(manual=False, auto_attempts=attempts)
_persist(db, employee_id, password, client, logged)
found = describe_credential(client)
hours = found["hours"]
official = found["official"]
profile = found["profile"]
profile = found["profile"] if isinstance(found["profile"], dict) else {}
valid = bool(found["valid"])
checked_alive = valid
if not valid:
import logging
logging.getLogger("euniv.service").warning(
"账号 %s 本次没同时取到个人信息和本月学时,记为已失效", employee_id
)
break
except Exception as exc:
if _switch_proxy(account, client, exc):
+34
View File
@@ -437,6 +437,40 @@ def test_refresh_records_outcome_and_skips_paused_accounts() -> None:
assert decrypt_secret(empty.password_cipher) == "secret-pass"
def test_refresh_relogs_when_month_hours_are_missing() -> None:
import euniv.service as service
_add_account("relogin-user", valid=False)
with SessionLocal() as db:
account = db.get(Account, "relogin-user")
assert account is not None
account.month_hours = "12"
db.commit()
class _Recover(_Alive):
def __init__(self, login_name: str, password: str, proxy: str | None = None) -> None:
super().__init__(login_name, password, proxy)
self.reads = 0
def month_hours(self) -> str:
self.reads += 1
if self.reads == 1:
return ""
return "700"
def login(self, manual: bool = False, auto_attempts: int = 3) -> dict:
self.login_called = True
return {"token": "new-token", "accountId": self.login_name, "userId": "user"}
service.refresh_stored_credentials(lambda login_name, password, proxy=None: _Recover(login_name, password, proxy))
with SessionLocal() as db:
account = db.get(Account, "relogin-user")
assert account is not None
assert account.credential_valid is True
assert account.month_hours == "700"
assert account.user_name == "莫英达"
def test_paused_account_still_updates_when_opening_home() -> None:
import euniv.service as service