refactor: 收口跨平台重复业务实现
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
"""虎牙活动查询统一 SID 发现与接口分流测试。"""
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
from web.backend.services.huya_activity_query import query_huya_activity
|
||||
|
||||
|
||||
class _Client:
|
||||
def __init__(self, *, sid: int = 0):
|
||||
self.sid = sid
|
||||
self.score_calls: list[dict] = []
|
||||
self.record_calls: list[dict] = []
|
||||
|
||||
def play_mall_query_allocation(self, **_kwargs):
|
||||
if not self.sid:
|
||||
raise RuntimeError("catalog unavailable")
|
||||
return SimpleNamespace(
|
||||
code=200,
|
||||
payload={
|
||||
"tag2": [
|
||||
{
|
||||
"tag2_2": {
|
||||
"tag0": {
|
||||
"tag7": self.sid,
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
def query_user_score(self, **kwargs):
|
||||
self.score_calls.append(kwargs)
|
||||
return SimpleNamespace(score=100)
|
||||
|
||||
def get_user_prize_records(self, **kwargs):
|
||||
self.record_calls.append(kwargs)
|
||||
return SimpleNamespace(records=[])
|
||||
|
||||
|
||||
def test_activity_query_prefers_live_sid_for_score_and_records():
|
||||
client = _Client(sid=2277)
|
||||
|
||||
result = query_huya_activity(
|
||||
client,
|
||||
uid=100,
|
||||
cookie="yyuid=100",
|
||||
configured_sid=2203,
|
||||
want_score=True,
|
||||
want_records=True,
|
||||
)
|
||||
|
||||
assert result.sid == 2277
|
||||
assert result.sid_source == "queryAllocation"
|
||||
assert result.configured_sid == 2203
|
||||
assert client.score_calls[0]["sid"] == 2277
|
||||
assert client.record_calls[0]["sid"] == 2277
|
||||
|
||||
|
||||
def test_activity_query_falls_back_to_configured_sid_when_catalog_fails():
|
||||
client = _Client()
|
||||
|
||||
result = query_huya_activity(
|
||||
client,
|
||||
uid=100,
|
||||
cookie="yyuid=100",
|
||||
configured_sid=2203,
|
||||
want_score=True,
|
||||
)
|
||||
|
||||
assert result.sid == 2203
|
||||
assert result.sid_source == "config"
|
||||
assert "catalog unavailable" in result.sid_error
|
||||
assert client.score_calls[0]["sid"] == 2203
|
||||
|
||||
|
||||
def test_activity_query_reuses_provided_sid_without_catalog_call(monkeypatch):
|
||||
client = _Client(sid=2277)
|
||||
monkeypatch.setattr(
|
||||
client,
|
||||
"play_mall_query_allocation",
|
||||
lambda **_kwargs: (_ for _ in ()).throw(
|
||||
AssertionError("provided SID must not trigger another catalog request")
|
||||
),
|
||||
)
|
||||
|
||||
result = query_huya_activity(
|
||||
client,
|
||||
uid=100,
|
||||
cookie="yyuid=100",
|
||||
configured_sid=2203,
|
||||
current_sid=2277,
|
||||
current_sid_source="queryAllocation",
|
||||
want_score=True,
|
||||
)
|
||||
|
||||
assert result.sid == 2277
|
||||
assert result.sid_source == "queryAllocation"
|
||||
assert client.score_calls[0]["sid"] == 2277
|
||||
@@ -0,0 +1,73 @@
|
||||
"""虎牙 HTTP/WSS 共用 RPC 请求构造测试。"""
|
||||
|
||||
from core.huya.activity_structs import GetUserPrizeRecordsReq
|
||||
from core.huya.rpc_requests import (
|
||||
activity_records_request,
|
||||
create_order_v6_request,
|
||||
play_mall_allocation_request,
|
||||
)
|
||||
from core.huya.shop_structs import (
|
||||
CreateOrderReqV6,
|
||||
PlayMallAppInfo,
|
||||
PlayMallQueryAllocationReq,
|
||||
UserId,
|
||||
)
|
||||
|
||||
|
||||
def test_activity_records_request_maps_user_and_sid():
|
||||
user = UserId()
|
||||
rpc = activity_records_request(user, 2277)
|
||||
|
||||
assert rpc.service == "webActUI"
|
||||
assert rpc.method == "getUserPrizeRecords"
|
||||
assert isinstance(rpc.request, GetUserPrizeRecordsReq)
|
||||
assert rpc.request.userId is user
|
||||
assert rpc.request.sid == 2277
|
||||
|
||||
|
||||
def test_play_mall_allocation_request_maps_tab_parameters():
|
||||
app = PlayMallAppInfo()
|
||||
rpc = play_mall_allocation_request(
|
||||
UserId(),
|
||||
app,
|
||||
page_code="heping_baodian_credit_center_page",
|
||||
scene=7,
|
||||
page_id=343,
|
||||
waterfall_tab_id=431,
|
||||
)
|
||||
|
||||
assert rpc.service == "PlayMallUI"
|
||||
assert rpc.method == "queryAllocation"
|
||||
assert isinstance(rpc.request, PlayMallQueryAllocationReq)
|
||||
assert rpc.request.appInfo is app
|
||||
assert rpc.request.appInfo.scene == 7
|
||||
assert rpc.request.blank7 == '{"waterfallTabId":"431"}'
|
||||
assert rpc.request.flag9 == 343
|
||||
assert rpc.request.flags11 == []
|
||||
|
||||
|
||||
def test_create_order_v6_request_maps_items_across_transports():
|
||||
rpc = create_order_v6_request(
|
||||
UserId(),
|
||||
object(),
|
||||
pid=123,
|
||||
items=[
|
||||
{
|
||||
"spu_id": "hy-8315982",
|
||||
"sku_id": 7747527,
|
||||
"item_count": 1,
|
||||
"goods_type": 5,
|
||||
"virtual_type": 507,
|
||||
}
|
||||
],
|
||||
source_id="yellowcarlist",
|
||||
biz_params="jifencenter",
|
||||
)
|
||||
|
||||
assert rpc.service == "shopMiddleUI"
|
||||
assert rpc.method == "createOrderV6"
|
||||
assert isinstance(rpc.request, CreateOrderReqV6)
|
||||
assert rpc.request.orderType == 6
|
||||
assert rpc.request.orderScene == 3
|
||||
assert rpc.request.items[0].spuId == "hy-8315982"
|
||||
assert rpc.request.items[0].skuId == 7747527
|
||||
@@ -35,7 +35,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
||||
PROJECT_ROOT = TESTS_DIR.parent
|
||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||
|
||||
HEAD_REVISION = "20261112_0080"
|
||||
HEAD_REVISION = "20261113_0081"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
"""全局代理配置到运行时来源的唯一映射测试。"""
|
||||
|
||||
from web.backend.models import ProxyConfig
|
||||
from web.backend.services.proxy_service import build_proxy_runtime
|
||||
|
||||
|
||||
def _config(**overrides) -> ProxyConfig:
|
||||
values = {
|
||||
"enabled": True,
|
||||
"api_url": "",
|
||||
"http": "",
|
||||
"https": "",
|
||||
"whitelist_enabled": False,
|
||||
"whitelist_platform": "xiequ",
|
||||
"whitelist_credentials": None,
|
||||
"whitelist_uid": "",
|
||||
"whitelist_ukey": "",
|
||||
}
|
||||
values.update(overrides)
|
||||
config = ProxyConfig()
|
||||
for key, value in values.items():
|
||||
setattr(config, key, value)
|
||||
return config
|
||||
|
||||
|
||||
def test_proxy_runtime_preserves_static_http_and_https_addresses():
|
||||
runtime = build_proxy_runtime(
|
||||
_config(
|
||||
http="http://10.0.0.1:8080",
|
||||
https="http://10.0.0.2:8080",
|
||||
)
|
||||
)
|
||||
|
||||
assert runtime.static_proxies == {
|
||||
"http": "http://10.0.0.1:8080",
|
||||
"https": "http://10.0.0.2:8080",
|
||||
}
|
||||
assert runtime.api_fetcher is None
|
||||
|
||||
|
||||
def test_proxy_runtime_builds_api_fetcher_with_legacy_whitelist_fields():
|
||||
runtime = build_proxy_runtime(
|
||||
_config(
|
||||
api_url="https://proxy.example/fetch",
|
||||
whitelist_enabled=True,
|
||||
whitelist_uid="legacy-user",
|
||||
whitelist_ukey="legacy-key",
|
||||
)
|
||||
)
|
||||
|
||||
assert runtime.static_proxies is None
|
||||
assert runtime.api_fetcher is not None
|
||||
assert runtime.api_fetcher.api_url == "https://proxy.example/fetch"
|
||||
|
||||
|
||||
def test_proxy_runtime_is_empty_when_caller_or_global_switch_is_off():
|
||||
assert not build_proxy_runtime(_config(), enabled=False).enabled
|
||||
assert not build_proxy_runtime(_config(enabled=False)).enabled
|
||||
@@ -9,6 +9,7 @@ from web.backend.models import AuditLog, User, WeiboAccount, WeiboExternalOrderM
|
||||
from web.backend.routers import weibo_collab as weibo_collab_router
|
||||
from web.backend.schemas import WeiboCollabCkExportOut
|
||||
from web.backend.services import weibo_collab_service as collab
|
||||
from web.backend.services import weibo_service
|
||||
|
||||
|
||||
def make_account(db: Session, uid: str = "4014000001") -> WeiboAccount:
|
||||
@@ -52,7 +53,7 @@ def test_create_orders_groups_and_balance_check(monkeypatch, db):
|
||||
def list_mall(self, *_args, **_kwargs):
|
||||
return {"weidou_balance": 400, "list": []}
|
||||
|
||||
monkeypatch.setattr(collab, "WeiboClient", FakeClient)
|
||||
monkeypatch.setattr(weibo_service, "WeiboClient", FakeClient)
|
||||
|
||||
first = collab.create_order(
|
||||
db,
|
||||
@@ -144,7 +145,7 @@ def test_exchange_record_unique_match_and_archive(monkeypatch, db):
|
||||
]
|
||||
}
|
||||
|
||||
monkeypatch.setattr(collab, "WeiboClient", FakeClient)
|
||||
monkeypatch.setattr(weibo_service, "WeiboClient", FakeClient)
|
||||
collab.set_status(db, user, first, collab.STATUS_RECHARGED)
|
||||
collab.set_status(db, user, first, collab.STATUS_SCRIPTED)
|
||||
collab.set_status(db, user, second, collab.STATUS_RECHARGED)
|
||||
@@ -210,7 +211,7 @@ def test_exchange_success_promotes_weidou_error_to_done(monkeypatch, db):
|
||||
]
|
||||
}
|
||||
|
||||
monkeypatch.setattr(collab, "WeiboClient", FakeClient)
|
||||
monkeypatch.setattr(weibo_service, "WeiboClient", FakeClient)
|
||||
|
||||
result = collab.check_exchange_records(db, user, [order])
|
||||
|
||||
@@ -266,7 +267,7 @@ def test_balance_check_does_not_mix_roles_after_rebind(monkeypatch, db):
|
||||
FakeClient.calls += 1
|
||||
return {"weidou_balance": 200, "list": []}
|
||||
|
||||
monkeypatch.setattr(collab, "WeiboClient", FakeClient)
|
||||
monkeypatch.setattr(weibo_service, "WeiboClient", FakeClient)
|
||||
|
||||
result = collab.check_balance_groups(db, user, [second], threshold=100)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user