feat(Administrateur::ArchivesControllers): add archives #index and #create for admin

This commit is contained in:
Martin 2022-06-23 13:51:58 +02:00 committed by mfo
parent f1f55c3e81
commit 7fb85c97cb
23 changed files with 270 additions and 135 deletions

View file

@ -0,0 +1,43 @@
describe Administrateurs::ArchivesController, type: :controller do
let(:admin) { create(:administrateur) }
let(:procedure) { create :procedure, administrateur: admin, groupe_instructeurs: [groupe_instructeur1, groupe_instructeur2] }
let(:groupe_instructeur1) { create(:groupe_instructeur) }
let(:groupe_instructeur2) { create(:groupe_instructeur) }
describe 'GET #index' do
subject { get :index, params: { procedure_id: procedure.id } }
context 'when logged out' do
it { is_expected.to have_http_status(302) }
end
context 'when logged in' do
before do
sign_in(admin.user)
end
it { is_expected.to have_http_status(200) }
it 'use all procedure.groupe_instructeurs' do
expect(Archive).to receive(:for_groupe_instructeur).with([groupe_instructeur1, groupe_instructeur2]).and_return([])
subject
end
end
end
describe 'GET #create' do
subject { post :create, params: { procedure_id: procedure.id, month: '22-06', type: 'monthly' } }
context 'when logged out' do
it { is_expected.to have_http_status(302) }
end
context 'when logged in' do
before do
sign_in(admin.user)
end
it { is_expected.to redirect_to(admin_procedure_archives_path(procedure)) }
it 'enqueue the creation job' do
expect { subject }.to have_enqueued_job(ArchiveCreationJob).with(procedure, an_instance_of(Archive), admin)
end
end
end
end

View file

@ -31,14 +31,12 @@ describe Instructeurs::ArchivesController, type: :controller do
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 { subject }.to have_enqueued_job(ArchiveCreationJob).with(procedure1, an_instance_of(Archive), instructeur)
expect(flash.notice).to include("Votre demande a été prise en compte")
end
end

View file

@ -8,7 +8,7 @@ describe ArchiveCreationJob, type: :job do
context 'when it fails' do
let(:status) { :pending }
let(:mailer) { double('mailer', deliver_later: true) }
before { expect(InstructeurMailer).not_to receive(:send_archive) }
before { expect(UserMailer).not_to receive(:send_archive) }
it 'does not send email and forward error for retry' do
allow(DownloadableFileService).to receive(:download_and_zip).and_raise(StandardError, "kaboom")
@ -21,7 +21,7 @@ describe ArchiveCreationJob, type: :job do
let(:mailer) { double('mailer', deliver_later: true) }
before do
allow(DownloadableFileService).to receive(:download_and_zip).and_return(true)
expect(InstructeurMailer).to receive(:send_archive).and_return(mailer)
expect(UserMailer).to receive(:send_archive).and_return(mailer)
end
context 'when archive failed previously' do

View file

@ -16,6 +16,10 @@ class UserMailerPreview < ActionMailer::Preview
UserMailer.france_connect_merge_confirmation('new.exemple.fr', '123456', 15.minutes.from_now)
end
def send_archive
UserMailer.send_archive(Instructeur.first, Procedure.first, Archive.first)
end
private
def user

View file

@ -35,4 +35,24 @@ RSpec.describe UserMailer, type: :mailer do
it { expect(subject.to).to eq([email]) }
it { expect(subject.body).to include(france_connect_particulier_mail_merge_with_existing_account_url(merge_token: code)) }
end
describe '.send_archive' do
let(:procedure) { create(:procedure) }
let(:archive) { create(:archive) }
subject { described_class.send_archive(role, procedure, archive) }
context 'instructeur' do
let(:role) { create(:instructeur) }
it { expect(subject.to).to eq([role.user.email]) }
it { expect(subject.body).to have_link('Consulter mes archives', href: instructeur_archives_url(procedure)) }
it { expect(subject.body).to have_link("#{procedure.id} #{procedure.libelle}", href: instructeur_procedure_url(procedure)) }
end
context 'instructeur' do
let(:role) { create(:administrateur) }
it { expect(subject.to).to eq([role.user.email]) }
it { expect(subject.body).to have_link('Consulter mes archives', href: admin_procedure_archives_url(procedure)) }
it { expect(subject.body).to have_link("#{procedure.id} #{procedure.libelle}", href: admin_procedure_url(procedure)) }
end
end
end

View file

@ -11,28 +11,6 @@ describe ProcedureArchiveService do
procedure.defaut_groupe_instructeur.add(instructeur)
end
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 '#make_and_upload_archive' do
let!(:dossier) { create_dossier_for_month(year, month) }
let!(:dossier_2020) { create_dossier_for_month(2020, month) }
@ -47,7 +25,7 @@ describe ProcedureArchiveService do
allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/d0e7862b24d8026a3c03516d865b28151eb3859029c6c6c2e86605891fbdcd7a/socketry/async-io")
VCR.use_cassette('archive/new_file_to_get_200') do
service.make_and_upload_archive(archive, instructeur)
service.make_and_upload_archive(archive)
end
archive.file.open do |f|
@ -69,7 +47,7 @@ describe ProcedureArchiveService do
allow_any_instance_of(ActiveStorage::Attached::One).to receive(:url).and_return("https://www.demarches-simplifiees.fr/error_1")
VCR.use_cassette('archive/new_file_to_get_400.html') do
service.make_and_upload_archive(archive, instructeur)
service.make_and_upload_archive(archive)
end
archive.file.open do |f|
files = ZipTricks::FileReader.read_zip_structure(io: f)
@ -112,11 +90,11 @@ describe ProcedureArchiveService do
end
it 'collect files without raising exception' do
expect { service.make_and_upload_archive(archive, instructeur) }.not_to raise_exception
expect { service.make_and_upload_archive(archive) }.not_to raise_exception
end
it 'add bug report to archive' do
service.make_and_upload_archive(archive, instructeur)
service.make_and_upload_archive(archive)
archive.file.open do |f|
zip_entries = ZipTricks::FileReader.read_zip_structure(io: f)
@ -148,7 +126,7 @@ describe ProcedureArchiveService do
allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/5e61989aecb78e369c93674f877d7bf4ecde378850114a9563cdf8b6a2472536/typhoeus/typhoeus/issues/110")
VCR.use_cassette('archive/old_file_to_get_200') do
service.make_and_upload_archive(archive, instructeur)
service.make_and_upload_archive(archive)
end
archive = Archive.last

View file

@ -0,0 +1,36 @@
require 'system/administrateurs/procedure_spec_helper'
describe 'Creating a new procedure', js: true do
include ProcedureSpecHelper
let(:administrateur) { create(:administrateur) }
let(:procedure) do
create(:procedure, :with_service, :with_instructeur,
aasm_state: :publiee,
administrateurs: [administrateur],
libelle: 'libellé de la procédure',
path: 'libelle-de-la-procedure')
end
let!(:dossiers) do
create(:dossier, :accepte, procedure: procedure)
end
before { login_as administrateur.user, scope: :user }
scenario "download archive" do
visit admin_procedure_path(id: procedure.id)
# check button
expect(page).to have_selector('#archive-procedure')
click_on "Télécharger"
# check page loading
expect(page).to have_content("Archives")
# check archive
expect {
page.first(".archive-table .button").click
}.to have_enqueued_job(ArchiveCreationJob).with(procedure, an_instance_of(Archive), administrateur)
expect(page).to have_content("Votre demande a été prise en compte. Selon le nombre de dossiers, cela peut prendre de quelques minutes a plusieurs heures. Vous recevrez un courriel lorsque le fichier sera disponible.")
end
end

View file

@ -24,6 +24,10 @@ describe 'administrateurs/procedures/show.html.haml', type: :view do
describe 'procedure path is not customized' do
it { expect(rendered).to have_content('Brouillon') }
end
describe 'archive button' do
it { expect(rendered).not_to have_css('#archive-procedure') }
end
end
end
@ -38,6 +42,9 @@ describe 'administrateurs/procedures/show.html.haml', type: :view do
it { expect(rendered).not_to have_css('#publish-procedure-link') }
it { expect(rendered).to have_css('#close-procedure-link') }
end
describe 'archive button' do
it { expect(rendered).to have_css('#archive-procedure') }
end
end
describe 'procedure is closed' do