187 lines
6.8 KiB
Python
187 lines
6.8 KiB
Python
"""鱼翅直充供应商 API 客户端测试。"""
|
|
|
|
import pytest
|
|
|
|
from unittest.mock import Mock
|
|
|
|
from core.douyu.recharge_api import (
|
|
FishFinRechargeClient,
|
|
FishFinRechargeConfig,
|
|
FishFinRechargeConfigError,
|
|
)
|
|
|
|
|
|
class TestFishFinRechargeClient:
|
|
def setup_method(self):
|
|
self.config = FishFinRechargeConfig(
|
|
base_url="https://supplier.example/",
|
|
app_id="15945681",
|
|
app_secret="ac9fa79db418fd2c61d9bd4c60956f2d5db3a6d781d6f91f2e96327c74dfaccd",
|
|
)
|
|
self.session = Mock()
|
|
self.client = FishFinRechargeClient(self.config, session=self.session)
|
|
|
|
def test_sign_matches_document_example(self):
|
|
sign = self.client.sign(
|
|
{
|
|
"app_id": "15945681 ",
|
|
"timestamp": 1740916504,
|
|
"charge_account": "18888888888",
|
|
"buy_num": 2,
|
|
"customer_price": 1.21,
|
|
"customer_order_no": "DD202507010010208888",
|
|
"product_id": "66668888",
|
|
"empty": " \n",
|
|
"sign": "ignored",
|
|
},
|
|
"POST",
|
|
)
|
|
assert sign == "4087a959f3488ecb13efe6ef58e3bc67"
|
|
|
|
def test_create_order_posts_new_document_payload(self):
|
|
response = Mock()
|
|
response.json.return_value = {"code": 200, "order_status": 0}
|
|
self.session.post.return_value = response
|
|
|
|
result = self.client.create_order(
|
|
buy_num=2,
|
|
pay_amount="1.20",
|
|
out_order_id="OUT202503010010208888",
|
|
product_id="product-1",
|
|
recharge_arg=[{"templateName": "斗鱼账号", "templateVal": "10001"}],
|
|
ext_arg={"skuid": 12},
|
|
)
|
|
|
|
assert result["code"] == 200
|
|
kwargs = self.session.post.call_args.kwargs
|
|
assert (
|
|
self.session.post.call_args.args[0]
|
|
== "https://supplier.example/adapter-apiaccess/open/api/createOrderV2"
|
|
)
|
|
assert kwargs["json"]["pay_amount"] == 1.2
|
|
assert kwargs["json"]["out_order_id"] == "OUT202503010010208888"
|
|
assert kwargs["json"]["order_type"] == 0
|
|
assert (
|
|
kwargs["json"]["recharge_arg"]
|
|
== '[{"templateName":"斗鱼账号","templateVal":"10001"}]'
|
|
)
|
|
assert kwargs["json"]["ext_arg"] == '{"skuid":12}'
|
|
assert "charge_account" not in kwargs["json"]
|
|
assert "customer_price" not in kwargs["json"]
|
|
assert "customer_order_no" not in kwargs["json"]
|
|
assert kwargs["json"]["sign"] == self.client.sign(kwargs["json"], "POST")
|
|
assert kwargs["timeout"] == (8, 20)
|
|
|
|
def test_create_order_omits_empty_optional_fields_and_serializes_integer_price(
|
|
self,
|
|
):
|
|
response = Mock()
|
|
response.json.return_value = {"code": 200}
|
|
self.session.post.return_value = response
|
|
|
|
self.client.create_order(
|
|
buy_num=1,
|
|
pay_amount="1.0",
|
|
out_order_id="merchant-001",
|
|
product_id="111570",
|
|
recharge_arg=[{"templateName": "斗鱼UID", "templateVal": "10001"}],
|
|
)
|
|
|
|
body = self.session.post.call_args.kwargs["json"]
|
|
assert body["pay_amount"] == 1
|
|
assert "notify_url" not in body
|
|
assert (
|
|
body["recharge_arg"] == '[{"templateName":"斗鱼UID","templateVal":"10001"}]'
|
|
)
|
|
assert "ext_arg" not in body
|
|
assert body["sign"] == self.client.sign(body, "POST")
|
|
|
|
def test_query_order_requires_identifier_and_uses_get_params(self):
|
|
with pytest.raises(ValueError, match="out_order_id"):
|
|
self.client.query_order("")
|
|
|
|
response = Mock()
|
|
response.json.return_value = {"code": 200, "order_status": 2}
|
|
self.session.get.return_value = response
|
|
self.client.query_order("OUT202503010010208888")
|
|
|
|
kwargs = self.session.get.call_args.kwargs
|
|
assert (
|
|
self.session.get.call_args.args[0]
|
|
== "https://supplier.example/adapter-apiaccess/open/api/queryOrderV2"
|
|
)
|
|
assert kwargs["params"]["out_order_id"] == "OUT202503010010208888"
|
|
assert kwargs["params"]["sign"] == self.client.sign(kwargs["params"], "GET")
|
|
|
|
def test_missing_configuration_is_rejected_before_request(self):
|
|
with pytest.raises(FishFinRechargeConfigError):
|
|
FishFinRechargeClient(
|
|
FishFinRechargeConfig(base_url="", app_id="", app_secret="")
|
|
)
|
|
|
|
def test_trace_excludes_replayable_signature_and_nested_account_data(self):
|
|
response = Mock()
|
|
response.status_code = 200
|
|
response.json.return_value = {"code": 200, "result": {"order_status": 0}}
|
|
self.session.post.return_value = response
|
|
events = []
|
|
client = FishFinRechargeClient(
|
|
self.config, session=self.session, trace=events.append
|
|
)
|
|
|
|
client.create_order(
|
|
buy_num=1,
|
|
pay_amount="1",
|
|
out_order_id="merchant-001",
|
|
product_id="111570",
|
|
recharge_arg=[{"templateName": "斗鱼UID", "templateVal": "10001"}],
|
|
)
|
|
|
|
assert [event["stage"] for event in events] == ["request", "response"]
|
|
assert "sign" not in events[0]["params"]
|
|
assert "recharge_arg" not in events[0]["params"]
|
|
assert events[0]["params"]["pay_amount"] == 1
|
|
|
|
def test_debug_trace_includes_complete_request_and_response_bodies(self):
|
|
response = Mock()
|
|
response.status_code = 200
|
|
response.headers = {
|
|
"Content-Type": "application/json",
|
|
"X-Request-Id": "request-1",
|
|
}
|
|
response.text = (
|
|
'{"code":1000,"message":"未传递支付金额","sign":"response-sign"}'
|
|
)
|
|
response.json.return_value = {
|
|
"code": 1000,
|
|
"message": "未传递支付金额",
|
|
"sign": "response-sign",
|
|
}
|
|
self.session.post.return_value = response
|
|
events = []
|
|
config = FishFinRechargeConfig(
|
|
base_url=self.config.base_url,
|
|
app_id=self.config.app_id,
|
|
app_secret=self.config.app_secret,
|
|
debug=True,
|
|
)
|
|
client = FishFinRechargeClient(
|
|
config, session=self.session, trace=events.append
|
|
)
|
|
|
|
client.create_order(
|
|
buy_num=1,
|
|
pay_amount="1",
|
|
out_order_id="merchant-001",
|
|
product_id="111570",
|
|
recharge_arg=[{"templateName": "斗鱼UID", "templateVal": "10001"}],
|
|
)
|
|
|
|
assert events[0]["json_body"]["sign"] == "<redacted>"
|
|
assert events[0]["json_body"]["pay_amount"] == 1
|
|
assert "sign" not in events[0]["sign_params"]
|
|
assert self.config.app_secret not in str(events[0])
|
|
assert "sign_source_digest" in events[0]
|
|
assert events[1]["response_body"]["sign"] == "<redacted>"
|
|
assert self.config.app_secret not in str(events[1])
|