Skip to content

Commit

Permalink
Add test for insufficient stock behavior after Stripe redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-Yorkley committed Jul 9, 2021
1 parent 2cd6b05 commit a199f80
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions spec/controllers/checkout_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a199f80

Please sign in to comment.