From 875766279dfd81e1ebf7c1da8bf4deeb223f9f15 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Mon, 5 Mar 2018 17:59:45 +0100 Subject: [PATCH] [Fix #1510] Send email after dossier is received --- .../new_user/dossiers_controller.rb | 4 +++- .../new_user/dossiers_controller_spec.rb | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/controllers/new_user/dossiers_controller.rb b/app/controllers/new_user/dossiers_controller.rb index c478d14b1..90976d902 100644 --- a/app/controllers/new_user/dossiers_controller.rb +++ b/app/controllers/new_user/dossiers_controller.rb @@ -69,11 +69,13 @@ module NewUser render :modifier else if @dossier.brouillon? + @dossier.en_construction! + NotificationMailer.send_notification(@dossier, @dossier.procedure.initiated_mail_template).deliver_now! redirect_to merci_dossier_path(@dossier) else + @dossier.en_construction! redirect_to users_dossier_recapitulatif_path(@dossier) end - @dossier.en_construction! end end diff --git a/spec/controllers/new_user/dossiers_controller_spec.rb b/spec/controllers/new_user/dossiers_controller_spec.rb index 3839bed55..68c68a9b1 100644 --- a/spec/controllers/new_user/dossiers_controller_spec.rb +++ b/spec/controllers/new_user/dossiers_controller_spec.rb @@ -167,6 +167,20 @@ describe NewUser::DossiersController, type: :controller do expect(dossier.reload.state).to eq('en_construction') end + it 'sends an email only on the first #update' do + delivery = double + expect(delivery).to receive(:deliver_now!).with(no_args) + + expect(NotificationMailer).to receive(:send_notification) + .and_return(delivery) + + subject + + expect(NotificationMailer).not_to receive(:send_notification) + + subject + end + context 'when the update fails' do before do expect_any_instance_of(Dossier).to receive(:update).and_return(false) @@ -178,6 +192,12 @@ describe NewUser::DossiersController, type: :controller do it { expect(response).to render_template(:modifier) } it { expect(flash.alert).to eq(['nop']) } + + it 'does not send an email' do + expect(NotificationMailer).not_to receive(:send_notification) + + subject + end end context 'when the pj service returns an error' do