From 0eeac59ecdf61e773b0cf198648147d36fb13d4f Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 6 Nov 2019 11:59:15 +0100 Subject: [PATCH 1/2] assigns: fix search among the unassigned instructeurs This was broken since moving the email out of the Instructeur column. --- app/controllers/admin/assigns_controller.rb | 2 +- .../admin/assigns_controller_spec.rb | 34 ++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/app/controllers/admin/assigns_controller.rb b/app/controllers/admin/assigns_controller.rb index 5a59330c4..57d05670a 100644 --- a/app/controllers/admin/assigns_controller.rb +++ b/app/controllers/admin/assigns_controller.rb @@ -18,7 +18,7 @@ class Admin::AssignsController < AdminController not_assign_scope = current_administrateur.instructeurs.where.not(id: assign_scope.ids) if params[:filter] - not_assign_scope = not_assign_scope.where("email LIKE ?", "%#{params[:filter]}%") + not_assign_scope = not_assign_scope.where('users.email LIKE ?', "%#{params[:filter]}%") end @instructeurs_not_assign = smart_listing_create :instructeurs_not_assign, diff --git a/spec/controllers/admin/assigns_controller_spec.rb b/spec/controllers/admin/assigns_controller_spec.rb index 95fb4474a..923d553d2 100644 --- a/spec/controllers/admin/assigns_controller_spec.rb +++ b/spec/controllers/admin/assigns_controller_spec.rb @@ -2,19 +2,45 @@ require 'spec_helper' describe Admin::AssignsController, type: :controller do let(:admin) { create(:administrateur) } - let(:procedure) { create :procedure, administrateur: admin } - let(:instructeur) { create :instructeur, administrateurs: [admin] } before do sign_in(admin.user) end describe 'GET #show' do - subject { get :show, params: { procedure_id: procedure.id } } - it { expect(subject.status).to eq(200) } + let(:procedure) { create :procedure, administrateur: admin, instructeurs: [instructeur_assigned_1, instructeur_assigned_2] } + let!(:instructeur_assigned_1) { create :instructeur, email: 'instructeur_1@ministere_a.gouv.fr', administrateurs: [admin] } + let!(:instructeur_assigned_2) { create :instructeur, email: 'instructeur_2@ministere_b.gouv.fr', administrateurs: [admin] } + let!(:instructeur_not_assigned_1) { create :instructeur, email: 'instructeur_3@ministere_a.gouv.fr', administrateurs: [admin] } + let!(:instructeur_not_assigned_2) { create :instructeur, email: 'instructeur_4@ministere_b.gouv.fr', administrateurs: [admin] } + let(:filter) { nil } + + subject! { get :show, params: { procedure_id: procedure.id, filter: filter } } + + it { expect(response.status).to eq(200) } + + it 'sets the assigned and not assigned instructeurs' do + expect(assigns(:instructeurs_assign)).to match_array([instructeur_assigned_1, instructeur_assigned_2]) + expect(assigns(:instructeurs_not_assign)).to match_array([instructeur_not_assigned_1, instructeur_not_assigned_2]) + end + + context 'with a search filter' do + let(:filter) { '@ministere_a.gouv.fr' } + + it 'filters the unassigned instructeurs' do + expect(assigns(:instructeurs_not_assign)).to match_array([instructeur_not_assigned_1]) + end + + it 'does not filter the assigned instructeurs' do + expect(assigns(:instructeurs_assign)).to match_array([instructeur_assigned_1, instructeur_assigned_2]) + end + end end describe 'PUT #update' do + let(:procedure) { create :procedure, administrateur: admin } + let(:instructeur) { create :instructeur, administrateurs: [admin] } + subject { put :update, params: { instructeur_id: instructeur.id, procedure_id: procedure.id, to: 'assign' } } it { expect(subject).to redirect_to admin_procedure_assigns_path(procedure_id: procedure.id) } From 8df91df9fa2b41a913f6825ba9e3a9de4fdc28ca Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 6 Nov 2019 12:05:41 +0100 Subject: [PATCH 2/2] assigns: strip and lowercase the search filter --- app/controllers/admin/assigns_controller.rb | 5 +++-- spec/controllers/admin/assigns_controller_spec.rb | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/assigns_controller.rb b/app/controllers/admin/assigns_controller.rb index 57d05670a..cf2899643 100644 --- a/app/controllers/admin/assigns_controller.rb +++ b/app/controllers/admin/assigns_controller.rb @@ -17,8 +17,9 @@ class Admin::AssignsController < AdminController not_assign_scope = current_administrateur.instructeurs.where.not(id: assign_scope.ids) - if params[:filter] - not_assign_scope = not_assign_scope.where('users.email LIKE ?', "%#{params[:filter]}%") + if params[:filter].present? + filter = params[:filter].downcase.strip + not_assign_scope = not_assign_scope.where('users.email LIKE ?', "%#{filter}%") end @instructeurs_not_assign = smart_listing_create :instructeurs_not_assign, diff --git a/spec/controllers/admin/assigns_controller_spec.rb b/spec/controllers/admin/assigns_controller_spec.rb index 923d553d2..741937dd8 100644 --- a/spec/controllers/admin/assigns_controller_spec.rb +++ b/spec/controllers/admin/assigns_controller_spec.rb @@ -34,6 +34,14 @@ describe Admin::AssignsController, type: :controller do it 'does not filter the assigned instructeurs' do expect(assigns(:instructeurs_assign)).to match_array([instructeur_assigned_1, instructeur_assigned_2]) end + + context 'when the filter has spaces or a mixed case' do + let(:filter) { ' @ministere_A.gouv.fr ' } + + it 'trims spaces and ignores the case' do + expect(assigns(:instructeurs_not_assign)).to match_array([instructeur_not_assigned_1]) + end + end end end