优化(登录预算与任务): 建议上限封顶2000、错误中文化及补全跳过触发来源
This commit is contained in:
@@ -206,3 +206,72 @@ def test_protected_without_cookie_and_captcha_included_in_protected_backlog(db):
|
||||
|
||||
assert recommendation["basis"]["protected_invalid_backlog"] == 2
|
||||
assert recommendation["recommended"]["relogin_protected_daily_limit"] >= 100
|
||||
|
||||
|
||||
def test_large_inventory_caps_daily_budget_at_2000_and_preserves_priority_order(db):
|
||||
now = datetime.now(UTC)
|
||||
accounts = [
|
||||
*[
|
||||
_account(f"large-valid-ordinary-{i}", ck_check_status="valid", status="active")
|
||||
for i in range(600)
|
||||
],
|
||||
*[
|
||||
_account(
|
||||
f"large-invalid-high-{i}",
|
||||
points=400,
|
||||
ck_check_status="invalid",
|
||||
status="invalid",
|
||||
)
|
||||
for i in range(400)
|
||||
],
|
||||
*[
|
||||
_account(f"large-invalid-ord-{i}", ck_check_status="invalid", status="invalid")
|
||||
for i in range(900)
|
||||
],
|
||||
*[
|
||||
_account(
|
||||
f"large-imported-{i}",
|
||||
cookie="",
|
||||
ck_check_status="",
|
||||
status="imported",
|
||||
)
|
||||
for i in range(1500)
|
||||
],
|
||||
]
|
||||
protected = _account(
|
||||
"large-protected-invalid",
|
||||
ck_check_status="invalid",
|
||||
status="invalid",
|
||||
)
|
||||
accounts.append(protected)
|
||||
db.add_all(accounts)
|
||||
db.flush()
|
||||
db.add(
|
||||
PlatformFulfillmentTask(
|
||||
source_order_id="large-protected-order",
|
||||
source_order_item_id="large-protected-item",
|
||||
sale_mode="presale",
|
||||
account_id=protected.id,
|
||||
binding_status="bound",
|
||||
execution_status="bound",
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
recommendation = recommend_login_budget(db, now=now)
|
||||
recommended = recommendation["recommended"]
|
||||
allocated = (
|
||||
recommended["relogin_protected_daily_limit"]
|
||||
+ recommended["lease_refresh_daily_limit"]
|
||||
+ recommended["relogin_high_value_daily_limit"]
|
||||
+ recommended["relogin_ordinary_daily_limit"]
|
||||
+ recommended["onboarding_daily_limit"]
|
||||
+ recommended["manual_daily_limit"]
|
||||
)
|
||||
|
||||
assert recommended["daily_attempt_limit"] <= 2000
|
||||
assert allocated <= recommended["daily_attempt_limit"]
|
||||
assert recommended["manual_daily_limit"] <= 200
|
||||
assert recommended["relogin_protected_daily_limit"] >= 100
|
||||
assert recommended["relogin_high_value_daily_limit"] > 0
|
||||
assert recommended["relogin_ordinary_daily_limit"] > 0
|
||||
|
||||
@@ -411,3 +411,209 @@ class TestDeleteGuard:
|
||||
assert res["success"] is True
|
||||
assert not self._exists(self.success_history)
|
||||
assert self._account_cookie(self.account.id) == "valid-ck-1"
|
||||
|
||||
|
||||
class TestSkippedTaskOriginAndChineseMessages:
|
||||
def setup_method(self):
|
||||
self.engine = get_test_engine()
|
||||
database_module.SessionLocal.configure(bind=self.engine)
|
||||
self.session = make_test_session(self.engine)
|
||||
|
||||
self.user = User(username="skip-admin", password_hash="h", role="super_admin")
|
||||
self.session.add(self.user)
|
||||
self.session.flush()
|
||||
|
||||
self.acc_auto = Account(
|
||||
username="skip-auto-acc",
|
||||
password="p",
|
||||
email="skip-auto@example.com",
|
||||
email_password="ep",
|
||||
status="invalid",
|
||||
ck_check_status="invalid",
|
||||
)
|
||||
self.acc_manual = Account(
|
||||
username="skip-manual-acc",
|
||||
password="p",
|
||||
email="skip-manual@example.com",
|
||||
email_password="ep",
|
||||
status="invalid",
|
||||
ck_check_status="invalid",
|
||||
)
|
||||
self.acc_geetest = Account(
|
||||
username="geetest-acc",
|
||||
password="p",
|
||||
email="geetest@example.com",
|
||||
email_password="ep",
|
||||
status="invalid",
|
||||
ck_check_status="invalid",
|
||||
)
|
||||
self.session.add_all([self.acc_auto, self.acc_manual, self.acc_geetest])
|
||||
self.session.flush()
|
||||
|
||||
from web.backend.models import ExecutionJob
|
||||
|
||||
# 整批被跳过且无 LoginAttempt 的自动化批次(历史英文前缀消息)
|
||||
self.session.add(
|
||||
LoginTask(
|
||||
batch_id="auto-skip-batch",
|
||||
account_id=self.acc_auto.id,
|
||||
created_by=self.user.id,
|
||||
status="skipped",
|
||||
attempt_status="skipped",
|
||||
attempt_batch_id="auto-skip-batch",
|
||||
attempt_message="未执行:relogin_ordinary 今日登录预算已用完",
|
||||
message="未执行:relogin_ordinary 今日登录预算已用完",
|
||||
)
|
||||
)
|
||||
# 人工批次触发但因全局预算被跳过(有 ExecutionJob、无 LoginAttempt)
|
||||
self.session.add(
|
||||
ExecutionJob(
|
||||
job_type="login_batch",
|
||||
lane_scope="auth",
|
||||
batch_id="manual-skip-batch",
|
||||
payload_json={"trigger_source": "manual", "mode": "login"},
|
||||
available_at=datetime.now(UTC),
|
||||
created_by=self.user.id,
|
||||
)
|
||||
)
|
||||
self.session.add(
|
||||
LoginTask(
|
||||
batch_id="manual-skip-batch",
|
||||
account_id=self.acc_manual.id,
|
||||
created_by=self.user.id,
|
||||
status="skipped",
|
||||
attempt_status="skipped",
|
||||
attempt_batch_id="manual-skip-batch",
|
||||
attempt_message="未执行:全局每日登录尝试预算已用完",
|
||||
message="未执行:全局每日登录尝试预算已用完",
|
||||
)
|
||||
)
|
||||
# 历史极验英文失败文案
|
||||
self.session.add(
|
||||
LoginTask(
|
||||
batch_id="geetest-batch",
|
||||
account_id=self.acc_geetest.id,
|
||||
created_by=self.user.id,
|
||||
status="failed",
|
||||
attempt_status="failed",
|
||||
attempt_batch_id="geetest-batch",
|
||||
attempt_message="重新登录失败: 极验 slide 验证失败: forbidden(旧 Cookie 已保留)",
|
||||
message="重新登录失败: 极验 slide 验证失败: forbidden(旧 Cookie 已保留)",
|
||||
logs=[
|
||||
{
|
||||
"level": "error",
|
||||
"message": (
|
||||
"重新登录失败: 极验验证异常: HTTPSConnectionPool(host='api.geetest.com', port=443): "
|
||||
"Max retries exceeded with url: /get.php (Caused by ReadTimeoutError('Read timed out.'))(旧 Cookie 已保留)"
|
||||
),
|
||||
}
|
||||
],
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
def teardown_method(self):
|
||||
self.session.close()
|
||||
|
||||
def _fresh(self):
|
||||
return sessionmaker(bind=self.engine)()
|
||||
|
||||
def test_skipped_tasks_recover_origin_and_chinese_messages(self):
|
||||
from web.backend.routers.login import get_task_logs
|
||||
from web.backend.services.login_quota_service import humanize_login_message
|
||||
|
||||
db = self._fresh()
|
||||
try:
|
||||
items = list_tasks(db=db, current=self.user)
|
||||
by_username = {item.account_username: item for item in items}
|
||||
|
||||
auto_item = by_username["skip-auto-acc"]
|
||||
assert auto_item.trigger_source == "automation"
|
||||
assert auto_item.category == "relogin_ordinary"
|
||||
assert auto_item.message == "未执行:普通重登今日登录预算已用完"
|
||||
|
||||
manual_item = by_username["skip-manual-acc"]
|
||||
assert manual_item.trigger_source == "manual"
|
||||
assert manual_item.category == "manual_login"
|
||||
assert manual_item.message == "未执行:全局每日登录尝试预算已用完"
|
||||
|
||||
geetest_item = by_username["geetest-acc"]
|
||||
assert "触发风控拦截" in geetest_item.message
|
||||
assert "slide" not in geetest_item.message
|
||||
assert "forbidden" not in geetest_item.message
|
||||
|
||||
logs = get_task_logs(task_id=geetest_item.id, db=db, current=self.user)
|
||||
assert "网络或代理请求超时" in logs["items"][0]["message"]
|
||||
assert "HTTPSConnectionPool" not in logs["items"][0]["message"]
|
||||
|
||||
assert (
|
||||
humanize_login_message("极验 slide 验证失败: fail(旧 Cookie 已保留)")
|
||||
== "极验滑块验证失败: 滑块未通过(旧 Cookie 已保留)"
|
||||
)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
def test_reserve_login_attempt_uses_chinese_category_label_when_exhausted(self):
|
||||
from web.backend.models import LoginQuotaSetting, LoginQuotaUsage
|
||||
from web.backend.services.login_quota_service import (
|
||||
CATEGORY_ALL,
|
||||
CATEGORY_RELOGIN_ORDINARY,
|
||||
BUSINESS_TZ,
|
||||
reserve_login_attempt,
|
||||
)
|
||||
|
||||
db = self._fresh()
|
||||
try:
|
||||
now = datetime.now(UTC)
|
||||
quota_day = now.astimezone(BUSINESS_TZ).date()
|
||||
quota_hour = now.astimezone(BUSINESS_TZ).hour
|
||||
db.add(
|
||||
LoginQuotaSetting(
|
||||
id=1,
|
||||
enabled=True,
|
||||
daily_attempt_limit=2000,
|
||||
hourly_attempt_limit=500,
|
||||
per_account_daily_limit=3,
|
||||
onboarding_daily_limit=100,
|
||||
relogin_high_value_daily_limit=200,
|
||||
relogin_ordinary_daily_limit=1,
|
||||
manual_daily_limit=100,
|
||||
relogin_protected_daily_limit=100,
|
||||
lease_refresh_daily_limit=50,
|
||||
login_concurrency=3,
|
||||
failure_pause_ratio=40,
|
||||
)
|
||||
)
|
||||
db.add_all(
|
||||
[
|
||||
LoginQuotaUsage(
|
||||
quota_day=quota_day,
|
||||
quota_hour=quota_hour,
|
||||
category=CATEGORY_RELOGIN_ORDINARY,
|
||||
attempt_count=1,
|
||||
),
|
||||
LoginQuotaUsage(
|
||||
quota_day=quota_day,
|
||||
quota_hour=quota_hour,
|
||||
category=CATEGORY_ALL,
|
||||
attempt_count=1,
|
||||
),
|
||||
]
|
||||
)
|
||||
db.commit()
|
||||
|
||||
acc = db.query(Account).filter(Account.username == "skip-auto-acc").one()
|
||||
decision = reserve_login_attempt(
|
||||
db,
|
||||
account=acc,
|
||||
mode="relogin",
|
||||
batch_id="quota-check-batch",
|
||||
trigger_source="automation",
|
||||
created_by=self.user.id,
|
||||
now=now,
|
||||
)
|
||||
assert decision.allowed is False
|
||||
assert decision.reason == "普通重登今日登录预算已用完"
|
||||
assert decision.category == CATEGORY_RELOGIN_ORDINARY
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
Reference in New Issue
Block a user