From 4481ca83f9126bb2467323972fc70018c36940dd Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Sun, 24 Jun 2012 19:58:09 +1000 Subject: [PATCH] OrdersController does not permit adding products with invalid distributors --- .../spree/orders_controller_decorator.rb | 6 +++ spec/controllers/orders_controller_spec.rb | 40 +++++++++++++++++++ spec/requests/consumer/add_to_cart_spec.rb | 14 ++++++- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index b87beb7e2c..be6b716639 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -30,6 +30,12 @@ Spree::OrdersController.class_eval do return false unless variant.product.distributors.include? distributor end if params[:variants] + # -- If products in cart, distributor can't be changed + order = current_order(false) + if !order.nil? && order.distributor != distributor + return false + end + true end end diff --git a/spec/controllers/orders_controller_spec.rb b/spec/controllers/orders_controller_spec.rb index 1bac3596dc..d7b82ae613 100644 --- a/spec/controllers/orders_controller_spec.rb +++ b/spec/controllers/orders_controller_spec.rb @@ -35,6 +35,46 @@ describe Spree::OrdersController do # Then our order should have its distributor set to the chosen distributor current_order(false).distributor.should == d end + end + context "adding a subsequent product to the cart" do + before(:each) do + # Given a product and a distributor + @distributor = create(:distributor) + @product = create(:product, :distributors => [@distributor]) + + # And the product is in the cart + spree_put :populate, :variants => {@product.id => 1}, :distributor_id => @distributor.id + current_order(false).line_items.map { |li| li.product }.should == [@product] + current_order(false).distributor.should == @distributor + end + + it "does not add the product if the product is not available at the order's distributor" do + # Given a product at another distributor + d2 = create(:distributor) + p2 = create(:product, :distributors => [d2]) + + # When I attempt to add the product to the cart + spree_put :populate, :variants => {p2.id => 1}, :distributor_id => d2.id + + # Then the product should not be added to the cart + current_order(false).line_items.map { |li| li.product }.should == [@product] + current_order(false).distributor.should == @distributor + end + + it "does not add the product if the product is not available at the given distributor" do + # Given a product at another distributor + d2 = create(:distributor) + p2 = create(:product, :distributors => [d2]) + + # When I attempt to add the product to the cart with a fake distributor_id + spree_put :populate, :variants => {p2.id => 1}, :distributor_id => @distributor.id + + # Then the product should not be added to the cart + current_order(false).line_items.map { |li| li.product }.should == [@product] + current_order(false).distributor.should == @distributor + end + + it "does not add the product if the chosen distributor is different from the order's distributor" end end diff --git a/spec/requests/consumer/add_to_cart_spec.rb b/spec/requests/consumer/add_to_cart_spec.rb index 98fd5f8323..fbe51c5e7a 100644 --- a/spec/requests/consumer/add_to_cart_spec.rb +++ b/spec/requests/consumer/add_to_cart_spec.rb @@ -51,8 +51,18 @@ feature %q{ context "adding a subsequent product to the cart" do it "does not allow the user to choose a distributor" do - # Instead, they see "Your distributor for this order is XYZ" - pending + # Given a product under a distributor + d = create(:distributor) + p = create(:product, :distributors => [d]) + + # And a product in my cart + visit spree.product_path p + click_button 'Add To Cart' + + # When I go to add it again, I should not have a choice of distributor + visit spree.product_path p + page.should_not have_selector 'select#distributor_id' + page.should have_selector 'p', :text => "Your distributor for this order is #{d.name}" end it "does not allow the user to add a product from another distributor" do