From a6ed003cb97478a3e65d113e6de845e1aef18da4 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 6 Oct 2020 17:49:33 +0100 Subject: [PATCH] Extract redirect stub to stripe_helper --- spec/features/admin/payments_stripe_spec.rb | 16 ++-------------- .../consumer/shopping/checkout_stripe_spec.rb | 17 +++-------------- spec/support/request/stripe_helper.rb | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 28 deletions(-) diff --git a/spec/features/admin/payments_stripe_spec.rb b/spec/features/admin/payments_stripe_spec.rb index 41a3801c51..4287608c38 100644 --- a/spec/features/admin/payments_stripe_spec.rb +++ b/spec/features/admin/payments_stripe_spec.rb @@ -89,21 +89,9 @@ feature ' end context "with a card that fails on registration because it requires(redirects) extra auth" do - let(:stripe_redirect_url) { checkout_path(payment_intent: "pi_123") } - let(:payment_intent_authorize_response) do - { status: 200, body: JSON.generate(id: "pi_123", - object: "payment_intent", - next_source_action: { - type: "authorize_with_url", - authorize_with_url: { url: stripe_redirect_url } - }, - status: "requires_source_action") } - end - before do - stub_request(:post, "https://api.stripe.com/v1/payment_intents") - .with(basic_auth: ["sk_test_12345", ""], body: /.*#{order.number}/) - .to_return(payment_intent_authorize_response) + stub_payment_intents_post_request_with_redirect order: order, + redirect_url: "www.dummy.org" end it "fails to add a payment due to card error" do diff --git a/spec/features/consumer/shopping/checkout_stripe_spec.rb b/spec/features/consumer/shopping/checkout_stripe_spec.rb index 273519456d..6c99489013 100644 --- a/spec/features/consumer/shopping/checkout_stripe_spec.rb +++ b/spec/features/consumer/shopping/checkout_stripe_spec.rb @@ -127,21 +127,10 @@ feature "Check out with Stripe", js: true do end context "when the card needs extra SCA authorization", js: true do - let(:stripe_redirect_url) { checkout_path(payment_intent: "pi_123") } - let(:payment_intent_authorize_response) do - { status: 200, body: JSON.generate(id: "pi_123", - object: "payment_intent", - next_source_action: { - type: "authorize_with_url", - authorize_with_url: { url: stripe_redirect_url } - }, - status: "requires_source_action") } - end - before do - stub_request(:post, "https://api.stripe.com/v1/payment_intents") - .with(basic_auth: ["sk_test_12345", ""], body: /.*#{order.number}/) - .to_return(payment_intent_authorize_response) + stripe_redirect_url = checkout_path(payment_intent: "pi_123") + stub_payment_intents_post_request_with_redirect order: order, + redirect_url: stripe_redirect_url end describe "and the authorization succeeds" do diff --git a/spec/support/request/stripe_helper.rb b/spec/support/request/stripe_helper.rb index b9f586f9d6..3f6eaa18e4 100644 --- a/spec/support/request/stripe_helper.rb +++ b/spec/support/request/stripe_helper.rb @@ -42,6 +42,12 @@ module StripeHelper 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 @@ -83,6 +89,16 @@ module StripeHelper 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",