2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-05-07 09:25:47 +02:00
|
|
|
describe ProcedureExportService do
|
|
|
|
let(:instructeur) { create(:instructeur) }
|
2024-05-21 17:05:34 +02:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :piece_justificative, libelle: 'pj' }, { type: :repetition, children: [{ type: :piece_justificative, libelle: 'repet_pj' }] }]) }
|
2024-05-17 17:38:54 +02:00
|
|
|
let(:dossiers) { create_list(:dossier, 10, procedure: procedure) }
|
2024-07-23 17:05:16 +02:00
|
|
|
let(:export_template) { create(:export_template, :enabled_pjs, groupe_instructeur: procedure.defaut_groupe_instructeur) }
|
2024-05-07 09:25:47 +02:00
|
|
|
let(:service) { ProcedureExportService.new(procedure, procedure.dossiers, instructeur, export_template) }
|
|
|
|
|
2024-09-27 15:37:11 +02:00
|
|
|
def pj_champ(d) = d.project_champs_public.find { _1.type == 'Champs::PieceJustificativeChamp' }
|
2024-05-07 09:25:47 +02:00
|
|
|
def repetition(d) = d.champs.find_by(type: "Champs::RepetitionChamp")
|
|
|
|
def attachments(champ) = champ.piece_justificative_file.attachments
|
|
|
|
|
|
|
|
before do
|
|
|
|
dossiers.each do |dossier|
|
|
|
|
attach_file_to_champ(pj_champ(dossier))
|
|
|
|
|
2024-08-27 10:29:10 +02:00
|
|
|
repetition(dossier).add_row(updated_by: 'test')
|
2024-05-07 09:25:47 +02:00
|
|
|
attach_file_to_champ(repetition(dossier).champs.first)
|
|
|
|
attach_file_to_champ(repetition(dossier).champs.first)
|
|
|
|
|
2024-08-27 10:29:10 +02:00
|
|
|
repetition(dossier).add_row(updated_by: 'test')
|
2024-05-07 09:25:47 +02:00
|
|
|
attach_file_to_champ(repetition(dossier).champs.second)
|
|
|
|
end
|
|
|
|
|
|
|
|
allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/d0e7862b24d8026a3c03516d865b28151eb3859029c6c6c2e86605891fbdcd7a/socketry/async-io")
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'to_zip' do
|
|
|
|
subject { service.to_zip }
|
|
|
|
|
|
|
|
describe 'generate_dossiers_export' do
|
|
|
|
context 'with export_template' do
|
|
|
|
let(:dossier_exports) { PiecesJustificativesService.new(user_profile: instructeur, export_template:).generate_dossiers_export(Dossier.where(id: dossier)) }
|
|
|
|
|
|
|
|
it 'returns a blob with custom filenames' do
|
|
|
|
VCR.use_cassette('archive/new_file_to_get_200', allow_playback_repeats: true) do
|
|
|
|
sql_count = 0
|
|
|
|
|
|
|
|
callback = lambda { |*_args| sql_count += 1 }
|
|
|
|
ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
|
|
|
|
subject
|
|
|
|
end
|
2024-06-10 09:57:34 +02:00
|
|
|
expect(sql_count <= 62).to be_truthy
|
2024-05-07 09:25:47 +02:00
|
|
|
|
|
|
|
dossier = dossiers.first
|
|
|
|
|
|
|
|
File.write('tmp.zip', subject.download, mode: 'wb')
|
|
|
|
File.open('tmp.zip') do |fd|
|
|
|
|
files = ZipTricks::FileReader.read_zip_structure(io: fd)
|
|
|
|
structure = [
|
|
|
|
"export/",
|
|
|
|
"export/dossier-#{dossier.id}/",
|
2024-07-23 17:05:16 +02:00
|
|
|
"export/dossier-#{dossier.id}/export-#{dossier.id}.pdf",
|
|
|
|
"export/dossier-#{dossier.id}/pj-#{dossier.id}-01.png",
|
|
|
|
"export/dossier-#{dossier.id}/repet_pj-#{dossier.id}-01-01.png",
|
|
|
|
"export/dossier-#{dossier.id}/repet_pj-#{dossier.id}-02-01.png",
|
|
|
|
"export/dossier-#{dossier.id}/repet_pj-#{dossier.id}-01-02.png"
|
2024-05-07 09:25:47 +02:00
|
|
|
]
|
|
|
|
|
2024-05-18 10:02:29 +02:00
|
|
|
expect(files.size).to eq(dossiers.count * 6 + 1)
|
2024-05-07 09:25:47 +02:00
|
|
|
expect(structure - files.map(&:filename)).to be_empty
|
|
|
|
end
|
|
|
|
FileUtils.remove_entry_secure('tmp.zip')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def attach_file_to_champ(champ, safe = true)
|
|
|
|
attach_file(champ.piece_justificative_file, safe)
|
|
|
|
end
|
|
|
|
|
|
|
|
def attach_file(attachable, safe = true)
|
|
|
|
to_be_attached = {
|
|
|
|
io: StringIO.new("toto"),
|
|
|
|
filename: "toto.png", content_type: "image/png"
|
|
|
|
}
|
|
|
|
|
|
|
|
if safe
|
|
|
|
to_be_attached[:metadata] = { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
|
|
|
end
|
|
|
|
|
|
|
|
attachable.attach(to_be_attached)
|
|
|
|
end
|
|
|
|
end
|