From 5ab1d45b8a9de2fb52da347a913ce809fab744c6 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Wed, 21 Feb 2024 12:13:47 +0100 Subject: [PATCH] fix(instructeur): don't crash when adding a filter too long --- .../instructeurs/procedures_controller.rb | 4 +++- app/models/procedure_presentation.rb | 12 +++++------ .../procedures_controller_spec.rb | 20 +++++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/app/controllers/instructeurs/procedures_controller.rb b/app/controllers/instructeurs/procedures_controller.rb index 86ed719b1..4025c5b4b 100644 --- a/app/controllers/instructeurs/procedures_controller.rb +++ b/app/controllers/instructeurs/procedures_controller.rb @@ -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 diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index 04bb0d9ec..040e6319a 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -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 diff --git a/spec/controllers/instructeurs/procedures_controller_spec.rb b/spec/controllers/instructeurs/procedures_controller_spec.rb index 7c9ad6af7..4b123bca3 100644 --- a/spec/controllers/instructeurs/procedures_controller_spec.rb +++ b/spec/controllers/instructeurs/procedures_controller_spec.rb @@ -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