From bc40b2c8561fab32be9cec08484fbbe146ea0b0a Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 21 Jun 2013 11:06:21 +1000 Subject: [PATCH] Test each distinct distributor/order cycle invalid combination case --- spec/controllers/orders_controller_spec.rb | 66 +++++++++++++--------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/spec/controllers/orders_controller_spec.rb b/spec/controllers/orders_controller_spec.rb index 50afbbb102..e3241c4509 100644 --- a/spec/controllers/orders_controller_spec.rb +++ b/spec/controllers/orders_controller_spec.rb @@ -4,7 +4,7 @@ describe Spree::OrdersController do def current_user controller.current_user end - + it "selects distributors" do d = create(:distributor_enterprise) p = create(:product, :distributors => [d]) @@ -37,7 +37,41 @@ describe Spree::OrdersController do @request.env["HTTP_REFERER"] = 'http://test.host/' end - it "produces an error when the distributor does not distribute the product" do + it "errors when an invalid distributor is selected" do + # Given a product and some distributors + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) + p = create(:product, :price => 12.34) + oc = create(:simple_order_cycle, :distributors => [d1], :variants => [p.master]) + + # When I attempt to add the product to the cart with an invalid distributor, it should not be added + expect do + spree_post :populate, variants: {p.master.id => 1}, distributor_id: d2.id, order_cycle_id: oc.id + response.should redirect_to :back + end.to change(self, :num_items_in_cart).by(0) + + # And I should see an error + flash[:error].should == "That product is not available from the chosen distributor or order cycle." + end + + it "errors when an invalid order cycle is selected" do + # Given a product and some order cycles + d = create(:distributor_enterprise) + p = create(:product, :price => 12.34) + oc1 = create(:simple_order_cycle, :distributors => [d], :variants => [p.master]) + oc2 = create(:simple_order_cycle, :distributors => [d], :variants => []) + + # When I attempt to add the product to my cart with an invalid order cycle, it should not be added + expect do + spree_post :populate, variants: {p.master.id => 1}, distributor_id: d.id, order_cycle_id: oc2.id + response.should redirect_to :back + end.to change(self, :num_items_in_cart).by(0) + + # And I should see an error + flash[:error].should == "That product is not available from the chosen distributor or order cycle." + end + + it "errors when distribution is valid for the new product but does not cover the cart" do # Given two products with different distributors d1 = create(:distributor_enterprise) d2 = create(:distributor_enterprise) @@ -54,36 +88,12 @@ describe Spree::OrdersController do # And I attempt to add the second, then the product should not be added to my cart expect do - spree_post :populate, variants: {p2.master.id => 1}, distributor_id: d1.id, order_cycle_id: oc1.id + spree_post :populate, variants: {p2.master.id => 1}, distributor_id: d2.id, order_cycle_id: oc2.id response.should redirect_to :back end.to change(self, :num_items_in_cart).by(0) # And I should see an error - flash[:error].should == "That product is not available from the chosen distributor or order cycle." - end - - it "produces an error when the order cycle does not distribute the product" do - # Given two products with the same distributor but different order cycles - d = create(:distributor_enterprise) - p1 = create(:product, :price => 12.34) - p2 = create(:product, :price => 23.45) - oc1 = create(:simple_order_cycle, :distributors => [d], :variants => [p1.master]) - oc2 = create(:simple_order_cycle, :distributors => [d], :variants => [p2.master]) - - # When I add the first to my cart - expect do - spree_post :populate, variants: {p1.master.id => 1}, distributor_id: d.id, order_cycle_id: oc1.id - response.should redirect_to spree.cart_path - end.to change(self, :num_items_in_cart).by(1) - - # And I attempt to add the second, then the product should not be added to my cart - expect do - spree_post :populate, variants: {p2.master.id => 1}, distributor_id: d.id, order_cycle_id: oc1.id - response.should redirect_to :back - end.to change(self, :num_items_in_cart).by(0) - - # And I should see an error - flash[:error].should == "That product is not available from the chosen distributor or order cycle." + flash[:error].should == "That distributor or order cycle can't supply all the products in your cart. Please choose another." end end