Add spec coverage, refactor, avoid double-render errors

👍
This commit is contained in:
Matt-Yorkley
2020-11-22 18:37:31 +00:00
parent cabec7e73f
commit 2fa2a30c67
4 changed files with 25 additions and 4 deletions

View File

@@ -25,7 +25,7 @@ class CheckoutController < Spree::StoreController
before_action :ensure_order_not_completed
before_action :ensure_checkout_allowed
before_action :ensure_sufficient_stock_lines
before_action :handle_insufficient_stock
before_action :associate_user
before_action :check_authorization

View File

@@ -3,10 +3,16 @@
module OrderStockCheck
extend ActiveSupport::Concern
def ensure_sufficient_stock_lines
return true if @order.insufficient_stock_lines.blank?
def handle_insufficient_stock
return if sufficient_stock?
flash[:error] = Spree.t(:inventory_error_flash_for_insufficient_quantity)
redirect_to main_app.cart_path
end
private
def sufficient_stock?
@sufficient_stock ||= @order.insufficient_stock_lines.blank?
end
end

View File

@@ -58,9 +58,10 @@ Spree::PaypalController.class_eval do
def confirm
@order = current_order || raise(ActiveRecord::RecordNotFound)
# At this point the user has come back from the Paypal form, and we get one
# last chance to interact with the payment process before the money moves...
return unless ensure_sufficient_stock_lines
return handle_insufficient_stock unless sufficient_stock?
@order.payments.create!({
source: Spree::PaypalExpressCheckout.create({

View File

@@ -27,6 +27,20 @@ module Spree
spree_post :confirm, payment_method_id: payment_method.id
expect(session[:access_token]).to eq(controller.current_order.token)
end
context "if the stock ran out whilst the payment was being placed" do
before do
allow(controller.current_order).to receive(:insufficient_stock_lines).and_return(true)
end
it "redirects to the cart with out of stock error" do
expect(spree_post(:confirm, payment_method_id: payment_method.id)).
to redirect_to cart_path
# And does not complete processing of the payment
expect(controller.current_order.reload.payments.count).to eq 0
end
end
end
describe '#expire_current_order' do