From 38e3469e9ae73123d8eca475cf43800d84bc3d3a Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Tue, 3 Aug 2021 13:17:35 +0200 Subject: [PATCH] add bug report to archive --- app/services/pieces_justificatives_service.rb | 4 ++ app/services/procedure_archive_service.rb | 7 ++- .../procedure_archive_service_spec.rb | 47 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 974e774b8..8b1d59a7c 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -95,6 +95,10 @@ class PiecesJustificativesService def attached? true end + + def record_type + 'Fake' + end end def self.generate_dossier_export(dossier) diff --git a/app/services/procedure_archive_service.rb b/app/services/procedure_archive_service.rb index f7ab33e0a..347989634 100644 --- a/app/services/procedure_archive_service.rb +++ b/app/services/procedure_archive_service.rb @@ -25,14 +25,19 @@ class ProcedureArchiveService tmp_file = Tempfile.new(['tc', '.zip']) Zip::OutputStream.open(tmp_file) do |zipfile| + bug_reports = '' files.each do |attachment, pj_filename| zipfile.put_next_entry("procedure-#{@procedure.id}/#{pj_filename}") begin zipfile.puts(attachment.download) rescue - raise "Problem while trying to attach #{pj_filename}" + bug_reports += "Impossible de récupérer le fichier #{pj_filename}\n" end end + if !bug_reports.empty? + zipfile.put_next_entry("LISEZMOI.txt") + zipfile.puts(bug_reports) + end end archive.file.attach(io: File.open(tmp_file), filename: archive.filename(@procedure)) diff --git a/spec/services/procedure_archive_service_spec.rb b/spec/services/procedure_archive_service_spec.rb index 80e013f74..dc6ec2ebc 100644 --- a/spec/services/procedure_archive_service_spec.rb +++ b/spec/services/procedure_archive_service_spec.rb @@ -53,6 +53,48 @@ describe ProcedureArchiveService do end expect(archive.file.attached?).to be_truthy end + + context 'with a missing file' do + let(:pj) do + PiecesJustificativesService::FakeAttachment.new( + file: StringIO.new('coucou'), + filename: "export-dossier.pdf", + name: 'pdf_export_for_instructeur', + id: 1, + created_at: Time.zone.now + ) + end + + let(:bad_pj) do + PiecesJustificativesService::FakeAttachment.new( + file: nil, + filename: "cni.png", + name: 'cni.png', + id: 2, + created_at: Time.zone.now + ) + end + + let(:documents) { [pj, bad_pj] } + before do + allow(PiecesJustificativesService).to receive(:liste_documents).and_return(documents) + end + + it 'collect files without raising exception' do + expect { service.collect_files_archive(archive, instructeur) }.not_to raise_exception + end + + it 'add bug report to archive' do + service.collect_files_archive(archive, instructeur) + + archive.file.open do |f| + files = ZipTricks::FileReader.read_zip_structure(io: f) + expect(files.size).to be 4 + expect(files.last.filename).to include("LISEZMOI") + expect(extract(f, files.last)).to match(/Impossible de .*cni.*png/) + end + end + end end context 'for all months' do @@ -80,4 +122,9 @@ describe ProcedureArchiveService do Timecop.freeze(Time.zone.local(year, month, 5)) create(:dossier, :accepte, :with_attestation, procedure: procedure) end + + def extract(zip_file, zip_entry) + extractor = zip_entry.extractor_from(zip_file) + extractor.extract + end end