From e0828dda135bd0b4fd4e7e45824ae7f03542dbbc Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Wed, 19 Apr 2017 10:16:30 +0200 Subject: [PATCH 1/4] PiecesJustificativesService: save_pj returns nil if no errors --- app/services/pieces_justificatives_service.rb | 4 ++-- spec/services/pieces_justificatives_service_spec.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 6b10ecbeb..7be9e0dc2 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -12,7 +12,7 @@ class PiecesJustificativesService errors += without_virus .map { |tpj, content| save_pj(content, dossier, tpj, user) } - .reject(&:empty?) + .compact() errors += missing_pj_error_messages(dossier) end @@ -40,7 +40,7 @@ class PiecesJustificativesService type_de_piece_justificative: tpj, user: user) - pj.save ? '' : "le fichier #{pj.libelle} n'a pas pu être sauvegardé" + pj.save ? nil : "le fichier #{pj.libelle} n'a pas pu être sauvegardé" end def self.missing_pj_error_messages(dossier) diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index 922befc4e..7a734bc48 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -63,7 +63,7 @@ describe PiecesJustificativesService do # we are messing around piece_justificative # because directly doubling carrierwave params seems complicated - allow(PiecesJustificativesService).to receive(:save_pj).and_return('') + allow(PiecesJustificativesService).to receive(:save_pj).and_return(nil) piece_justificative_double = double(type_de_piece_justificative: tpj_mandatory) expect(dossier).to receive(:pieces_justificatives).and_return([piece_justificative_double]) end From 395c9d5bb15b8eff2999e63a61b83c5276699bbe Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Wed, 19 Apr 2017 10:20:32 +0200 Subject: [PATCH 2/4] PiecesJustificativesService: improve error message when the file cannot be saved --- app/services/pieces_justificatives_service.rb | 2 +- spec/services/pieces_justificatives_service_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 7be9e0dc2..3bc0291c4 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -40,7 +40,7 @@ class PiecesJustificativesService type_de_piece_justificative: tpj, user: user) - pj.save ? nil : "le fichier #{pj.libelle} n'a pas pu être sauvegardé" + pj.save ? nil : "le fichier #{content.original_filename} (#{pj.libelle}) n'a pas pu être sauvegardé" end def self.missing_pj_error_messages(dossier) diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index 7a734bc48..b9181c01e 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -31,11 +31,11 @@ describe PiecesJustificativesService do let(:hash) do { "piece_justificative_#{tpj_not_mandatory.id}" => - double(path: '', original_filename: 'file') + double(path: '', original_filename: 'filename') } end - it { expect(errors).to match(["le fichier not mandatory n'a pas pu être sauvegardé"]) } + it { expect(errors).to match(["le fichier filename (not mandatory) n'a pas pu être sauvegardé"]) } end context 'when a virus is provided' do From 21d7a960626247c33bb2ba2363d19eec691ba36f Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Wed, 19 Apr 2017 10:22:30 +0200 Subject: [PATCH 3/4] PiecesJustificativesService: less violent error message in case of virus --- app/services/pieces_justificatives_service.rb | 2 +- spec/services/pieces_justificatives_service_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 3bc0291c4..dd611550b 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -8,7 +8,7 @@ class PiecesJustificativesService .partition { |_, content| ClamavService.safe_file?(content.path) } errors = with_virus - .map { |_, content| content.original_filename + ': Virus détecté !!' } + .map { |_, content| content.original_filename + ' : virus détecté' } errors += without_virus .map { |tpj, content| save_pj(content, dossier, tpj, user) } diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index b9181c01e..d6ac03c87 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -47,7 +47,7 @@ describe PiecesJustificativesService do } end - it { expect(errors).to match(['bad_file: Virus détecté !!']) } + it { expect(errors).to match(['bad_file : virus détecté']) } end end From 71542f6a3a947cae0f0be5c1f3ea0167daafcde1 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Wed, 19 Apr 2017 10:24:08 +0200 Subject: [PATCH 4/4] PiecesJustificativesService: more explicit selection of content --- app/services/pieces_justificatives_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index dd611550b..1298897e5 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -2,7 +2,7 @@ class PiecesJustificativesService def self.upload!(dossier, user, params) tpj_contents = dossier.types_de_piece_justificative .map { |tpj| [tpj, params["piece_justificative_#{tpj.id}"]] } - .select { |_, content| content } + .select { |_, content| content.present? } without_virus, with_virus = tpj_contents .partition { |_, content| ClamavService.safe_file?(content.path) }