Merge pull request #6468 from ismo1/6467-Fix_file_size_validation_message

This commit is contained in:
Pierre de La Morinerie 2021-09-21 12:21:35 -05:00 committed by GitHub
commit 4be584d59d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 46 additions and 19 deletions

View file

@ -14,6 +14,7 @@
class AttestationTemplate < ApplicationRecord
include ActionView::Helpers::NumberHelper
include TagsSubstitutionConcern
include FileValidationConcern
belongs_to :procedure, optional: false
@ -21,8 +22,10 @@ class AttestationTemplate < ApplicationRecord
has_one_attached :signature
validates :footer, length: { maximum: 190 }
validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: { less_than: 1.megabytes }
validates :signature, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: { less_than: 1.megabytes }
FILE_MAX_SIZE = 1.megabytes
validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: file_size_validation(FILE_MAX_SIZE)
validates :signature, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: file_size_validation(FILE_MAX_SIZE)
DOSSIER_STATE = Dossier.states.fetch(:accepte)

View file

@ -17,6 +17,7 @@
#
class Avis < ApplicationRecord
include EmailSanitizableConcern
include FileValidationConcern
belongs_to :dossier, inverse_of: :avis, touch: true, optional: false
belongs_to :experts_procedure, optional: false
@ -27,19 +28,19 @@ class Avis < ApplicationRecord
has_one :expert, through: :experts_procedure
has_one :procedure, through: :experts_procedure
FILE_MAX_SIZE = 20.megabytes
validates :piece_justificative_file,
content_type: AUTHORIZED_CONTENT_TYPES,
size: { less_than: 20.megabytes }
size: file_size_validation(FILE_MAX_SIZE)
validates :introduction_file,
content_type: AUTHORIZED_CONTENT_TYPES,
size: { less_than: 20.megabytes }
size: file_size_validation(FILE_MAX_SIZE)
validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true
validates :claimant, presence: true
validates :piece_justificative_file, size: { less_than: 20.megabytes }
validates :introduction_file, size: { less_than: 20.megabytes }
validates :piece_justificative_file, size: file_size_validation(FILE_MAX_SIZE)
validates :introduction_file, size: file_size_validation(FILE_MAX_SIZE)
before_validation -> { sanitize_email(:email) }
default_scope { joins(:dossier) }

View file

@ -18,10 +18,11 @@
# type_de_champ_id :integer
#
class Champs::PieceJustificativeChamp < Champ
MAX_SIZE = 200.megabytes
include FileValidationConcern
FILE_MAX_SIZE = 200.megabytes
validates :piece_justificative_file,
size: { less_than: MAX_SIZE },
size: file_size_validation(FILE_MAX_SIZE),
if: -> { !type_de_champ.skip_pj_validation }
validates :piece_justificative_file,

View file

@ -18,7 +18,8 @@
# type_de_champ_id :integer
#
class Champs::TitreIdentiteChamp < Champ
MAX_SIZE = 20.megabytes
include FileValidationConcern
FILE_MAX_SIZE = 20.megabytes
ACCEPTED_FORMATS = [
"image/png",
@ -30,7 +31,7 @@ class Champs::TitreIdentiteChamp < Champ
#
validates :piece_justificative_file,
content_type: ACCEPTED_FORMATS,
size: { less_than: MAX_SIZE }
size: file_size_validation(FILE_MAX_SIZE)
def main_value_name
:piece_justificative_file

View file

@ -12,6 +12,8 @@
# instructeur_id :bigint
#
class Commentaire < ApplicationRecord
include FileValidationConcern
self.ignored_columns = [:user_id]
belongs_to :dossier, inverse_of: :commentaires, touch: true, optional: false
@ -24,9 +26,10 @@ class Commentaire < ApplicationRecord
validates :body, presence: { message: "ne peut être vide" }
FILE_MAX_SIZE = 20.megabytes
validates :piece_jointe,
content_type: AUTHORIZED_CONTENT_TYPES,
size: { less_than: 20.megabytes }
size: file_size_validation(FILE_MAX_SIZE)
default_scope { order(created_at: :asc) }
scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) }

View file

@ -0,0 +1,8 @@
module FileValidationConcern
extend ActiveSupport::Concern
class_methods do
def file_size_validation(file_max_size = 200.megabytes)
{ less_than: file_max_size, message: I18n.t('errors.messages.file_size_out_of_range', file_size_limit: ActiveSupport::NumberHelper.number_to_human_size(file_max_size)) }
end
end
end

View file

@ -52,6 +52,7 @@
class Procedure < ApplicationRecord
include ProcedureStatsConcern
include EncryptableConcern
include FileValidationConcern
include Discard::Model
self.discard_column = :hidden_at
@ -240,6 +241,8 @@ class Procedure < ApplicationRecord
validates :duree_conservation_dossiers_dans_ds, allow_nil: false, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: MAX_DUREE_CONSERVATION }
validates :duree_conservation_dossiers_hors_ds, allow_nil: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates_with MonAvisEmbedValidator
FILE_MAX_SIZE = 20.megabytes
validates :notice, content_type: [
"application/msword",
"application/pdf",
@ -252,7 +255,7 @@ class Procedure < ApplicationRecord
"image/jpg",
"image/png",
"text/plain"
], size: { less_than: 20.megabytes }, if: -> { new_record? || created_at > Date.new(2020, 2, 28) }
], size: file_size_validation(FILE_MAX_SIZE), if: -> { new_record? || created_at > Date.new(2020, 2, 28) }
validates :deliberation, content_type: [
"application/msword",
@ -263,10 +266,11 @@ class Procedure < ApplicationRecord
"image/jpg",
"image/png",
"text/plain"
], size: { less_than: 20.megabytes }, if: -> { new_record? || created_at > Date.new(2020, 4, 29) }
], size: file_size_validation(FILE_MAX_SIZE), if: -> { new_record? || created_at > Date.new(2020, 4, 29) }
LOGO_MAX_SIZE = 5.megabytes
validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'],
size: { less_than: 5.megabytes },
size: file_size_validation(LOGO_MAX_SIZE),
if: -> { new_record? || created_at > Date.new(2020, 11, 13) }
validates :api_entreprise_token, jwt_token: true, allow_blank: true

View file

@ -100,6 +100,7 @@ ignore_unused:
- 'activerecord.attributes.*'
- 'activerecord.errors.*'
- 'errors.messages.blank'
- 'errors.messages.content_type_invalid'
- 'pluralize.*'
- 'views.pagination.*'
- 'time.formats.default'

View file

@ -0,0 +1,5 @@
en:
errors:
messages:
content_type_invalid: "is not of an accepted type"
file_size_out_of_range: "is too big. The file must be at most %{file_size_limit}."

View file

@ -1,5 +1,5 @@
fr:
errors:
messages:
content_type_invalid: nest pas dun type accepté
file_size_out_of_range: "est trop lourde, elle doit faire au plus 200 Mo."
content_type_invalid: "nest pas dun type accepté"
file_size_out_of_range: "est trop lourd(e). Le fichier doit faire au plus %{file_size_limit}."

View file

@ -21,7 +21,7 @@ describe Champs::PieceJustificativeChamp do
subject { champ_pj }
context "by default" do
it { is_expected.to validate_size_of(:piece_justificative_file).less_than(Champs::PieceJustificativeChamp::MAX_SIZE) }
it { is_expected.to validate_size_of(:piece_justificative_file).less_than(Champs::PieceJustificativeChamp::FILE_MAX_SIZE) }
it { is_expected.to validate_content_type_of(:piece_justificative_file).rejecting('application/x-ms-dos-executable') }
it { expect(champ_pj.type_de_champ.skip_pj_validation).to be_falsy }
end
@ -29,7 +29,7 @@ describe Champs::PieceJustificativeChamp do
context "when validation is disabled" do
before { champ_pj.type_de_champ.update(skip_pj_validation: true) }
it { is_expected.not_to validate_size_of(:piece_justificative_file).less_than(Champs::PieceJustificativeChamp::MAX_SIZE) }
it { is_expected.not_to validate_size_of(:piece_justificative_file).less_than(Champs::PieceJustificativeChamp::FILE_MAX_SIZE) }
end
context "when content-type validation is disabled" do