204 lines
6.4 KiB
Python
204 lines
6.4 KiB
Python
"""登录重试策略:不可重试终态与邮箱超时上限。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from types import SimpleNamespace
|
|
|
|
from core.douyu.login import (
|
|
DouyuLogin,
|
|
EmailCodeTimeoutError,
|
|
TerminalLoginError,
|
|
)
|
|
|
|
|
|
def _login(max_retries: int = 20) -> DouyuLogin:
|
|
account = SimpleNamespace(
|
|
username="u",
|
|
password="p",
|
|
email="e@example.com",
|
|
email_password="ep",
|
|
email_imap_server="imap.example.com",
|
|
email_imap_port=993,
|
|
email_imap_ssl=True,
|
|
)
|
|
return DouyuLogin(
|
|
account=account,
|
|
proxy="http://127.0.0.1:1",
|
|
max_login_retries=max_retries,
|
|
)
|
|
|
|
|
|
def test_must_bind_mobile_is_terminal(monkeypatch):
|
|
client = _login()
|
|
calls: list[int] = []
|
|
|
|
def steps(deadline: float = 0) -> str:
|
|
calls.append(1)
|
|
raise TerminalLoginError(
|
|
"跳过手机号绑定失败: 必须绑定手机号", code="must_bind_mobile"
|
|
)
|
|
|
|
monkeypatch.setattr(client, "_run_login_steps", steps)
|
|
monkeypatch.setattr(client, "_prepare_retry", lambda: True)
|
|
|
|
result = client.login()
|
|
|
|
assert result.success is False
|
|
assert result.code == "must_bind_mobile"
|
|
assert len(calls) == 1 # 不再重试 20 轮
|
|
|
|
|
|
def test_email_code_timeout_stops_after_attempt_limit(monkeypatch):
|
|
"""取码超时不再整轮重试:重试=重新发信,会把斗鱼邮件队列压更长。"""
|
|
monkeypatch.delenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", raising=False)
|
|
client = _login()
|
|
calls: list[int] = []
|
|
|
|
def steps(deadline: float = 0) -> str:
|
|
calls.append(1)
|
|
raise EmailCodeTimeoutError("等待验证码超时")
|
|
|
|
monkeypatch.setattr(client, "_run_login_steps", steps)
|
|
monkeypatch.setattr(client, "_prepare_retry", lambda: True)
|
|
monkeypatch.setattr(client, "_sleep_interruptible", lambda _s: None)
|
|
|
|
result = client.login()
|
|
|
|
assert result.success is False
|
|
assert result.code == "email_code_timeout"
|
|
# 只跑一轮,不再靠重试补发邮件;重试交给批次级退避
|
|
assert len(calls) == 1
|
|
assert client.api.name == "wgapi"
|
|
|
|
|
|
def test_retryable_error_still_retries(monkeypatch):
|
|
client = _login(max_retries=3)
|
|
calls: list[int] = []
|
|
|
|
def steps(deadline: float = 0) -> str:
|
|
calls.append(1)
|
|
raise ValueError("极验验证失败: slide")
|
|
|
|
monkeypatch.setattr(client, "_run_login_steps", steps)
|
|
monkeypatch.setattr(client, "_prepare_retry", lambda: True)
|
|
monkeypatch.setattr(client, "_sleep_interruptible", lambda _s: None)
|
|
|
|
result = client.login()
|
|
|
|
assert result.success is False
|
|
assert len(calls) == 3
|
|
assert "slide" in result.message
|
|
|
|
|
|
def test_skip_mobile_bind_raises_terminal_for_phone_requirement(monkeypatch):
|
|
client = _login()
|
|
monkeypatch.setattr(
|
|
client,
|
|
"_request_json",
|
|
lambda *_a, **_k: {"msg": "必须绑定手机号"},
|
|
)
|
|
|
|
try:
|
|
client._skip_mobile_bind("unique-key")
|
|
except TerminalLoginError as exc:
|
|
assert exc.code == "must_bind_mobile"
|
|
else: # pragma: no cover - 断言失败路径
|
|
raise AssertionError("必须绑定手机号 应当是不可重试的终态")
|
|
|
|
|
|
def test_skip_mobile_bind_keeps_retrying_other_errors(monkeypatch):
|
|
client = _login()
|
|
monkeypatch.setattr(
|
|
client,
|
|
"_request_json",
|
|
lambda *_a, **_k: {"msg": "系统繁忙"},
|
|
)
|
|
|
|
try:
|
|
client._skip_mobile_bind("unique-key")
|
|
except TerminalLoginError: # pragma: no cover - 不应走到这里
|
|
raise AssertionError("系统繁忙不应被当作终态")
|
|
except ValueError as exc:
|
|
assert "系统繁忙" in str(exc)
|
|
|
|
|
|
def test_missing_email_fails_fast_before_waiting(monkeypatch):
|
|
"""没有邮箱却仍走「等验证码」会白等两轮 60 秒,必须直接收口。"""
|
|
account = SimpleNamespace(
|
|
username="u",
|
|
password="p",
|
|
email="",
|
|
email_password="",
|
|
email_imap_server="",
|
|
email_imap_port=143,
|
|
email_imap_ssl=False,
|
|
)
|
|
client = DouyuLogin(account=account, proxy="http://127.0.0.1:1")
|
|
|
|
monkeypatch.setattr(
|
|
client,
|
|
"_first_login",
|
|
lambda: ("gt", "challenge", "code-token", {}),
|
|
)
|
|
monkeypatch.setattr(
|
|
client,
|
|
"_solve_geetest",
|
|
lambda gt, challenge, deadline=0: ("v", "v|jordan", challenge),
|
|
)
|
|
monkeypatch.setattr(
|
|
client,
|
|
"_second_login",
|
|
lambda *args, **kwargs: ("remote_email", "value"),
|
|
)
|
|
|
|
try:
|
|
client._run_login_steps()
|
|
except TerminalLoginError as exc:
|
|
assert exc.code == "email_missing"
|
|
else: # pragma: no cover - 断言失败路径
|
|
raise AssertionError("缺少邮箱时应当直接失败")
|
|
|
|
|
|
def test_email_wait_seconds_is_configurable(monkeypatch):
|
|
monkeypatch.delenv("DOUYU_EMAIL_CODE_WAIT_SECONDS", raising=False)
|
|
# 默认 120 秒(配合 60 秒补等,一轮邮箱验证码合计最多 3 分钟)
|
|
assert DouyuLogin._email_code_wait_seconds() == 120
|
|
monkeypatch.setenv("DOUYU_EMAIL_CODE_WAIT_SECONDS", "1800")
|
|
assert DouyuLogin._email_code_wait_seconds() == 180
|
|
# 下限 15 秒
|
|
monkeypatch.setenv("DOUYU_EMAIL_CODE_WAIT_SECONDS", "5")
|
|
assert DouyuLogin._email_code_wait_seconds() == 15
|
|
|
|
|
|
def test_email_rescue_seconds_is_configurable(monkeypatch):
|
|
monkeypatch.delenv("DOUYU_EMAIL_CODE_RESCUE_SECONDS", raising=False)
|
|
assert DouyuLogin._email_code_rescue_seconds() == 60
|
|
monkeypatch.setenv("DOUYU_EMAIL_CODE_RESCUE_SECONDS", "0")
|
|
assert DouyuLogin._email_code_rescue_seconds() == 0
|
|
|
|
|
|
def test_email_code_round_total_wait_capped_at_three_minutes(monkeypatch):
|
|
"""即使环境变量同时调大主等待和补等,一轮验证码总等待也绝不超过 3 分钟(180s)。"""
|
|
monkeypatch.setenv("DOUYU_EMAIL_CODE_WAIT_SECONDS", "180")
|
|
monkeypatch.setenv("DOUYU_EMAIL_CODE_RESCUE_SECONDS", "180")
|
|
client = _login()
|
|
waits: list[int] = []
|
|
|
|
class _AlwaysTimeoutVerifier:
|
|
def get_verification_code(self, max_wait: int, **kwargs):
|
|
waits.append(max_wait)
|
|
raise TimeoutError("等待验证码超时")
|
|
|
|
try:
|
|
client._wait_for_email_code(
|
|
_AlwaysTimeoutVerifier(), # type: ignore[arg-type]
|
|
baseline_uid=1,
|
|
email_sent_at=0.0,
|
|
)
|
|
except EmailCodeTimeoutError:
|
|
pass
|
|
else: # pragma: no cover
|
|
raise AssertionError("应当在超时后抛出 EmailCodeTimeoutError")
|
|
|
|
assert sum(waits) <= 180
|