Fix problem with misssing params in schedules controller and adapt its spec

This commit is contained in:
Luis Ramos
2020-03-09 13:01:00 +00:00
parent 4ea891ee2f
commit 014e22a7ad
2 changed files with 16 additions and 7 deletions

View File

@@ -3,6 +3,7 @@ require 'open_food_network/proxy_order_syncer'
module Admin
class SchedulesController < ResourceController
before_filter :adapt_params, only: [:create, :update]
before_filter :check_editable_order_cycle_ids, only: [:create, :update]
before_filter :check_dependent_subscriptions, only: [:destroy]
create.after :sync_subscriptions
@@ -48,6 +49,14 @@ module Admin
[:index]
end
# In this controller, params like params[:name] are moved into params[:schedule] becoming params[:schedule][:name]
# For some reason in rails 4, this is not happening for params[:order_cycle_ids]
# We do it manually in this filter
def adapt_params
params[:schedule] = {} if params[:schedule].blank?
params[:schedule][:order_cycle_ids] = params[:order_cycle_ids]
end
def check_editable_order_cycle_ids
return unless params[:schedule][:order_cycle_ids]

View File

@@ -93,7 +93,7 @@ describe Admin::SchedulesController, type: :controller do
it "allows me to add/remove only order cycles I coordinate to/from the schedule" do
order_cycle_ids = [coordinated_order_cycle2.id, uncoordinated_order_cycle2.id, uncoordinated_order_cycle3.id]
spree_put :update, format: :json, id: coordinated_schedule.id, schedule: { order_cycle_ids: order_cycle_ids }
spree_put :update, format: :json, id: coordinated_schedule.id, order_cycle_ids: order_cycle_ids
expect(assigns(:schedule)).to eq coordinated_schedule
# coordinated_order_cycle2 is added, uncoordinated_order_cycle is NOT removed
expect(coordinated_schedule.reload.order_cycles).to include coordinated_order_cycle2, uncoordinated_order_cycle, uncoordinated_order_cycle3
@@ -106,9 +106,9 @@ describe Admin::SchedulesController, type: :controller do
allow(OpenFoodNetwork::ProxyOrderSyncer).to receive(:new) { syncer_mock }
expect(syncer_mock).to receive(:sync!).exactly(2).times
spree_put :update, format: :json, id: coordinated_schedule.id, schedule: { order_cycle_ids: [coordinated_order_cycle.id, coordinated_order_cycle2.id] }
spree_put :update, format: :json, id: coordinated_schedule.id, schedule: { order_cycle_ids: [coordinated_order_cycle.id] }
spree_put :update, format: :json, id: coordinated_schedule.id, schedule: { order_cycle_ids: [coordinated_order_cycle.id] }
spree_put :update, format: :json, id: coordinated_schedule.id, order_cycle_ids: [coordinated_order_cycle.id, coordinated_order_cycle2.id]
spree_put :update, format: :json, id: coordinated_schedule.id, order_cycle_ids: [coordinated_order_cycle.id]
spree_put :update, format: :json, id: coordinated_schedule.id, order_cycle_ids: [coordinated_order_cycle.id]
end
end
@@ -151,7 +151,7 @@ describe Admin::SchedulesController, type: :controller do
context "where I manage at least one of the order cycles to be added to the schedules" do
before do
params[:schedule].merge!( order_cycle_ids: [coordinated_order_cycle.id, uncoordinated_order_cycle.id] )
params.merge!( order_cycle_ids: [coordinated_order_cycle.id, uncoordinated_order_cycle.id] )
end
it "allows me to create the schedule, adding only order cycles that I manage" do
@@ -172,7 +172,7 @@ describe Admin::SchedulesController, type: :controller do
context "where I don't manage any of the order cycles to be added to the schedules" do
before do
params[:schedule].merge!( order_cycle_ids: [uncoordinated_order_cycle.id] )
params.merge!( order_cycle_ids: [uncoordinated_order_cycle.id] )
end
it "prevents me from creating the schedule" do
@@ -184,7 +184,7 @@ describe Admin::SchedulesController, type: :controller do
context 'as an admin user' do
before do
allow(controller).to receive(:spree_current_user) { create(:admin_user) }
params[:schedule].merge!( order_cycle_ids: [coordinated_order_cycle.id, uncoordinated_order_cycle.id] )
params.merge!( order_cycle_ids: [coordinated_order_cycle.id, uncoordinated_order_cycle.id] )
end
it "allows me to create a schedule" do