Add test for insufficient stock behavior after Stripe redirect

This commit is contained in:
Matt-Yorkley
2021-06-14 19:51:37 +01:00
parent 2cd6b05aba
commit a199f80ed4

View File

@@ -91,19 +91,47 @@ describe CheckoutController, type: :controller do
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
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) }
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