优化(账号自动化): 履约保护登录配额与重登优先级
This commit is contained in:
@@ -117,3 +117,65 @@ def test_batch_max_seconds_default_and_env(monkeypatch):
|
||||
# 0 = 不限
|
||||
monkeypatch.setenv("LOGIN_BATCH_MAX_SECONDS", "0")
|
||||
assert login_service._batch_max_seconds() == 0.0
|
||||
|
||||
|
||||
def test_batch_budget_skipped_account_releases_quota_and_avoids_email_quota(monkeypatch):
|
||||
"""超时跳过走完整 run() 结算后,不消耗日配额、不记失败、也不触发 email_quota 误判。"""
|
||||
from web.backend.models import LoginAttempt, LoginQuotaSetting, LoginQuotaUsage
|
||||
from web.backend.services.login_failure import classify_failure
|
||||
|
||||
engine, session, user, account = _setup()
|
||||
try:
|
||||
session.add(
|
||||
LoginQuotaSetting(
|
||||
id=1,
|
||||
daily_attempt_limit=10,
|
||||
hourly_attempt_limit=10,
|
||||
per_account_daily_limit=1,
|
||||
onboarding_daily_limit=10,
|
||||
enabled=True,
|
||||
)
|
||||
)
|
||||
session.commit()
|
||||
|
||||
monkeypatch.setattr(login_service, "DouyuLogin", _FakeLogin)
|
||||
monkeypatch.setattr(login_service, "_batch_max_seconds", lambda: 0.001)
|
||||
_FakeLogin.calls = 0
|
||||
|
||||
original_submit = login_service.submit
|
||||
|
||||
def delayed_submit(lane, fn, /, *args, **kwargs):
|
||||
time.sleep(0.02)
|
||||
return original_submit(lane, fn, *args, **kwargs)
|
||||
|
||||
monkeypatch.setattr(login_service, "submit", delayed_submit)
|
||||
|
||||
runner_db = sessionmaker(bind=engine)()
|
||||
runner = login_service.LoginBatchRunner(
|
||||
db=runner_db,
|
||||
account_ids=[account.id],
|
||||
created_by=user.id,
|
||||
creator_permissions=["login:view_all"],
|
||||
proxy_config=None,
|
||||
concurrency=1,
|
||||
mode="login",
|
||||
)
|
||||
runner.run()
|
||||
|
||||
check = sessionmaker(bind=engine)()
|
||||
try:
|
||||
task = check.query(LoginTask).filter_by(account_id=account.id).one()
|
||||
attempt = check.query(LoginAttempt).filter_by(account_id=account.id).one()
|
||||
usage_rows = check.query(LoginQuotaUsage).all()
|
||||
|
||||
assert task.attempt_status == "skipped"
|
||||
assert classify_failure(task.attempt_message) != "email_quota"
|
||||
assert attempt.status == "skipped"
|
||||
assert attempt.failure_kind == ""
|
||||
assert all(int(row.attempt_count or 0) == 0 for row in usage_rows)
|
||||
assert all(int(row.failure_count or 0) == 0 for row in usage_rows)
|
||||
assert _FakeLogin.calls == 0
|
||||
finally:
|
||||
check.close()
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
Reference in New Issue
Block a user