Refactor NotificationMailer

This commit is contained in:
Paul Chavard 2021-04-29 19:10:22 +02:00
parent a4fd629f4a
commit f6508899de
9 changed files with 128 additions and 70 deletions

View file

@ -210,7 +210,7 @@ describe Instructeurs::DossiersController, type: :controller do
end
it 'Notification email is sent' do
expect(NotificationMailer).to receive(:send_refused_notification)
expect(NotificationMailer).to receive(:send_refuse_notification)
.with(dossier).and_return(NotificationMailer)
expect(NotificationMailer).to receive(:deliver_later)
@ -250,7 +250,7 @@ describe Instructeurs::DossiersController, type: :controller do
end
it 'Notification email is sent' do
expect(NotificationMailer).to receive(:send_without_continuation_notification)
expect(NotificationMailer).to receive(:send_sans_suite_notification)
.with(dossier).and_return(NotificationMailer)
expect(NotificationMailer).to receive(:deliver_later)
@ -280,7 +280,7 @@ describe Instructeurs::DossiersController, type: :controller do
dossier.passer_en_instruction!(instructeur)
sign_in(instructeur.user)
expect(NotificationMailer).to receive(:send_closed_notification)
expect(NotificationMailer).to receive(:send_accepte_notification)
.with(dossier)
.and_return(NotificationMailer)

View file

@ -476,12 +476,12 @@ describe Users::DossiersController, type: :controller do
delivery = double
expect(delivery).to receive(:deliver_later).with(no_args)
expect(NotificationMailer).to receive(:send_initiated_notification)
expect(NotificationMailer).to receive(:send_en_construction_notification)
.and_return(delivery)
subject
expect(NotificationMailer).not_to receive(:send_initiated_notification)
expect(NotificationMailer).not_to receive(:send_en_construction_notification)
subject
end
@ -499,7 +499,7 @@ describe Users::DossiersController, type: :controller do
it { expect(flash.alert).to eq(['nop']) }
it 'does not send an email' do
expect(NotificationMailer).not_to receive(:send_initiated_notification)
expect(NotificationMailer).not_to receive(:send_en_construction_notification)
subject
end
@ -687,7 +687,7 @@ describe Users::DossiersController, type: :controller do
end
it 'does not send an email' do
expect(NotificationMailer).not_to receive(:send_initiated_notification)
expect(NotificationMailer).not_to receive(:send_en_construction_notification)
subject
end

View file

@ -1,19 +1,21 @@
RSpec.describe NotificationMailer, type: :mailer do
let(:administrateur) { create(:administrateur) }
let(:user) { create(:user) }
let(:procedure) { create(:simple_procedure) }
let(:dossier) { create(:dossier, :en_construction, :with_individual, :with_service, user: user, procedure: procedure) }
describe '.send_dossier_received' do
describe 'send_en_instruction_notification' do
let(:dossier) { create(:dossier, :en_construction, :with_individual, :with_service, user: user, procedure: procedure) }
let(:email_template) { create(:received_mail, subject: 'Email subject', body: 'Your dossier was processed. Thanks.') }
before do
dossier.procedure.received_mail = email_template
end
subject(:mail) { described_class.send_dossier_received(dossier) }
subject(:mail) { described_class.send_en_instruction_notification(dossier) }
it 'creates a commentaire in the messagerie' do
expect { subject.deliver_now }.to change { Commentaire.count }.by(1)
expect(subject.perform_deliveries).to be_truthy
commentaire = Commentaire.last
expect(commentaire.body).to include(email_template.subject_for_dossier(dossier), email_template.body_for_dossier(dossier))
@ -59,4 +61,27 @@ RSpec.describe NotificationMailer, type: :mailer do
expect(subject.from.first).to eq(Mail::Address.new(NO_REPLY_EMAIL).address)
end
end
describe 'send_accepte_notification' do
let(:dossier) { create(:dossier, :en_instruction, :with_individual, :with_service, user: user, procedure: procedure) }
let(:email_template) { create(:closed_mail, subject: 'Email subject', body: 'Your dossier was accepted. Thanks.') }
before do
dossier.procedure.closed_mail = email_template
end
subject(:mail) { described_class.send_accepte_notification(dossier) }
context 'when dossier user is deleted' do
before do
dossier.user.delete_and_keep_track_dossiers(administrateur)
dossier.reload
end
it 'should not send notification' do
expect { subject.deliver_now }.not_to change { Commentaire.count }
expect(subject.perform_deliveries).to be_falsey
end
end
end
end

View file

@ -1,23 +1,36 @@
class NotificationMailerPreview < ActionMailer::Preview
def send_initiated_notification
p = Procedure.where(id: Mails::InitiatedMail.where("body like ?", "%<img%").pluck(:procedure_id).uniq).order("RANDOM()").first
NotificationMailer.send_initiated_notification(p.dossiers.last)
def send_en_construction_notification
NotificationMailer.send_en_construction_notification(dossier_with_image)
end
def send_dossier_received
NotificationMailer.send_dossier_received(Dossier.last)
def send_en_instruction_notification
NotificationMailer.send_en_instruction_notification(dossier)
end
def send_closed_notification
NotificationMailer.send_closed_notification(Dossier.last)
def send_accepte_notification
NotificationMailer.send_accepte_notification(dossier)
end
def send_refused_notification
dossier = Dossier.last.tap { |d| d.assign_attributes(motivation: 'Le montant demandé dépasse le plafond autorisé') }
NotificationMailer.send_refused_notification(dossier)
def send_refuse_notification
NotificationMailer.send_refuse_notification(dossier_with_motivation)
end
def send_without_continuation_notification
NotificationMailer.send_without_continuation_notification(Dossier.last)
def send_sans_suite_notification
NotificationMailer.send_sans_suite_notification(dossier)
end
private
def dossier
Dossier.last
end
def dossier_with_image
procedure = Procedure.where(id: Mails::InitiatedMail.where("body like ?", "%<img%").pluck(:procedure_id).uniq).order("RANDOM()").first
procedure.dossiers.last
end
def dossier_with_motivation
Dossier.last.tap { |d| d.assign_attributes(motivation: 'Le montant demandé dépasse le plafond autorisé') }
end
end

View file

@ -512,17 +512,17 @@ describe Dossier do
let(:instructeur) { create(:instructeur) }
before do
allow(NotificationMailer).to receive(:send_dossier_received).and_return(double(deliver_later: nil))
allow(NotificationMailer).to receive(:send_en_instruction_notification).and_return(double(deliver_later: nil))
end
it "sends an email when the dossier becomes en_instruction" do
dossier.passer_en_instruction!(instructeur)
expect(NotificationMailer).to have_received(:send_dossier_received).with(dossier)
expect(NotificationMailer).to have_received(:send_en_instruction_notification).with(dossier)
end
it "does not an email when the dossier becomes accepte" do
dossier.accepte!
expect(NotificationMailer).to_not have_received(:send_dossier_received)
expect(NotificationMailer).to_not have_received(:send_en_instruction_notification)
end
end
@ -935,7 +935,7 @@ describe Dossier do
let(:attestation) { Attestation.new }
before do
allow(NotificationMailer).to receive(:send_closed_notification).and_return(double(deliver_later: true))
allow(NotificationMailer).to receive(:send_accepte_notification).and_return(double(deliver_later: true))
allow(dossier).to receive(:build_attestation).and_return(attestation)
Timecop.freeze(now)
@ -957,7 +957,7 @@ describe Dossier do
it { expect(operation_serialized['operation']).to eq('accepter') }
it { expect(operation_serialized['dossier_id']).to eq(dossier.id) }
it { expect(operation_serialized['executed_at']).to eq(last_operation.executed_at.iso8601) }
it { expect(NotificationMailer).to have_received(:send_closed_notification).with(dossier) }
it { expect(NotificationMailer).to have_received(:send_accepte_notification).with(dossier) }
it { expect(dossier.attestation).to eq(attestation) }
end
@ -968,7 +968,7 @@ describe Dossier do
let(:attestation) { Attestation.new }
before do
allow(NotificationMailer).to receive(:send_closed_notification).and_return(double(deliver_later: true))
allow(NotificationMailer).to receive(:send_accepte_notification).and_return(double(deliver_later: true))
allow(dossier).to receive(:build_attestation).and_return(attestation)
Timecop.freeze(now)
@ -984,7 +984,7 @@ describe Dossier do
it { expect(dossier.state).to eq('accepte') }
it { expect(last_operation.operation).to eq('accepter') }
it { expect(last_operation.automatic_operation?).to be_truthy }
it { expect(NotificationMailer).to have_received(:send_closed_notification).with(dossier) }
it { expect(NotificationMailer).to have_received(:send_accepte_notification).with(dossier) }
it { expect(dossier.attestation).to eq(attestation) }
end