From 07b1c147d9e024849470afaf37bb6e60bf823cae Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Fri, 25 May 2018 18:05:28 +0200 Subject: [PATCH] [Fix #1972] Always send mails asynchronously --- app/controllers/admin/gestionnaires_controller.rb | 2 +- app/controllers/invites_controller.rb | 4 ++-- app/controllers/new_gestionnaire/dossiers_controller.rb | 6 +++--- app/controllers/new_user/dossiers_controller.rb | 2 +- app/controllers/users/description_controller.rb | 2 +- app/jobs/find_dubious_procedures_job.rb | 2 +- app/models/administrateur.rb | 2 +- app/models/administration.rb | 2 +- app/models/avis.rb | 2 +- app/models/commentaire.rb | 2 +- app/models/dossier.rb | 2 +- app/models/gestionnaire.rb | 2 +- spec/controllers/admin/gestionnaires_controller_spec.rb | 6 +++--- spec/controllers/invites_controller_spec.rb | 4 ++-- .../manager/administrateurs_controller_spec.rb | 4 ++-- .../new_gestionnaire/dossiers_controller_spec.rb | 6 +++--- spec/controllers/new_user/dossiers_controller_spec.rb | 2 +- .../users/description_controller_shared_example.rb | 2 +- .../users/dossiers/commentaires_controller_spec.rb | 2 +- spec/features/new_gestionnaire/gestionnaire_spec.rb | 8 +++++++- spec/jobs/find_dubious_procedures_job_spec.rb | 2 +- spec/models/avis_spec.rb | 2 +- spec/models/dossier_spec.rb | 9 ++++++++- 23 files changed, 45 insertions(+), 32 deletions(-) diff --git a/app/controllers/admin/gestionnaires_controller.rb b/app/controllers/admin/gestionnaires_controller.rb index 305400bee..831dd6f38 100644 --- a/app/controllers/admin/gestionnaires_controller.rb +++ b/app/controllers/admin/gestionnaires_controller.rb @@ -50,7 +50,7 @@ class Admin::GestionnairesController < AdminController @gestionnaire.invite! if User.exists?(email: @gestionnaire.email) - GestionnaireMailer.user_to_gestionnaire(@gestionnaire.email).deliver_now! + GestionnaireMailer.user_to_gestionnaire(@gestionnaire.email).deliver_later else User.create(email: email, password: password) end diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rb index 8e5518d7f..25277784b 100644 --- a/app/controllers/invites_controller.rb +++ b/app/controllers/invites_controller.rb @@ -13,9 +13,9 @@ class InvitesController < ApplicationController if invite.valid? if invite.user.present? - InviteMailer.invite_user(invite).deliver_now! + InviteMailer.invite_user(invite).deliver_later else - InviteMailer.invite_guest(invite).deliver_now! + InviteMailer.invite_guest(invite).deliver_later end flash.notice = "Invitation envoyée (#{invite.email})" diff --git a/app/controllers/new_gestionnaire/dossiers_controller.rb b/app/controllers/new_gestionnaire/dossiers_controller.rb index e796aef24..a62359a18 100644 --- a/app/controllers/new_gestionnaire/dossiers_controller.rb +++ b/app/controllers/new_gestionnaire/dossiers_controller.rb @@ -93,18 +93,18 @@ module NewGestionnaire dossier.refuse! dossier.save flash.notice = "Dossier considéré comme refusé." - NotificationMailer.send_refused_notification(dossier).deliver_now! + NotificationMailer.send_refused_notification(dossier).deliver_later when "classer_sans_suite" dossier.sans_suite! dossier.save flash.notice = "Dossier considéré comme sans suite." - NotificationMailer.send_without_continuation_notification(dossier).deliver_now! + NotificationMailer.send_without_continuation_notification(dossier).deliver_later when "accepter" dossier.accepte! dossier.attestation = dossier.build_attestation dossier.save flash.notice = "Dossier traité avec succès." - NotificationMailer.send_closed_notification(dossier).deliver_now! + NotificationMailer.send_closed_notification(dossier).deliver_later end redirect_to gestionnaire_dossier_path(procedure, dossier) diff --git a/app/controllers/new_user/dossiers_controller.rb b/app/controllers/new_user/dossiers_controller.rb index b8f398b99..56bf50f4a 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_initiated_notification(@dossier).deliver_now! + NotificationMailer.send_initiated_notification(@dossier).deliver_later 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 be547152a..9080ae890 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_initiated_notification(dossier).deliver_now! + NotificationMailer.send_initiated_notification(dossier).deliver_later 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/jobs/find_dubious_procedures_job.rb b/app/jobs/find_dubious_procedures_job.rb index ecfb3b56c..a1aa3dbba 100644 --- a/app/jobs/find_dubious_procedures_job.rb +++ b/app/jobs/find_dubious_procedures_job.rb @@ -24,6 +24,6 @@ class FindDubiousProceduresJob < ApplicationJob .group_by(&:procedure_id) .map { |_procedure_id, tdcs| [tdcs[0].procedure, tdcs] } - AdministrationMailer.dubious_procedures(dubious_procedures_and_tdcs).deliver_now + AdministrationMailer.dubious_procedures(dubious_procedures_and_tdcs).deliver_later end end diff --git a/app/models/administrateur.rb b/app/models/administrateur.rb index e1063b294..164ff18be 100644 --- a/app/models/administrateur.rb +++ b/app/models/administrateur.rb @@ -51,7 +51,7 @@ class Administrateur < ApplicationRecord reset_password_token = set_reset_password_token - AdministrationMailer.invite_admin(self, reset_password_token).deliver_now! + AdministrationMailer.invite_admin(self, reset_password_token).deliver_later reset_password_token end diff --git a/app/models/administration.rb b/app/models/administration.rb index d0da076e5..bfc452d63 100644 --- a/app/models/administration.rb +++ b/app/models/administration.rb @@ -17,7 +17,7 @@ class Administration < ApplicationRecord }) if administrateur.save - AdministrationMailer.new_admin_email(administrateur, self).deliver_now! + AdministrationMailer.new_admin_email(administrateur, self).deliver_later administrateur.invite! User.create({ email: email, diff --git a/app/models/avis.rb b/app/models/avis.rb index 108d24777..37d8e96cf 100644 --- a/app/models/avis.rb +++ b/app/models/avis.rb @@ -34,7 +34,7 @@ class Avis < ApplicationRecord private def notify_gestionnaire - AvisMailer.avis_invitation(self).deliver_now + AvisMailer.avis_invitation(self).deliver_later end def try_to_assign_gestionnaire diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index 75e34596a..be89d2ef8 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -42,7 +42,7 @@ class Commentaire < ApplicationRecord end def notify_user - NotificationMailer.new_answer(dossier).deliver_now! + NotificationMailer.new_answer(dossier).deliver_later end def is_virus_free? diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 2d6ce598a..8fbe99ee4 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -322,7 +322,7 @@ class Dossier < ApplicationRecord def send_draft_notification_email if brouillon? - NotificationMailer.send_draft_notification(self).deliver_now! + NotificationMailer.send_draft_notification(self).deliver_later end end diff --git a/app/models/gestionnaire.rb b/app/models/gestionnaire.rb index d96c603df..253fc1523 100644 --- a/app/models/gestionnaire.rb +++ b/app/models/gestionnaire.rb @@ -147,7 +147,7 @@ class Gestionnaire < ApplicationRecord def invite! reset_password_token = set_reset_password_token - GestionnaireMailer.invite_gestionnaire(self, reset_password_token).deliver_now! + GestionnaireMailer.invite_gestionnaire(self, reset_password_token).deliver_later end private diff --git a/spec/controllers/admin/gestionnaires_controller_spec.rb b/spec/controllers/admin/gestionnaires_controller_spec.rb index 6fe2da4d0..0e495f9e7 100644 --- a/spec/controllers/admin/gestionnaires_controller_spec.rb +++ b/spec/controllers/admin/gestionnaires_controller_spec.rb @@ -71,7 +71,7 @@ describe Admin::GestionnairesController, type: :controller do describe 'Email Notification' do it { expect(GestionnaireMailer).not_to receive(:new_gestionnaire) - expect(GestionnaireMailer).not_to receive(:deliver_now!) + expect(GestionnaireMailer).not_to receive(:deliver_later) subject } end @@ -87,7 +87,7 @@ describe Admin::GestionnairesController, type: :controller do it 'Notification email is not send' do expect(GestionnaireMailer).not_to receive(:new_gestionnaire) - expect(GestionnaireMailer).not_to receive(:deliver_now!) + expect(GestionnaireMailer).not_to receive(:deliver_later) end end @@ -104,7 +104,7 @@ describe Admin::GestionnairesController, type: :controller do describe 'Email notification' do it 'is not sent when email already exists' do expect(GestionnaireMailer).not_to receive(:new_gestionnaire) - expect(GestionnaireMailer).not_to receive(:deliver_now!) + expect(GestionnaireMailer).not_to receive(:deliver_later) subject end diff --git a/spec/controllers/invites_controller_spec.rb b/spec/controllers/invites_controller_spec.rb index 3765184e1..d65754633 100644 --- a/spec/controllers/invites_controller_spec.rb +++ b/spec/controllers/invites_controller_spec.rb @@ -131,7 +131,7 @@ describe InvitesController, type: :controller do context 'when user does not exist' do it 'send email' do expect(InviteMailer).to receive(:invite_guest).and_return(InviteMailer) - expect(InviteMailer).to receive(:deliver_now!) + expect(InviteMailer).to receive(:deliver_later) subject end @@ -144,7 +144,7 @@ describe InvitesController, type: :controller do it 'send email' do expect(InviteMailer).to receive(:invite_user).and_return(InviteMailer) - expect(InviteMailer).to receive(:deliver_now!) + expect(InviteMailer).to receive(:deliver_later) subject end diff --git a/spec/controllers/manager/administrateurs_controller_spec.rb b/spec/controllers/manager/administrateurs_controller_spec.rb index 86fbe4ea5..432aa9daa 100644 --- a/spec/controllers/manager/administrateurs_controller_spec.rb +++ b/spec/controllers/manager/administrateurs_controller_spec.rb @@ -18,9 +18,9 @@ describe Manager::AdministrateursController, type: :controller do it 'alert new mail are send' do expect(AdministrationMailer).to receive(:new_admin_email).and_return(AdministrationMailer) - expect(AdministrationMailer).to receive(:deliver_now!) + expect(AdministrationMailer).to receive(:deliver_later) expect(AdministrationMailer).to receive(:invite_admin).and_return(AdministrationMailer) - expect(AdministrationMailer).to receive(:deliver_now!) + expect(AdministrationMailer).to receive(:deliver_later) subject end end diff --git a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb index dd6ace6be..960595898 100644 --- a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb @@ -153,7 +153,7 @@ describe NewGestionnaire::DossiersController, type: :controller do it 'Notification email is sent' do expect(NotificationMailer).to receive(:send_refused_notification) .with(dossier).and_return(NotificationMailer) - expect(NotificationMailer).to receive(:deliver_now!) + expect(NotificationMailer).to receive(:deliver_later) subject end @@ -179,7 +179,7 @@ describe NewGestionnaire::DossiersController, type: :controller do it 'Notification email is sent' do expect(NotificationMailer).to receive(:send_without_continuation_notification) .with(dossier).and_return(NotificationMailer) - expect(NotificationMailer).to receive(:deliver_now!) + expect(NotificationMailer).to receive(:deliver_later) subject end @@ -196,7 +196,7 @@ describe NewGestionnaire::DossiersController, type: :controller do .with(dossier) .and_return(NotificationMailer) - expect(NotificationMailer).to receive(:deliver_now!) + expect(NotificationMailer).to receive(:deliver_later) end subject { post :terminer, params: { process_action: "accepter", procedure_id: procedure.id, dossier_id: dossier.id } } diff --git a/spec/controllers/new_user/dossiers_controller_spec.rb b/spec/controllers/new_user/dossiers_controller_spec.rb index 9b1479ddc..0f2b958eb 100644 --- a/spec/controllers/new_user/dossiers_controller_spec.rb +++ b/spec/controllers/new_user/dossiers_controller_spec.rb @@ -221,7 +221,7 @@ describe NewUser::DossiersController, type: :controller do it 'sends an email only on the first #update' do delivery = double - expect(delivery).to receive(:deliver_now!).with(no_args) + expect(delivery).to receive(:deliver_later).with(no_args) expect(NotificationMailer).to receive(:send_initiated_notification) .and_return(delivery) diff --git a/spec/controllers/users/description_controller_shared_example.rb b/spec/controllers/users/description_controller_shared_example.rb index 08595cf04..f9d8f755b 100644 --- a/spec/controllers/users/description_controller_shared_example.rb +++ b/spec/controllers/users/description_controller_shared_example.rb @@ -133,7 +133,7 @@ shared_examples 'description_controller_spec' do it 'sets the state of the dossier before sending the mail' do sender = double("notification sender") - allow(sender).to receive(:deliver_now!) + allow(sender).to receive(:deliver_later) expect(NotificationMailer) .to receive(:send_initiated_notification) .with(have_attributes(en_construction_at: DateTime.now)) diff --git a/spec/controllers/users/dossiers/commentaires_controller_spec.rb b/spec/controllers/users/dossiers/commentaires_controller_spec.rb index 00519c3e2..3cae043b9 100644 --- a/spec/controllers/users/dossiers/commentaires_controller_spec.rb +++ b/spec/controllers/users/dossiers/commentaires_controller_spec.rb @@ -24,7 +24,7 @@ describe Users::Dossiers::CommentairesController, type: :controller do it 'should notify user' do expect(NotificationMailer).to receive(:new_answer).and_return(NotificationMailer) - expect(NotificationMailer).to receive(:deliver_now!) + expect(NotificationMailer).to receive(:deliver_later) subject end diff --git a/spec/features/new_gestionnaire/gestionnaire_spec.rb b/spec/features/new_gestionnaire/gestionnaire_spec.rb index c67cbe71f..5c7617d17 100644 --- a/spec/features/new_gestionnaire/gestionnaire_spec.rb +++ b/spec/features/new_gestionnaire/gestionnaire_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' feature 'The gestionnaire part' do + include ActiveJob::TestHelper + let(:password) { 'secret_password' } let!(:gestionnaire) { create(:gestionnaire, password: password) } @@ -56,6 +58,7 @@ feature 'The gestionnaire part' do scenario 'A gestionnaire can use avis' do ActionMailer::Base.deliveries = [] + ActiveJob::Base.queue_adapter = :test log_in(gestionnaire.email, password) @@ -66,7 +69,10 @@ feature 'The gestionnaire part' do expect(page).to have_current_path(avis_gestionnaire_dossier_path(procedure, dossier)) expert_email = 'expert@tps.com' - ask_confidential_avis(expert_email, 'a good introduction') + + perform_enqueued_jobs do + ask_confidential_avis(expert_email, 'a good introduction') + end log_out diff --git a/spec/jobs/find_dubious_procedures_job_spec.rb b/spec/jobs/find_dubious_procedures_job_spec.rb index 8303b9d39..2c1fb450a 100644 --- a/spec/jobs/find_dubious_procedures_job_spec.rb +++ b/spec/jobs/find_dubious_procedures_job_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' RSpec.describe FindDubiousProceduresJob, type: :job do describe 'perform' do - let(:mailer_double) { double('mailer', deliver_now: true) } + let(:mailer_double) { double('mailer', deliver_later: true) } let(:procedure) { create(:procedure) } let(:allowed_tdc) { create(:type_de_champ, libelle: 'fournir') } diff --git a/spec/models/avis_spec.rb b/spec/models/avis_spec.rb index 2975e0d5d..c692f0d60 100644 --- a/spec/models/avis_spec.rb +++ b/spec/models/avis_spec.rb @@ -90,7 +90,7 @@ RSpec.describe Avis, type: :model do describe '#notify_gestionnaire' do context 'when an avis is created' do before do - avis_invitation_double = double('avis_invitation', deliver_now: true) + avis_invitation_double = double('avis_invitation', deliver_later: true) allow(AvisMailer).to receive(:avis_invitation).and_return(avis_invitation_double) Avis.create(claimant: claimant, email: 'email@l.com') end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 36062085d..09501c4d8 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' describe Dossier do + include ActiveJob::TestHelper + let(:user) { create(:user) } describe "without_followers scope" do @@ -619,7 +621,12 @@ describe Dossier do end it "send an email when the dossier is created for the very first time" do - expect { Dossier.create(procedure: procedure, state: "brouillon", user: user) }.to change(ActionMailer::Base.deliveries, :size).from(0).to(1) + ActiveJob::Base.queue_adapter = :test + expect do + perform_enqueued_jobs do + Dossier.create(procedure: procedure, state: "brouillon", user: user) + end + end.to change(ActionMailer::Base.deliveries, :size).from(0).to(1) mail = ActionMailer::Base.deliveries.last expect(mail.subject).to eq("Retrouvez votre brouillon pour la démarche : #{procedure.libelle}")