From 245ef2befd633ac276ea8ede6b0f1e3882599032 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 25 Mar 2020 16:56:54 +0100 Subject: [PATCH 1/3] specs: fix an Rspec warning --- spec/models/user_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 084d2a6e0..a0d31295f 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -251,7 +251,7 @@ describe User, type: :model do context 'with a dossier in instruction' do let!(:dossier_en_instruction) { create(:dossier, :en_instruction, user: user) } it 'raises' do - expect { user.delete_and_keep_track_dossiers(administration) }.to raise_error + expect { user.delete_and_keep_track_dossiers(administration) }.to raise_error(RuntimeError) end end From f3c210e87516cca79115e77de42eb6ea8fe74083 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Thu, 26 Mar 2020 16:26:44 +0000 Subject: [PATCH 2/3] dossier: remove dead validation code This code used to be called by CarrierWave, but is unused since the migration to ActiveStorage. This means that these validations are currently disabled though. See https://github.com/betagouv/demarches-simplifiees.fr/issues/4926 --- .../champs/piece_justificative_champ.rb | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/app/models/champs/piece_justificative_champ.rb b/app/models/champs/piece_justificative_champ.rb index 5677ae7d5..0a3526a07 100644 --- a/app/models/champs/piece_justificative_champ.rb +++ b/app/models/champs/piece_justificative_champ.rb @@ -28,28 +28,6 @@ class Champs::PieceJustificativeChamp < Champ mandatory? && !piece_justificative_file.attached? end - def piece_justificative_file_errors - errors = [] - - if piece_justificative_file.attached? && piece_justificative_file.previous_changes.present? - if piece_justificative_file.blob.byte_size > PIECE_JUSTIFICATIVE_FILE_MAX_SIZE - errors << "Le fichier #{piece_justificative_file.filename} est trop lourd, il doit faire au plus #{PIECE_JUSTIFICATIVE_FILE_MAX_SIZE.to_s(:human_size, precision: 2)}" - end - - if !piece_justificative_file.blob.content_type.in?(PIECE_JUSTIFICATIVE_FILE_ACCEPTED_FORMATS) - errors << "Le fichier #{piece_justificative_file.filename} est dans un format que nous n'acceptons pas" - end - - # FIXME: add Clamav check - end - - if errors.present? - piece_justificative_file.purge_later - end - - errors - end - def for_api if piece_justificative_file.attached? && (piece_justificative_file.virus_scanner.safe? || piece_justificative_file.virus_scanner.pending?) piece_justificative_file.service_url From fe13043efd6889a5a0ddaeadfbc08a5e4228db7a Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Thu, 26 Mar 2020 16:28:24 +0000 Subject: [PATCH 3/3] dossier: prepare validations to piece_justificative champs We can't yet enable the validations, because of an issue that will (hopefully) be solved with Rails 6. See https://github.com/betagouv/demarches-simplifiees.fr/issues/4926 --- app/models/champs/piece_justificative_champ.rb | 12 ++++++++++-- config/locales/active_storage_validations.fr.yml | 4 ++++ config/locales/models/champs/fr.yml | 1 + .../champs/piece_justificative_champ_spec.rb | 15 +++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 config/locales/active_storage_validations.fr.yml diff --git a/app/models/champs/piece_justificative_champ.rb b/app/models/champs/piece_justificative_champ.rb index 0a3526a07..8fcf58027 100644 --- a/app/models/champs/piece_justificative_champ.rb +++ b/app/models/champs/piece_justificative_champ.rb @@ -1,7 +1,8 @@ class Champs::PieceJustificativeChamp < Champ - PIECE_JUSTIFICATIVE_FILE_MAX_SIZE = 200.megabytes + MAX_SIZE = 200.megabytes - PIECE_JUSTIFICATIVE_FILE_ACCEPTED_FORMATS = [ + ACCEPTED_FORMATS = [ + "text/plain", "application/pdf", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", @@ -16,6 +17,13 @@ class Champs::PieceJustificativeChamp < Champ "image/jpeg" ] + # TODO: once we're running on Rails 6, re-enable this validation. + # See https://github.com/betagouv/demarches-simplifiees.fr/issues/4926 + # + # validates :piece_justificative_file, + # content_type: ACCEPTED_FORMATS, + # size: { less_than: MAX_SIZE } + def main_value_name :piece_justificative_file end diff --git a/config/locales/active_storage_validations.fr.yml b/config/locales/active_storage_validations.fr.yml new file mode 100644 index 000000000..1494483b0 --- /dev/null +++ b/config/locales/active_storage_validations.fr.yml @@ -0,0 +1,4 @@ +fr: + errors: + messages: + content_type_invalid: n’est pas d’un type accepté diff --git a/config/locales/models/champs/fr.yml b/config/locales/models/champs/fr.yml index 9d9e6b2f0..7a0730a32 100644 --- a/config/locales/models/champs/fr.yml +++ b/config/locales/models/champs/fr.yml @@ -3,3 +3,4 @@ fr: attributes: champ: value: La valeur du champ + piece_justificative_file: La pièce justificative diff --git a/spec/models/champs/piece_justificative_champ_spec.rb b/spec/models/champs/piece_justificative_champ_spec.rb index 506eba015..c2f691884 100644 --- a/spec/models/champs/piece_justificative_champ_spec.rb +++ b/spec/models/champs/piece_justificative_champ_spec.rb @@ -1,4 +1,19 @@ +require 'active_storage_validations/matchers' + describe Champs::PieceJustificativeChamp do + include ActiveStorageValidations::Matchers + + # TODO: once we're running on Rails 6, re-enable the PieceJustificativeChamp validator, + # and re-enable this spec. + # + # See https://github.com/betagouv/demarches-simplifiees.fr/issues/4926 + describe "validations", pending: true do + subject(:champ_pj) { build(:champ_piece_justificative) } + + it { is_expected.to validate_size_of(:piece_justificative_file).less_than(Champs::PieceJustificativeChamp::MAX_SIZE) } + it { is_expected.to validate_content_type_of(:piece_justificative_file).allowing(Champs::PieceJustificativeChamp::ACCEPTED_FORMATS) } + end + describe '#for_api' do let(:champ_pj) { create(:champ_piece_justificative) } let(:metadata) { champ_pj.piece_justificative_file.blob.metadata }