From bd14db25c32aaebc6a67b1bdcce2514e988021c8 Mon Sep 17 00:00:00 2001 From: jimmy Date: Fri, 10 Jul 2015 15:53:20 +0800 Subject: [PATCH] refine & rewrite test --- lib/wx_pay.rb | 6 +++++- lib/wx_pay/service.rb | 7 ++----- test/test_helper.rb | 1 + test/wx_pay/service_test.rb | 18 +++++++++++------- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/wx_pay.rb b/lib/wx_pay.rb index 254a9a1..b801ac5 100644 --- a/lib/wx_pay.rb +++ b/lib/wx_pay.rb @@ -4,7 +4,7 @@ module WxPay class<< self - attr_accessor :appid, :mch_id, :key, :apiclient_cert_file + attr_accessor :appid, :mch_id, :key, :apiclient_cert_path def extra_rest_client_options=(options) @rest_client_options = options @@ -13,5 +13,9 @@ def extra_rest_client_options=(options) def extra_rest_client_options @rest_client_options || {} end + + def apiclient_cert + @apiclient_cert ||= OpenSSL::PKCS12.new(WxPay.apiclient_cert_path, WxPay.mch_id) + end end end diff --git a/lib/wx_pay/service.rb b/lib/wx_pay/service.rb index 4d51d30..23d1414 100644 --- a/lib/wx_pay/service.rb +++ b/lib/wx_pay/service.rb @@ -52,13 +52,10 @@ def self.invoke_refund(params) # 微信退款需要双向证书 # https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4 # https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3 - - p12_file = File.read WxPay.apiclient_cert_file - cert = OpenSSL::PKCS12.new(p12_file, WxPay.mch_id) WxPay.extra_rest_client_options = { - ssl_client_cert: cert.certificate, - ssl_client_key: cert.key, + ssl_client_cert: WxPay.apiclient_cert.certificate, + ssl_client_key: WxPay.apiclient_cert.key, verify_ssl: OpenSSL::SSL::VERIFY_NONE } diff --git a/test/test_helper.rb b/test/test_helper.rb index 5f9e6f8..5f5448d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -6,3 +6,4 @@ WxPay.appid = 'wxd930ea5d5a258f4f' WxPay.key = '8934e7d15453e97507ef794cf7b0519d' WxPay.mch_id = '1900000109' +WxPay.apiclient_cert_path = '/path/to/your/cert/file.p12' diff --git a/test/wx_pay/service_test.rb b/test/wx_pay/service_test.rb index 5d8afd5..499a738 100644 --- a/test/wx_pay/service_test.rb +++ b/test/wx_pay/service_test.rb @@ -1,10 +1,6 @@ + class ServiceTest < MiniTest::Test def setup - WxPay.appid = 'xxxxxxxxxxxxxx' - WxPay.key = 'xxxxxxxxxxxxxxx' - WxPay.mch_id = 'xxxxxxxxxxxxxx' - WxPay.apiclient_cert_file = '/Users/jimmy/Workspace/shopshow-ultron/public/apiclient_cert.p12' - @params = { transaction_id: '1217752501201407033233368018', op_user_id: '10000100', @@ -13,9 +9,15 @@ def setup refund_fee: 1, total_fee: 1 } + + @apiclient_cert = Minitest::Mock.new + @apiclient_cert.expect(:certificate, 'certificate') + @apiclient_cert.expect(:key, 'key') end def test_invoke_refund + + response_body = <<-EOF @@ -41,7 +43,9 @@ def test_invoke_refund body: response_body ) - r = WxPay::Service.invoke_refund(@params) - assert_equal r.success?, true + WxPay.stub :apiclient_cert, @apiclient_cert do + r = WxPay::Service.invoke_refund(@params) + assert_equal r.success?, true + end end end \ No newline at end of file