2021-04-29 17:29:47 +02:00
|
|
|
describe ProcedureArchiveService do
|
|
|
|
let(:procedure) { create(:procedure, :published) }
|
|
|
|
let(:instructeur) { create(:instructeur) }
|
|
|
|
let(:service) { ProcedureArchiveService.new(procedure) }
|
|
|
|
let(:year) { 2020 }
|
|
|
|
let(:month) { 3 }
|
|
|
|
let(:date_month) { Date.strptime("#{year}-#{month}", "%Y-%m") }
|
2022-01-13 12:35:07 +01:00
|
|
|
let(:groupe_instructeurs) { instructeur.groupe_instructeurs }
|
|
|
|
|
|
|
|
before do
|
|
|
|
procedure.defaut_groupe_instructeur.add(instructeur)
|
|
|
|
end
|
2021-11-29 15:43:51 +01:00
|
|
|
|
2022-03-30 16:19:14 +02:00
|
|
|
describe '#make_and_upload_archive' do
|
2021-11-29 15:43:51 +01:00
|
|
|
let!(:dossier) { create_dossier_for_month(year, month) }
|
|
|
|
let!(:dossier_2020) { create_dossier_for_month(2020, month) }
|
2021-04-29 17:29:47 +02:00
|
|
|
|
|
|
|
after { Timecop.return }
|
|
|
|
|
|
|
|
context 'for a specific month' do
|
2022-07-07 18:15:48 +02:00
|
|
|
let(:archive) { create(:archive, time_span_type: 'monthly', job_status: 'pending', month: date_month, groupe_instructeurs: groupe_instructeurs) }
|
2021-04-29 17:29:47 +02:00
|
|
|
let(:year) { 2021 }
|
2022-01-06 15:42:56 +01:00
|
|
|
|
|
|
|
it 'collects files with success' do
|
2022-04-04 17:02:02 +02:00
|
|
|
allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/d0e7862b24d8026a3c03516d865b28151eb3859029c6c6c2e86605891fbdcd7a/socketry/async-io")
|
2022-03-30 16:19:14 +02:00
|
|
|
|
2022-01-06 15:42:56 +01:00
|
|
|
VCR.use_cassette('archive/new_file_to_get_200') do
|
2022-06-23 13:51:58 +02:00
|
|
|
service.make_and_upload_archive(archive)
|
2021-12-13 14:35:13 +01:00
|
|
|
end
|
2021-04-29 17:29:47 +02:00
|
|
|
|
|
|
|
archive.file.open do |f|
|
|
|
|
files = ZipTricks::FileReader.read_zip_structure(io: f)
|
2021-11-29 15:43:51 +01:00
|
|
|
|
|
|
|
structure = [
|
2022-03-18 15:00:58 +01:00
|
|
|
"procedure-#{procedure.id}-#{archive.id}/",
|
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/",
|
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/pieces_justificatives/",
|
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/pieces_justificatives/attestation-dossier--05-03-2021-00-00-#{dossier.attestation.pdf.id % 10000}.pdf",
|
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2021-00-00-#{dossier.id}.pdf"
|
2021-11-29 15:43:51 +01:00
|
|
|
]
|
|
|
|
expect(files.map(&:filename)).to match_array(structure)
|
2021-04-29 17:29:47 +02:00
|
|
|
end
|
|
|
|
expect(archive.file.attached?).to be_truthy
|
|
|
|
end
|
2021-08-03 13:17:35 +02:00
|
|
|
|
2022-01-06 15:42:56 +01:00
|
|
|
it 'retry errors files with errors' do
|
|
|
|
allow_any_instance_of(ActiveStorage::Attached::One).to receive(:url).and_return("https://www.demarches-simplifiees.fr/error_1")
|
2022-03-30 16:19:14 +02:00
|
|
|
|
2022-01-06 15:42:56 +01:00
|
|
|
VCR.use_cassette('archive/new_file_to_get_400.html') do
|
2022-06-23 13:51:58 +02:00
|
|
|
service.make_and_upload_archive(archive)
|
2022-01-06 15:42:56 +01:00
|
|
|
end
|
|
|
|
archive.file.open do |f|
|
|
|
|
files = ZipTricks::FileReader.read_zip_structure(io: f)
|
|
|
|
structure = [
|
2022-03-18 15:00:58 +01:00
|
|
|
"procedure-#{procedure.id}-#{archive.id}/",
|
2022-10-20 09:33:26 +02:00
|
|
|
"procedure-#{procedure.id}-#{archive.id}/-LISTE-DES-FICHIERS-EN-ERREURS.txt",
|
2022-03-18 15:00:58 +01:00
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/",
|
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/pieces_justificatives/",
|
2022-10-20 09:33:26 +02:00
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2021-00-00-#{dossier.id}.pdf"
|
2022-01-06 15:42:56 +01:00
|
|
|
]
|
|
|
|
expect(files.map(&:filename)).to match_array(structure)
|
|
|
|
end
|
|
|
|
expect(archive.file.attached?).to be_truthy
|
|
|
|
end
|
|
|
|
|
2021-08-03 13:17:35 +02:00
|
|
|
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
|
|
|
|
|
2022-04-04 23:00:54 +02:00
|
|
|
let(:documents) { [pj, bad_pj].map { |p| ActiveStorage::DownloadableFile.pj_and_path(dossier.id, p) } }
|
2021-08-03 13:17:35 +02:00
|
|
|
before do
|
|
|
|
allow(PiecesJustificativesService).to receive(:liste_documents).and_return(documents)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'collect files without raising exception' do
|
2022-06-23 13:51:58 +02:00
|
|
|
expect { service.make_and_upload_archive(archive) }.not_to raise_exception
|
2021-08-03 13:17:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'add bug report to archive' do
|
2022-06-23 13:51:58 +02:00
|
|
|
service.make_and_upload_archive(archive)
|
2021-08-03 13:17:35 +02:00
|
|
|
|
|
|
|
archive.file.open do |f|
|
2021-12-14 14:18:59 +01:00
|
|
|
zip_entries = ZipTricks::FileReader.read_zip_structure(io: f)
|
2021-11-29 15:43:51 +01:00
|
|
|
structure = [
|
2022-03-18 15:00:58 +01:00
|
|
|
"procedure-#{procedure.id}-#{archive.id}/",
|
2022-10-20 09:33:26 +02:00
|
|
|
"procedure-#{procedure.id}-#{archive.id}/-LISTE-DES-FICHIERS-EN-ERREURS.txt",
|
2022-03-18 15:00:58 +01:00
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/",
|
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/export-dossier-05-03-2020-00-00-1.pdf",
|
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/pieces_justificatives/",
|
2022-10-20 09:33:26 +02:00
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2021-00-00-#{dossier.id}.pdf"
|
2021-11-29 15:43:51 +01:00
|
|
|
]
|
2021-12-14 14:18:59 +01:00
|
|
|
expect(zip_entries.map(&:filename)).to match_array(structure)
|
|
|
|
zip_entries.map do |entry|
|
2022-10-20 09:33:26 +02:00
|
|
|
next unless entry.filename == "procedure-#{procedure.id}-#{archive.id}/-LISTE-DES-FICHIERS-EN-ERREURS.txt"
|
2021-12-14 14:18:59 +01:00
|
|
|
extracted_content = ""
|
|
|
|
extractor = entry.extractor_from(f)
|
|
|
|
extracted_content << extractor.extract(1024 * 1024) until extractor.eof?
|
|
|
|
expect(extracted_content).to match(/Impossible de .* .*cni.*png/)
|
|
|
|
end
|
2021-08-03 13:17:35 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-04-29 17:29:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for all months' do
|
2022-07-07 18:15:48 +02:00
|
|
|
let(:archive) { create(:archive, time_span_type: 'everything', job_status: 'pending', groupe_instructeurs: groupe_instructeurs) }
|
2021-04-29 17:29:47 +02:00
|
|
|
|
|
|
|
it 'collect files' do
|
2022-04-04 17:02:02 +02:00
|
|
|
allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/5e61989aecb78e369c93674f877d7bf4ecde378850114a9563cdf8b6a2472536/typhoeus/typhoeus/issues/110")
|
2021-04-29 17:29:47 +02:00
|
|
|
|
2022-01-06 15:42:56 +01:00
|
|
|
VCR.use_cassette('archive/old_file_to_get_200') do
|
2022-06-23 13:51:58 +02:00
|
|
|
service.make_and_upload_archive(archive)
|
2021-12-13 14:35:13 +01:00
|
|
|
end
|
2021-04-29 17:29:47 +02:00
|
|
|
|
|
|
|
archive = Archive.last
|
|
|
|
archive.file.open do |f|
|
|
|
|
files = ZipTricks::FileReader.read_zip_structure(io: f)
|
2021-11-29 15:43:51 +01:00
|
|
|
structure = [
|
2022-03-18 15:00:58 +01:00
|
|
|
"procedure-#{procedure.id}-#{archive.id}/",
|
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/",
|
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/pieces_justificatives/",
|
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/pieces_justificatives/attestation-dossier--05-03-2020-00-00-#{dossier.attestation.pdf.id % 10000}.pdf",
|
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2020-00-00-#{dossier.id}.pdf",
|
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier_2020.id}/",
|
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier_2020.id}/export-#{dossier_2020.id}-05-03-2020-00-00-#{dossier_2020.id}.pdf",
|
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier_2020.id}/pieces_justificatives/",
|
|
|
|
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier_2020.id}/pieces_justificatives/attestation-dossier--05-03-2020-00-00-#{dossier_2020.attestation.pdf.id % 10000}.pdf"
|
2021-11-29 15:43:51 +01:00
|
|
|
]
|
|
|
|
expect(files.map(&:filename)).to match_array(structure)
|
2021-04-29 17:29:47 +02:00
|
|
|
end
|
|
|
|
expect(archive.file.attached?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_dossier_for_month(year, month)
|
|
|
|
Timecop.freeze(Time.zone.local(year, month, 5))
|
|
|
|
create(:dossier, :accepte, :with_attestation, procedure: procedure)
|
|
|
|
end
|
2021-08-03 13:17:35 +02:00
|
|
|
|
|
|
|
def extract(zip_file, zip_entry)
|
|
|
|
extractor = zip_entry.extractor_from(zip_file)
|
|
|
|
extractor.extract
|
|
|
|
end
|
2021-04-29 17:29:47 +02:00
|
|
|
end
|