diff --git a/spec/services/expired_dossiers_deletion_service_spec.rb b/spec/services/expired_dossiers_deletion_service_spec.rb index 2bd3f53d2..c927755d8 100644 --- a/spec/services/expired_dossiers_deletion_service_spec.rb +++ b/spec/services/expired_dossiers_deletion_service_spec.rb @@ -41,6 +41,100 @@ describe ExpiredDossiersDeletionService do end end + describe '#send_brouillon_expiration_notices' do + let!(:conservation_par_defaut) { 3.months } + + before { Timecop.freeze(Time.zone.now) } + 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 + let!(:dossier) { create(:dossier, created_at: created_at) } + + before { ExpiredDossiersDeletionService.send_brouillon_expiration_notices } + + context 'when the dossier is not closed to expiration' do + let(:created_at) { (conservation_par_defaut - 1.month - 1.day).ago } + + 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 + + context 'when the dossier is closed to expiration' do + let(:created_at) { (conservation_par_defaut - 1.month + 1.day).ago } + + 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 } + it { expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).with(dossier.user, [dossier]) } + end + end + + context 'with 2 dossiers to notice' do + let!(:user) { create(:user) } + let!(:dossier_1) { create(:dossier, user: user, created_at: (conservation_par_defaut - 1.month + 1.day).ago) } + let!(:dossier_2) { create(:dossier, user: user, created_at: (conservation_par_defaut - 1.month + 1.day).ago) } + + before { ExpiredDossiersDeletionService.send_brouillon_expiration_notices } + + it { expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).once } + it { expect(DossierMailer).to have_received(:notify_brouillon_near_deletion).with(user, match_array([dossier_1, dossier_2])) } + end + end + + describe '#delete_expired_brouillons_and_notify' do + let!(:warning_period) { 1.month + 5.days } + + before { Timecop.freeze(Time.zone.now) } + 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 + let!(:dossier) { create(:dossier, brouillon_close_to_expiration_notice_sent_at: notice_sent_at) } + + before { ExpiredDossiersDeletionService.delete_expired_brouillons_and_notify } + + 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 + let(:notice_sent_at) { (warning_period - 1.day).ago } + + 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 + let(:notice_sent_at) { (warning_period + 1.day).ago } + + it { expect { dossier.reload }.to raise_error(ActiveRecord::RecordNotFound) } + + it { expect(DossierMailer).to have_received(:notify_brouillon_deletion).once } + it { expect(DossierMailer).to have_received(:notify_brouillon_deletion).with(dossier.user, [dossier.hash_for_deletion_mail]) } + end + end + + context 'with 2 dossiers to delete' do + let!(:user) { create(:user) } + let!(:dossier_1) { create(:dossier, user: user, brouillon_close_to_expiration_notice_sent_at: (warning_period + 1.day).ago) } + let!(:dossier_2) { create(:dossier, user: user, brouillon_close_to_expiration_notice_sent_at: (warning_period + 1.day).ago) } + + before { ExpiredDossiersDeletionService.delete_expired_brouillons_and_notify } + + it { expect(DossierMailer).to have_received(:notify_brouillon_deletion).once } + it { expect(DossierMailer).to have_received(:notify_brouillon_deletion).with(user, match_array([dossier_1, dossier_2].map(&:hash_for_deletion_mail))) } + end + end + describe '#send_en_construction_expiration_notices' do let!(:conservation_par_defaut) { 3.months }