[#10951] Create a cron job to warn user with a old brouillon
This commit is contained in:
parent
b77a5f4c6e
commit
38e9ca4979
10 changed files with 221 additions and 1 deletions
24
spec/jobs/cron/notify_old_brouillon_dossiers_job_spec.rb
Normal file
24
spec/jobs/cron/notify_old_brouillon_dossiers_job_spec.rb
Normal file
|
@ -0,0 +1,24 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe Cron::NotifyOldBrouillonDossiersSoonDeletedJob, type: :job do
|
||||
let(:procedure) { create(:procedure) }
|
||||
|
||||
let!(:recent_brouillon) { create(:dossier, :brouillon, procedure: procedure, updated_at: 2.months.ago) }
|
||||
let!(:old_brouillon) { create(:dossier, :brouillon, procedure: procedure, updated_at: 4.months.ago) }
|
||||
let!(:old_en_construction) { create(:dossier, :en_construction, procedure: procedure, updated_at: 4.months.ago) }
|
||||
|
||||
subject(:perform_job) { described_class.perform_now }
|
||||
|
||||
describe '#perform' do
|
||||
before do
|
||||
allow(DossierMailer).to receive(:notify_old_brouillon_soon_deleted).and_return(double(deliver_later: true))
|
||||
perform_job
|
||||
end
|
||||
|
||||
it 'sends email only for old brouillon dossiers' do
|
||||
expect(DossierMailer).to have_received(:notify_old_brouillon_soon_deleted).with(old_brouillon).once
|
||||
expect(DossierMailer).not_to have_received(:notify_old_brouillon_soon_deleted).with(recent_brouillon)
|
||||
expect(DossierMailer).not_to have_received(:notify_old_brouillon_soon_deleted).with(old_en_construction)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,53 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe Cron::NotifyOldBrouillonDossiersSoonDeletedJob, type: :job do
|
||||
describe "#perform" do
|
||||
let(:job) { described_class.new }
|
||||
|
||||
context "when there are old draft dossiers" do
|
||||
let!(:old_draft_never_notified) { travel_to(4.months.ago) { create(:dossier, :brouillon) } }
|
||||
let!(:old_draft_notified_before_update) do
|
||||
travel_to(4.months.ago) do
|
||||
create(:dossier, :brouillon, notified_soon_deleted_sent_at: 1.month.ago)
|
||||
end
|
||||
end
|
||||
let!(:old_draft_recently_notified) do
|
||||
travel_to(4.months.ago) do
|
||||
create(:dossier, :brouillon, notified_soon_deleted_sent_at: 3.months.from_now)
|
||||
end
|
||||
end
|
||||
let!(:recent_draft) { travel_to(2.months.ago) { create(:dossier, :brouillon) } }
|
||||
let!(:old_non_draft) { travel_to(4.months.ago) { create(:dossier, :en_construction) } }
|
||||
|
||||
it "sends notifications only for eligible draft dossiers" do
|
||||
expect(DossierMailer).to receive(:notify_old_brouillon_soon_deleted)
|
||||
.with(old_draft_never_notified)
|
||||
.and_return(double(deliver_later: true))
|
||||
.once
|
||||
|
||||
expect(DossierMailer).to receive(:notify_old_brouillon_soon_deleted)
|
||||
.with(old_draft_notified_before_update)
|
||||
.and_return(double(deliver_later: true))
|
||||
.once
|
||||
|
||||
expect(DossierMailer).not_to receive(:notify_old_brouillon_soon_deleted)
|
||||
.with(old_draft_recently_notified)
|
||||
|
||||
job.perform
|
||||
|
||||
expect(old_draft_never_notified.reload.notified_soon_deleted_sent_at).to be_present
|
||||
expect(old_draft_notified_before_update.reload.notified_soon_deleted_sent_at).to be_present
|
||||
end
|
||||
end
|
||||
|
||||
context "when there are no old draft dossiers" do
|
||||
let!(:recent_draft) { create(:dossier, :brouillon, updated_at: 2.months.ago) }
|
||||
|
||||
it "doesn't send any notifications" do
|
||||
expect(DossierMailer).not_to receive(:notify_old_brouillon_soon_deleted)
|
||||
|
||||
job.perform
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue