优化(账号自动化): 完善受保护账号重登筛选与登录配额分配
This commit is contained in:
@@ -166,3 +166,37 @@ def test_onboarding_job_is_registered_but_default_off():
|
||||
assert spec.default_enabled is False
|
||||
assert "首次登录" in spec.title
|
||||
assert spec.batch_size == 20
|
||||
|
||||
|
||||
def test_selector_prioritizes_protected_missing_cookie_and_allows_captcha(db):
|
||||
from web.backend.models import PlatformFulfillmentTask
|
||||
|
||||
ordinary = _account("onboarding-ordinary-first")
|
||||
protected_plain = _account("onboarding-protected-plain")
|
||||
protected_captcha = _account(
|
||||
"onboarding-protected-captcha",
|
||||
relogin_failure_kind="captcha",
|
||||
)
|
||||
db.add_all([ordinary, protected_plain, protected_captcha])
|
||||
db.flush()
|
||||
for index, acc in enumerate((protected_plain, protected_captcha)):
|
||||
db.add(
|
||||
PlatformFulfillmentTask(
|
||||
source_order_id=f"onb-order-{index}",
|
||||
source_order_item_id=f"onb-item-{index}",
|
||||
sale_mode="presale",
|
||||
account_id=acc.id,
|
||||
binding_status="bound",
|
||||
execution_status="bound",
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
rows = job.build_select_query(db, _job()).all()
|
||||
|
||||
assert [row.id for row in rows] == [
|
||||
protected_plain.id,
|
||||
protected_captcha.id,
|
||||
ordinary.id,
|
||||
]
|
||||
assert job.count_pending_onboarding(db) == 3
|
||||
|
||||
@@ -707,3 +707,80 @@ def test_p2_high_value_claims_before_aged_p3_ordinary_while_p0_wins_all(db):
|
||||
)
|
||||
|
||||
assert [item.account_id for item in claimed] == [p0.id, p2.id, p3.id]
|
||||
|
||||
|
||||
def test_protected_missing_cookie_and_prior_day_exhausted_reserve_p0_quota(db):
|
||||
from web.backend.models import LoginAttempt, LoginQuotaSetting
|
||||
from web.backend.services.login_quota_service import (
|
||||
CATEGORY_RELOGIN_PROTECTED,
|
||||
get_login_quota_usage_summary,
|
||||
reserve_login_attempt,
|
||||
)
|
||||
|
||||
no_cookie = _account("p0-nocookie", ck_status="")
|
||||
no_cookie.cookie = ""
|
||||
no_cookie.cookie_updated_at = None
|
||||
no_cookie.status = "imported"
|
||||
|
||||
error_ck = _account("p0-error", ck_status="error")
|
||||
|
||||
prior_email = _account("p0-prior-email", ck_status="invalid")
|
||||
prior_email.relogin_attempts = 2
|
||||
prior_email.relogin_failure_kind = "email_code"
|
||||
prior_email.status = "manual"
|
||||
|
||||
credential_broken = _account("p0-cred", ck_status="invalid")
|
||||
credential_broken.relogin_attempts = 2
|
||||
credential_broken.relogin_failure_kind = "credential"
|
||||
credential_broken.status = "manual"
|
||||
|
||||
db.add_all([no_cookie, error_ck, prior_email, credential_broken])
|
||||
db.flush()
|
||||
for acc in (no_cookie, error_ck, prior_email, credential_broken):
|
||||
db.add(_presale_task(acc))
|
||||
db.add(_health_job())
|
||||
db.add(
|
||||
LoginQuotaSetting(
|
||||
id=1,
|
||||
daily_attempt_limit=100,
|
||||
hourly_attempt_limit=50,
|
||||
per_account_daily_limit=2,
|
||||
relogin_protected_daily_limit=50,
|
||||
enabled=True,
|
||||
)
|
||||
)
|
||||
from web.backend.services.account_query_filters import quota_day_for
|
||||
|
||||
yesterday = quota_day_for(NOW - timedelta(days=1))
|
||||
for acc in (prior_email, credential_broken):
|
||||
db.add(
|
||||
LoginAttempt(
|
||||
account_id=acc.id,
|
||||
batch_id="prev-day",
|
||||
category=CATEGORY_RELOGIN_PROTECTED,
|
||||
trigger_source="automation",
|
||||
started_at=NOW - timedelta(days=1),
|
||||
finished_at=NOW - timedelta(days=1),
|
||||
status="failed",
|
||||
failure_kind=acc.relogin_failure_kind,
|
||||
quota_day=yesterday,
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
summary = get_login_quota_usage_summary(db, now=NOW)
|
||||
by_cat = {row["category"]: row for row in summary["categories"]}
|
||||
assert by_cat[CATEGORY_RELOGIN_PROTECTED]["pending"] == 3
|
||||
|
||||
decision = reserve_login_attempt(
|
||||
db,
|
||||
account=no_cookie,
|
||||
mode="login",
|
||||
trigger_source="automation",
|
||||
batch_id="b-prot-login",
|
||||
created_by=None,
|
||||
now=NOW,
|
||||
)
|
||||
assert decision.allowed
|
||||
assert decision.attempt is not None
|
||||
assert decision.attempt.category == CATEGORY_RELOGIN_PROTECTED
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -171,3 +171,38 @@ def test_protected_inventory_is_allocated_once_within_daily_budget(db):
|
||||
assert recommended["relogin_protected_daily_limit"] >= 50
|
||||
assert recommended["lease_refresh_daily_limit"] >= 50
|
||||
assert allocated <= recommended["daily_attempt_limit"]
|
||||
|
||||
|
||||
def test_protected_without_cookie_and_captcha_included_in_protected_backlog(db):
|
||||
now = datetime.now(UTC)
|
||||
missing_cookie = _account(
|
||||
"budget-prot-nocookie",
|
||||
cookie="",
|
||||
ck_check_status="",
|
||||
status="imported",
|
||||
)
|
||||
captcha_account = _account(
|
||||
"budget-prot-captcha",
|
||||
ck_check_status="invalid",
|
||||
status="manual",
|
||||
)
|
||||
captcha_account.relogin_failure_kind = "captcha"
|
||||
db.add_all([missing_cookie, captcha_account])
|
||||
db.flush()
|
||||
for idx, acc in enumerate((missing_cookie, captcha_account)):
|
||||
db.add(
|
||||
PlatformFulfillmentTask(
|
||||
source_order_id=f"budget-prot-order-{idx}",
|
||||
source_order_item_id=f"budget-prot-item-{idx}",
|
||||
sale_mode="presale",
|
||||
account_id=acc.id,
|
||||
binding_status="bound",
|
||||
execution_status="bound",
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
recommendation = recommend_login_budget(db, now=now)
|
||||
|
||||
assert recommendation["basis"]["protected_invalid_backlog"] == 2
|
||||
assert recommendation["recommended"]["relogin_protected_daily_limit"] >= 100
|
||||
|
||||
Reference in New Issue
Block a user