generate export with export_template

This commit is contained in:
Christophe Robillard 2024-03-08 17:14:21 +01:00 committed by simon lehericey
parent 7a39752630
commit 357c07456c
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
17 changed files with 131 additions and 48 deletions

View file

@ -121,7 +121,7 @@ describe Experts::AvisController, type: :controller do
context 'with a valid avis' do
it do
service = instance_double(PiecesJustificativesService)
expect(PiecesJustificativesService).to receive(:new).with(user_profile: expert).and_return(service)
expect(PiecesJustificativesService).to receive(:new).with(user_profile: expert, export_template: nil).and_return(service)
expect(service).to receive(:generate_dossiers_export).with(Dossier.where(id: dossier)).and_return([])
expect(service).to receive(:liste_documents).with(Dossier.where(id: dossier)).and_return([])
is_expected.to have_http_status(:success)

View file

@ -936,7 +936,7 @@ describe Instructeurs::DossiersController, type: :controller do
subject
end
it { expect(assigns(:acls)).to eq(PiecesJustificativesService.new(user_profile: instructeur).acl_for_dossier_export(dossier.procedure)) }
it { expect(assigns(:acls)).to eq(PiecesJustificativesService.new(user_profile: instructeur, export_template: nil).acl_for_dossier_export(dossier.procedure)) }
it { expect(assigns(:is_dossier_in_batch_operation)).to eq(false) }
it { expect(response).to render_template 'dossiers/show' }

View file

@ -736,6 +736,18 @@ describe Instructeurs::ProceduresController, type: :controller do
end
it { expect { subject }.to change { Export.where(user_profile: instructeur).count }.by(1) }
context 'with an export template' do
let(:export_template) { create(:export_template) }
subject do
get :download_export, params: { export_template_id: export_template.id, procedure_id: procedure.id }
end
it 'displays an notice' do
is_expected.to redirect_to(exports_instructeur_procedure_url(procedure))
expect(flash.notice).to be_present
end
end
end
context 'when the export is not ready' do

View file

@ -1142,7 +1142,7 @@ describe Users::DossiersController, type: :controller do
end
context 'when the dossier has been submitted' do
it { expect(assigns(:acls)).to eq(PiecesJustificativesService.new(user_profile: user).acl_for_dossier_export(dossier.procedure)) }
it { expect(assigns(:acls)).to eq(PiecesJustificativesService.new(user_profile: user, export_template: nil).acl_for_dossier_export(dossier.procedure)) }
it { expect(response).to render_template('dossiers/show') }
end
end

View file

@ -109,6 +109,14 @@ RSpec.describe Export, type: :model do
end
end
context 'with export template' do
let(:export_template) { build(:export_template) }
it 'creates new export' do
expect { Export.find_or_create_fresh_export(:zip, [gi_1], instructeur, export_template: export_template, time_span_type: Export.time_span_types.fetch(:everything), statut: Export.statuts.fetch(:tous), procedure_presentation: pp) }
.to change { Export.count }.by(1)
end
end
context 'with existing matching export' do
def find_or_create =
Export.find_or_create_fresh_export(:zip, [gi_1], instructeur, time_span_type: Export.time_span_types.fetch(:everything), statut: Export.statuts.fetch(:tous), procedure_presentation: pp)

View file

@ -2,8 +2,9 @@ describe PiecesJustificativesService do
describe '.liste_documents' do
let(:dossier) { create(:dossier, procedure: procedure) }
let(:dossiers) { Dossier.where(id: dossier.id) }
let(:export_template) { nil }
subject do
PiecesJustificativesService.new(user_profile:).liste_documents(dossiers).map(&:first)
PiecesJustificativesService.new(user_profile:, export_template:).liste_documents(dossiers).map(&:first)
end
context 'no acl' do
@ -19,6 +20,11 @@ describe PiecesJustificativesService do
end
it { expect(subject).to match_array(pj_champ.call(dossier).piece_justificative_file.attachments) }
context 'with export_template' do
let(:export_template) { create(:export_template, groupe_instructeur: procedure.defaut_groupe_instructeur).tap(&:set_default_values) }
it { expect(subject).to match_array(pj_champ.call(dossier).piece_justificative_file.attachments) }
end
end
context 'with a multiple attachments' do
@ -303,7 +309,7 @@ describe PiecesJustificativesService do
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :repetition, children: [{ type: :piece_justificative }] }]) }
let(:dossier) { create(:dossier, :with_populated_champs, procedure: procedure) }
let(:dossiers) { Dossier.where(id: dossier.id) }
subject { PiecesJustificativesService.new(user_profile:).generate_dossiers_export(dossiers) }
subject { PiecesJustificativesService.new(user_profile:, export_template: nil).generate_dossiers_export(dossiers) }
it "doesn't update dossier" do
expect { subject }.not_to change { dossier.updated_at }
@ -315,7 +321,7 @@ describe PiecesJustificativesService do
let!(:not_confidentiel_avis) { create(:avis, :not_confidentiel, dossier: dossier) }
let!(:expert_avis) { create(:avis, :confidentiel, dossier: dossier, expert: user_profile) }
subject { PiecesJustificativesService.new(user_profile:).generate_dossiers_export(dossiers) }
subject { PiecesJustificativesService.new(user_profile:, export_template: nil).generate_dossiers_export(dossiers) }
it "includes avis not confidentiel as well as expert's avis" do
expect_any_instance_of(Dossier).to receive(:avis_for_expert).with(user_profile).and_return([])
subject

View file

@ -2,8 +2,9 @@ require 'csv'
describe ProcedureExportService do
let(:instructeur) { create(:instructeur) }
let(:procedure) { create(:procedure, :published, :for_individual, :with_all_champs, instructeurs: [instructeur]) }
let(:service) { ProcedureExportService.new(procedure, procedure.dossiers, instructeur) }
let(:procedure) { create(:procedure, :published, :for_individual, :with_all_champs) }
let(:service) { ProcedureExportService.new(procedure, procedure.dossiers, instructeur, export_template) }
let(:export_template) { nil }
describe 'to_xlsx' do
subject do
@ -243,7 +244,7 @@ describe ProcedureExportService do
context 'as csv' do
subject do
ProcedureExportService.new(procedure, procedure.dossiers, instructeur)
ProcedureExportService.new(procedure, procedure.dossiers, instructeur, export_template)
.to_csv
.open { |f| CSV.read(f.path) }
end
@ -519,39 +520,68 @@ describe ProcedureExportService do
end
end
context 'generate_dossiers_export' do
describe 'generate_dossiers_export' do
it 'include_infos_administration (so it includes avis, champs privés)' do
expect(ActiveStorage::DownloadableFile).to receive(:create_list_from_dossiers).with(dossiers: anything, user_profile: instructeur).and_return([])
expect(ActiveStorage::DownloadableFile).to receive(:create_list_from_dossiers).with(dossiers: anything, user_profile: instructeur, export_template:).and_return([])
subject
end
end
context 'with files (and http calls)' do
let!(:dossier) { create(:dossier, :accepte, :with_populated_champs, :with_individual, procedure: procedure) }
let(:dossier_exports) { PiecesJustificativesService.new(user_profile: instructeur).generate_dossiers_export(Dossier.where(id: dossier)) }
before do
allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/d0e7862b24d8026a3c03516d865b28151eb3859029c6c6c2e86605891fbdcd7a/socketry/async-io")
context 'with export_template' do
let!(:dossier) { create(:dossier, :accepte, :with_populated_champs, :with_individual, procedure: procedure) }
let(:dossier_exports) { PiecesJustificativesService.new(user_profile: instructeur, export_template:).generate_dossiers_export(Dossier.where(id: dossier)) }
let(:export_template) { create(:export_template, groupe_instructeur: procedure.defaut_groupe_instructeur).tap(&:set_default_values) }
before do
allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/d0e7862b24d8026a3c03516d865b28151eb3859029c6c6c2e86605891fbdcd7a/socketry/async-io")
end
it 'returns a blob with custom filenames' do
VCR.use_cassette('archive/new_file_to_get_200') do
subject
File.write('tmp.zip', subject.download, mode: 'wb')
File.open('tmp.zip') do |fd|
files = ZipTricks::FileReader.read_zip_structure(io: fd)
base_fn = "export"
structure = [
"#{base_fn}/",
"#{base_fn}/dossier-#{dossier.id}/",
"#{base_fn}/dossier-#{dossier.id}/piece_justificative-#{dossier.id}.txt",
"#{base_fn}/dossier-#{dossier.id}/export_#{dossier.id}.pdf"
]
expect(files.size).to eq(structure.size)
expect(files.map(&:filename)).to match_array(structure)
end
FileUtils.remove_entry_secure('tmp.zip')
end
end
end
it 'returns a blob with valid files' do
VCR.use_cassette('archive/new_file_to_get_200') do
subject
context 'with files (and http calls)' do
let!(:dossier) { create(:dossier, :accepte, :with_populated_champs, :with_individual, procedure: procedure) }
let(:dossier_exports) { PiecesJustificativesService.new(user_profile: instructeur, export_template: nil).generate_dossiers_export(Dossier.where(id: dossier)) }
before do
allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/d0e7862b24d8026a3c03516d865b28151eb3859029c6c6c2e86605891fbdcd7a/socketry/async-io")
end
File.write('tmp.zip', subject.download, mode: 'wb')
File.open('tmp.zip') do |fd|
files = ZipTricks::FileReader.read_zip_structure(io: fd)
base_fn = 'export'
structure = [
"#{base_fn}/",
"#{base_fn}/dossier-#{dossier.id}/",
"#{base_fn}/dossier-#{dossier.id}/pieces_justificatives/",
"#{base_fn}/dossier-#{dossier.id}/#{ActiveStorage::DownloadableFile.timestamped_filename(ActiveStorage::Attachment.where(record_type: "Champ").first)}",
"#{base_fn}/dossier-#{dossier.id}/#{ActiveStorage::DownloadableFile.timestamped_filename(dossier_exports.first.first)}"
]
expect(files.size).to eq(structure.size)
expect(files.map(&:filename)).to match_array(structure)
it 'returns a blob with valid files' do
VCR.use_cassette('archive/new_file_to_get_200') do
subject
File.write('tmp.zip', subject.download, mode: 'wb')
File.open('tmp.zip') do |fd|
files = ZipTricks::FileReader.read_zip_structure(io: fd)
base_fn = 'export'
structure = [
"#{base_fn}/",
"#{base_fn}/dossier-#{dossier.id}/",
"#{base_fn}/dossier-#{dossier.id}/pieces_justificatives/",
"#{base_fn}/dossier-#{dossier.id}/#{ActiveStorage::DownloadableFile.timestamped_filename(ActiveStorage::Attachment.where(record_type: "Champ").first)}",
"#{base_fn}/dossier-#{dossier.id}/#{ActiveStorage::DownloadableFile.timestamped_filename(dossier_exports.first.first)}"
]
expect(files.size).to eq(structure.size)
expect(files.map(&:filename)).to match_array(structure)
end
FileUtils.remove_entry_secure('tmp.zip')
end
FileUtils.remove_entry_secure('tmp.zip')
end
end
end