Fix rubocop issue, long module, by extracting StripeStubs to a separate helper module

This commit is contained in:
Luis Ramos
2020-10-16 11:16:49 +01:00
parent 487aaf4862
commit f5f9a733fb
6 changed files with 110 additions and 105 deletions

View File

@@ -848,7 +848,6 @@ Metrics/ModuleLength:
- spec/models/spree/variant_spec.rb
- spec/services/permissions/order_spec.rb
- spec/services/variant_units/option_value_namer_spec.rb
- spec/support/request/stripe_helper.rb
Metrics/ParameterLists:
Max: 5

View File

@@ -5,6 +5,7 @@ require 'spree/core/gateway_error'
describe Spree::Admin::PaymentsController, type: :controller do
include StripeHelper
include StripeStubs
let!(:shop) { create(:enterprise) }
let!(:user) { shop.owner }

View File

@@ -8,6 +8,7 @@ feature '
' do
include AuthenticationHelper
include StripeHelper
include StripeStubs
let!(:order) { create(:completed_order_with_fees) }
let!(:stripe_payment_method) do

View File

@@ -7,6 +7,7 @@ feature "Check out with Stripe", js: true do
include ShopWorkflow
include CheckoutHelper
include StripeHelper
include StripeStubs
let(:distributor) { create(:distributor_enterprise) }
let!(:order_cycle) { create(:simple_order_cycle, distributors: [distributor], variants: [variant]) }

View File

@@ -34,108 +34,4 @@ module StripeHelper
allow(Stripe).to receive(:publishable_key) { "pk_test_12345" }
Spree::Config.set(stripe_connect_enabled: true)
end
def stub_payment_intents_post_request(order:, response: {}, stripe_account_header: true)
stub = stub_request(:post, "https://api.stripe.com/v1/payment_intents")
.with(basic_auth: ["sk_test_12345", ""], body: /.*#{order.number}/)
stub = stub.with(headers: { 'Stripe-Account' => 'abc123' }) if stripe_account_header
stub.to_return(payment_intent_authorize_response_mock(response))
end
def stub_payment_intents_post_request_with_redirect(order:, redirect_url:)
stub_request(:post, "https://api.stripe.com/v1/payment_intents")
.with(basic_auth: ["sk_test_12345", ""], body: /.*#{order.number}/)
.to_return(payment_intent_redirect_response_mock(redirect_url))
end
def stub_payment_intent_get_request(response: {}, stripe_account_header: true)
stub = stub_request(:get, "https://api.stripe.com/v1/payment_intents/pi_123")
stub = stub.with(headers: { 'Stripe-Account' => 'abc123' }) if stripe_account_header
stub.to_return(payment_intent_authorize_response_mock(response))
end
def stub_payment_methods_post_request(request: { payment_method: "pm_123" }, response: {})
stub_request(:post, "https://api.stripe.com/v1/payment_methods")
.with(body: request,
headers: { 'Stripe-Account' => 'abc123' })
.to_return(hub_payment_method_response_mock(response))
end
# Attaches the payment method to the customer in the hub's stripe account
def stub_payment_method_attach_request
stub_request(:post,
"https://api.stripe.com/v1/payment_methods/pm_123/attach")
.with(body: { customer: "cus_A123" })
.to_return(hub_payment_method_response_mock({ pm_id: "pm_123" }))
end
def stub_successful_capture_request(order:, response: {})
stub_capture_request(order, payment_successful_capture_mock(response))
end
def stub_failed_capture_request(order:, response: {})
stub_capture_request(order, payment_failed_capture_mock(response))
end
def stub_capture_request(order, response_mock)
stub_request(:post, "https://api.stripe.com/v1/payment_intents/pi_123/capture")
.with(body: { amount_to_capture: Spree::Money.new(order.total).cents },
headers: { 'Stripe-Account' => 'abc123' })
.to_return(response_mock)
end
def stub_refund_request
stub_request(:post, "https://api.stripe.com/v1/charges/ch_1234/refunds")
.with(body: { amount: 2000, expand: ["charge"] },
headers: { 'Stripe-Account' => 'abc123' })
.to_return(payment_successful_refund_mock)
end
private
def payment_intent_authorize_response_mock(options)
{ status: options[:code] || 200,
body: JSON.generate(id: "pi_123",
object: "payment_intent",
amount: 2000,
amount_received: 2000,
status: options[:intent_status] || "requires_capture",
last_payment_error: nil,
charges: { data: [{ id: "ch_1234", amount: 2000 }] }) }
end
def payment_intent_redirect_response_mock(redirect_url)
{ status: 200, body: JSON.generate(id: "pi_123",
object: "payment_intent",
next_source_action: {
type: "authorize_with_url",
authorize_with_url: { url: redirect_url }
},
status: "requires_source_action") }
end
def payment_successful_capture_mock(options)
{ status: options[:code] || 200,
body: JSON.generate(object: "payment_intent",
amount: 2000,
charges: { data: [{ id: "ch_1234", amount: 2000 }] }) }
end
def payment_failed_capture_mock(options)
{ status: options[:code] || 402,
body: JSON.generate(error: { message:
options[:message] || "payment-method-failure" }) }
end
def hub_payment_method_response_mock(options)
{ status: options[:code] || 200,
body: JSON.generate(id: options[:pm_id] || "pm_456", customer: "cus_A123") }
end
def payment_successful_refund_mock
{ status: 200,
body: JSON.generate(object: "refund",
amount: 2000,
charge: "ch_1234") }
end
end

View File

@@ -0,0 +1,107 @@
# frozen_string_literal: true
module StripeStubs
def stub_payment_intents_post_request(order:, response: {}, stripe_account_header: true)
stub = stub_request(:post, "https://api.stripe.com/v1/payment_intents")
.with(basic_auth: ["sk_test_12345", ""], body: /.*#{order.number}/)
stub = stub.with(headers: { 'Stripe-Account' => 'abc123' }) if stripe_account_header
stub.to_return(payment_intent_authorize_response_mock(response))
end
def stub_payment_intents_post_request_with_redirect(order:, redirect_url:)
stub_request(:post, "https://api.stripe.com/v1/payment_intents")
.with(basic_auth: ["sk_test_12345", ""], body: /.*#{order.number}/)
.to_return(payment_intent_redirect_response_mock(redirect_url))
end
def stub_payment_intent_get_request(response: {}, stripe_account_header: true)
stub = stub_request(:get, "https://api.stripe.com/v1/payment_intents/pi_123")
stub = stub.with(headers: { 'Stripe-Account' => 'abc123' }) if stripe_account_header
stub.to_return(payment_intent_authorize_response_mock(response))
end
def stub_payment_methods_post_request(request: { payment_method: "pm_123" }, response: {})
stub_request(:post, "https://api.stripe.com/v1/payment_methods")
.with(body: request,
headers: { 'Stripe-Account' => 'abc123' })
.to_return(hub_payment_method_response_mock(response))
end
# Attaches the payment method to the customer in the hub's stripe account
def stub_payment_method_attach_request
stub_request(:post,
"https://api.stripe.com/v1/payment_methods/pm_123/attach")
.with(body: { customer: "cus_A123" })
.to_return(hub_payment_method_response_mock({ pm_id: "pm_123" }))
end
def stub_successful_capture_request(order:, response: {})
stub_capture_request(order, payment_successful_capture_mock(response))
end
def stub_failed_capture_request(order:, response: {})
stub_capture_request(order, payment_failed_capture_mock(response))
end
def stub_capture_request(order, response_mock)
stub_request(:post, "https://api.stripe.com/v1/payment_intents/pi_123/capture")
.with(body: { amount_to_capture: Spree::Money.new(order.total).cents },
headers: { 'Stripe-Account' => 'abc123' })
.to_return(response_mock)
end
def stub_refund_request
stub_request(:post, "https://api.stripe.com/v1/charges/ch_1234/refunds")
.with(body: { amount: 2000, expand: ["charge"] },
headers: { 'Stripe-Account' => 'abc123' })
.to_return(payment_successful_refund_mock)
end
private
def payment_intent_authorize_response_mock(options)
{ status: options[:code] || 200,
body: JSON.generate(id: "pi_123",
object: "payment_intent",
amount: 2000,
amount_received: 2000,
status: options[:intent_status] || "requires_capture",
last_payment_error: nil,
charges: { data: [{ id: "ch_1234", amount: 2000 }] }) }
end
def payment_intent_redirect_response_mock(redirect_url)
{ status: 200, body: JSON.generate(id: "pi_123",
object: "payment_intent",
next_source_action: {
type: "authorize_with_url",
authorize_with_url: { url: redirect_url }
},
status: "requires_source_action") }
end
def payment_successful_capture_mock(options)
{ status: options[:code] || 200,
body: JSON.generate(object: "payment_intent",
amount: 2000,
charges: { data: [{ id: "ch_1234", amount: 2000 }] }) }
end
def payment_failed_capture_mock(options)
{ status: options[:code] || 402,
body: JSON.generate(error: { message:
options[:message] || "payment-method-failure" }) }
end
def hub_payment_method_response_mock(options)
{ status: options[:code] || 200,
body: JSON.generate(id: options[:pm_id] || "pm_456", customer: "cus_A123") }
end
def payment_successful_refund_mock
{ status: 200,
body: JSON.generate(object: "refund",
amount: 2000,
charge: "ch_1234") }
end
end