From bdbb4deb87a0010d6e4fa6f279f944a3760c8422 Mon Sep 17 00:00:00 2001 From: Ismael MOUSSA S Date: Tue, 14 Sep 2021 18:03:40 +0200 Subject: [PATCH] ADD FILE_MAX_SIZE constant to set allow file size limit --- app/models/attestation_template.rb | 6 ++++-- app/models/avis.rb | 9 +++++---- app/models/champs/piece_justificative_champ.rb | 4 ++-- app/models/champs/titre_identite_champ.rb | 4 ++-- app/models/commentaire.rb | 3 ++- app/models/procedure.rb | 9 ++++++--- spec/models/champs/piece_justificative_champ_spec.rb | 4 ++-- 7 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app/models/attestation_template.rb b/app/models/attestation_template.rb index ac2600fec..16f463d7f 100644 --- a/app/models/attestation_template.rb +++ b/app/models/attestation_template.rb @@ -21,8 +21,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: { less_than: FILE_MAX_SIZE } + validates :signature, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: { less_than: FILE_MAX_SIZE } DOSSIER_STATE = Dossier.states.fetch(:accepte) diff --git a/app/models/avis.rb b/app/models/avis.rb index 2ab886a18..f64c802c4 100644 --- a/app/models/avis.rb +++ b/app/models/avis.rb @@ -27,18 +27,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: { less_than: FILE_MAX_SIZE } validates :introduction_file, content_type: AUTHORIZED_CONTENT_TYPES, - size: { less_than: 20.megabytes } + size: { less_than: 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: { less_than: FILE_MAX_SIZE } + validates :introduction_file, size: { less_than: FILE_MAX_SIZE } before_validation -> { sanitize_email(:email) } diff --git a/app/models/champs/piece_justificative_champ.rb b/app/models/champs/piece_justificative_champ.rb index 405274d4e..decc64cfd 100644 --- a/app/models/champs/piece_justificative_champ.rb +++ b/app/models/champs/piece_justificative_champ.rb @@ -18,10 +18,10 @@ # type_de_champ_id :integer # class Champs::PieceJustificativeChamp < Champ - MAX_SIZE = 200.megabytes + FILE_MAX_SIZE = 200.megabytes validates :piece_justificative_file, - size: { less_than: MAX_SIZE }, + size: { less_than: FILE_MAX_SIZE }, if: -> { !type_de_champ.skip_pj_validation } validates :piece_justificative_file, diff --git a/app/models/champs/titre_identite_champ.rb b/app/models/champs/titre_identite_champ.rb index b4e32e141..fc339f30e 100644 --- a/app/models/champs/titre_identite_champ.rb +++ b/app/models/champs/titre_identite_champ.rb @@ -18,7 +18,7 @@ # type_de_champ_id :integer # class Champs::TitreIdentiteChamp < Champ - MAX_SIZE = 20.megabytes + FILE_MAX_SIZE = 20.megabytes ACCEPTED_FORMATS = [ "image/png", @@ -30,7 +30,7 @@ class Champs::TitreIdentiteChamp < Champ # validates :piece_justificative_file, content_type: ACCEPTED_FORMATS, - size: { less_than: MAX_SIZE } + size: { less_than: FILE_MAX_SIZE } def main_value_name :piece_justificative_file diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index f68b462a5..3df4c0dc6 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -24,9 +24,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: { less_than: FILE_MAX_SIZE } default_scope { order(created_at: :asc) } scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) } diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 5cfc9b618..0fe05a42c 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -240,6 +240,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 +254,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: { less_than: FILE_MAX_SIZE }, if: -> { new_record? || created_at > Date.new(2020, 2, 28) } validates :deliberation, content_type: [ "application/msword", @@ -263,10 +265,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: { less_than: 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: { less_than: LOGO_MAX_SIZE }, if: -> { new_record? || created_at > Date.new(2020, 11, 13) } validates :api_entreprise_token, jwt_token: true, allow_blank: true diff --git a/spec/models/champs/piece_justificative_champ_spec.rb b/spec/models/champs/piece_justificative_champ_spec.rb index 9886224d1..9d3d239b1 100644 --- a/spec/models/champs/piece_justificative_champ_spec.rb +++ b/spec/models/champs/piece_justificative_champ_spec.rb @@ -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