add stubs for stripe requests

This commit is contained in:
Andy Brett
2020-11-19 12:51:00 -08:00
parent fea7576ac1
commit 3d47ad7e33
7 changed files with 101 additions and 9 deletions

View File

@@ -24,6 +24,9 @@ feature '
before do
stub_payment_methods_post_request
stub_payment_intent_get_request
stub_retrieve_payment_method_request("pm_123")
stub_list_customers_request(email: order.user.email, response: {})
stub_get_customer_payment_methods_request(customer: "cus_A456", response: {})
end
context "for a complete order" do

View File

@@ -4,11 +4,14 @@ require 'spec_helper'
feature "Credit Cards", js: true do
include AuthenticationHelper
include StripeHelper
include StripeStubs
describe "as a logged in user" do
let(:user) { create(:user) }
let!(:customer) { create(:customer, user: user) }
let!(:default_card) { create(:credit_card, user_id: user.id, gateway_customer_profile_id: 'cus_AZNMJ', is_default: true) }
let!(:non_default_card) { create(:credit_card, user_id: user.id, gateway_customer_profile_id: 'cus_FDTG') }
let!(:default_card) { create(:stored_credit_card, user_id: user.id, gateway_customer_profile_id: 'cus_AZNMJ', is_default: true) }
let!(:non_default_card) { create(:stored_credit_card, user_id: user.id, gateway_customer_profile_id: 'cus_FDTG') }
around do |example|
original_stripe_connect_enabled = Spree::Config[:stripe_connect_enabled]
@@ -19,7 +22,7 @@ feature "Credit Cards", js: true do
before do
login_as user
allow(Stripe).to receive(:api_key) { "sk_test_xxxx" }
allow(Stripe).to receive(:api_key) { "sk_test_12345" }
allow(Stripe).to receive(:publishable_key) { "some_token" }
Spree::Config.set(stripe_connect_enabled: true)
@@ -28,6 +31,9 @@ feature "Credit Cards", js: true do
stub_request(:delete, "https://api.stripe.com/v1/customers/cus_AZNMJ").
to_return(status: 200, body: JSON.generate(deleted: true, id: "cus_AZNMJ"))
stub_retrieve_payment_method_request("card_1EY...")
stub_list_customers_request(email: user.email, response: {})
stub_get_customer_payment_methods_request(customer: "cus_AZNMJ", response: {})
end
it "passes the smoke test" do

View File

@@ -97,6 +97,12 @@ feature "Check out with Stripe", js: true do
end
context "with guest checkout" do
before do
stub_retrieve_payment_method_request("pm_123")
stub_list_customers_request(email: order.user.email, response: {})
stub_get_customer_payment_methods_request(customer: "cus_A456", response: {})
end
context "when the card is accepted" do
before do
stub_payment_intents_post_request order: order
@@ -205,7 +211,12 @@ feature "Check out with Stripe", js: true do
context "saving a card and re-using it" do
before do
stub_retrieve_payment_method_request("pm_123")
stub_list_customers_request(email: order.user.email, response: {})
stub_get_customer_payment_methods_request(customer: "cus_A456", response: {})
stub_get_customer_payment_methods_request(customer: "cus_A123", response: {})
stub_payment_methods_post_request request: { payment_method: "pm_123", customer: "cus_A123" }, response: { pm_id: "pm_123" }
stub_add_metadata_request(payment_method: "pm_123", response: {})
stub_payment_intents_post_request order: order
stub_successful_capture_request order: order
stub_customers_post_request email: user.email

View File

@@ -5,7 +5,7 @@ require 'stripe/credit_card_cloner'
module Stripe
describe CreditCardCloner do
describe "#clone" do
describe "#find_or_clone" do
include StripeStubs
let(:cloner) { Stripe::CreditCardCloner.new }
@@ -14,7 +14,7 @@ module Stripe
let(:payment_method_id) { "pm_1234" }
let(:new_customer_id) { "cus_A456" }
let(:new_payment_method_id) { "pm_456" }
let(:stripe_account_id) { "acct_456" }
let(:stripe_account_id) { "abc123" }
let(:payment_method_response_mock) { { status: 200, body: payment_method_response_body } }
let(:credit_card) { create(:credit_card, user: create(:user)) }
@@ -29,6 +29,12 @@ module Stripe
stub_customers_post_request email: credit_card.user.email,
response: { customer_id: new_customer_id },
stripe_account_header: true
stub_retrieve_payment_method_request(payment_method_id)
stub_list_customers_request(email: credit_card.user.email, response: {})
stub_get_customer_payment_methods_request(customer: "cus_A456", response: {})
stub_add_metadata_request(payment_method: "pm_456", response: {})
stub_request(:post,
"https://api.stripe.com/v1/payment_methods/#{new_payment_method_id}/attach")
.with(body: { customer: new_customer_id },
@@ -47,7 +53,7 @@ module Stripe
end
it "clones the payment method only" do
customer_id, payment_method_id = cloner.clone(credit_card, stripe_account_id)
customer_id, payment_method_id = cloner.find_or_clone(credit_card, stripe_account_id)
expect(payment_method_id).to eq new_payment_method_id
expect(customer_id).to eq nil
@@ -65,7 +71,7 @@ module Stripe
end
it "clones both the payment method and the customer" do
customer_id, payment_method_id = cloner.clone(credit_card, stripe_account_id)
customer_id, payment_method_id = cloner.find_or_clone(credit_card, stripe_account_id)
expect(payment_method_id).to eq new_payment_method_id
expect(customer_id).to eq new_customer_id

View File

@@ -8,7 +8,7 @@ module Stripe
describe "#call" do
let(:validator) { Stripe::PaymentIntentValidator.new }
let(:payment_intent_id) { "pi_123" }
let(:stripe_account_id) { "acct_456" }
let(:stripe_account_id) { "abc123" }
let(:payment_intent_response_mock) { { status: 200, body: payment_intent_response_body } }
before do

View File

@@ -6,6 +6,8 @@ describe "checking out an order with a Stripe SCA payment method", type: :reques
include ShopWorkflow
include AuthenticationHelper
include OpenFoodNetwork::ApiHelper
include StripeHelper
include StripeStubs
let!(:order_cycle) { create(:simple_order_cycle) }
let!(:enterprise) { create(:distributor_enterprise) }
@@ -104,6 +106,11 @@ describe "checking out an order with a Stripe SCA payment method", type: :reques
stub_request(:post, "https://api.stripe.com/v1/payment_intents/#{payment_intent_id}/capture")
.with(basic_auth: ["sk_test_12345", ""], body: { amount_to_capture: "1234" })
.to_return(payment_intent_response_mock)
stub_retrieve_payment_method_request("pm_123")
stub_list_customers_request(email: order.user.email, response: {})
stub_get_customer_payment_methods_request(customer: "cus_A456", response: {})
stub_add_metadata_request(payment_method: "pm_456", response: {})
end
context "when the user submits a new card and doesn't request that the card is saved for later" do

View File

@@ -35,14 +35,47 @@ module StripeStubs
.to_return(hub_payment_method_response_mock({ pm_id: "pm_123" }))
end
def stub_retrieve_payment_method_request(payment_method_id = "pm_1234")
stub_request(:get, "https://api.stripe.com/v1/payment_methods/#{payment_method_id}")
.with(headers: { 'Authorization' => 'Bearer sk_test_12345' })
.to_return(retrieve_payment_method_response_mock({}))
end
# Stubs the customers call to both the main stripe account and the connected account
def stub_customers_post_request(email:, response: {}, stripe_account_header: false)
stub = stub_request(:post, "https://api.stripe.com/v1/customers")
.with(body: { email: email })
stub = stub.with(headers: { 'Stripe-Account' => 'acct_456' }) if stripe_account_header
stub = stub.with(headers: { 'Stripe-Account' => 'abc123' }) if stripe_account_header
stub.to_return(customers_response_mock(response))
end
def stub_list_customers_request(email:, response: {})
stub = stub_request(:get, "https://api.stripe.com/v1/customers?email=#{email}&limit=100")
stub = stub.with(
headers: { 'Authorization' => 'Bearer sk_test_12345', 'Stripe-Account' => 'abc123' }
)
stub.to_return(list_customers_response_mock(response))
end
def stub_get_customer_payment_methods_request(customer: "cus_A456", response: {})
stub = stub_request(
:get, "https://api.stripe.com/v1/payment_methods?customer=#{customer}&limit=100&type=card"
)
stub = stub.with(
headers: { 'Authorization' => 'Bearer sk_test_12345', 'Stripe-Account' => 'abc123' }
)
stub.to_return(get_customer_payment_methods_response_mock(response))
end
def stub_add_metadata_request(payment_method: "pm_456", response: {})
stub = stub_request(:post, "https://api.stripe.com/v1/payment_methods/#{payment_method}")
stub = stub.with(body: { metadata: { "ofn-clone": true } })
stub = stub.with(
headers: { 'Authorization' => 'Bearer sk_test_12345', 'Stripe-Account' => 'abc123' }
)
stub.to_return(add_metadata_response_mock(response))
end
def stub_successful_capture_request(order:, response: {})
stub_capture_request(order, payment_successful_capture_mock(response))
end
@@ -119,4 +152,30 @@ module StripeStubs
amount: 2000,
charge: "ch_1234") }
end
def retrieve_payment_method_response_mock(options)
{ status: options[:code] || 200,
body: JSON.generate(
id: options[:pm_id] || "pm_456", customer: "cus_A123", card: { fingerprint: "12345" }
) }
end
def list_customers_response_mock(options)
{ status: options[:code] || 200,
body: JSON.generate(has_more: false, data: [{ id: "cus_A456" }]) }
end
def get_customer_payment_methods_response_mock(options)
payment_method = options[:payment_method] || "pm_456"
fingerprint = options[:fingerprint] || "7890"
{ status: options[:code] || 200,
body: JSON.generate(
has_more: false, data: [{ id: payment_method, card: { fingerprint: fingerprint } }]
) }
end
def add_metadata_response_mock(options)
{ status: options[:code] || 200,
body: JSON.generate({}) }
end
end