From df4ec67974c3ec8641cb76861d0dd5dbe24d0aa9 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sat, 3 Oct 2020 11:01:29 +0100 Subject: [PATCH] Add feature spec that covers stripe_sca redirect case --- .../consumer/shopping/checkout_stripe_spec.rb | 51 ++++++++++++++++--- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/spec/features/consumer/shopping/checkout_stripe_spec.rb b/spec/features/consumer/shopping/checkout_stripe_spec.rb index e9b5e65230..bfcd521961 100644 --- a/spec/features/consumer/shopping/checkout_stripe_spec.rb +++ b/spec/features/consumer/shopping/checkout_stripe_spec.rb @@ -100,7 +100,6 @@ feature "Check out with Stripe", js: true do it "completes checkout successfully" do visit checkout_path - checkout_as_guest fill_out_form( @@ -108,13 +107,10 @@ feature "Check out with Stripe", js: true do stripe_sca_payment_method.name, save_default_addresses: false ) - fill_out_card_details - place_order expect(page).to have_content "Confirmed" - expect(order.reload.completed?).to eq true expect(order.payments.first.state).to eq "completed" end @@ -132,7 +128,6 @@ feature "Check out with Stripe", js: true do it "shows an error message from the Stripe response" do visit checkout_path - checkout_as_guest fill_out_form( @@ -140,17 +135,57 @@ feature "Check out with Stripe", js: true do stripe_sca_payment_method.name, save_default_addresses: false ) - fill_out_card_details - place_order expect(page).to have_content error_message - expect(order.reload.state).to eq "cart" expect(order.payments.first.state).to eq "failed" end 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_payment_intent_get_request + stub_hub_payment_methods_request + 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_successful_capture_request order: order + end + + it "completes checkout successfully" do + visit checkout_path + checkout_as_guest + + fill_out_form( + free_shipping.name, + stripe_sca_payment_method.name, + save_default_addresses: false + ) + fill_out_card_details + place_order + + # We make stripe return stripe_redirect_url (which is already sending the user back to the checkout) as if the authorization was done + # We can then control the actual authorization or failure of the authorization through the mock stub_payment_intent_get_request + + expect(page).to have_content "Confirmed" + expect(order.reload.completed?).to eq true + expect(order.payments.first.state).to eq "completed" + end + end end end end