diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index dfb9b593de..56b163cec0 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -169,7 +169,7 @@ class CheckoutController < Spree::CheckoutController def load_order @order = current_order redirect_to main_app.shop_path and return unless @order and @order.checkout_allowed? - raise_insufficient_quantity and return if @order.insufficient_stock_lines.present? + redirect_to_cart_path and return unless valid_order_line_items? redirect_to main_app.shop_path and return if @order.completed? before_address setup_for_current_state @@ -184,8 +184,11 @@ class CheckoutController < Spree::CheckoutController @order.ship_address = finder.ship_address end - # Overriding Spree's methods - def raise_insufficient_quantity + def valid_order_line_items? + @order.insufficient_stock_lines.empty? && OrderCycleDistributedVariants.new(@order.order_cycle, @order.distributor).distributes_order_variants?(@order) + end + + def redirect_to_cart_path respond_to do |format| format.html do redirect_to cart_path diff --git a/spec/controllers/checkout_controller_spec.rb b/spec/controllers/checkout_controller_spec.rb index bf75bc6128..c83f8007ef 100644 --- a/spec/controllers/checkout_controller_spec.rb +++ b/spec/controllers/checkout_controller_spec.rb @@ -36,22 +36,40 @@ describe CheckoutController, type: :controller do flash[:info].should == "The hub you have selected is temporarily closed for orders. Please try again later." end - it "redirects to the cart when some items are out of stock" do - controller.stub(:current_distributor).and_return(distributor) - controller.stub(:current_order_cycle).and_return(order_cycle) - controller.stub(:current_order).and_return(order) - order.stub_chain(:insufficient_stock_lines, :present?).and_return true - get :edit - response.should redirect_to spree.cart_path - end + describe "redirection to the cart" do + let(:order_cycle_distributed_variants) { double(:order_cycle_distributed_variants) } - it "renders when both distributor and order cycle is selected" do - controller.stub(:current_distributor).and_return(distributor) - controller.stub(:current_order_cycle).and_return(order_cycle) - controller.stub(:current_order).and_return(order) - order.stub_chain(:insufficient_stock_lines, :present?).and_return false - get :edit - response.should be_success + before do + controller.stub(:current_order).and_return(order) + order.stub(:distributor).and_return(distributor) + order.order_cycle = order_cycle + + allow(OrderCycleDistributedVariants).to receive(:new).with(order_cycle, distributor).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 + + get :edit + expect(response).to redirect_to spree.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) + + get :edit + expect(response).to redirect_to spree.cart_path + end + + + it "does not redirect when items are available and in stock" 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(true) + + get :edit + expect(response).to be_success + end end describe "building the order" do diff --git a/spec/requests/checkout/failed_checkout_spec.rb b/spec/requests/checkout/failed_checkout_spec.rb index 859ca41a1b..345b6a86df 100644 --- a/spec/requests/checkout/failed_checkout_spec.rb +++ b/spec/requests/checkout/failed_checkout_spec.rb @@ -23,6 +23,10 @@ describe "checking out an order that initially fails", type: :request do end before do + order_cycle_distributed_variants = double(:order_cycle_distributed_variants) + allow(OrderCycleDistributedVariants).to receive(:new).and_return(order_cycle_distributed_variants) + allow(order_cycle_distributed_variants).to receive(:distributes_order_variants?).and_return(true) + order.reload.update_totals set_order order end diff --git a/spec/requests/checkout/stripe_connect_spec.rb b/spec/requests/checkout/stripe_connect_spec.rb index fa8fb703a5..cd5eceb796 100644 --- a/spec/requests/checkout/stripe_connect_spec.rb +++ b/spec/requests/checkout/stripe_connect_spec.rb @@ -27,6 +27,10 @@ describe "checking out an order with a Stripe Connect payment method", type: :re end before do + order_cycle_distributed_variants = double(:order_cycle_distributed_variants) + allow(OrderCycleDistributedVariants).to receive(:new).and_return(order_cycle_distributed_variants) + allow(order_cycle_distributed_variants).to receive(:distributes_order_variants?).and_return(true) + allow(Stripe).to receive(:api_key) { "sk_test_12345" } order.update_attributes(distributor_id: enterprise.id, order_cycle_id: order_cycle.id) order.reload.update_totals