fix(douyu): 登录后自动复检 CK 并收紧分配池
This commit is contained in:
@@ -86,8 +86,31 @@ class TestLoginAttemptIsolation:
|
||||
def teardown_method(self):
|
||||
self.session.close()
|
||||
|
||||
def _run_batch(self, monkeypatch, *, mode="login", account_ids=None, stop=False):
|
||||
def _run_batch(
|
||||
self,
|
||||
monkeypatch,
|
||||
*,
|
||||
mode="login",
|
||||
account_ids=None,
|
||||
stop=False,
|
||||
cookie_check=None,
|
||||
):
|
||||
monkeypatch.setattr(login_service, "DouyuLogin", _FakeLogin)
|
||||
monkeypatch.setattr(
|
||||
login_service,
|
||||
"check_douyu_cookie",
|
||||
cookie_check
|
||||
or (
|
||||
lambda _cookie: {
|
||||
"valid": True,
|
||||
"message": "ok",
|
||||
"fish_ball": 1,
|
||||
"nickname": "n",
|
||||
"level": 1,
|
||||
"checked_at": datetime.now(UTC),
|
||||
}
|
||||
),
|
||||
)
|
||||
# 批次结束时 runner 会关掉自己的会话,因此单独给它一个。
|
||||
runner_db = sessionmaker(bind=self.engine)()
|
||||
runner = login_service.LoginBatchRunner(
|
||||
@@ -163,7 +186,7 @@ class TestLoginAttemptIsolation:
|
||||
task = self._fresh(self.task_id)
|
||||
account = self._fresh_account()
|
||||
assert task.status == "success"
|
||||
assert task.message == "登录成功"
|
||||
assert task.message == "登录成功,Cookie 有效"
|
||||
assert task.attempt_status == "success"
|
||||
assert task.attempt_at is not None
|
||||
assert task.batch_id != "old-batch"
|
||||
@@ -171,6 +194,9 @@ class TestLoginAttemptIsolation:
|
||||
assert account.cookie == "new-cookie"
|
||||
assert account.cookie_updated_at is not None
|
||||
assert account.cookie_source == "web"
|
||||
assert account.ck_check_status == "valid"
|
||||
assert account.ck_checked_at >= account.cookie_updated_at
|
||||
assert account.status == "active"
|
||||
|
||||
def test_relogin_success_writes_ck_verdict_on_the_account(self, monkeypatch):
|
||||
"""重登成功后的复检必须写到账号上,不能写到登录器的临时对象上。
|
||||
@@ -181,19 +207,6 @@ class TestLoginAttemptIsolation:
|
||||
"""
|
||||
self.account.ck_check_status = "invalid"
|
||||
self.session.commit()
|
||||
monkeypatch.setattr(
|
||||
login_service,
|
||||
"check_douyu_cookie",
|
||||
lambda _cookie: {
|
||||
"valid": True,
|
||||
"message": "ok",
|
||||
"fish_ball": 1,
|
||||
"nickname": "n",
|
||||
"level": 1,
|
||||
"checked_at": datetime.now(UTC),
|
||||
},
|
||||
)
|
||||
|
||||
self._run_batch(monkeypatch, mode="relogin")
|
||||
|
||||
task = self._fresh(self.task_id)
|
||||
@@ -203,6 +216,46 @@ class TestLoginAttemptIsolation:
|
||||
assert account.cookie == "new-cookie"
|
||||
assert account.ck_check_status == "valid"
|
||||
|
||||
def test_normal_login_replaces_stale_invalid_verdict_after_probe(self, monkeypatch):
|
||||
self.account.ck_check_status = "invalid"
|
||||
self.account.status = "invalid"
|
||||
self.session.commit()
|
||||
|
||||
self._run_batch(monkeypatch)
|
||||
|
||||
task = self._fresh(self.task_id)
|
||||
account = self._fresh_account()
|
||||
assert task.attempt_status == "success"
|
||||
assert task.message == "登录成功,Cookie 有效"
|
||||
assert account.cookie == "new-cookie"
|
||||
assert account.ck_check_status == "valid"
|
||||
assert account.ck_checked_at >= account.cookie_updated_at
|
||||
assert account.status == "active"
|
||||
|
||||
def test_login_success_with_failed_probe_keeps_account_out_of_pool(
|
||||
self, monkeypatch
|
||||
):
|
||||
self.account.ck_check_status = "invalid"
|
||||
self.account.status = "invalid"
|
||||
self.session.commit()
|
||||
|
||||
self._run_batch(
|
||||
monkeypatch,
|
||||
cookie_check=lambda _cookie: {
|
||||
"valid": False,
|
||||
"message": "Cookie 已失效",
|
||||
"checked_at": datetime.now(UTC),
|
||||
},
|
||||
)
|
||||
|
||||
task = self._fresh(self.task_id)
|
||||
account = self._fresh_account()
|
||||
assert task.attempt_status == "success"
|
||||
assert "Cookie 有效性检测失败" in task.message
|
||||
assert account.cookie == "new-cookie"
|
||||
assert account.ck_check_status == "invalid"
|
||||
assert account.status == "invalid"
|
||||
|
||||
def test_stopped_batch_keeps_existing_cookie(self, monkeypatch):
|
||||
self._run_batch(monkeypatch, stop=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user