生产实测斗鱼验证码邮件是攒批投递的:早高峰延迟 6~20 秒,高峰劣化到 8~13 分钟,同批请求的邮件会在同一分钟内成批到达。抽 40 个「超时」账号核对邮箱, 30 个其实收到了邮件(75%),只是迟到;另有 25% 在批次内根本没投递。 原实现等 60 秒就判超时,并在超时后换通道重试 —— 重试 = 重新发信,把邮件队列 压得更长:每账号发信量从 1.5 封涨到 3.3 封,延迟从 10 秒级恶化到 8~13 分钟, 取码成功率从 100% 掉到 20%。两个通道写的是同一个队列,发两次不会多拿码。 - 等待窗口 60 → 900 秒,新增补等 600 秒(不重发,只等迟到邮件) - 取码超时不换通道、不整轮重试;只有发信/提交接口报错才切 iframe/wgapi - 自动重登与接入作业不再对取码失败账号切 iframe 重跑 - 单账号总时长上限 240/300 → 1800 秒,等待接入 deadline 约束 - 文档记录实测机制与判定依据 验证:1727 passed / 6 skipped,前端 tsc -b 通过,pyright 生产代码 0 错误
169 lines
5.1 KiB
Python
169 lines
5.1 KiB
Python
"""首次登录自动化作业与邮箱双通道回退测试。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from web.backend.models import Account, AutomationJob, LoginTask, User
|
|
from web.backend.services.automation.jobs import account_onboarding as job
|
|
from web.backend.services.login_automation import LoginBatchOutcome
|
|
|
|
|
|
def _account(name: str, **overrides) -> Account:
|
|
values = {
|
|
"username": name,
|
|
"password": "p",
|
|
"email": f"{name}@example.com",
|
|
"email_password": "ep",
|
|
"status": "imported",
|
|
"auto_managed": True,
|
|
"cookie": "",
|
|
"ck_check_status": "",
|
|
}
|
|
values.update(overrides)
|
|
return Account(**values)
|
|
|
|
|
|
def _job() -> AutomationJob:
|
|
return AutomationJob(
|
|
key="account_onboarding",
|
|
title="账号首次登录(CK 获取)",
|
|
enabled=True,
|
|
run_interval_seconds=300,
|
|
batch_size=20,
|
|
concurrency=3,
|
|
)
|
|
|
|
|
|
def _admin(db) -> User:
|
|
user = User(
|
|
username="onboarding-admin",
|
|
password_hash="h",
|
|
role="super_admin",
|
|
is_active=True,
|
|
)
|
|
db.add(user)
|
|
db.commit()
|
|
return user
|
|
|
|
|
|
def test_selector_only_picks_unlogged_automatable_accounts(db):
|
|
admin = _admin(db)
|
|
selectable = _account("onboarding-selectable")
|
|
has_cookie = _account("onboarding-has-cookie", cookie="acf_uid=1")
|
|
opted_out = _account("onboarding-opted-out", auto_managed=False)
|
|
manual = _account("onboarding-manual", status="manual")
|
|
in_flight = _account("onboarding-in-flight")
|
|
db.add_all([selectable, has_cookie, opted_out, manual, in_flight])
|
|
db.flush()
|
|
db.add(
|
|
LoginTask(
|
|
batch_id="inflight",
|
|
account_id=in_flight.id,
|
|
created_by=admin.id,
|
|
attempt_status="pending",
|
|
)
|
|
)
|
|
db.commit()
|
|
|
|
rows = job.build_select_query(db, _job()).all()
|
|
|
|
assert [row.id for row in rows] == [selectable.id]
|
|
assert job.count_pending_onboarding(db) == 1
|
|
|
|
|
|
def test_selector_skips_missing_password_and_manual_failure(db):
|
|
_admin(db)
|
|
selectable = _account("onboarding-eligible")
|
|
missing_password = _account("onboarding-no-password", password="")
|
|
manual_failure = _account("onboarding-captcha", relogin_failure_kind="captcha")
|
|
db.add_all([selectable, missing_password, manual_failure])
|
|
db.commit()
|
|
|
|
rows = job.build_select_query(db, _job()).all()
|
|
|
|
assert [row.id for row in rows] == [selectable.id]
|
|
|
|
|
|
def test_run_account_onboarding_returns_batch_summary(db, monkeypatch):
|
|
_admin(db)
|
|
first = _account("onboarding-first")
|
|
second = _account("onboarding-second")
|
|
db.add_all([first, second])
|
|
db.commit()
|
|
outcome = LoginBatchOutcome(
|
|
account_ids=[first.id, second.id],
|
|
attempted_ids=[first.id, second.id],
|
|
success_ids=[first.id],
|
|
failed_ids=[second.id],
|
|
failure_kinds={second.id: "email_code"},
|
|
batch_ids=["batch-wgapi"],
|
|
fallback_batch_ids=["batch-iframe"],
|
|
fallback_used=True,
|
|
fallback_success_ids=[],
|
|
)
|
|
monkeypatch.setattr(
|
|
job,
|
|
"run_login_batch_with_fallback",
|
|
lambda *args, **kwargs: outcome,
|
|
)
|
|
|
|
result = job.run_account_onboarding(db, _job())
|
|
|
|
assert result["scanned"] == 2
|
|
assert result["valid"] == 1
|
|
assert result["invalid"] == 1
|
|
assert "邮箱回退" in result["message"]
|
|
assert result["detail"]["fallbackUsed"] is True
|
|
assert result["detail"]["fallbackBatchIds"] == ["batch-iframe"]
|
|
|
|
|
|
def test_email_code_failure_does_not_resend_via_iframe(db, monkeypatch):
|
|
"""取码失败不再切 iframe 重跑:重跑 = 重新发信,会把斗鱼邮件队列压更长。"""
|
|
from web.backend.services import login_automation as automation
|
|
|
|
calls: list[tuple[str, list[int]]] = []
|
|
|
|
def fake_run(_db, *, account_ids, api_strategy, **_kwargs):
|
|
calls.append((api_strategy, list(account_ids)))
|
|
return "batch-wgapi" if api_strategy == "wgapi" else "batch-iframe"
|
|
|
|
def fake_collect(batch_id):
|
|
if batch_id == "batch-wgapi":
|
|
return {
|
|
11: ("failed", "email_code"),
|
|
12: ("failed", "credential"),
|
|
13: ("success", ""),
|
|
}
|
|
return {11: ("success", "")}
|
|
|
|
monkeypatch.setattr(automation, "run_login_batch", fake_run)
|
|
monkeypatch.setattr(automation, "collect_batch_results", fake_collect)
|
|
|
|
outcome = automation.run_login_batch_with_fallback(
|
|
db,
|
|
account_ids=[11, 12, 13],
|
|
mode="login",
|
|
created_by=1,
|
|
)
|
|
|
|
assert calls == [("wgapi", [11, 12, 13])]
|
|
assert outcome.success_ids == [13]
|
|
assert outcome.failed_ids == [11, 12]
|
|
assert outcome.failure_kinds == {11: "email_code", 12: "credential"}
|
|
assert outcome.fallback_success_ids == []
|
|
assert outcome.fallback_used is False
|
|
|
|
|
|
def test_onboarding_job_is_registered_but_default_off():
|
|
from web.backend.services.automation import (
|
|
register_default_jobs,
|
|
registered_jobs,
|
|
reset_registry,
|
|
)
|
|
|
|
reset_registry()
|
|
register_default_jobs()
|
|
spec = registered_jobs()["account_onboarding"]
|
|
assert spec.default_enabled is False
|
|
assert "首次登录" in spec.title
|
|
assert spec.batch_size == 20
|