feat(huya): 统一实时商品目录并增加兑换代理开关
This commit is contained in:
@@ -9,6 +9,7 @@ from unittest.mock import Mock
|
||||
from core.huya.http_client import HuyaHttpClient
|
||||
from web.backend.models import HuyaAccount, ProxyConfig
|
||||
from web.backend.services import huya_points_exchange as exchange
|
||||
from web.backend.services import huya_runner_goods
|
||||
from web.backend.services.huya_runner import HuyaBatchRunner
|
||||
|
||||
|
||||
@@ -76,6 +77,24 @@ def _direct_client():
|
||||
return client
|
||||
|
||||
|
||||
def _mock_live_goods(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
huya_runner_goods,
|
||||
"fetch_points_center_catalog",
|
||||
lambda *_args, **_kwargs: SimpleNamespace(
|
||||
goods=[
|
||||
{
|
||||
"product_id": "hy-8315982",
|
||||
"name": "gift",
|
||||
"price": 20,
|
||||
"remain_text": "100",
|
||||
}
|
||||
],
|
||||
message="已拉取 1 个礼包",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def test_non_exchange_task_does_not_read_proxy_config():
|
||||
db = Mock()
|
||||
runner = HuyaBatchRunner(
|
||||
@@ -98,6 +117,7 @@ def test_static_proxy_is_used_for_entire_exchange_task(monkeypatch):
|
||||
)
|
||||
)
|
||||
client = _direct_client()
|
||||
_mock_live_goods(monkeypatch)
|
||||
calls = []
|
||||
marks = []
|
||||
|
||||
@@ -119,7 +139,11 @@ def test_static_proxy_is_used_for_entire_exchange_task(monkeypatch):
|
||||
SimpleNamespace(id=1),
|
||||
HuyaAccount(uid="100", cookie="yyuid=100"),
|
||||
{"uid": 100, "cookie": "yyuid=100"},
|
||||
{"room_pid": "123", "pay_channel": "Zfb"},
|
||||
{
|
||||
"room_pid": "123",
|
||||
"pay_channel": "Zfb",
|
||||
"exchange_use_proxy": "1",
|
||||
},
|
||||
)
|
||||
|
||||
assert calls == [
|
||||
@@ -141,6 +165,7 @@ def test_api_proxy_is_fetched_once_per_exchange_task(monkeypatch):
|
||||
fetcher.fetch_new_proxy.return_value = "http://10.0.0.2:3128"
|
||||
runner._proxy_fetcher = fetcher
|
||||
client = _direct_client()
|
||||
_mock_live_goods(monkeypatch)
|
||||
calls = []
|
||||
marks = []
|
||||
|
||||
@@ -162,7 +187,11 @@ def test_api_proxy_is_fetched_once_per_exchange_task(monkeypatch):
|
||||
SimpleNamespace(id=2),
|
||||
HuyaAccount(uid="100", cookie="yyuid=100"),
|
||||
{"uid": 100, "cookie": "yyuid=100"},
|
||||
{"room_pid": "123", "pay_channel": "Zfb"},
|
||||
{
|
||||
"room_pid": "123",
|
||||
"pay_channel": "Zfb",
|
||||
"exchange_use_proxy": "1",
|
||||
},
|
||||
)
|
||||
|
||||
fetcher.fetch_new_proxy.assert_called_once_with(max_attempts=3)
|
||||
@@ -197,7 +226,7 @@ def test_enabled_proxy_without_source_fails_exchange_before_request(monkeypatch)
|
||||
SimpleNamespace(id=3),
|
||||
HuyaAccount(uid="100", cookie="yyuid=100"),
|
||||
{"uid": 100, "cookie": "yyuid=100"},
|
||||
{"room_pid": "123"},
|
||||
{"room_pid": "123", "exchange_use_proxy": "1"},
|
||||
)
|
||||
|
||||
assert marks == [
|
||||
@@ -209,6 +238,53 @@ def test_enabled_proxy_without_source_fails_exchange_before_request(monkeypatch)
|
||||
]
|
||||
|
||||
|
||||
def test_exchange_proxy_switch_defaults_to_direct_connection(monkeypatch):
|
||||
runner = _runner(
|
||||
_cfg(
|
||||
enabled=True,
|
||||
http="http://10.0.0.1:8080",
|
||||
https="http://10.0.0.1:8080",
|
||||
)
|
||||
)
|
||||
client = _direct_client()
|
||||
_mock_live_goods(monkeypatch)
|
||||
calls = []
|
||||
marks = []
|
||||
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_http_client",
|
||||
lambda **kwargs: calls.append(kwargs) or client,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_exchange_proxies",
|
||||
lambda: (_ for _ in ()).throw(
|
||||
AssertionError("proxy switch is off; proxy config must not be read")
|
||||
),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_mark_task",
|
||||
lambda _db, _task, status, message, result=None: marks.append(
|
||||
(status, message, result)
|
||||
),
|
||||
)
|
||||
|
||||
runner._execute_exchange_goods(
|
||||
None,
|
||||
SimpleNamespace(id=4),
|
||||
HuyaAccount(uid="100", cookie="yyuid=100"),
|
||||
{"uid": 100, "cookie": "yyuid=100"},
|
||||
{"room_pid": "123", "pay_channel": "Zfb"},
|
||||
)
|
||||
|
||||
assert calls == [{"proxies": None}]
|
||||
assert marks[-1][0] == "success"
|
||||
assert marks[-1][2]["proxy_used"] is False
|
||||
assert marks[-1][2]["proxy_source"] == "disabled"
|
||||
|
||||
|
||||
def test_huya_http_client_routes_methods_through_configured_opener(monkeypatch):
|
||||
opener = Mock()
|
||||
opener.open.return_value = SimpleNamespace()
|
||||
|
||||
Reference in New Issue
Block a user