refactor(mail.delay): use simplier implementation using after_action to prevent email delivery with delay

This commit is contained in:
Martin 2021-11-16 15:12:05 +01:00
parent e48b6e0c1f
commit 8bbd77f89f
6 changed files with 29 additions and 48 deletions

View file

@ -1,22 +0,0 @@
describe NotifyNewAnswerWithDelayJob, type: :job do
let(:dossier) { double }
let(:body) { "bim un body" }
context 'when commentaire not discarded?' do
let(:commentaire) { create(:commentaire) }
it 'call DossierMailer.notify_new_answer' do
expect(DossierMailer).to receive(:notify_new_answer).with(dossier, body).and_return(double(deliver_now: true))
NotifyNewAnswerWithDelayJob.perform_now(dossier, body, commentaire)
end
end
context 'when commentaire is discarded?' do
let(:commentaire) { create(:commentaire, discarded_at: 2.hours.ago) }
it 'skips DossierMailer.notify_new_anser' do
expect(DossierMailer).not_to receive(:notify_new_answer).with(dossier, body)
NotifyNewAnswerWithDelayJob.perform_now(dossier, body, commentaire)
end
end
end

View file

@ -28,8 +28,8 @@ RSpec.describe DossierMailer, type: :mailer do
describe '.notify_new_answer with dossier brouillon' do
let(:dossier) { create(:dossier, procedure: build(:simple_procedure)) }
subject { described_class.notify_new_answer(dossier) }
let(:commentaire) { create(:commentaire, dossier: dossier) }
subject { described_class.with(commentaire: commentaire).notify_new_answer }
it { expect(subject.subject).to include("Nouveau message") }
it { expect(subject.subject).to include(dossier.id.to_s) }
@ -40,8 +40,8 @@ RSpec.describe DossierMailer, type: :mailer do
describe '.notify_new_answer with dossier en construction' do
let(:dossier) { create(:dossier, state: "en_construction", procedure: build(:simple_procedure)) }
subject { described_class.notify_new_answer(dossier) }
let(:commentaire) { create(:commentaire, dossier: dossier) }
subject { described_class.with(commentaire: commentaire).notify_new_answer }
it { expect(subject.subject).to include("Nouveau message") }
it { expect(subject.subject).to include(dossier.id.to_s) }
@ -50,6 +50,15 @@ RSpec.describe DossierMailer, type: :mailer do
it_behaves_like 'a dossier notification'
end
describe '.notify_new_answer with commentaire discarded' do
let(:dossier) { create(:dossier, procedure: build(:simple_procedure)) }
let(:commentaire) { create(:commentaire, dossier: dossier, discarded_at: 2.minutes.ago) }
subject { described_class.with(commentaire: commentaire).notify_new_answer }
it { expect(subject.perform_deliveries).to be_falsy }
end
describe '.notify_deletion_to_user' do
let(:deleted_dossier) { build(:deleted_dossier) }

View file

@ -87,7 +87,7 @@ describe Commentaire do
let(:commentaire) { CommentaireService.build(instructeur, dossier, body: "Mon commentaire") }
it "calls notify_user with delay so instructeur can destroy his comment in case of failure" do
expect(commentaire).to receive(:notify_user_with_delay)
expect(commentaire).to receive(:notify_user).with(wait: 5.minutes)
commentaire.save
end
end
@ -96,7 +96,7 @@ describe Commentaire do
let(:commentaire) { CommentaireService.build(expert, dossier, body: "Mon commentaire") }
it "calls notify_user" do
expect(commentaire).to receive(:notify_user)
expect(commentaire).to receive(:notify_user).with(no_args)
commentaire.save
end
end
@ -105,7 +105,7 @@ describe Commentaire do
let(:commentaire) { CommentaireService.build_with_email(CONTACT_EMAIL, dossier, body: "Mon commentaire") }
it "does not call notify_user" do
expect(commentaire).not_to receive(:notify_user)
expect(commentaire).not_to receive(:notify_user).with(no_args)
commentaire.save
end
end