From a199f80ed40aaa5948f7596f96c230d8a33fae37 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Mon, 14 Jun 2021 19:51:37 +0100 Subject: [PATCH] Add test for insufficient stock behavior after Stripe redirect --- spec/controllers/checkout_controller_spec.rb | 48 ++++++++++++++++---- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/spec/controllers/checkout_controller_spec.rb b/spec/controllers/checkout_controller_spec.rb index 24d4ec6e914..bde41ddce50 100644 --- a/spec/controllers/checkout_controller_spec.rb +++ b/spec/controllers/checkout_controller_spec.rb @@ -91,19 +91,47 @@ allow(OrderCycleDistributedVariants).to receive(:new).and_return(order_cycle_distributed_variants) end - it "redirects when some items are out of stock" do - allow(order).to receive_message_chain(:insufficient_stock_lines, :empty?).and_return false + context "running out of stock" do + it "redirects when some items are out of stock" do + allow(order).to receive_message_chain(:insufficient_stock_lines, :empty?).and_return false - get :edit - expect(response).to redirect_to cart_path - end + get :edit + expect(response).to redirect_to cart_path + end + + it "redirects when some items are not available" do + allow(order).to receive_message_chain(:insufficient_stock_lines, :empty?).and_return true + expect(order_cycle_distributed_variants).to receive(:distributes_order_variants?).with(order).and_return(false) - it "redirects when some items are not available" do - allow(order).to receive_message_chain(:insufficient_stock_lines, :empty?).and_return true - expect(order_cycle_distributed_variants).to receive(:distributes_order_variants?).with(order).and_return(false) + get :edit + expect(response).to redirect_to cart_path + end + + context "after redirecting back from Stripe" do + let(:order) { create(:order_with_totals_and_distribution) } + let!(:payment) { create(:payment, state: "pending", amount: order.total, order: order) } - get :edit - expect(response).to redirect_to cart_path + before do + allow(order).to receive_message_chain(:insufficient_stock_lines, :empty?).and_return(false) + allow(order_cycle_distributed_variants).to receive(:distributes_order_variants?). + with(order).and_return(true) + allow(controller).to receive(:valid_payment_intent_provided?) { true } + order.save + allow(order).to receive_message_chain(:payments, :completed) { [] } + allow(order).to receive_message_chain(:payments, :pending) { [payment] } + end + + it "cancels the payment and resets the order to cart" do + expect(payment).to receive(:void!).and_call_original + + spree_post :edit + + expect(response).to redirect_to cart_path + expect(flash[:notice]).to eq I18n.t('checkout.payment_cancelled_due_to_stock') + + expect(order.state).to eq "cart" + end + end end describe "when items are available and in stock" do