功能: 接入鱼翅供应商直充渠道

This commit is contained in:
yml2213
2026-08-13 21:11:20 +08:00
parent 420ee3d95c
commit 902e669614
14 changed files with 586 additions and 15 deletions
+108
View File
@@ -0,0 +1,108 @@
"""斗鱼鱼翅充值渠道分流测试。"""
import os
import unittest
from unittest.mock import Mock, patch
os.environ.setdefault("DATABASE_URL", "sqlite://")
os.environ.setdefault("APP_ENCRYPTION_KEY", "MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY=")
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from web.backend.database import Base
from web.backend.models import Account, DouyuTask, User
from web.backend.services.douyu_runner import DouyuBatchRunner
class DouyuGoldRechargeChannelTests(unittest.TestCase):
def setUp(self):
self.engine = create_engine("sqlite://")
Base.metadata.create_all(self.engine)
self.session = sessionmaker(bind=self.engine)()
user = User(username="operator", password_hash="hash", role="super_admin")
self.session.add(user)
self.session.commit()
self.account = Account(
username="douyu-user",
password="password",
email="mail@example.com",
email_password="mail-password",
uid="10001",
)
self.session.add(self.account)
self.session.commit()
self.task = DouyuTask(
batch_id="batch", account_id=self.account.id, task_type="create_gold_qr",
handbook_scope="elite", status="running", created_by=user.id,
)
self.session.add(self.task)
self.session.commit()
self.runner = DouyuBatchRunner(self.session, "batch", "create_gold_qr")
def tearDown(self):
self.session.close()
Base.metadata.drop_all(self.engine)
self.engine.dispose()
@patch("web.backend.services.douyu_runner.FishFinRechargeClient")
def test_supplier_channel_creates_order_using_uid_and_finishes_on_success(self, client_class):
supplier = Mock()
supplier.create_order.return_value = {
"code": 200,
"result": {"order_status": 2, "order_no": "supplier-001"},
"sign": "not-stored",
}
client_class.return_value = supplier
config = {
"gold_recharge_channel": "supplier_api",
"gold_api_product_id": "gold-product",
"gold_api_account_template_name": "斗鱼账号",
}
self.task.result = {"payload": {"amount": 10}}
self.session.commit()
self.runner._execute_create_gold_qr(
self.session, self.task, self.account, "acf_uid=10001", config,
)
supplier.create_order.assert_called_once_with(
charge_account="10001",
buy_num=10,
customer_price=unittest.mock.ANY,
customer_order_no=f"DYGF{self.task.id}",
product_id="gold-product",
recharge_arg=[{"templateName": "斗鱼账号", "templateVal": "10001"}],
)
self.assertEqual(str(supplier.create_order.call_args.kwargs["customer_price"]), "10")
self.session.refresh(self.task)
self.assertEqual(self.task.status, "success")
self.assertEqual(self.task.result["recharge_channel"], "supplier_api")
self.assertEqual(self.task.result["supplier_order_status"], 2)
self.assertEqual(self.task.result["buy_num"], 10)
self.assertEqual(self.task.result["customer_price"], "10")
self.assertNotIn("pay_url", self.task.result)
self.assertNotIn("sign", self.task.result["supplier_order"])
@patch("web.backend.services.douyu_runner.FishFinRechargeClient")
def test_supplier_channel_marks_non_200_create_response_as_failed(self, client_class):
supplier = Mock()
supplier.create_order.return_value = {"code": 400, "msg": "商品已下架"}
client_class.return_value = supplier
config = {
"gold_recharge_channel": "supplier_api",
"gold_api_product_id": "gold-product",
"gold_api_account_template_name": "斗鱼UID",
}
self.runner._execute_create_gold_qr(
self.session, self.task, self.account, "acf_uid=10001", config,
)
self.session.refresh(self.task)
self.assertEqual(self.task.status, "failed")
self.assertEqual(self.task.message, "商品已下架")
if __name__ == "__main__":
unittest.main()
+65 -2
View File
@@ -34,7 +34,7 @@ class FishFinRechargeClientTests(unittest.TestCase):
}, "POST")
self.assertEqual(sign, "4087a959f3488ecb13efe6ef58e3bc67")
def test_create_order_posts_json_with_signed_nested_arguments(self):
def test_create_order_posts_json_with_signed_optional_arguments(self):
response = Mock()
response.json.return_value = {"code": 200, "order_status": 0}
self.session.post.return_value = response
@@ -52,10 +52,27 @@ class FishFinRechargeClientTests(unittest.TestCase):
self.assertEqual(result["code"], 200)
kwargs = self.session.post.call_args.kwargs
self.assertEqual(self.session.post.call_args.args[0], "https://supplier.example/adapter-apiaccess/open/api/createOrderV2")
self.assertEqual(kwargs["json"]["customer_price"], "1.2")
self.assertEqual(kwargs["json"]["customer_price"], 1.2)
self.assertEqual(kwargs["json"]["sign"], self.client.sign(kwargs["json"], "POST"))
self.assertEqual(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(
charge_account="10001", buy_num=1, customer_price="1.0",
customer_order_no="merchant-001", product_id="111570",
)
body = self.session.post.call_args.kwargs["json"]
self.assertEqual(body["customer_price"], 1)
self.assertNotIn("notify_url", body)
self.assertNotIn("recharge_arg", body)
self.assertNotIn("ext_arg", body)
self.assertEqual(body["sign"], self.client.sign(body, "POST"))
def test_query_order_requires_identifier_and_uses_get_params(self):
with self.assertRaisesRegex(ValueError, "订单标识"):
self.client.query_order()
@@ -74,6 +91,52 @@ class FishFinRechargeClientTests(unittest.TestCase):
with self.assertRaises(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(
charge_account="10001", buy_num=1, customer_price="0.993",
customer_order_no="merchant-001", product_id="111570",
recharge_arg=[{"templateName": "斗鱼UID", "templateVal": "10001"}],
)
self.assertEqual([event["stage"] for event in events], ["request", "response"])
self.assertNotIn("sign", events[0]["params"])
self.assertNotIn("recharge_arg", events[0]["params"])
self.assertEqual(events[0]["params"]["customer_price"], 0.993)
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(
charge_account="10001", buy_num=1, customer_price="0.993",
customer_order_no="merchant-001", product_id="111570",
recharge_arg=[{"templateName": "斗鱼UID", "templateVal": "10001"}],
)
self.assertEqual(events[0]["json_body"]["sign"], client.sign(events[0]["json_body"], "POST"))
self.assertEqual(events[0]["json_body"]["customer_price"], 0.993)
self.assertNotIn("sign", events[0]["sign_params"])
self.assertIn(self.config.app_secret, events[0]["sign_source"])
self.assertEqual(events[1]["response_body"]["sign"], "response-sign")
self.assertEqual(events[1]["response_text"], response.text)
if __name__ == "__main__":
unittest.main()