优化(账号自动化): 完善受保护账号重登筛选与登录配额分配
This commit is contained in:
@@ -645,6 +645,105 @@ class TestSelection(_Base):
|
||||
ids = self._ids(job)
|
||||
assert ids == [both.id, one_suit.id, plain.id]
|
||||
|
||||
def test_protected_missing_cookie_and_error_ck_are_selected_and_claimed(self):
|
||||
job = self.job(batch_size=10)
|
||||
no_cookie_plain = self.protect(
|
||||
self.make_account("prot-no-ck", cookie="", ck_check_status="")
|
||||
)
|
||||
no_cookie_captcha = self.protect(
|
||||
self.make_account(
|
||||
"prot-no-ck-cap",
|
||||
cookie="",
|
||||
ck_check_status="",
|
||||
relogin_failure_kind="captcha",
|
||||
)
|
||||
)
|
||||
error_ck = self.protect(
|
||||
self.make_account("prot-error-ck", ck_check_status="error")
|
||||
)
|
||||
unchecked_with_cookie = self.protect(
|
||||
self.make_account("prot-unchecked-with-ck", ck_check_status="")
|
||||
)
|
||||
ordinary_no_cookie = self.make_account(
|
||||
"ordinary-no-ck", cookie="", ck_check_status=""
|
||||
)
|
||||
|
||||
ids = self._ids(job)
|
||||
|
||||
assert no_cookie_plain.id in ids
|
||||
assert no_cookie_captcha.id in ids
|
||||
assert error_ck.id in ids
|
||||
assert unchecked_with_cookie.id not in ids
|
||||
assert ordinary_no_cookie.id not in ids
|
||||
|
||||
def test_protected_prior_day_exhausted_recovers_on_new_day_and_caps_same_day(self):
|
||||
from web.backend.services.account_query_filters import quota_day_for
|
||||
|
||||
job = self.job(batch_size=10)
|
||||
now = datetime.now(UTC)
|
||||
yesterday = quota_day_for(now - timedelta(days=1))
|
||||
today = quota_day_for(now)
|
||||
|
||||
email_recover = self.protect(
|
||||
self.make_account(
|
||||
"prot-prior-email",
|
||||
relogin_attempts=2,
|
||||
relogin_failure_kind="email_code",
|
||||
)
|
||||
)
|
||||
same_day_exhausted = self.protect(
|
||||
self.make_account(
|
||||
"prot-same-day-exhausted",
|
||||
relogin_attempts=2,
|
||||
relogin_failure_kind="email_code",
|
||||
)
|
||||
)
|
||||
credential_broken = self.protect(
|
||||
self.make_account(
|
||||
"prot-credential",
|
||||
relogin_attempts=2,
|
||||
relogin_failure_kind="credential",
|
||||
)
|
||||
)
|
||||
for idx, (acc, qday) in enumerate(
|
||||
[
|
||||
(email_recover, yesterday),
|
||||
(credential_broken, yesterday),
|
||||
(same_day_exhausted, today),
|
||||
(same_day_exhausted, today),
|
||||
]
|
||||
):
|
||||
self.session.add(
|
||||
LoginAttempt(
|
||||
account_id=acc.id,
|
||||
batch_id=f"hist-{idx}",
|
||||
category="relogin_protected",
|
||||
trigger_source="automation",
|
||||
started_at=now - timedelta(days=1 if qday == yesterday else 0),
|
||||
finished_at=now - timedelta(days=1 if qday == yesterday else 0),
|
||||
status="failed",
|
||||
failure_kind=acc.relogin_failure_kind,
|
||||
quota_day=qday,
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
ids = self._ids(job)
|
||||
assert email_recover.id in ids
|
||||
assert same_day_exhausted.id not in ids
|
||||
assert credential_broken.id not in ids
|
||||
|
||||
reset = ck_relogin._reconcile_protected_new_day_attempts(
|
||||
self.session,
|
||||
accounts=[email_recover],
|
||||
protection=load_protection_snapshot(self.session),
|
||||
max_attempts=2,
|
||||
now=now,
|
||||
)
|
||||
assert reset == 1
|
||||
assert email_recover.relogin_attempts == 0
|
||||
assert email_recover.status == "invalid"
|
||||
|
||||
|
||||
class TestSettlement(_Base):
|
||||
"""结算:成功清零;失败按分型决定重试还是转人工。
|
||||
@@ -1078,3 +1177,33 @@ class TestSettlementVisibility(_Base):
|
||||
assert counters["valid"] == 1 and counters["invalid"] == 0
|
||||
assert account.relogin_failure_kind == ""
|
||||
assert account.relogin_next_at is None
|
||||
|
||||
def test_protected_captcha_retry_failures_do_not_trip_global_circuit(self):
|
||||
protected = self.protect(
|
||||
self.make_account("prot-cap-retry", relogin_failure_kind="captcha")
|
||||
)
|
||||
ordinary = self.make_account("ord-ok")
|
||||
fail_task = self.make_attempt(
|
||||
protected,
|
||||
attempt_status="failed",
|
||||
attempt_message="极验验证失败: slide",
|
||||
)
|
||||
ok_task = self.make_attempt(ordinary, attempt_status="success")
|
||||
now = datetime.now(UTC)
|
||||
|
||||
counters = ck_relogin.settle_results(
|
||||
self.session,
|
||||
{protected.id: (protected, fail_task), ordinary.id: (ordinary, ok_task)},
|
||||
[protected.id, ordinary.id],
|
||||
now=now,
|
||||
max_attempts=2,
|
||||
protected_captcha_retry_ids={protected.id},
|
||||
)
|
||||
paused, hard_failures, _ = ck_relogin.evaluate_failure_circuit(
|
||||
counters,
|
||||
{"failure_pause_min_scanned": 1, "failure_pause_ratio": 40},
|
||||
)
|
||||
|
||||
assert counters["protected_captcha_failures"] == 1
|
||||
assert hard_failures == 0
|
||||
assert paused is False
|
||||
|
||||
Reference in New Issue
Block a user