From 0e289a23c1d90f274cd7d6b5eb4ccfd31c8d758f Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Sat, 3 Jun 2023 11:41:05 +0100 Subject: [PATCH] Set some legacy checkout specs that we might want to rewrite later to pending --- .../requests/checkout/failed_checkout_spec.rb | 2 +- spec/requests/checkout/stripe_sca_spec.rb | 4 +- spec/system/consumer/multilingual_spec.rb | 7 - .../consumer/shopping/checkout_paypal_spec.rb | 6 +- .../system/consumer/shopping/checkout_spec.rb | 6 +- .../consumer/shopping/checkout_stripe_spec.rb | 222 ++++++++++++++++++ .../shopping/variant_overrides_spec.rb | 16 +- 7 files changed, 240 insertions(+), 23 deletions(-) create mode 100644 spec/system/consumer/shopping/checkout_stripe_spec.rb diff --git a/spec/requests/checkout/failed_checkout_spec.rb b/spec/requests/checkout/failed_checkout_spec.rb index 2851659605..25c98f7bcd 100644 --- a/spec/requests/checkout/failed_checkout_spec.rb +++ b/spec/requests/checkout/failed_checkout_spec.rb @@ -44,7 +44,7 @@ describe "checking out an order that initially fails", type: :request do set_order order end - context "when shipping and payment fees apply" do + pending "when shipping and payment fees apply" do let(:calculator) { Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) } before do diff --git a/spec/requests/checkout/stripe_sca_spec.rb b/spec/requests/checkout/stripe_sca_spec.rb index 4bae1defa6..517fec564f 100644 --- a/spec/requests/checkout/stripe_sca_spec.rb +++ b/spec/requests/checkout/stripe_sca_spec.rb @@ -114,7 +114,7 @@ describe "checking out an order with a Stripe SCA payment method", type: :reques 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 + pending "when the user submits a new card and doesn't request that the card is saved for later" do let(:hubs_payment_method_response_mock) do { status: 200, body: JSON.generate(id: hubs_stripe_payment_method) } end @@ -161,7 +161,7 @@ describe "checking out an order with a Stripe SCA payment method", type: :reques end end - context "when saving a card or using a stored card is involved" do + pending "when saving a card or using a stored card is involved" do let(:hubs_payment_method_response_mock) do { status: 200, diff --git a/spec/system/consumer/multilingual_spec.rb b/spec/system/consumer/multilingual_spec.rb index 66a94431fc..1ce556a2f2 100644 --- a/spec/system/consumer/multilingual_spec.rb +++ b/spec/system/consumer/multilingual_spec.rb @@ -61,13 +61,6 @@ describe 'Multilingual' do expect_menu_and_cookie_in_es expect(page).to have_content 'Precio' end - - it "in the checkout page" do - visit checkout_path(locale: 'es') - - expect_menu_and_cookie_in_es - expect(page).to have_content 'Total del carrito' - end end end diff --git a/spec/system/consumer/shopping/checkout_paypal_spec.rb b/spec/system/consumer/shopping/checkout_paypal_spec.rb index 8cc040d337..8a5e232cb8 100644 --- a/spec/system/consumer/shopping/checkout_paypal_spec.rb +++ b/spec/system/consumer/shopping/checkout_paypal_spec.rb @@ -47,7 +47,7 @@ describe "Check out with Paypal" do end shared_examples "checking out with paypal" do |user_type| - context user_type.to_s do + pending user_type.to_s do before do fill_out_details fill_out_form(free_shipping.name, paypal.name, save_default_addresses: false) @@ -82,7 +82,7 @@ describe "Check out with Paypal" do end describe "shared_examples" do - context "as a guest user" do + pending "as a guest user" do before do visit checkout_path checkout_as_guest @@ -90,7 +90,7 @@ describe "Check out with Paypal" do it_behaves_like "checking out with paypal", "as guest" end - context "as a logged in user" do + pending "as a logged in user" do before do login_as user visit checkout_path diff --git a/spec/system/consumer/shopping/checkout_spec.rb b/spec/system/consumer/shopping/checkout_spec.rb index 077ad9d51d..6de138b04d 100644 --- a/spec/system/consumer/shopping/checkout_spec.rb +++ b/spec/system/consumer/shopping/checkout_spec.rb @@ -71,7 +71,7 @@ describe "As a consumer I want to check out my cart" do distributor.shipping_methods << tagged_shipping end - describe "when I have an out of stock product in my cart" do + pending "when I have an out of stock product in my cart" do before do variant.on_demand = false variant.on_hand = 0 @@ -87,7 +87,7 @@ describe "As a consumer I want to check out my cart" do end end - context 'login in as user' do + pending 'login in as user' do let(:user) { create(:user) } let(:pdf_upload) { Rack::Test::UploadedFile.new( @@ -335,7 +335,7 @@ describe "As a consumer I want to check out my cart" do end end - context "guest checkout" do + pending "guest checkout" do before do visit checkout_path checkout_as_guest diff --git a/spec/system/consumer/shopping/checkout_stripe_spec.rb b/spec/system/consumer/shopping/checkout_stripe_spec.rb new file mode 100644 index 0000000000..ae62de53ae --- /dev/null +++ b/spec/system/consumer/shopping/checkout_stripe_spec.rb @@ -0,0 +1,222 @@ +# frozen_string_literal: true + +require 'system_helper' + +describe "Check out with Stripe" do + include AuthenticationHelper + include ShopWorkflow + include CheckoutRequestsHelper + include StripeHelper + include StripeStubs + + let(:distributor) { create(:distributor_enterprise) } + let!(:order_cycle) { + create(:simple_order_cycle, distributors: [distributor], variants: [variant]) + } + let(:product) { create(:product, price: 10) } + let(:variant) { product.variants.first } + let(:order) { + create(:order, order_cycle: order_cycle, distributor: distributor, bill_address_id: nil, + ship_address_id: nil) + } + + let(:shipping_with_fee) { + create(:shipping_method, require_ship_address: false, name: "Donkeys", + calculator: Calculator::FlatRate.new(preferred_amount: 4.56)) + } + let(:free_shipping) { create(:shipping_method) } + let!(:check_with_fee) { + create(:payment_method, distributors: [distributor], + calculator: Calculator::FlatRate.new(preferred_amount: 5.67)) + } + + around do |example| + with_stripe_setup { example.run } + end + + before do + stripe_enable + set_order order + add_product_to_cart order, product + distributor.shipping_methods << [shipping_with_fee, free_shipping] + end + + pending "using Stripe SCA" do + let!(:stripe_account) { create(:stripe_account, enterprise: distributor) } + let!(:stripe_sca_payment_method) { + create(:stripe_sca_payment_method, distributors: [distributor]) + } + let!(:shipping_method) { create(:shipping_method) } + let(:error_message) { "Card was declined: insufficient funds." } + + before do + stub_payment_intent_get_request + stub_payment_methods_post_request + 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 + stub_successful_capture_request order: order + end + + it "completes checkout successfully" do + checkout_with_stripe + + expect(page).to have_content "Confirmed" + expect(order.reload.completed?).to eq true + expect(order.payments.first.state).to eq "completed" + end + end + + context "when the card is rejected" do + before do + stub_payment_intents_post_request order: order + stub_failed_capture_request order: order, response: { message: error_message } + end + + it "shows an error message from the Stripe response" do + checkout_with_stripe + + 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" do + before do + 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 + before do + stub_successful_capture_request order: order + end + + it "completes checkout successfully" do + checkout_with_stripe + + # 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 payment through the mock + # stub_successful_capture_request + + expect(page).to have_content "Confirmed" + expect(order.reload.completed?).to eq true + expect(order.payments.first.state).to eq "completed" + end + end + + describe "and the authorization fails" do + before do + stub_failed_capture_request order: order, response: { message: error_message } + end + + it "shows an error message from the Stripe response" do + checkout_with_stripe + + # 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 payment through the mock stub_failed_capture_request + expect(page).to have_content error_message + expect(order.reload.state).to eq "cart" + expect(order.payments.first.state).to eq "failed" + end + end + end + + context "with multiple payment attempts; one failed and one succeeded" do + before do + stub_payment_intents_post_request order: order + end + + it "records failed payment attempt and allows order completion" do + # First payment attempt is rejected + stub_failed_capture_request(order: order, response: { message: error_message }) + checkout_with_stripe + expect(page).to have_content error_message + + expect(order.reload.payments.count).to eq 1 + expect(order.state).to eq "cart" + expect(order.payments.first.state).to eq "failed" + + # Second payment attempt is accepted + stub_successful_capture_request order: order + place_order + expect(page).to have_content "Confirmed" + + expect(order.reload.payments.count).to eq 2 + expect(order.state).to eq "complete" + expect(order.payments.last.state).to eq "completed" + end + end + end + + context "with a logged in user" do + let(:user) { order.user } + + before do + login_as user + end + + 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: "test@test.com" # First checkout with default details + stub_customers_post_request email: user.email # Second checkout with saved user details + stub_payment_method_attach_request + end + + it "allows saving a card and re-using it" do + checkout_with_stripe guest_checkout: false, remember_card: true + + expect(page).to have_content "Confirmed" + expect(order.reload.completed?).to eq true + expect(order.payments.first.state).to eq "completed" + + # Verify card has been saved with correct stripe IDs + user_credit_card = order.reload.user.credit_cards.first + expect(user_credit_card.gateway_payment_profile_id).to eq "pm_123" + expect(user_credit_card.gateway_customer_profile_id).to eq "cus_A123" + + # Prepare a second order + new_order = create(:order, user: user, order_cycle: order_cycle, + distributor: distributor, bill_address_id: nil, + ship_address_id: nil) + set_order(new_order) + add_product_to_cart(new_order, product, quantity: 10) + stub_payment_intents_post_request order: new_order + stub_successful_capture_request order: new_order + + # Checkout with saved card + visit checkout_path + choose free_shipping.name + choose stripe_sca_payment_method.name + expect(page).to have_content "Use a saved card" + expect(page).to have_select 'selected_card', selected: "Visa x-4242 Exp:10/2050" + place_order + end + end + end + end +end diff --git a/spec/system/consumer/shopping/variant_overrides_spec.rb b/spec/system/consumer/shopping/variant_overrides_spec.rb index 05f89716f1..a0512ef3b2 100644 --- a/spec/system/consumer/shopping/variant_overrides_spec.rb +++ b/spec/system/consumer/shopping/variant_overrides_spec.rb @@ -150,17 +150,19 @@ describe "shopping with variant overrides defined" do expect(page).to have_selector "#edit-cart .grand-total", text: with_currency(122.22) end - it "shows the correct prices in the checkout" do - click_add_to_cart product1_variant1, 2 - click_checkout + pending "prices in the checkout" do + it "shows the correct prices" do + click_add_to_cart product1_variant1, 2 + click_checkout - expect(page).to have_selector 'form.edit_order .cart-total', text: with_currency(122.22) - expect(page).to have_selector 'form.edit_order .shipping', text: with_currency(0.00) - expect(page).to have_selector 'form.edit_order .total', text: with_currency(122.22) + expect(page).to have_selector 'form.edit_order .cart-total', text: with_currency(122.22) + expect(page).to have_selector 'form.edit_order .shipping', text: with_currency(0.00) + expect(page).to have_selector 'form.edit_order .total', text: with_currency(122.22) + end end end - describe "creating orders" do + pending "creating orders" do it "creates the order with the correct prices" do click_add_to_cart product1_variant1, 2 click_checkout