[Fix #1972] Always send mails asynchronously

This commit is contained in:
Frederic Merizen 2018-05-25 18:05:28 +02:00
parent cce00e19c6
commit 07b1c147d9
23 changed files with 45 additions and 32 deletions

View file

@ -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

View file

@ -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})"

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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,

View file

@ -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

View file

@ -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?

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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 } }

View file

@ -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)

View file

@ -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))

View file

@ -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

View file

@ -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

View file

@ -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') }

View file

@ -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

View file

@ -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}")