fix: 会话失效时自动重新登录,管理端可改判断依据
This commit is contained in:
@@ -527,6 +527,8 @@ def test_check_skips_empty_credential_and_updates_saved_one(monkeypatch) -> None
|
||||
"hours_month",
|
||||
"study_active",
|
||||
"state",
|
||||
"message",
|
||||
"hours_met",
|
||||
}
|
||||
assert saved["has_credential"] is True
|
||||
assert saved["refresh_paused"] is False
|
||||
@@ -985,6 +987,29 @@ def test_admin_can_save_study_settings_in_one_request() -> None:
|
||||
assert one["proxy"] == "HTTP 9.9.9.9:8080"
|
||||
|
||||
|
||||
def test_admin_can_set_account_hours_source() -> None:
|
||||
_add_account("source-user")
|
||||
_add_account("other-user")
|
||||
api = _app()
|
||||
client = TestClient(api.app)
|
||||
assert client.post("/api/login", json={"employee_id": "admin", "password": INITIAL_PASSWORD}).status_code == 200
|
||||
saved = client.put(
|
||||
"/api/admin/users/study-settings",
|
||||
json={"employee_ids": ["source-user"], "hours_source": "history"},
|
||||
)
|
||||
assert saved.status_code == 200
|
||||
listed = client.get("/api/admin/users").json()
|
||||
one = next(item for item in listed if item["employee_id"] == "source-user")
|
||||
other = next(item for item in listed if item["employee_id"] == "other-user")
|
||||
assert one["hours_source"] == "history"
|
||||
assert other["hours_source"] == "api"
|
||||
bad = client.put(
|
||||
"/api/admin/users/study-settings",
|
||||
json={"employee_ids": ["source-user"], "hours_source": "other"},
|
||||
)
|
||||
assert bad.status_code == 400
|
||||
|
||||
|
||||
def test_admin_can_switch_hours_source() -> None:
|
||||
api = _app()
|
||||
client = TestClient(api.app)
|
||||
|
||||
@@ -1505,3 +1505,58 @@ def test_progress_updates_do_not_call_into_the_page_connections(monkeypatch) ->
|
||||
halt_work("u-quiet")
|
||||
assert seen == [("u-quiet", True)]
|
||||
_clear_user_status("u-quiet")
|
||||
|
||||
|
||||
def test_dead_saved_session_logs_in_again(monkeypatch) -> None:
|
||||
from euniv.autostudy import _prepare_session
|
||||
from euniv.client import SessionExpired
|
||||
|
||||
logins: list[bool] = []
|
||||
|
||||
class _Jar:
|
||||
def __iter__(self):
|
||||
return iter(())
|
||||
|
||||
class _Client:
|
||||
def __init__(self, *_args, **_kwargs) -> None:
|
||||
self.client = type("HTTP", (), {"cookies": type("Cookies", (), {"jar": _Jar()})()})()
|
||||
self._profile = {"userId": "u", "accountId": "a"}
|
||||
|
||||
def restore(self, _credential: dict) -> None:
|
||||
return None
|
||||
|
||||
def credential_alive(self) -> bool:
|
||||
return False
|
||||
|
||||
def _ensure_site(self) -> str:
|
||||
raise AssertionError("失效会话不应再打开学习页")
|
||||
|
||||
def login(self, manual: bool = False) -> dict:
|
||||
logins.append(manual)
|
||||
return {"token": "new"}
|
||||
|
||||
def session_token(self) -> str:
|
||||
return "yxt-new"
|
||||
|
||||
def close(self) -> None:
|
||||
return None
|
||||
|
||||
monkeypatch.setattr("euniv.autostudy.EunivClient", _Client)
|
||||
monkeypatch.setattr("euniv.service._persist", lambda *_args, **_kwargs: None)
|
||||
token, _user, _org, _cookies, _proxy, reason = _prepare_session("u1", "secret", {"yxt_token": "old"}, None, False)
|
||||
assert logins == [False]
|
||||
assert token == "yxt-new"
|
||||
assert reason == ""
|
||||
|
||||
class _SiteDead(_Client):
|
||||
def credential_alive(self) -> bool:
|
||||
return True
|
||||
|
||||
def _ensure_site(self) -> str:
|
||||
raise SessionExpired("授权失败")
|
||||
|
||||
monkeypatch.setattr("euniv.autostudy.EunivClient", _SiteDead)
|
||||
token, _user, _org, _cookies, _proxy, reason = _prepare_session("u1", "secret", {"yxt_token": "old"}, None, False)
|
||||
assert logins == [False, False]
|
||||
assert token == "yxt-new"
|
||||
assert reason == ""
|
||||
|
||||
Reference in New Issue
Block a user