2021-03-29 22:15:49 +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") }
|
2021-04-21 16:54:15 +02:00
|
|
|
describe '#create_pending_archive' do
|
2021-03-29 22:15:49 +02:00
|
|
|
|
2021-04-21 16:54:15 +02:00
|
|
|
context 'for a specific month' do
|
|
|
|
it 'creates a pending archive' do
|
|
|
|
archive = service.create_pending_archive(instructeur, 'monthly', date_month)
|
|
|
|
|
|
|
|
expect(archive.content_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.content_type).to eq 'everything'
|
|
|
|
expect(archive.month).to eq nil
|
|
|
|
expect(archive.pending?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#collect_files_archive' do
|
2021-03-29 22:15:49 +02:00
|
|
|
before do
|
|
|
|
create_dossier_for_month(year, month)
|
|
|
|
create_dossier_for_month(2020, month)
|
|
|
|
end
|
|
|
|
|
|
|
|
after { Timecop.return }
|
|
|
|
|
|
|
|
context 'for a specific month' do
|
2021-04-21 16:54:15 +02:00
|
|
|
let(:archive) { create(:archive, content_type: 'monthly', status: 'pending', month: date_month) }
|
2021-03-29 22:15:49 +02:00
|
|
|
let(:year) { 2021 }
|
|
|
|
let(:mailer) { double('mailer', deliver_later: true) }
|
|
|
|
|
2021-04-21 16:54:15 +02:00
|
|
|
it 'collect files' do
|
2021-03-29 22:15:49 +02:00
|
|
|
expect(InstructeurMailer).to receive(:send_archive).and_return(mailer)
|
|
|
|
|
2021-04-21 16:54:15 +02:00
|
|
|
service.collect_files_archive(archive, instructeur)
|
2021-03-29 22:15:49 +02:00
|
|
|
|
|
|
|
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
|
2021-04-21 16:54:15 +02:00
|
|
|
let(:archive) { create(:archive, content_type: 'everything', status: 'pending') }
|
2021-03-29 22:15:49 +02:00
|
|
|
let(:mailer) { double('mailer', deliver_later: true) }
|
|
|
|
|
2021-04-21 16:54:15 +02:00
|
|
|
it 'collect files' do
|
2021-03-29 22:15:49 +02:00
|
|
|
expect(InstructeurMailer).to receive(:send_archive).and_return(mailer)
|
|
|
|
|
2021-04-21 16:54:15 +02:00
|
|
|
service.collect_files_archive(archive, instructeur)
|
2021-03-29 22:15:49 +02:00
|
|
|
|
|
|
|
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
|