ADD FILE_MAX_SIZE constant to set allow file size limit

This commit is contained in:
Ismael MOUSSA S 2021-09-14 18:03:40 +02:00 committed by Pierre de La Morinerie
parent bda3270b58
commit bdbb4deb87
7 changed files with 23 additions and 16 deletions

View file

@ -21,8 +21,10 @@ class AttestationTemplate < ApplicationRecord
has_one_attached :signature has_one_attached :signature
validates :footer, length: { maximum: 190 } 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) DOSSIER_STATE = Dossier.states.fetch(:accepte)

View file

@ -27,18 +27,19 @@ class Avis < ApplicationRecord
has_one :expert, through: :experts_procedure has_one :expert, through: :experts_procedure
has_one :procedure, through: :experts_procedure has_one :procedure, through: :experts_procedure
FILE_MAX_SIZE = 20.megabytes
validates :piece_justificative_file, validates :piece_justificative_file,
content_type: AUTHORIZED_CONTENT_TYPES, content_type: AUTHORIZED_CONTENT_TYPES,
size: { less_than: 20.megabytes } size: { less_than: FILE_MAX_SIZE }
validates :introduction_file, validates :introduction_file,
content_type: AUTHORIZED_CONTENT_TYPES, 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 :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true
validates :claimant, presence: true validates :claimant, presence: true
validates :piece_justificative_file, size: { less_than: 20.megabytes } validates :piece_justificative_file, size: { less_than: FILE_MAX_SIZE }
validates :introduction_file, size: { less_than: 20.megabytes } validates :introduction_file, size: { less_than: FILE_MAX_SIZE }
before_validation -> { sanitize_email(:email) } before_validation -> { sanitize_email(:email) }

View file

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

View file

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

View file

@ -24,9 +24,10 @@ class Commentaire < ApplicationRecord
validates :body, presence: { message: "ne peut être vide" } validates :body, presence: { message: "ne peut être vide" }
FILE_MAX_SIZE = 20.megabytes
validates :piece_jointe, validates :piece_jointe,
content_type: AUTHORIZED_CONTENT_TYPES, content_type: AUTHORIZED_CONTENT_TYPES,
size: { less_than: 20.megabytes } size: { less_than: FILE_MAX_SIZE }
default_scope { order(created_at: :asc) } default_scope { order(created_at: :asc) }
scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) } scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) }

View file

@ -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_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 :duree_conservation_dossiers_hors_ds, allow_nil: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates_with MonAvisEmbedValidator validates_with MonAvisEmbedValidator
FILE_MAX_SIZE = 20.megabytes
validates :notice, content_type: [ validates :notice, content_type: [
"application/msword", "application/msword",
"application/pdf", "application/pdf",
@ -252,7 +254,7 @@ class Procedure < ApplicationRecord
"image/jpg", "image/jpg",
"image/png", "image/png",
"text/plain" "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: [ validates :deliberation, content_type: [
"application/msword", "application/msword",
@ -263,10 +265,11 @@ class Procedure < ApplicationRecord
"image/jpg", "image/jpg",
"image/png", "image/png",
"text/plain" "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'], 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) } if: -> { new_record? || created_at > Date.new(2020, 11, 13) }
validates :api_entreprise_token, jwt_token: true, allow_blank: true validates :api_entreprise_token, jwt_token: true, allow_blank: true

View file

@ -21,7 +21,7 @@ describe Champs::PieceJustificativeChamp do
subject { champ_pj } subject { champ_pj }
context "by default" do 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 { 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 } it { expect(champ_pj.type_de_champ.skip_pj_validation).to be_falsy }
end end
@ -29,7 +29,7 @@ describe Champs::PieceJustificativeChamp do
context "when validation is disabled" do context "when validation is disabled" do
before { champ_pj.type_de_champ.update(skip_pj_validation: true) } 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 end
context "when content-type validation is disabled" do context "when content-type validation is disabled" do