功能:支持动态配置每日比价次数上限

This commit is contained in:
unknown
2026-07-28 21:49:24 +08:00
parent 90c6fe599a
commit 252a2fdc5d
6 changed files with 74 additions and 13 deletions
+48
View File
@@ -3,14 +3,32 @@ from __future__ import annotations
import time
from datetime import datetime, timedelta
import pytest
from sqlalchemy import func, select
from app.core.config_schema import RISK_COMPARE_DAILY_THRESHOLD_KEY
from app.core.rewards import CN_TZ
from app.core.security import decode_token
from app.db.session import SessionLocal
from app.models.app_config import AppConfig
from app.models.comparison import ComparisonRecord
@pytest.fixture(autouse=True)
def _reset_compare_daily_limit():
with SessionLocal() as db:
db.query(AppConfig).filter(
AppConfig.key == RISK_COMPARE_DAILY_THRESHOLD_KEY
).delete()
db.commit()
yield
with SessionLocal() as db:
db.query(AppConfig).filter(
AppConfig.key == RISK_COMPARE_DAILY_THRESHOLD_KEY
).delete()
db.commit()
def _login(client) -> tuple[str, int]:
phone = f"137{int(time.time() * 1000) % 100000000:08d}"
sent = client.post("/api/v1/auth/sms/send", json={"phone": phone})
@@ -118,3 +136,33 @@ def test_compare_start_rejects_101st_beijing_day_attempt(client) -> None:
ComparisonRecord.trace_id == rejected_trace
)
) == 0
def test_compare_start_uses_dynamic_risk_monitor_limit(client) -> None:
with SessionLocal() as db:
db.add(AppConfig(key=RISK_COMPARE_DAILY_THRESHOLD_KEY, value=2))
db.commit()
token, user_id = _login(client)
first = client.post(
"/api/v1/compare/start",
json={"trace_id": f"quota-dynamic-{user_id}-1"},
headers=_headers(token),
)
second = client.post(
"/api/v1/compare/start",
json={"trace_id": f"quota-dynamic-{user_id}-2"},
headers=_headers(token),
)
rejected = client.post(
"/api/v1/compare/start",
json={"trace_id": f"quota-dynamic-{user_id}-3"},
headers=_headers(token),
)
assert first.status_code == 200
assert first.json() == {"limit": 2, "used": 1, "remaining": 1}
assert second.status_code == 200
assert second.json() == {"limit": 2, "used": 2, "remaining": 0}
assert rejected.status_code == 429
assert rejected.json()["detail"] == "今日已比价超过2次,请明天再试"
+12
View File
@@ -529,6 +529,18 @@ def test_admin_can_edit_rules_and_current_window_is_reconciled() -> None:
now + timedelta(seconds=2)
).replace(tzinfo=None)
compare_limit = client.patch(
"/admin/api/risk-monitor/rules",
headers=headers,
json={
"sms_hourly_threshold": 3,
"oneclick_daily_threshold": 20,
"compare_daily_threshold": 120,
},
)
assert compare_limit.status_code == 200
assert compare_limit.json()["compare_daily_threshold"] == 120
invalid = client.patch(
"/admin/api/risk-monitor/rules",
headers=headers,