From b0449be3027bc04ab9d53ebbddfd0e308e76d027 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Thu, 20 Dec 2018 01:29:19 +0000 Subject: [PATCH] Add spec to test cart controller handling of variant overrides on cart populate --- app/services/variants_stock_levels.rb | 3 ++ spec/controllers/cart_controller_spec.rb | 40 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/app/services/variants_stock_levels.rb b/app/services/variants_stock_levels.rb index 69e91cc828..f9e8fc9ead 100644 --- a/app/services/variants_stock_levels.rb +++ b/app/services/variants_stock_levels.rb @@ -5,6 +5,9 @@ class VariantsStockLevels def call(order, requested_variant_ids) variant_stock_levels = variant_stock_levels(order.line_items) + # Potentially, the following lines are dead code, they are never reached + # Additionally, the variants are not scoped here and so the stock levels reported would be incorrect + # See cart_controller_spec for more details and #3222 li_variant_ids = variant_stock_levels.keys (requested_variant_ids - li_variant_ids).each do |variant_id| variant_on_hand = Spree::Variant.find(variant_id).on_hand diff --git a/spec/controllers/cart_controller_spec.rb b/spec/controllers/cart_controller_spec.rb index a983dd360d..d9c3a29942 100644 --- a/spec/controllers/cart_controller_spec.rb +++ b/spec/controllers/cart_controller_spec.rb @@ -28,6 +28,46 @@ describe CartController, type: :controller do end end + context "handles variant overrides correctly" do + let(:product) { create(:simple_product, supplier: producer) } + let(:producer) { create(:supplier_enterprise) } + let!(:variant_in_the_order) { create(:variant, count_on_hand: 4) } + let!(:variant_not_in_the_order) { create(:variant, count_on_hand: 2) } + + let(:hub) { create(:distributor_enterprise, with_payment_and_shipping: true) } + let!(:variant_override_in_the_order) { create(:variant_override, hub: hub, variant: variant_in_the_order, price: 55.55, count_on_hand: 20, default_stock: nil, resettable: false) } + let!(:variant_override_not_in_the_order) { create(:variant_override, hub: hub, variant: variant_not_in_the_order, count_on_hand: 7, default_stock: nil, resettable: false) } + + let(:order_cycle) { create(:simple_order_cycle, suppliers: [producer], coordinator: hub, distributors: [hub]) } + let!(:order) { subject.current_order(true) } + let!(:line_item) { create(:line_item, order: order, variant: variant_in_the_order, quantity: 2, max_quantity: 3) } + + before do + order_cycle.exchanges.outgoing.first.variants = [variant_in_the_order, variant_not_in_the_order] + order.order_cycle = order_cycle + order.distributor = hub + order.save + end + + it "returns the variant override stock levels of the variant in the order" do + spree_post :populate, variants: { variant_in_the_order.id => 1 } + + data = JSON.parse(response.body) + expect(data['stock_levels'][variant_in_the_order.id.to_s]["on_hand"]).to eq 20 + end + + it "returns the variant override stock levels of the variant requested but not in the order" do + # This test passes because the variant requested gets added to the order + # If the variant was not added to the order, VariantsStockLevels alternative calculation would fail + # See #3222 for more details + # This indicates that the VariantsStockLevels alternative calculation is never reached + spree_post :populate, variants: { variant_not_in_the_order.id => 1 } + + data = JSON.parse(response.body) + expect(data['stock_levels'][variant_not_in_the_order.id.to_s]["on_hand"]).to eq 7 + end + end + context "adding a group buy product to the cart" do it "sets a variant attribute for the max quantity" do distributor_product = create(:distributor_enterprise)