2023-11-07 07:24:57 +01:00
|
|
|
describe Expired::DossiersDeletionService do
|
2020-04-02 15:07:30 +02:00
|
|
|
let(:warning_period) { 1.month + 5.days }
|
|
|
|
let(:conservation_par_defaut) { 3.months }
|
|
|
|
let(:user) { create(:user) }
|
2021-12-02 14:21:39 +01:00
|
|
|
let(:procedure_opts) { {} }
|
|
|
|
let(:procedure) { create(:procedure, :published, procedure_opts) }
|
2024-06-01 22:50:48 +02:00
|
|
|
let(:procedure_2) { create(:procedure, :published, :new_administrateur, procedure_opts) }
|
2020-05-05 12:41:45 +02:00
|
|
|
let(:reference_date) { Date.parse("March 8") }
|
2023-11-07 07:24:57 +01:00
|
|
|
let(:service) { Expired::DossiersDeletionService.new }
|
2020-02-26 17:28:39 +01:00
|
|
|
describe '#process_expired_dossiers_brouillon' do
|
2022-05-17 10:14:43 +02:00
|
|
|
before { Timecop.freeze(reference_date) }
|
|
|
|
after { Timecop.return }
|
|
|
|
|
2021-12-21 17:20:32 +01:00
|
|
|
let(:today) { Time.zone.now.at_beginning_of_day }
|
|
|
|
let(:date_close_to_expiration) { today - procedure.duree_conservation_dossiers_dans_ds.months + 13.days }
|
|
|
|
let(:date_expired) { today - procedure.duree_conservation_dossiers_dans_ds.months - 6.days }
|
|
|
|
let(:date_not_expired) { today - procedure.duree_conservation_dossiers_dans_ds.months + 2.months }
|
2020-02-26 17:28:39 +01:00
|
|
|
|
|
|
|
context 'send messages for dossiers expiring soon and delete expired' do
|
2021-03-02 12:14:55 +01:00
|
|
|
let!(:expired_brouillon) { create(:dossier, procedure: procedure, created_at: date_expired, brouillon_close_to_expiration_notice_sent_at: today - (warning_period + 3.days)) }
|
2020-02-26 17:28:39 +01:00
|
|
|
let!(:brouillon_close_to_expiration) { create(:dossier, procedure: procedure, created_at: date_close_to_expiration) }
|
|
|
|
let!(:brouillon_close_but_with_notice_sent) { create(:dossier, procedure: procedure, created_at: date_close_to_expiration, brouillon_close_to_expiration_notice_sent_at: Time.zone.now) }
|
|
|
|
let!(:valid_brouillon) { create(:dossier, procedure: procedure, created_at: date_not_expired) }
|
|
|
|
|
|
|
|
before do
|
2020-05-18 16:24:08 +02:00
|
|
|
allow(DossierMailer).to receive(:notify_brouillon_near_deletion).and_call_original
|
|
|
|
allow(DossierMailer).to receive(:notify_brouillon_deletion).and_call_original
|
2020-02-26 17:28:39 +01:00
|
|
|
|
2023-06-26 10:37:32 +02:00
|
|
|
service.process_expired_dossiers_brouillon
|
2020-02-26 17:28:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'emails should be sent' do
|
|
|
|
expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).once
|
2020-03-19 09:49:25 +01:00
|
|
|
expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).with([brouillon_close_to_expiration], brouillon_close_to_expiration.user.email)
|
2020-02-26 17:28:39 +01:00
|
|
|
end
|
|
|
|
|
2020-04-02 15:07:30 +02:00
|
|
|
it 'dossier brouillon_close_to_expiration_notice_sent_at should change' do
|
2020-02-26 17:28:39 +01:00
|
|
|
expect(brouillon_close_to_expiration.reload.brouillon_close_to_expiration_notice_sent_at).not_to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'deletes and notify expired brouillon' do
|
|
|
|
expect(DossierMailer).to have_received(:notify_brouillon_deletion).once
|
2020-03-19 09:49:25 +01:00
|
|
|
expect(DossierMailer).to have_received(:notify_brouillon_deletion).with([expired_brouillon.hash_for_deletion_mail], expired_brouillon.user.email)
|
2020-03-19 11:53:25 +01:00
|
|
|
expect(DeletedDossier.find_by(dossier_id: expired_brouillon.id)).not_to be_present
|
2020-02-26 17:28:39 +01:00
|
|
|
expect { expired_brouillon.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
|
|
|
end
|
2020-03-04 17:35:25 +01:00
|
|
|
end
|
|
|
|
|
2020-03-05 10:17:59 +01:00
|
|
|
describe '#send_brouillon_expiration_notices' do
|
2020-05-05 12:41:45 +02:00
|
|
|
before { Timecop.freeze(reference_date) }
|
2020-03-05 10:17:59 +01:00
|
|
|
after { Timecop.return }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(DossierMailer).to receive(:notify_brouillon_near_deletion).and_return(double(deliver_later: nil))
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a single dossier' do
|
2020-04-02 15:07:30 +02:00
|
|
|
let!(:dossier) { create(:dossier, procedure: procedure, created_at: created_at) }
|
2020-03-05 10:17:59 +01:00
|
|
|
|
2023-06-26 10:37:32 +02:00
|
|
|
before { service.send_brouillon_expiration_notices }
|
2020-03-05 10:17:59 +01:00
|
|
|
|
2021-08-26 10:14:23 +02:00
|
|
|
context 'when the dossier is not close to expiration' do
|
|
|
|
let(:created_at) { (conservation_par_defaut - 2.weeks - 1.day).ago }
|
2020-03-05 10:17:59 +01:00
|
|
|
|
|
|
|
it { expect(dossier.reload.brouillon_close_to_expiration_notice_sent_at).to be_nil }
|
|
|
|
it { expect(DossierMailer).not_to have_received(:notify_brouillon_near_deletion) }
|
|
|
|
end
|
|
|
|
|
2021-08-26 10:14:23 +02:00
|
|
|
context 'when the dossier is close to expiration' do
|
|
|
|
let(:created_at) { (conservation_par_defaut - 2.weeks + 1.day).ago }
|
2020-03-05 10:17:59 +01:00
|
|
|
|
|
|
|
it { expect(dossier.reload.brouillon_close_to_expiration_notice_sent_at).not_to be_nil }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).once }
|
2020-03-19 09:49:25 +01:00
|
|
|
it { expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).with([dossier], dossier.user.email) }
|
2020-03-05 10:17:59 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with 2 dossiers to notice' do
|
2021-08-26 10:14:23 +02:00
|
|
|
let!(:dossier_1) { create(:dossier, procedure: procedure, user: user, created_at: (conservation_par_defaut - 2.weeks + 1.day).ago) }
|
|
|
|
let!(:dossier_2) { create(:dossier, procedure: procedure_2, user: user, created_at: (conservation_par_defaut - 2.weeks + 1.day).ago) }
|
2020-03-05 10:17:59 +01:00
|
|
|
|
2023-06-26 10:37:32 +02:00
|
|
|
before { service.send_brouillon_expiration_notices }
|
2020-03-05 10:17:59 +01:00
|
|
|
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).once }
|
2020-03-19 09:49:25 +01:00
|
|
|
it { expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).with(match_array([dossier_1, dossier_2]), user.email) }
|
2020-03-05 10:17:59 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#delete_expired_brouillons_and_notify' do
|
2020-05-05 12:41:45 +02:00
|
|
|
before { Timecop.freeze(reference_date) }
|
2020-03-05 10:17:59 +01:00
|
|
|
after { Timecop.return }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(DossierMailer).to receive(:notify_brouillon_deletion).and_return(double(deliver_later: nil))
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a single dossier' do
|
2020-04-02 15:07:30 +02:00
|
|
|
let!(:dossier) { create(:dossier, procedure: procedure, brouillon_close_to_expiration_notice_sent_at: notice_sent_at) }
|
2020-03-05 10:17:59 +01:00
|
|
|
|
2023-06-26 10:37:32 +02:00
|
|
|
before { service.delete_expired_brouillons_and_notify }
|
2020-03-05 10:17:59 +01:00
|
|
|
|
|
|
|
context 'when no notice has been sent' do
|
|
|
|
let(:notice_sent_at) { nil }
|
|
|
|
|
|
|
|
it { expect { dossier.reload }.not_to raise_error }
|
|
|
|
it { expect(DossierMailer).not_to have_received(:notify_brouillon_deletion) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a notice has been sent not so long ago' do
|
2020-03-31 11:05:43 +02:00
|
|
|
let(:notice_sent_at) { (warning_period - 4.days).ago }
|
2020-03-05 10:17:59 +01:00
|
|
|
|
|
|
|
it { expect { dossier.reload }.not_to raise_error }
|
|
|
|
it { expect(DossierMailer).not_to have_received(:notify_brouillon_deletion) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a notice has been sent a long time ago' do
|
2020-03-31 11:05:43 +02:00
|
|
|
let(:notice_sent_at) { (warning_period + 4.days).ago }
|
2020-03-05 10:17:59 +01:00
|
|
|
|
|
|
|
it { expect { dossier.reload }.to raise_error(ActiveRecord::RecordNotFound) }
|
|
|
|
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_brouillon_deletion).once }
|
2020-03-19 09:49:25 +01:00
|
|
|
it { expect(DossierMailer).to have_received(:notify_brouillon_deletion).with([dossier.hash_for_deletion_mail], dossier.user.email) }
|
2020-03-05 10:17:59 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with 2 dossiers to delete' do
|
2020-04-02 15:07:30 +02:00
|
|
|
let!(:dossier_1) { create(:dossier, procedure: procedure, user: user, brouillon_close_to_expiration_notice_sent_at: (warning_period + 1.day).ago) }
|
|
|
|
let!(:dossier_2) { create(:dossier, procedure: procedure_2, user: user, brouillon_close_to_expiration_notice_sent_at: (warning_period + 1.day).ago) }
|
2020-03-05 10:17:59 +01:00
|
|
|
|
2023-06-26 10:37:32 +02:00
|
|
|
before { service.delete_expired_brouillons_and_notify }
|
2020-03-05 10:17:59 +01:00
|
|
|
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_brouillon_deletion).once }
|
2020-03-19 09:49:25 +01:00
|
|
|
it { expect(DossierMailer).to have_received(:notify_brouillon_deletion).with(match_array([dossier_1.hash_for_deletion_mail, dossier_2.hash_for_deletion_mail]), user.email) }
|
2020-03-05 10:17:59 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-04 18:33:32 +01:00
|
|
|
describe '#send_en_construction_expiration_notices' do
|
2020-05-05 12:41:45 +02:00
|
|
|
before { Timecop.freeze(reference_date) }
|
2020-03-04 18:33:32 +01:00
|
|
|
after { Timecop.return }
|
|
|
|
|
|
|
|
before do
|
2020-04-01 17:08:50 +02:00
|
|
|
allow(DossierMailer).to receive(:notify_near_deletion_to_user).and_return(double(deliver_later: nil))
|
|
|
|
allow(DossierMailer).to receive(:notify_near_deletion_to_administration).and_return(double(deliver_later: nil))
|
2020-03-04 18:33:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a single dossier' do
|
2020-04-02 15:07:30 +02:00
|
|
|
let!(:dossier) { create(:dossier, :en_construction, :followed, procedure: procedure, en_construction_at: en_construction_at) }
|
2020-03-04 18:33:32 +01:00
|
|
|
|
2023-06-26 10:37:32 +02:00
|
|
|
before { service.send_en_construction_expiration_notices }
|
2020-03-04 18:33:32 +01:00
|
|
|
|
|
|
|
context 'when the dossier is not near deletion' do
|
2021-08-26 10:14:23 +02:00
|
|
|
let(:en_construction_at) { (conservation_par_defaut - 2.weeks - 1.day).ago }
|
2020-03-04 18:33:32 +01:00
|
|
|
|
|
|
|
it { expect(dossier.reload.en_construction_close_to_expiration_notice_sent_at).to be_nil }
|
2020-04-01 17:08:50 +02:00
|
|
|
it { expect(DossierMailer).not_to have_received(:notify_near_deletion_to_user) }
|
|
|
|
it { expect(DossierMailer).not_to have_received(:notify_near_deletion_to_administration) }
|
2020-03-04 18:33:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the dossier is near deletion' do
|
2021-08-26 10:14:23 +02:00
|
|
|
let(:en_construction_at) { (conservation_par_defaut - 2.weeks + 1.day).ago }
|
2020-03-04 18:33:32 +01:00
|
|
|
|
|
|
|
it { expect(dossier.reload.en_construction_close_to_expiration_notice_sent_at).not_to be_nil }
|
|
|
|
|
2020-04-01 17:08:50 +02:00
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_user).once }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_administration).twice }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_user).with([dossier], dossier.user.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_administration).with([dossier], dossier.procedure.administrateurs.first.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_administration).with([dossier], dossier.followers_instructeurs.first.email) }
|
2020-03-04 18:33:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with 2 dossiers to notice' do
|
2021-08-26 10:14:23 +02:00
|
|
|
let!(:dossier_1) { create(:dossier, :en_construction, procedure: procedure, user: user, en_construction_at: (conservation_par_defaut - 2.weeks + 1.day).ago) }
|
|
|
|
let!(:dossier_2) { create(:dossier, :en_construction, procedure: procedure_2, user: user, en_construction_at: (conservation_par_defaut - 2.weeks + 1.day).ago) }
|
2020-03-04 18:33:32 +01:00
|
|
|
|
|
|
|
let!(:instructeur) { create(:instructeur) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
instructeur.followed_dossiers << dossier_1 << dossier_2
|
2023-06-26 10:37:32 +02:00
|
|
|
service.send_en_construction_expiration_notices
|
2020-03-04 18:33:32 +01:00
|
|
|
end
|
|
|
|
|
2020-04-01 17:08:50 +02:00
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_user).once }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_administration).exactly(3).times }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_user).with(match_array([dossier_1, dossier_2]), user.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_administration).with(match_array([dossier_1, dossier_2]), instructeur.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_administration).with([dossier_1], dossier_1.procedure.administrateurs.first.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_administration).with([dossier_2], dossier_2.procedure.administrateurs.first.email) }
|
2020-03-04 18:33:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when an instructeur is also administrateur' do
|
|
|
|
let!(:administrateur) { procedure.administrateurs.first }
|
2021-08-26 10:14:23 +02:00
|
|
|
let!(:dossier) { create(:dossier, :en_construction, procedure: procedure, en_construction_at: (conservation_par_defaut - 2.weeks + 1.day).ago) }
|
2020-03-04 18:33:32 +01:00
|
|
|
|
|
|
|
before do
|
|
|
|
administrateur.instructeur.followed_dossiers << dossier
|
2023-06-26 10:37:32 +02:00
|
|
|
service.send_en_construction_expiration_notices
|
2020-03-04 18:33:32 +01:00
|
|
|
end
|
|
|
|
|
2020-04-01 17:08:50 +02:00
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_user).once }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_user).with([dossier], dossier.user.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_administration).with([dossier], administrateur.email) }
|
2020-03-04 18:33:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-04 17:35:25 +01:00
|
|
|
describe '#delete_expired_en_construction_and_notify' do
|
|
|
|
let!(:warning_period) { 1.month + 5.days }
|
|
|
|
|
2020-05-05 12:41:45 +02:00
|
|
|
before { Timecop.freeze(reference_date) }
|
2020-03-04 17:35:25 +01:00
|
|
|
after { Timecop.return }
|
|
|
|
|
|
|
|
before do
|
2020-05-18 16:24:08 +02:00
|
|
|
allow(DossierMailer).to receive(:notify_automatic_deletion_to_user).and_call_original
|
|
|
|
allow(DossierMailer).to receive(:notify_automatic_deletion_to_administration).and_call_original
|
2020-03-04 17:35:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a single dossier' do
|
2020-04-02 15:07:30 +02:00
|
|
|
let!(:dossier) { create(:dossier, :en_construction, :followed, procedure: procedure, en_construction_close_to_expiration_notice_sent_at: notice_sent_at) }
|
2020-03-04 17:35:25 +01:00
|
|
|
|
2023-06-26 10:37:32 +02:00
|
|
|
before { service.delete_expired_en_construction_and_notify }
|
2020-03-04 17:35:25 +01:00
|
|
|
|
|
|
|
context 'when no notice has been sent' do
|
|
|
|
let(:notice_sent_at) { nil }
|
|
|
|
|
|
|
|
it { expect { dossier.reload }.not_to raise_error }
|
|
|
|
it { expect(DossierMailer).not_to have_received(:notify_automatic_deletion_to_user) }
|
|
|
|
it { expect(DossierMailer).not_to have_received(:notify_automatic_deletion_to_administration) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a notice has been sent not so long ago' do
|
2020-03-31 11:05:43 +02:00
|
|
|
let(:notice_sent_at) { (warning_period - 4.days).ago }
|
2020-03-04 17:35:25 +01:00
|
|
|
|
|
|
|
it { expect { dossier.reload }.not_to raise_error }
|
|
|
|
it { expect(DossierMailer).not_to have_received(:notify_automatic_deletion_to_user) }
|
|
|
|
it { expect(DossierMailer).not_to have_received(:notify_automatic_deletion_to_administration) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a notice has been sent a long time ago' do
|
2020-03-31 11:05:43 +02:00
|
|
|
let(:notice_sent_at) { (warning_period + 4.days).ago }
|
2020-03-04 17:35:25 +01:00
|
|
|
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_user).once }
|
2024-06-04 17:52:01 +02:00
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_user).with([dossier], dossier.user.email) }
|
2020-03-04 17:35:25 +01:00
|
|
|
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).twice }
|
2024-06-04 17:52:01 +02:00
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([dossier], dossier.procedure.administrateurs.first.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([dossier], dossier.followers_instructeurs.first.email) }
|
|
|
|
|
2024-07-15 18:02:54 +02:00
|
|
|
it { expect(dossier.reload.hidden_by_user_at).to eq(nil) }
|
|
|
|
it { expect(dossier.reload.hidden_by_administration_at).to eq(nil) }
|
|
|
|
it { expect(dossier.reload.hidden_by_expired_at).to be_an_instance_of(ActiveSupport::TimeWithZone) }
|
2024-06-04 17:52:01 +02:00
|
|
|
it { expect(dossier.reload.hidden_by_reason).to eq('expired') }
|
2020-03-04 17:35:25 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with 2 dossiers to delete' do
|
2020-04-02 15:07:30 +02:00
|
|
|
let!(:dossier_1) { create(:dossier, :en_construction, procedure: procedure, user: user, en_construction_close_to_expiration_notice_sent_at: (warning_period + 1.day).ago) }
|
|
|
|
let!(:dossier_2) { create(:dossier, :en_construction, procedure: procedure_2, user: user, en_construction_close_to_expiration_notice_sent_at: (warning_period + 1.day).ago) }
|
2020-03-04 17:35:25 +01:00
|
|
|
|
|
|
|
let!(:instructeur) { create(:instructeur) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
instructeur.followed_dossiers << dossier_1 << dossier_2
|
2023-06-26 10:37:32 +02:00
|
|
|
service.delete_expired_en_construction_and_notify
|
2020-03-04 17:35:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_user).once }
|
2024-06-04 17:52:01 +02:00
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_user).with(match_array([dossier_1, dossier_2]), user.email) }
|
2020-03-04 17:35:25 +01:00
|
|
|
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).thrice }
|
2024-06-04 17:52:01 +02:00
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with(match_array([dossier_1, dossier_2]), instructeur.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([dossier_1], dossier_1.procedure.administrateurs.first.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([dossier_2], dossier_2.procedure.administrateurs.first.email) }
|
|
|
|
|
2024-07-15 18:02:54 +02:00
|
|
|
it { expect(dossier_1.reload.hidden_by_expired_at).to be_an_instance_of(ActiveSupport::TimeWithZone) }
|
2024-06-04 17:52:01 +02:00
|
|
|
it { expect(dossier_1.reload.hidden_by_reason).to eq('expired') }
|
2024-07-15 18:02:54 +02:00
|
|
|
it { expect(dossier_2.reload.hidden_by_expired_at).to be_an_instance_of(ActiveSupport::TimeWithZone) }
|
2024-06-04 17:52:01 +02:00
|
|
|
it { expect(dossier_2.reload.hidden_by_reason).to eq('expired') }
|
2020-03-04 17:35:25 +01:00
|
|
|
end
|
2020-02-26 17:28:39 +01:00
|
|
|
end
|
2020-04-02 15:08:16 +02:00
|
|
|
|
|
|
|
describe '#send_termine_expiration_notices' do
|
2020-05-05 12:41:45 +02:00
|
|
|
before { Timecop.freeze(reference_date) }
|
2020-04-02 15:08:16 +02:00
|
|
|
after { Timecop.return }
|
2021-12-02 14:21:39 +01:00
|
|
|
let(:procedure_opts) do
|
|
|
|
{
|
|
|
|
procedure_expires_when_termine_enabled: true
|
|
|
|
}
|
2021-08-18 11:19:19 +02:00
|
|
|
end
|
2020-04-02 15:08:16 +02:00
|
|
|
before do
|
2020-05-18 16:24:08 +02:00
|
|
|
allow(DossierMailer).to receive(:notify_near_deletion_to_user).and_call_original
|
|
|
|
allow(DossierMailer).to receive(:notify_near_deletion_to_administration).and_call_original
|
2020-04-02 15:08:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a single dossier' do
|
2021-12-02 14:21:39 +01:00
|
|
|
let!(:dossier) { create(:dossier, :followed, state: :accepte, procedure: procedure, processed_at: processed_at) }
|
2020-04-02 15:08:16 +02:00
|
|
|
|
2023-06-26 10:37:32 +02:00
|
|
|
before { service.send_termine_expiration_notices }
|
2020-04-02 15:08:16 +02:00
|
|
|
|
|
|
|
context 'when the dossier is not near deletion' do
|
2021-08-26 10:14:23 +02:00
|
|
|
let(:processed_at) { (conservation_par_defaut - 2.weeks - 1.day).ago }
|
2020-04-02 15:08:16 +02:00
|
|
|
|
|
|
|
it { expect(dossier.reload.termine_close_to_expiration_notice_sent_at).to be_nil }
|
|
|
|
it { expect(DossierMailer).not_to have_received(:notify_near_deletion_to_user) }
|
|
|
|
it { expect(DossierMailer).not_to have_received(:notify_near_deletion_to_administration) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the dossier is near deletion' do
|
2021-08-26 10:14:23 +02:00
|
|
|
let(:processed_at) { (conservation_par_defaut - 2.weeks + 1.day).ago }
|
2020-04-02 15:08:16 +02:00
|
|
|
|
|
|
|
it { expect(dossier.reload.termine_close_to_expiration_notice_sent_at).not_to be_nil }
|
|
|
|
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_user).once }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_administration).twice }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_user).with([dossier], dossier.user.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_administration).with([dossier], dossier.procedure.administrateurs.first.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_administration).with([dossier], dossier.followers_instructeurs.first.email) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with 2 dossiers to notice' do
|
2021-12-02 14:21:39 +01:00
|
|
|
let!(:dossier_1) { create(:dossier, state: :accepte, procedure: procedure, user: user, processed_at: (conservation_par_defaut - 2.weeks + 1.day).ago) }
|
|
|
|
let!(:dossier_2) { create(:dossier, state: :accepte, procedure: procedure_2, user: user, processed_at: (conservation_par_defaut - 2.weeks + 1.day).ago) }
|
2020-04-02 15:08:16 +02:00
|
|
|
|
|
|
|
let!(:instructeur) { create(:instructeur) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
instructeur.followed_dossiers << dossier_1 << dossier_2
|
2023-06-26 10:37:32 +02:00
|
|
|
service.send_termine_expiration_notices
|
2020-04-02 15:08:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_user).once }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_administration).exactly(3).times }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_user).with(match_array([dossier_1, dossier_2]), user.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_administration).with(match_array([dossier_1, dossier_2]), instructeur.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_administration).with([dossier_1], dossier_1.procedure.administrateurs.first.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_administration).with([dossier_2], dossier_2.procedure.administrateurs.first.email) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when an instructeur is also administrateur' do
|
|
|
|
let!(:administrateur) { procedure.administrateurs.first }
|
2021-12-02 14:21:39 +01:00
|
|
|
let!(:dossier) { create(:dossier, state: :accepte, procedure: procedure, processed_at: (conservation_par_defaut - 2.weeks + 1.day).ago) }
|
2020-04-02 15:08:16 +02:00
|
|
|
|
|
|
|
before do
|
|
|
|
administrateur.instructeur.followed_dossiers << dossier
|
2023-06-26 10:37:32 +02:00
|
|
|
service.send_termine_expiration_notices
|
2020-04-02 15:08:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_user).once }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_user).with([dossier], dossier.user.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_near_deletion_to_administration).with([dossier], administrateur.email) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#delete_expired_termine_and_notify' do
|
2020-05-05 12:41:45 +02:00
|
|
|
before { Timecop.freeze(reference_date) }
|
2020-04-02 15:08:16 +02:00
|
|
|
after { Timecop.return }
|
|
|
|
|
2021-12-02 14:21:39 +01:00
|
|
|
let(:procedure_opts) do
|
|
|
|
{
|
|
|
|
procedure_expires_when_termine_enabled: true
|
|
|
|
}
|
2021-08-18 11:19:19 +02:00
|
|
|
end
|
|
|
|
|
2020-04-02 15:08:16 +02:00
|
|
|
before do
|
2020-05-18 16:24:08 +02:00
|
|
|
allow(DossierMailer).to receive(:notify_automatic_deletion_to_user).and_call_original
|
|
|
|
allow(DossierMailer).to receive(:notify_automatic_deletion_to_administration).and_call_original
|
2020-04-02 15:08:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a single dossier' do
|
2021-12-02 14:21:39 +01:00
|
|
|
let!(:dossier) { create(:dossier, :followed, :accepte, procedure: procedure, termine_close_to_expiration_notice_sent_at: notice_sent_at) }
|
2020-04-02 15:08:16 +02:00
|
|
|
|
2023-06-26 10:37:32 +02:00
|
|
|
before { service.delete_expired_termine_and_notify }
|
2020-04-02 15:08:16 +02:00
|
|
|
|
|
|
|
context 'when no notice has been sent' do
|
|
|
|
let(:notice_sent_at) { nil }
|
|
|
|
|
|
|
|
it { expect { dossier.reload }.not_to raise_error }
|
|
|
|
it { expect(DossierMailer).not_to have_received(:notify_automatic_deletion_to_user) }
|
|
|
|
it { expect(DossierMailer).not_to have_received(:notify_automatic_deletion_to_administration) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a notice has been sent not so long ago' do
|
|
|
|
let(:notice_sent_at) { (warning_period - 4.days).ago }
|
|
|
|
|
|
|
|
it { expect { dossier.reload }.not_to raise_error }
|
|
|
|
it { expect(DossierMailer).not_to have_received(:notify_automatic_deletion_to_user) }
|
|
|
|
it { expect(DossierMailer).not_to have_received(:notify_automatic_deletion_to_administration) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a notice has been sent a long time ago' do
|
|
|
|
let(:notice_sent_at) { (warning_period + 4.days).ago }
|
|
|
|
|
2024-07-15 18:02:54 +02:00
|
|
|
it { expect(dossier.reload.hidden_by_expired_at).to be_an_instance_of(ActiveSupport::TimeWithZone) }
|
2024-06-04 17:52:01 +02:00
|
|
|
it { expect(dossier.reload.hidden_by_reason).to eq('expired') }
|
2020-04-02 15:08:16 +02:00
|
|
|
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_user).once }
|
2024-06-04 17:52:01 +02:00
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_user).with([dossier], dossier.user.email) }
|
2020-04-02 15:08:16 +02:00
|
|
|
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).twice }
|
2024-06-04 17:52:01 +02:00
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([dossier], dossier.procedure.administrateurs.first.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([dossier], dossier.followers_instructeurs.first.email) }
|
2020-04-02 15:08:16 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with 2 dossiers to delete' do
|
|
|
|
let!(:dossier_1) { create(:dossier, :accepte, procedure: procedure, user: user, termine_close_to_expiration_notice_sent_at: (warning_period + 1.day).ago) }
|
|
|
|
let!(:dossier_2) { create(:dossier, :refuse, procedure: procedure_2, user: user, termine_close_to_expiration_notice_sent_at: (warning_period + 1.day).ago) }
|
|
|
|
|
|
|
|
let!(:instructeur) { create(:instructeur) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
instructeur.followed_dossiers << dossier_1 << dossier_2
|
2023-06-26 10:37:32 +02:00
|
|
|
service.delete_expired_termine_and_notify
|
2020-04-02 15:08:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_user).once }
|
2024-06-04 17:52:01 +02:00
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_user).with(match_array([dossier_1, dossier_2]), user.email) }
|
2020-04-02 15:08:16 +02:00
|
|
|
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).thrice }
|
2024-06-04 17:52:01 +02:00
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with(match_array([dossier_1, dossier_2]), instructeur.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([dossier_1], dossier_1.procedure.administrateurs.first.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([dossier_2], dossier_2.procedure.administrateurs.first.email) }
|
|
|
|
|
2024-07-15 18:02:54 +02:00
|
|
|
it { expect(dossier_1.reload.hidden_by_expired_at).to be_an_instance_of(ActiveSupport::TimeWithZone) }
|
2024-06-04 17:52:01 +02:00
|
|
|
it { expect(dossier_1.reload.hidden_by_reason).to eq('expired') }
|
2024-07-15 18:02:54 +02:00
|
|
|
it { expect(dossier_2.reload.hidden_by_expired_at).to be_an_instance_of(ActiveSupport::TimeWithZone) }
|
2024-06-04 17:52:01 +02:00
|
|
|
it { expect(dossier_2.reload.hidden_by_reason).to eq('expired') }
|
2020-04-02 15:08:16 +02:00
|
|
|
end
|
2022-01-19 15:17:15 +01:00
|
|
|
|
|
|
|
context 'with 1 dossier deleted by user and 1 dossier deleted by administration' do
|
|
|
|
let!(:dossier_1) { create(:dossier, :accepte, procedure: procedure, user: user, hidden_by_administration_at: 1.hour.ago, termine_close_to_expiration_notice_sent_at: (warning_period + 1.day).ago) }
|
|
|
|
let!(:dossier_2) { create(:dossier, :refuse, procedure: procedure_2, user: user, hidden_by_user_at: 1.hour.ago, termine_close_to_expiration_notice_sent_at: (warning_period + 1.day).ago) }
|
|
|
|
|
|
|
|
let!(:instructeur) { create(:instructeur) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
instructeur.followed_dossiers << dossier_1 << dossier_2
|
2023-06-26 10:37:32 +02:00
|
|
|
service.delete_expired_termine_and_notify
|
2022-01-19 15:17:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_user).once }
|
2024-06-04 17:52:01 +02:00
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_user).with(match_array([dossier_1]), user.email) }
|
2022-01-19 15:17:15 +01:00
|
|
|
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).twice }
|
2024-06-04 17:52:01 +02:00
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with(match_array([dossier_2]), instructeur.email) }
|
|
|
|
it { expect(DossierMailer).to have_received(:notify_automatic_deletion_to_administration).with([dossier_2], dossier_2.procedure.administrateurs.first.email) }
|
2022-01-19 15:17:15 +01:00
|
|
|
end
|
2020-04-02 15:08:16 +02:00
|
|
|
end
|
2020-02-26 17:28:39 +01:00
|
|
|
end
|