From d0800b5e32820d487694273b42989c9d17718ba8 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 30 Dec 2020 12:53:05 +0000 Subject: [PATCH 1/9] Specify json format for API routes that return json in OrderCycleResource --- .../darkswarm/services/order_cycle_resource.js.coffee | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/darkswarm/services/order_cycle_resource.js.coffee b/app/assets/javascripts/darkswarm/services/order_cycle_resource.js.coffee index 32d70d54a5..ba8d5266fe 100644 --- a/app/assets/javascripts/darkswarm/services/order_cycle_resource.js.coffee +++ b/app/assets/javascripts/darkswarm/services/order_cycle_resource.js.coffee @@ -1,21 +1,21 @@ Darkswarm.factory 'OrderCycleResource', ($resource) -> - $resource('/api/order_cycles/:id', {}, { + $resource('/api/order_cycles/:id.json', {}, { 'products': method: 'GET' isArray: true - url: '/api/order_cycles/:id/products' + url: '/api/order_cycles/:id/products.json' params: id: '@id' 'taxons': method: 'GET' isArray: true - url: '/api/order_cycles/:id/taxons' + url: '/api/order_cycles/:id/taxons.json' params: id: '@id' 'properties': method: 'GET' isArray: true - url: '/api/order_cycles/:id/properties' + url: '/api/order_cycles/:id/properties.json' params: id: '@id' }) From 261ed751cb851afe3b61a7cb8fae4e11942bf3a0 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 30 Dec 2020 12:55:02 +0000 Subject: [PATCH 2/9] Remove explicit `content_type` declarations in `Api::OrderCyclesController` This should be handled in `Api::BaseController` --- app/controllers/api/order_cycles_controller.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/order_cycles_controller.rb b/app/controllers/api/order_cycles_controller.rb index ddc7b15fa5..bc1bba3d5a 100644 --- a/app/controllers/api/order_cycles_controller.rb +++ b/app/controllers/api/order_cycles_controller.rb @@ -20,7 +20,7 @@ module Api search_params ).products_json - render plain: products, content_type: "application/json" + render plain: products rescue ProductsRenderer::NoProducts render_no_products end @@ -33,13 +33,13 @@ module Api render plain: ActiveModel::ArraySerializer.new( taxons, each_serializer: Api::TaxonSerializer - ).to_json, content_type: "application/json" + ).to_json end def properties render plain: ActiveModel::ArraySerializer.new( product_properties | producer_properties, each_serializer: Api::PropertySerializer - ).to_json, content_type: "application/json" + ).to_json end private From 4ba3c81f904a37052f12abbbeac9dcd93af2211d Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 30 Dec 2020 13:12:50 +0000 Subject: [PATCH 3/9] Update route declaration in karma test --- .../javascripts/unit/darkswarm/services/products_spec.js.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee index db9ed1e628..d0114c05dd 100644 --- a/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee @@ -13,7 +13,7 @@ describe 'Products service', -> properties = null taxons = null GmapsGeo = {} - endpoint = "/api/order_cycles/1/products?distributor=1" + endpoint = "/api/order_cycles/1/products.json?distributor=1" beforeEach -> product = From 04e4893723ffe9fefe956f29f3accc45e84bf7b8 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 30 Dec 2020 13:15:22 +0000 Subject: [PATCH 4/9] Update route declarations in caching spec --- spec/features/consumer/caching/shops_caching_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/features/consumer/caching/shops_caching_spec.rb b/spec/features/consumer/caching/shops_caching_spec.rb index c525e569d9..6f71868753 100644 --- a/spec/features/consumer/caching/shops_caching_spec.rb +++ b/spec/features/consumer/caching/shops_caching_spec.rb @@ -47,8 +47,8 @@ feature "Shops caching", js: true, caching: true do let(:exchange) { order_cycle.exchanges.to_enterprises(distributor).outgoing.first } let(:test_domain) { "#{Capybara.current_session.server.host}:#{Capybara.current_session.server.port}" } - let(:taxons_key) { "views/#{test_domain}/api/order_cycles/#{order_cycle.id}/taxons?distributor=#{distributor.id}.json" } - let(:properties_key) { "views/#{test_domain}/api/order_cycles/#{order_cycle.id}/properties?distributor=#{distributor.id}.json" } + let(:taxons_key) { "views/#{test_domain}/api/order_cycles/#{order_cycle.id}/taxons.json?distributor=#{distributor.id}" } + let(:properties_key) { "views/#{test_domain}/api/order_cycles/#{order_cycle.id}/properties.json?distributor=#{distributor.id}" } let(:options) { { expires_in: CacheService::FILTERS_EXPIRY } } before do From 0dd57fc3de3ca8b712df78ead87888c0c769617d Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 30 Dec 2020 18:16:51 +0000 Subject: [PATCH 5/9] Render an empty JSON object instead of a blank string --- app/controllers/api/order_cycles_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/order_cycles_controller.rb b/app/controllers/api/order_cycles_controller.rb index bc1bba3d5a..c40ec641b2 100644 --- a/app/controllers/api/order_cycles_controller.rb +++ b/app/controllers/api/order_cycles_controller.rb @@ -45,7 +45,7 @@ module Api private def render_no_products - render status: :not_found, json: '' + render status: :not_found, json: {} end def product_properties From d90ed566033bd9958bac82703442dedcbafc26e2 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 6 Jan 2021 11:50:29 +0000 Subject: [PATCH 6/9] Add missing test for Api::OrderCyclesController responses --- .../controllers/api/order_cycles_controller_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/controllers/api/order_cycles_controller_spec.rb b/spec/controllers/api/order_cycles_controller_spec.rb index aacbeda57b..c16a3294d2 100644 --- a/spec/controllers/api/order_cycles_controller_spec.rb +++ b/spec/controllers/api/order_cycles_controller_spec.rb @@ -148,6 +148,19 @@ module Api expect(product_ids).to include product3.id end end + + context "when the order cycle is closed" do + before do + allow_any_instance_of(OrderCycle).to receive(:open?) { false } + end + + xit "throws an error, ActionView::MissingTemplate: Missing template api/order_cycles/products" do + api_get :products, id: order_cycle.id, distributor: distributor.id + + expect(json_response).to eq({}) + expect(response).to have_http_status :not_found + end + end end describe "#taxons" do From eec9f27353008db8c9f14c614b939bfb0b87fabe Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 6 Jan 2021 12:23:26 +0000 Subject: [PATCH 7/9] Move controller concern to correct directory This is a controller concern, it should live in app/controllers/concerns/ --- app/{models => controllers}/concerns/api_action_caching.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/{models => controllers}/concerns/api_action_caching.rb (100%) diff --git a/app/models/concerns/api_action_caching.rb b/app/controllers/concerns/api_action_caching.rb similarity index 100% rename from app/models/concerns/api_action_caching.rb rename to app/controllers/concerns/api_action_caching.rb From 3b1ad29d20dfa482c75e4f2740a37a29ae7cc006 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 6 Jan 2021 12:26:47 +0000 Subject: [PATCH 8/9] Fix view rendering in Api::BaseController --- app/controllers/api/base_controller.rb | 3 +++ spec/controllers/api/order_cycles_controller_spec.rb | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/base_controller.rb b/app/controllers/api/base_controller.rb index b75c92e22e..54feee3802 100644 --- a/app/controllers/api/base_controller.rb +++ b/app/controllers/api/base_controller.rb @@ -10,6 +10,9 @@ module Api include Spree::Core::ControllerHelpers::SSL include ::ActionController::Head include ::ActionController::ConditionalGet + include ActionView::Layouts + + layout false attr_accessor :current_api_user diff --git a/spec/controllers/api/order_cycles_controller_spec.rb b/spec/controllers/api/order_cycles_controller_spec.rb index c16a3294d2..3301a16818 100644 --- a/spec/controllers/api/order_cycles_controller_spec.rb +++ b/spec/controllers/api/order_cycles_controller_spec.rb @@ -154,7 +154,8 @@ module Api allow_any_instance_of(OrderCycle).to receive(:open?) { false } end - xit "throws an error, ActionView::MissingTemplate: Missing template api/order_cycles/products" do + # Regression test for https://github.com/openfoodfoundation/openfoodnetwork/issues/6491 + it "renders no products without error" do api_get :products, id: order_cycle.id, distributor: distributor.id expect(json_response).to eq({}) From 87b14f0237f8cc1979b354fab754934e387bf75e Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Fri, 8 Jan 2021 11:57:48 +0000 Subject: [PATCH 9/9] Avoid using #allow_any_instance_of --- spec/controllers/api/order_cycles_controller_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/controllers/api/order_cycles_controller_spec.rb b/spec/controllers/api/order_cycles_controller_spec.rb index 3301a16818..d41d26aa43 100644 --- a/spec/controllers/api/order_cycles_controller_spec.rb +++ b/spec/controllers/api/order_cycles_controller_spec.rb @@ -151,7 +151,8 @@ module Api context "when the order cycle is closed" do before do - allow_any_instance_of(OrderCycle).to receive(:open?) { false } + allow(controller).to receive(:order_cycle) { order_cycle } + allow(order_cycle).to receive(:open?) { false } end # Regression test for https://github.com/openfoodfoundation/openfoodnetwork/issues/6491