Skip to content

Commit

Permalink
[#10950] Test for the process_never_touched_dossiers_brouillon method
Browse files Browse the repository at this point in the history
  • Loading branch information
mmagn committed Jan 21, 2025
1 parent 7a1e33c commit 22424af
Showing 1 changed file with 12 additions and 59 deletions.
71 changes: 12 additions & 59 deletions spec/services/expired/expired_dossiers_deletion_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,65 +91,18 @@
end

describe '#process_never_touched_dossiers_brouillon' do
let(:types) { [{ type: :text }] }
let(:procedure_opts) { { types_de_champ_public: types } }

before do
allow(DossierMailer).to receive(:notify_brouillon_deletion).and_return(double(deliver_later: nil))
end

subject { service.process_never_touched_dossiers_brouillon(3.weeks.ago..2.weeks.ago) }

context 'with empty brouillon dossiers' do
let!(:empty_brouillon) { travel_to(15.days.ago) { create(:dossier, procedure: procedure) } }
let!(:empty_brouillon_2) { travel_to(15.days.ago) { create(:dossier, procedure: procedure, user: empty_brouillon.user) } }
let!(:empty_brouillon_3) { travel_to(7.days.ago) { create(:dossier, procedure: procedure, user: empty_brouillon.user) } }

it 'deletes empty brouillons and sends notifications' do
subject
expect(DossierMailer).to have_received(:notify_brouillon_deletion).once
expect(DossierMailer).to have_received(:notify_brouillon_deletion).with(
match_array([empty_brouillon.hash_for_deletion_mail, empty_brouillon_2.hash_for_deletion_mail]),
empty_brouillon.user.email
)
expect { empty_brouillon.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { empty_brouillon_2.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { empty_brouillon_3.reload }.not_to raise_error
end
end

context 'with non-empty brouillon dossiers' do
let!(:not_empty_brouillon) { travel_to(15.days.ago) { create(:dossier, procedure: procedure) } }

before do
not_empty_brouillon.champs.first.update!(value: 'filled')
end

it 'does not delete non-empty brouillons' do
subject
expect(DossierMailer).not_to have_received(:notify_brouillon_deletion)
expect { not_empty_brouillon.reload }.not_to raise_error
end
end

context 'with dossiers too recent' do
let!(:recent_empty_brouillon) { travel_to(7.days.ago) { create(:dossier, procedure: procedure) } }

it 'does not delete recent brouillons' do
subject
expect(DossierMailer).not_to have_received(:notify_brouillon_deletion)
expect { recent_empty_brouillon.reload }.not_to raise_error
end
end

context 'with non-brouillon dossiers' do
let!(:en_construction_dossier) { travel_to(15.days.ago) { create(:dossier, :en_construction, procedure: procedure) } }

it 'does not delete non-brouillon dossiers' do
subject

expect(DossierMailer).not_to have_received(:notify_brouillon_deletion)
expect { en_construction_dossier.reload }.not_to raise_error
subject { service.process_never_touched_dossiers_brouillon }

context 'with never touched brouillon dossiers' do
let!(:never_touched_brouillon) { travel_to(20.days.ago) { create(:dossier, procedure: procedure, last_champ_updated_at: nil, last_champ_piece_jointe_updated_at: nil) } }
let!(:never_touched_brouillon_2) { travel_to(7.days.ago) { create(:dossier, procedure: procedure, last_champ_updated_at: nil, last_champ_piece_jointe_updated_at: nil) } }
let!(:never_touched_en_construction) { travel_to(20.days.ago) { create(:dossier, :en_construction, procedure: procedure, last_champ_updated_at: nil, last_champ_piece_jointe_updated_at: nil) } }
let!(:touched_brouillon) { travel_to(20.days.ago) { create(:dossier, procedure: procedure, last_champ_updated_at: 1.day.ago, last_champ_piece_jointe_updated_at: nil) } }
let!(:touched_brouillon_2) { travel_to(20.days.ago) { create(:dossier, procedure: procedure, last_champ_updated_at: nil, last_champ_piece_jointe_updated_at: 1.day.ago) } }

it 'deletes never touched brouillons ' do
expect { subject }.to change { Dossier.never_touched_brouillon.count }.from(2).to(0)
expect(Dossier.all).to include(never_touched_en_construction, touched_brouillon, touched_brouillon_2)
end
end
end
Expand Down

0 comments on commit 22424af

Please sign in to comment.