Merge pull request #11061 from colinux/drop-contact-empty-file

ETQ usager je ne peux pas soumettre le form de contact si la PJ n'est pas un fichier
This commit is contained in:
Colin Darie 2024-11-20 09:43:10 +00:00 committed by GitHub
commit 6acbeba63f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 23 additions and 0 deletions

View file

@ -2,6 +2,7 @@
class ContactController < ApplicationController class ContactController < ApplicationController
invisible_captcha only: [:create], on_spam: :redirect_to_root invisible_captcha only: [:create], on_spam: :redirect_to_root
before_action :reject_invalid_attachment, only: [:create]
def index def index
@form = ContactForm.new(tags: contact_form_params.fetch(:tags, []), dossier_id: dossier&.id) @form = ContactForm.new(tags: contact_form_params.fetch(:tags, []), dossier_id: dossier&.id)
@ -75,4 +76,14 @@ class ContactController < ApplicationController
params.permit(:dossier_id, tags: []) # prefilling form params.permit(:dossier_id, tags: []) # prefilling form
end end
end end
def reject_invalid_attachment
piece_jointe = params.dig(:contact_form, :piece_jointe)
return if piece_jointe.nil?
return if piece_jointe.is_a?(ActionDispatch::Http::UploadedFile)
@form = ContactForm.new(user: current_user)
flash.alert = t('invalid_piece_jointe', scope: "contact.create")
render(@form.for_admin ? :admin : :index, status: :unprocessable_entity)
end
end end

View file

@ -71,3 +71,4 @@ en:
create: create:
direct_message_sent: Your message has been sent to the mailbox in your file. direct_message_sent: Your message has been sent to the mailbox in your file.
message_sent: Your message has been sent. message_sent: Your message has been sent.
invalid_piece_jointe: 'The attachment must be a file'

View file

@ -72,3 +72,4 @@ fr:
create: create:
direct_message_sent: Votre message a été envoyé sur la messagerie de votre dossier. direct_message_sent: Votre message a été envoyé sur la messagerie de votre dossier.
message_sent: Votre message a été envoyé. message_sent: Votre message a été envoyé.
invalid_piece_jointe: 'La pièce jointe doit être un fichier'

View file

@ -198,6 +198,16 @@ describe ContactController, question_type: :controller do
expect(response.body).to include("un message") expect(response.body).to include("un message")
end end
end end
context "with an invalid attachment type" do
let(:params) { super().merge(piece_jointe: "not_a_file") }
it "returns unprocessable entity status" do
subject
expect(response).to have_http_status(:unprocessable_entity)
expect(response.body).to include("La pièce jointe doit être un fichier")
end
end
end end
end end