From da7456e6e09d5ccb4f7f980fa35c52b7afc0ec04 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Fri, 4 Oct 2019 12:04:56 +0100 Subject: [PATCH] Remove old shop/products route, action, and spec --- app/controllers/shop_controller.rb | 36 ----- config/routes.rb | 1 - spec/controllers/shop_controller_spec.rb | 162 ----------------------- spec/requests/shop_spec.rb | 64 --------- 4 files changed, 263 deletions(-) delete mode 100644 spec/requests/shop_spec.rb diff --git a/app/controllers/shop_controller.rb b/app/controllers/shop_controller.rb index 1d9d727aa2..a2af630863 100644 --- a/app/controllers/shop_controller.rb +++ b/app/controllers/shop_controller.rb @@ -9,19 +9,6 @@ class ShopController < BaseController redirect_to main_app.enterprise_shop_path(current_distributor) end - def products - renderer = OpenFoodNetwork::CachedProductsRenderer.new(current_distributor, - current_order_cycle) - - # If we add any more filtering logic, we should probably - # move it all to a lib class like 'CachedProductsFilterer' - products_json = filter(renderer.products_json) - - render json: products_json - rescue OpenFoodNetwork::CachedProductsRenderer::NoProducts - render status: :not_found, json: '' - end - def order_cycle if request.post? if oc = OrderCycle.with_distributor(@distributor).active.find_by_id(params[:order_cycle_id]) @@ -39,27 +26,4 @@ class ShopController < BaseController def changeable_orders_alert render layout: false end - - private - - def filtered_json(products_json) - if applicator.rules.any? - filter(products_json) - else - products_json - end - end - - def filter(products_json) - products_hash = JSON.parse(products_json) - applicator.filter!(products_hash) - JSON.unparse(products_hash) - end - - def applicator - return @applicator unless @applicator.nil? - @applicator = OpenFoodNetwork::TagRuleApplicator.new(current_distributor, - "FilterProducts", - current_customer.andand.tag_list) - end end diff --git a/config/routes.rb b/config/routes.rb index 6e7df6ea64..65a70165e1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -33,7 +33,6 @@ Openfoodnetwork::Application.routes.draw do end resource :shop, controller: "shop" do - get :products post :order_cycle get :order_cycle get :changeable_orders_alert diff --git a/spec/controllers/shop_controller_spec.rb b/spec/controllers/shop_controller_spec.rb index 64b4e9281b..233fe9d36a 100644 --- a/spec/controllers/shop_controller_spec.rb +++ b/spec/controllers/shop_controller_spec.rb @@ -82,167 +82,5 @@ describe ShopController, type: :controller do expect(controller.current_order_cycle).to be_nil end end - - describe "returning products" do - let(:order_cycle) { create(:simple_order_cycle, distributors: [distributor]) } - let(:exchange) { order_cycle.exchanges.to_enterprises(distributor).outgoing.first } - - describe "requests and responses" do - let(:product) { create(:product) } - - before do - exchange.variants << product.variants.first - end - - it "returns products via JSON" do - allow(controller).to receive(:current_order_cycle).and_return order_cycle - xhr :get, :products - expect(response).to be_success - end - - it "does not return products if no order cycle is selected" do - allow(controller).to receive(:current_order_cycle).and_return nil - xhr :get, :products - expect(response.status).to eq(404) - expect(response.body).to be_empty - end - end - end - - describe "determining rule relevance" do - let(:products_json) { double(:products_json) } - let(:applicator) { double(:applicator) } - - before do - allow(applicator).to receive(:rules) { tag_rules } - allow(controller).to receive(:applicator) { applicator } - allow(controller).to receive(:filter) { "some filtered json" } - end - - context "when no relevant rules exist" do - let(:tag_rules) { [] } - - it "does not attempt to apply any rules" do - controller.send(:filtered_json, products_json) - expect(expect(controller).to_not(have_received(:filter))) - end - - it "returns products as JSON" do - expect(controller.send(:filtered_json, products_json)).to eq products_json - end - end - - context "when relevant rules exist" do - let(:tag_rule) { create(:filter_products_tag_rule, preferred_customer_tags: "tag1", preferred_variant_tags: "tag1", preferred_matched_variants_visibility: "hidden" ) } - let(:tag_rules) { [tag_rule] } - - it "attempts to apply any rules" do - controller.send(:filtered_json, products_json) - expect(controller).to have_received(:filter).with(products_json) - end - - it "returns filtered JSON" do - expect(controller.send(:filtered_json, products_json)).to eq "some filtered json" - end - end - end - - describe "loading available order cycles" do - let(:user) { create(:user) } - before { allow(controller).to receive(:spree_current_user) { user } } - - context "when FilterProducts tag rules are in effect" do - let(:customer) { create(:customer, user: user, enterprise: distributor) } - let!(:tag_rule) { - create(:filter_products_tag_rule, - enterprise: distributor, - preferred_customer_tags: "member", - preferred_variant_tags: "members-only") - } - let!(:default_tag_rule) { - create(:filter_products_tag_rule, - enterprise: distributor, - is_default: true, - preferred_variant_tags: "members-only") - } - let(:product1) { { "id" => 1, "name" => 'product 1', "variants" => [{ "id" => 4, "tag_list" => ["members-only"] }] } } - let(:product2) { { "id" => 2, "name" => 'product 2', "variants" => [{ "id" => 5, "tag_list" => ["members-only"] }, { "id" => 9, "tag_list" => ["something"] }] } } - let(:product3) { { "id" => 3, "name" => 'product 3', "variants" => [{ "id" => 6, "tag_list" => ["something-else"] }] } } - let(:product2_without_v5) { { "id" => 2, "name" => 'product 2', "variants" => [{ "id" => 9, "tag_list" => ["something"] }] } } - let!(:products_array) { [product1, product2, product3] } - let!(:products_json) { JSON.unparse( products_array ) } - - before do - allow(controller).to receive(:current_order) { order } - end - - context "with a preferred visiblity of 'visible', default visibility of 'hidden'" do - before { tag_rule.update_attribute(:preferred_matched_variants_visibility, 'visible') } - before { default_tag_rule.update_attribute(:preferred_matched_variants_visibility, 'hidden') } - - let(:filtered_products) { JSON.parse(controller.send(:filter, products_json)) } - - context "when the customer is nil" do - it "applies default action (hide)" do - expect(controller.current_customer).to be nil - expect(filtered_products).to include product2_without_v5, product3 - expect(filtered_products).to_not include product1, product2 - end - end - - context "when the customer's tags match" do - before { customer.update_attribute(:tag_list, 'member') } - - it "applies the action (show)" do - expect(controller.current_customer).to eq customer - expect(filtered_products).to include product1, product2, product3 - end - end - - context "when the customer's tags don't match" do - before { customer.update_attribute(:tag_list, 'something') } - - it "applies the default action (hide)" do - expect(controller.current_customer).to eq customer - expect(filtered_products).to include product2_without_v5, product3 - expect(filtered_products).to_not include product1, product2 - end - end - end - - context "with a preferred visiblity of 'hidden', default visibility of 'visible'" do - before { tag_rule.update_attribute(:preferred_matched_variants_visibility, 'hidden') } - before { default_tag_rule.update_attribute(:preferred_matched_variants_visibility, 'visible') } - - let(:filtered_products) { JSON.parse(controller.send(:filter, products_json)) } - - context "when the customer is nil" do - it "applies default action (show)" do - expect(controller.current_customer).to be nil - expect(filtered_products).to include product1, product2, product3 - end - end - - context "when the customer's tags match" do - before { customer.update_attribute(:tag_list, 'member') } - - it "applies the action (hide)" do - expect(controller.current_customer).to eq customer - expect(filtered_products).to include product2_without_v5, product3 - expect(filtered_products).to_not include product1, product2 - end - end - - context "when the customer's tags don't match" do - before { customer.update_attribute(:tag_list, 'something') } - - it "applies the default action (show)" do - expect(controller.current_customer).to eq customer - expect(filtered_products).to include product1, product2, product3 - end - end - end - end - end end end diff --git a/spec/requests/shop_spec.rb b/spec/requests/shop_spec.rb deleted file mode 100644 index c73e8bd099..0000000000 --- a/spec/requests/shop_spec.rb +++ /dev/null @@ -1,64 +0,0 @@ -require 'spec_helper' - -describe "Shop API", type: :request do - include ShopWorkflow - - describe "filtering products" do - let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) } - let(:supplier) { create(:supplier_enterprise) } - let(:oc1) { create(:simple_order_cycle, distributors: [distributor], coordinator: create(:distributor_enterprise), orders_close_at: 2.days.from_now) } - let(:p4) { create(:simple_product, on_demand: false) } - let(:p5) { create(:simple_product, on_demand: false) } - let(:p6) { create(:simple_product, on_demand: false) } - let(:p7) { create(:simple_product, on_demand: false) } - let(:v41) { p4.variants.first } - let(:v42) { create(:variant, product: p4, unit_value: 3, on_demand: false) } - let(:v43) { create(:variant, product: p4, unit_value: 4, on_demand: true) } - let(:v51) { p5.variants.first } - let(:v52) { create(:variant, product: p5) } - let(:v61) { p6.variants.first } - let(:v71) { p7.variants.first } - let(:order) { create(:order, distributor: distributor, order_cycle: oc1) } - - before do - set_order order - - v61.update_attribute(:on_hand, 1) - p6.destroy - v71.update_attribute(:on_hand, 1) - v41.update_attribute(:on_hand, 1) - v42.update_attribute(:on_hand, 0) - v43.update_attribute(:on_hand, 0) - v51.update_attribute(:on_hand, 1) - v52.update_attribute(:on_hand, 0) - v71.update_attribute(:on_hand, 1) - v71.update_attribute(:deleted_at, Time.zone.now) - exchange = Exchange.find(oc1.exchanges.to_enterprises(distributor).outgoing.first.id) - exchange.update_attribute :pickup_time, "frogs" - exchange.variants << v61 - exchange.variants << v41 - exchange.variants << v42 - exchange.variants << v43 - # v51 is in stock but not in distribution - # v52 is out of stock and in the distribution - # Neither should display, nor should their product, p5 - exchange.variants << v52 - exchange.variants << v71 - get products_shop_path - end - - it "filters products based on availability" do - # It shows on demand variants - expect(response.body).to include v43.options_text - # It does not show variants that are neither on hand or on demand - expect(response.body).not_to include v42.options_text - # It does not show products that have no available variants in this distribution - expect(response.body).not_to include p5.name - # It does not show deleted products - expect(response.body).not_to include p6.name - # It does not show deleted variants - expect(response.body).not_to include v71.name - expect(response.body).not_to include p7.name - end - end -end