diff --git a/app/controllers/new_gestionnaire/dossiers_controller.rb b/app/controllers/new_gestionnaire/dossiers_controller.rb index f3b7928a0..e796aef24 100644 --- a/app/controllers/new_gestionnaire/dossiers_controller.rb +++ b/app/controllers/new_gestionnaire/dossiers_controller.rb @@ -91,25 +91,22 @@ module NewGestionnaire case params[:process_action] when "refuser" dossier.refuse! - notice = "Dossier considéré comme refusé." - template = procedure.refused_mail_template + dossier.save + flash.notice = "Dossier considéré comme refusé." + NotificationMailer.send_refused_notification(dossier).deliver_now! when "classer_sans_suite" dossier.sans_suite! - notice = "Dossier considéré comme sans suite." - template = procedure.without_continuation_mail_template + dossier.save + flash.notice = "Dossier considéré comme sans suite." + NotificationMailer.send_without_continuation_notification(dossier).deliver_now! when "accepter" dossier.accepte! dossier.attestation = dossier.build_attestation - notice = "Dossier traité avec succès." - template = procedure.closed_mail_template + dossier.save + flash.notice = "Dossier traité avec succès." + NotificationMailer.send_closed_notification(dossier).deliver_now! end - dossier.save - - flash.notice = notice - - NotificationMailer.send_notification(dossier, template).deliver_now! - redirect_to gestionnaire_dossier_path(procedure, dossier) end diff --git a/app/controllers/new_user/dossiers_controller.rb b/app/controllers/new_user/dossiers_controller.rb index e05ce8a5f..b8f398b99 100644 --- a/app/controllers/new_user/dossiers_controller.rb +++ b/app/controllers/new_user/dossiers_controller.rb @@ -71,7 +71,7 @@ module NewUser render :modifier elsif @dossier.brouillon? @dossier.en_construction! - NotificationMailer.send_notification(@dossier, @dossier.procedure.initiated_mail_template).deliver_now! + NotificationMailer.send_initiated_notification(@dossier).deliver_now! redirect_to merci_dossier_path(@dossier) elsif owns_dossier? redirect_to users_dossier_recapitulatif_path(@dossier) diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb index 6e6b548df..be547152a 100644 --- a/app/controllers/users/description_controller.rb +++ b/app/controllers/users/description_controller.rb @@ -55,7 +55,7 @@ class Users::DescriptionController < UsersController if dossier.brouillon? dossier.en_construction! # TODO move to model - NotificationMailer.send_notification(dossier, procedure.initiated_mail_template).deliver_now! + NotificationMailer.send_initiated_notification(dossier).deliver_now! end flash.notice = 'Félicitations, votre demande a bien été enregistrée.' redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: dossier.id) diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 5119693a1..b493f8407 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -8,15 +8,6 @@ class NotificationMailer < ApplicationMailer send_notification(dossier, dossier.procedure.received_mail_template) end - def send_notification(dossier, mail_template) - vars_mailer(dossier) - - @subject = mail_template.subject_for_dossier dossier - @body = mail_template.body_for_dossier dossier - - mail(subject: @subject) { |format| format.html { @body } } - end - def send_draft_notification(dossier) vars_mailer(dossier) @@ -25,12 +16,41 @@ class NotificationMailer < ApplicationMailer mail(subject: @subject) end + def send_initiated_notification(dossier) + send_notification(dossier, dossier.procedure.initiated_mail_template) + end + + def send_received_notification(dossier) + send_notification(dossier, dossier.procedure.received_mail_template) + end + + def send_closed_notification(dossier) + send_notification(dossier, dossier.procedure.closed_mail_template) + end + + def send_refused_notification(dossier) + send_notification(dossier, dossier.procedure.refused_mail_template) + end + + def send_without_continuation_notification(dossier) + send_notification(dossier, dossier.procedure.without_continuation_mail_template) + end + def new_answer(dossier) send_mail dossier, "Nouveau message pour votre dossier demarches-simplifiees.fr nº #{dossier.id}" end private + def send_notification(dossier, mail_template) + vars_mailer(dossier) + + @subject = mail_template.subject_for_dossier dossier + @body = mail_template.body_for_dossier dossier + + mail(subject: @subject) { |format| format.html { @body } } + end + def create_commentaire_for_notification Commentaire.create( dossier: @dossier, diff --git a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb index e23bfa5f8..dd6ace6be 100644 --- a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb @@ -151,8 +151,8 @@ describe NewGestionnaire::DossiersController, type: :controller do end it 'Notification email is sent' do - expect(NotificationMailer).to receive(:send_notification) - .with(dossier, kind_of(Mails::RefusedMail)).and_return(NotificationMailer) + expect(NotificationMailer).to receive(:send_refused_notification) + .with(dossier).and_return(NotificationMailer) expect(NotificationMailer).to receive(:deliver_now!) subject @@ -177,8 +177,8 @@ describe NewGestionnaire::DossiersController, type: :controller do end it 'Notification email is sent' do - expect(NotificationMailer).to receive(:send_notification) - .with(dossier, kind_of(Mails::WithoutContinuationMail)).and_return(NotificationMailer) + expect(NotificationMailer).to receive(:send_without_continuation_notification) + .with(dossier).and_return(NotificationMailer) expect(NotificationMailer).to receive(:deliver_now!) subject @@ -192,8 +192,8 @@ describe NewGestionnaire::DossiersController, type: :controller do dossier.en_instruction! sign_in gestionnaire - expect(NotificationMailer).to receive(:send_notification) - .with(dossier, kind_of(Mails::ClosedMail)) + expect(NotificationMailer).to receive(:send_closed_notification) + .with(dossier) .and_return(NotificationMailer) expect(NotificationMailer).to receive(:deliver_now!) diff --git a/spec/controllers/new_user/dossiers_controller_spec.rb b/spec/controllers/new_user/dossiers_controller_spec.rb index 005d39287..9b1479ddc 100644 --- a/spec/controllers/new_user/dossiers_controller_spec.rb +++ b/spec/controllers/new_user/dossiers_controller_spec.rb @@ -223,12 +223,12 @@ describe NewUser::DossiersController, type: :controller do delivery = double expect(delivery).to receive(:deliver_now!).with(no_args) - expect(NotificationMailer).to receive(:send_notification) + expect(NotificationMailer).to receive(:send_initiated_notification) .and_return(delivery) subject - expect(NotificationMailer).not_to receive(:send_notification) + expect(NotificationMailer).not_to receive(:send_initiated_notification) subject end @@ -246,7 +246,7 @@ describe NewUser::DossiersController, type: :controller do it { expect(flash.alert).to eq(['nop']) } it 'does not send an email' do - expect(NotificationMailer).not_to receive(:send_notification) + expect(NotificationMailer).not_to receive(:send_received_notification) subject end diff --git a/spec/controllers/users/description_controller_shared_example.rb b/spec/controllers/users/description_controller_shared_example.rb index 07a544046..08595cf04 100644 --- a/spec/controllers/users/description_controller_shared_example.rb +++ b/spec/controllers/users/description_controller_shared_example.rb @@ -132,9 +132,12 @@ shared_examples 'description_controller_spec' do after { Timecop.return } it 'sets the state of the dossier before sending the mail' do - expect_any_instance_of(Mails::InitiatedMail) - .to receive(:subject_for_dossier) + sender = double("notification sender") + allow(sender).to receive(:deliver_now!) + expect(NotificationMailer) + .to receive(:send_initiated_notification) .with(have_attributes(en_construction_at: DateTime.now)) + .and_return(sender) submit_dossier end diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb index d6d1dd1dd..d60fb10a3 100644 --- a/spec/mailers/notification_mailer_spec.rb +++ b/spec/mailers/notification_mailer_spec.rb @@ -18,7 +18,19 @@ RSpec.describe NotificationMailer, type: :mailer do describe '.send_notification' do let(:email_template) { instance_double('email_template', subject_for_dossier: 'subject', body_for_dossier: 'body') } - subject { described_class.send_notification(dossier, email_template) } + subject do + klass = Class.new(described_class) do + # We’re testing the (private) method `NotificationMailer#send_notification`. + # + # The standard trick to test a private method would be to `send(:send_notification`, but doesn’t work here, + # because ActionMailer does some magic to expose public instace methods as class methods. + # So, we use inheritance instead to make the private method public for testing purposes. + def send_notification(dossier, template) + super + end + end + klass.send_notification(dossier, email_template) + end it { expect(subject.subject).to eq(email_template.subject_for_dossier) } it { expect(subject.body).to eq(email_template.body_for_dossier) } diff --git a/spec/mailers/previews/notification_mailer_preview.rb b/spec/mailers/previews/notification_mailer_preview.rb index 6fdcc4174..a5349a255 100644 --- a/spec/mailers/previews/notification_mailer_preview.rb +++ b/spec/mailers/previews/notification_mailer_preview.rb @@ -1,6 +1,6 @@ class NotificationMailerPreview < ActionMailer::Preview def send_notification - NotificationMailer.send_notification(Dossier.last, Dossier.last.procedure.initiated_mail_template) + NotificationMailer.send_initiated_notification(Dossier.last) end def send_draft_notification