Revert "Revert "Export de tous les dossier d'une démarche""

This reverts commit d9a588b52e.
This commit is contained in:
Christophe Robillard 2021-04-29 17:29:47 +02:00
parent 533fa903bc
commit f40d96fbd2
32 changed files with 675 additions and 36 deletions

View file

@ -0,0 +1,56 @@
describe Instructeurs::ArchivesController, type: :controller do
let(:procedure1) { create(:procedure, :published, groupe_instructeurs: [gi1]) }
let(:procedure2) { create(:procedure, :published, groupe_instructeurs: [gi2]) }
let!(:instructeur) { create(:instructeur, groupe_instructeurs: [gi1, gi2]) }
let!(:archive1) { create(:archive, :generated, groupe_instructeurs: [gi1]) }
let!(:archive2) { create(:archive, :generated, groupe_instructeurs: [gi2]) }
let(:gi1) { create(:groupe_instructeur) }
let(:gi2) { create(:groupe_instructeur) }
before do
sign_in(instructeur.user)
Flipper.enable(:archive_zip_globale, procedure1)
end
after { Timecop.return }
describe '#index' do
before do
create_dossier_for_month(procedure1, 2021, 3)
create_dossier_for_month(procedure1, 2021, 3)
create_dossier_for_month(procedure1, 2021, 2)
Timecop.freeze(Time.zone.local(2021, 3, 5))
end
it 'displays archives' do
get :index, { params: { procedure_id: procedure1.id } }
expect(assigns(:dossiers_termines).size).to eq(3)
expect(assigns(:archives)).to eq([archive1])
end
end
describe '#create' do
let(:month) { '21-03' }
let(:date_month) { Date.strptime(month, "%Y-%m") }
let(:archive) { create(:archive) }
let(:subject) do
post :create, {
params: { procedure_id: procedure1.id, type: 'monthly', month: month }
}
end
it "performs archive creation job" do
allow_any_instance_of(ProcedureArchiveService).to receive(:create_pending_archive).and_return(archive)
expect { subject }.to have_enqueued_job(ArchiveCreationJob).with(procedure1, archive, instructeur)
expect(flash.notice).to include("Votre demande a été prise en compte")
end
end
private
def create_dossier_for_month(procedure, year, month)
Timecop.freeze(Time.zone.local(year, month, 5))
create(:dossier, :accepte, :with_attestation, procedure: procedure)
end
end

View file

@ -712,13 +712,6 @@ describe Instructeurs::DossiersController, type: :controller do
dossier_id: dossier.id
}
end
context 'when zip download is disabled through flipflop' do
it 'is forbidden' do
subject
expect(response).to have_http_status(:forbidden)
end
end
end
describe "#delete_dossier" do

15
spec/factories/archive.rb Normal file
View file

@ -0,0 +1,15 @@
FactoryBot.define do
factory :archive do
time_span_type { 'everything' }
groupe_instructeurs { [association(:groupe_instructeur)] }
key { 'unique-key' }
trait :pending do
status { 'pending' }
end
trait :generated do
status { 'generated' }
end
end
end

View file

@ -12,6 +12,8 @@ feature 'Inviting an expert:', js: true do
context 'as an Instructeur' do
scenario 'I can invite an expert' do
allow(ClamavService).to receive(:safe_file?).and_return(true)
# assign instructeur to linked dossier
instructeur.assign_to_procedure(linked_dossier.procedure)

View file

@ -165,10 +165,10 @@ feature 'Instructing a dossier:', js: true do
expect(DownloadHelpers.download).to include "dossier-#{dossier.id}.zip"
expect(files.size).to be 3
expect(files[0].filename.include?('piece_justificative_0')).to be_truthy
expect(files[0].uncompressed_size).to be File.size(path)
expect(files[1].filename.include?('horodatage/operation')).to be_truthy
expect(files[2].filename.include?('dossier/export')).to be_truthy
expect(files[0].filename.include?('export')).to be_truthy
expect(files[1].filename.include?('piece_justificative_0')).to be_truthy
expect(files[1].uncompressed_size).to be File.size(path)
expect(files[2].filename.include?('horodatage/operation')).to be_truthy
end
scenario 'A instructeur can download an archive containing several identical attachments' do
@ -180,13 +180,13 @@ feature 'Instructing a dossier:', js: true do
expect(DownloadHelpers.download).to include "dossier-#{dossier.id}.zip"
expect(files.size).to be 4
expect(files[0].filename.include?('piece_justificative_0')).to be_truthy
expect(files[0].filename.include?('export')).to be_truthy
expect(files[1].filename.include?('piece_justificative_0')).to be_truthy
expect(files[0].filename).not_to eq files[1].filename
expect(files[0].uncompressed_size).to be File.size(path)
expect(files[2].filename.include?('piece_justificative_0')).to be_truthy
expect(files[1].filename).not_to eq files[2].filename
expect(files[1].uncompressed_size).to be File.size(path)
expect(files[2].filename.include?('horodatage/operation')).to be_truthy
expect(files[3].filename.include?('dossier/export')).to be_truthy
expect(files[2].uncompressed_size).to be File.size(path)
expect(files[3].filename.include?('horodatage/operation')).to be_truthy
end
before { DownloadHelpers.clear_downloads }

View file

@ -0,0 +1,51 @@
describe Dossier do
include ActiveJob::TestHelper
before { Timecop.freeze(Time.zone.now) }
after { Timecop.return }
let(:archive) { create(:archive) }
describe 'scopes' do
describe 'staled' do
let(:recent_archive) { create(:archive) }
let(:staled_archive) { create(:archive, updated_at: (Archive::RETENTION_DURATION + 2).days.ago) }
subject do
archive; recent_archive; staled_archive
Archive.stale
end
it { is_expected.to match_array([staled_archive]) }
end
end
describe '.status' do
it { expect(archive.status).to eq('pending') }
end
describe '#make_available!' do
before { archive.make_available! }
it { expect(archive.status).to eq('generated') }
end
describe '#available?' do
subject { archive.available? }
context 'without attachment' do
let(:archive) { create(:archive, file: nil) }
it { is_expected.to eq(false) }
end
context 'with an attachment' do
context 'when the attachment was created but the process was not over' do
let(:archive) { create(:archive, :pending, file: Rack::Test::UploadedFile.new('spec/fixtures/files/file.pdf', 'application/pdf')) }
it { is_expected.to eq(false) }
end
context 'when the attachment was created but the process was not over' do
let(:archive) { create(:archive, :generated, file: Rack::Test::UploadedFile.new('spec/fixtures/files/file.pdf', 'application/pdf')) }
it { is_expected.to eq(true) }
end
end
end
end

View file

@ -16,7 +16,12 @@ describe PiecesJustificativesService do
# to be exported
it 'ensures no titre identite is given' do
expect(champ_identite.piece_justificative_file).to be_attached
expect(subject).to eq([])
expect(subject.any? { |piece| piece.name == 'piece_justificative_file' }).to be_falsy
end
it 'returns export pdf of the dossier' do
expect(champ_identite.piece_justificative_file).to be_attached
expect(subject.any? { |piece| piece.name == 'pdf_export_for_instructeur' }).to be_truthy
end
end
end

View file

@ -0,0 +1,83 @@
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") }
describe '#create_pending_archive' do
context 'for a specific month' do
it 'creates a pending archive' do
archive = service.create_pending_archive(instructeur, 'monthly', date_month)
expect(archive.time_span_type).to eq 'monthly'
expect(archive.month).to eq date_month
expect(archive.pending?).to be_truthy
end
end
context 'for all months' do
it 'creates a pending archive' do
archive = service.create_pending_archive(instructeur, 'everything')
expect(archive.time_span_type).to eq 'everything'
expect(archive.month).to eq nil
expect(archive.pending?).to be_truthy
end
end
end
describe '#collect_files_archive' do
before do
create_dossier_for_month(year, month)
create_dossier_for_month(2020, month)
end
after { Timecop.return }
context 'for a specific month' do
let(:archive) { create(:archive, time_span_type: 'monthly', status: 'pending', month: date_month) }
let(:year) { 2021 }
let(:mailer) { double('mailer', deliver_later: true) }
it 'collect files' do
expect(InstructeurMailer).to receive(:send_archive).and_return(mailer)
service.collect_files_archive(archive, instructeur)
archive.file.open do |f|
files = ZipTricks::FileReader.read_zip_structure(io: f)
expect(files.size).to be 2
expect(files.first.filename).to include("export")
expect(files.last.filename).to include("attestation")
end
expect(archive.file.attached?).to be_truthy
end
end
context 'for all months' do
let(:archive) { create(:archive, time_span_type: 'everything', status: 'pending') }
let(:mailer) { double('mailer', deliver_later: true) }
it 'collect files' do
expect(InstructeurMailer).to receive(:send_archive).and_return(mailer)
service.collect_files_archive(archive, instructeur)
archive = Archive.last
archive.file.open do |f|
files = ZipTricks::FileReader.read_zip_structure(io: f)
expect(files.size).to be 4
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
end

View file

@ -12,5 +12,15 @@ describe 'instructeurs/procedures/_download_dossiers.html.haml', type: :view do
context "when procedure has at least 1 dossier" do
let(:dossier_count) { 1 }
it { is_expected.to include("Télécharger tous les dossiers") }
context "With zip archive enabled" do
before { Flipper.enable(:archive_zip_globale, procedure) }
it { is_expected.to include("Télécharger une archive au format .zip") }
end
context "With zip archive disabled" do
before { Flipper.disable(:archive_zip_globale, procedure) }
it { is_expected.not_to include("Télécharger une archive au format .zip") }
end
end
end