fix(instructeur): don't crash when adding a filter too long

This commit is contained in:
Colin Darie 2024-02-21 12:13:47 +01:00
parent 5a8859cd28
commit 5ab1d45b8a
3 changed files with 29 additions and 7 deletions

View file

@ -148,7 +148,9 @@ module Instructeurs
end
def add_filter
procedure_presentation.add_filter(statut, params[:field], params[:value])
if !procedure_presentation.add_filter(statut, params[:field], params[:value])
flash.alert = procedure_presentation.errors.full_messages
end
redirect_back(fallback_location: instructeur_procedure_url(procedure))
end

View file

@ -319,7 +319,7 @@ class ProcedurePresentation < ApplicationRecord
'value' => value
}
update!(filters: updated_filters)
update(filters: updated_filters)
end
end
@ -457,11 +457,11 @@ class ProcedurePresentation < ApplicationRecord
end
def check_filters_max_length
individual_filters = filters.values.flatten.filter { |f| f.is_a?(Hash) }
individual_filters.each do |filter|
if filter['value']&.length.to_i > FILTERS_VALUE_MAX_LENGTH
errors.add(:filters, :too_long)
end
filters.values.flatten.each do |filter|
next if !filter.is_a?(Hash)
next if filter['value']&.length.to_i <= FILTERS_VALUE_MAX_LENGTH
errors.add(:base, "Le filtre #{filter['label']} est trop long (maximum: #{FILTERS_VALUE_MAX_LENGTH} caractères)")
end
end

View file

@ -830,4 +830,24 @@ describe Instructeurs::ProceduresController, type: :controller do
it { is_expected.to have_http_status(:forbidden) }
end
end
describe '#add_filter' do
let(:instructeur) { create(:instructeur) }
let(:procedure) { create(:procedure, :for_individual) }
before do
create(:assign_to, instructeur:, groupe_instructeur: build(:groupe_instructeur, procedure:))
sign_in(instructeur.user)
end
subject do
post :add_filter, params: { procedure_id: procedure.id, field: "individual/nom", value: "n" * 110, statut: "a-suivre" }
end
it 'should render the error' do
subject
expect(flash.alert[0]).to include("Le filtre Nom est trop long (maximum: 100 caractères)")
end
end
end