feat(mail): from and link with host for recipient in dossier_mailer

This commit is contained in:
Colin Darie 2024-03-27 09:16:40 +01:00
parent 3512e071d3
commit f8a9e72aa2
No known key found for this signature in database
GPG key ID: 8C76CADD40253590
7 changed files with 75 additions and 9 deletions

View file

@ -1 +1,2 @@
APP_HOST="test.host" # must match host defined in spec/rails_helper.rb
APP_HOST_LEGACY="test-legacy.host"

View file

@ -1,5 +1,6 @@
class ApplicationMailer < ActionMailer::Base
include MailerDolistConcern
include MailerHeadersConfigurableConcern
include MailerMonitoringConcern
include BalancedDeliveryConcern
include PriorityDeliveryConcern

View file

@ -3,6 +3,8 @@ module MailerHeadersConfigurableConcern
included do
def configure_defaults_for_user(user)
return if user.nil? || !user.is_a?(User) # not for super-admins
I18n.locale = user.locale
if user.preferred_domain_demarches_gouv_fr?
@ -17,10 +19,22 @@ module MailerHeadersConfigurableConcern
Current.no_reply_email = NO_REPLY_EMAIL
end
from = "#{Current.application_name} <#{Current.contact_email}>"
@@original_default_from ||= self.class.default[:from]
from = if @@original_default_from.include?(NO_REPLY_EMAIL)
Current.no_reply_email
else
"#{Current.application_name} <#{Current.contact_email}>"
end
self.class.default from: from, reply_to: from
self.class.default_url_options = { host: Current.host }
self.class.asset_host = Current.application_base_url
end
def configure_defaults_for_email(email)
user = User.find_by(email: email)
configure_defaults_for_user(user)
end
end
end

View file

@ -17,6 +17,8 @@ class DossierMailer < ApplicationMailer
def notify_new_draft
@dossier = params[:dossier]
configure_defaults_for_user(@dossier.user)
I18n.with_locale(@dossier.user_locale) do
@service = @dossier.procedure.service
@logo_url = procedure_logo_url(@dossier.procedure)
@ -31,6 +33,8 @@ class DossierMailer < ApplicationMailer
def notify_new_answer
commentaire = params[:commentaire]
dossier = commentaire.dossier
configure_defaults_for_user(dossier.user)
I18n.with_locale(dossier.user_locale) do
@dossier = dossier
@service = dossier.procedure.service
@ -45,6 +49,8 @@ class DossierMailer < ApplicationMailer
end
def notify_new_commentaire_to_instructeur(dossier, instructeur_email)
configure_defaults_for_email(instructeur_email)
I18n.with_locale(dossier.user_locale) do
@dossier = dossier
@subject = default_i18n_subject(dossier_id: dossier.id, libelle_demarche: dossier.procedure.libelle)
@ -56,6 +62,8 @@ class DossierMailer < ApplicationMailer
def notify_pending_correction
commentaire = params[:commentaire]
dossier = commentaire.dossier
configure_defaults_for_user(dossier.user)
I18n.with_locale(dossier.user_locale) do
@dossier = dossier
@service = dossier.procedure.service
@ -71,6 +79,8 @@ class DossierMailer < ApplicationMailer
end
def notify_new_avis_to_instructeur(avis, instructeur_email)
configure_defaults_for_email(instructeur_email)
I18n.with_locale(avis.dossier.user_locale) do
@avis = avis
@subject = default_i18n_subject(dossier_id: avis.dossier.id, libelle_demarche: avis.procedure.libelle)
@ -80,6 +90,8 @@ class DossierMailer < ApplicationMailer
end
def notify_new_dossier_depose_to_instructeur(dossier, instructeur_email)
configure_defaults_for_email(instructeur_email)
I18n.with_locale(dossier.user_locale) do
@dossier = dossier
@subject = default_i18n_subject(dossier_id: dossier.id, libelle_demarche: dossier.procedure.libelle)
@ -89,6 +101,8 @@ class DossierMailer < ApplicationMailer
end
def notify_brouillon_near_deletion(dossiers, to_email)
configure_defaults_for_email(to_email)
I18n.with_locale(dossiers.first.user_locale) do
@subject = default_i18n_subject(count: dossiers.size)
@dossiers = dossiers
@ -98,6 +112,8 @@ class DossierMailer < ApplicationMailer
end
def notify_brouillon_deletion(dossier_hashes, to_email)
configure_defaults_for_email(to_email)
@subject = default_i18n_subject(count: dossier_hashes.size)
@dossier_hashes = dossier_hashes
@ -105,6 +121,8 @@ class DossierMailer < ApplicationMailer
end
def notify_en_construction_deletion_to_administration(dossier, to_email)
configure_defaults_for_email(to_email)
@subject = default_i18n_subject(dossier_id: dossier.id)
@dossier = dossier
@ -112,6 +130,8 @@ class DossierMailer < ApplicationMailer
end
def notify_deletion_to_administration(deleted_dossier, to_email)
configure_defaults_for_email(to_email)
@subject = default_i18n_subject(dossier_id: deleted_dossier.dossier_id)
@deleted_dossier = deleted_dossier
@ -119,6 +139,8 @@ class DossierMailer < ApplicationMailer
end
def notify_automatic_deletion_to_user(deleted_dossiers, to_email)
configure_defaults_for_email(to_email)
I18n.with_locale(deleted_dossiers.first.user_locale) do
@state = deleted_dossiers.first.state
@subject = default_i18n_subject(count: deleted_dossiers.size)
@ -129,6 +151,8 @@ class DossierMailer < ApplicationMailer
end
def notify_automatic_deletion_to_administration(deleted_dossiers, to_email)
configure_defaults_for_email(to_email)
@subject = default_i18n_subject(count: deleted_dossiers.size)
@deleted_dossiers = deleted_dossiers
@ -136,6 +160,8 @@ class DossierMailer < ApplicationMailer
end
def notify_near_deletion_to_user(dossiers, to_email)
configure_defaults_for_email(to_email)
I18n.with_locale(dossiers.first.user_locale) do
@state = dossiers.first.state
@subject = default_i18n_subject(count: dossiers.size, state: @state)
@ -146,6 +172,8 @@ class DossierMailer < ApplicationMailer
end
def notify_near_deletion_to_administration(dossiers, to_email)
configure_defaults_for_email(to_email)
@state = dossiers.first.state
@subject = default_i18n_subject(count: dossiers.size, state: @state)
@dossiers = dossiers
@ -154,6 +182,8 @@ class DossierMailer < ApplicationMailer
end
def notify_groupe_instructeur_changed(instructeur, dossier)
configure_defaults_for_user(instructeur.user)
@subject = default_i18n_subject(dossier_id: dossier.id)
@dossier = dossier
@ -161,6 +191,8 @@ class DossierMailer < ApplicationMailer
end
def notify_brouillon_not_submitted(dossier)
configure_defaults_for_user(dossier.user)
I18n.with_locale(dossier.user_locale) do
@subject = default_i18n_subject(dossier_id: dossier.id)
@dossier = dossier
@ -172,6 +204,8 @@ class DossierMailer < ApplicationMailer
def notify_transfer
@transfer = params[:dossier_transfer]
configure_defaults_for_email(@transfer.email)
I18n.with_locale(@transfer.user_locale) do
@subject = default_i18n_subject()

View file

@ -65,8 +65,4 @@ RSpec.describe DeviseUserMailer, type: :mailer do
end
end
end
def header_value(name, message)
message.header.fields.find { _1.name == name }.value
end
end

View file

@ -12,18 +12,28 @@ RSpec.describe DossierMailer, type: :mailer do
end
describe '.notify_new_draft' do
let(:dossier) { create(:dossier, procedure: create(:simple_procedure, :with_auto_archive)) }
let(:user) { create(:user) }
let(:dossier) { create(:dossier, procedure: create(:simple_procedure, :with_auto_archive), user:) }
subject { described_class.with(dossier:).notify_new_draft }
it { expect(subject.subject).to include("brouillon") }
it { expect(subject.subject).to include(dossier.procedure.libelle) }
it { expect(subject.body).to include(dossier.procedure.libelle) }
it { expect(subject.body).to include(dossier_url(dossier)) }
it { expect(subject.body).to include(dossier_url(dossier, host: ENV.fetch("APP_HOST_LEGACY"))) }
it { expect(subject.body).to include("Vous pouvez déposer votre dossier jusquau") }
it { expect(subject.body).to include("heure de") }
it_behaves_like 'a dossier notification'
context "user prefers new domain" do
let(:user) { create(:user, preferred_domain: :demarches_gouv_fr) }
it do
expect(subject.body).to include(dossier_url(dossier, host: ENV.fetch('APP_HOST')))
expect(header_value("From", subject)).to include("ne-pas-repondre@demarches.gouv.fr")
end
end
end
describe '.notify_new_answer with dossier brouillon' do
@ -36,7 +46,7 @@ RSpec.describe DossierMailer, type: :mailer do
it { expect(subject.subject).to include("Nouveau message") }
it { expect(subject.subject).to include(dossier.id.to_s) }
it { expect(subject.body).to include(dossier.procedure.service.email) }
it { expect(subject.body).not_to include(messagerie_dossier_url(dossier)) }
it { expect(subject.body).not_to include(messagerie_dossier_url(dossier, host: ENV.fetch("APP_HOST_LEGACY"))) }
it_behaves_like 'a dossier notification'
@ -54,7 +64,7 @@ RSpec.describe DossierMailer, type: :mailer do
it { expect(subject.subject).to include("Nouveau message") }
it { expect(subject.subject).to include(dossier.id.to_s) }
it { expect(subject.body).to include(messagerie_dossier_url(dossier)) }
it { expect(subject.body).to include(messagerie_dossier_url(dossier, host: ENV.fetch("APP_HOST_LEGACY"))) }
it_behaves_like 'a dossier notification'
end
@ -280,6 +290,11 @@ RSpec.describe DossierMailer, type: :mailer do
it { expect(subject.body).to include(procedure.libelle.to_s) }
end
context 'when recipient has preferred domain' do
let(:dossier_transfer) { create(:dossier_transfer, email: create(:user, preferred_domain: :demarches_gouv_fr).email) }
it { expect(subject.body).to include(dossiers_url(statut: "dossiers-transferes", host: ENV.fetch("APP_HOST"))) }
end
context 'when it is a transfer of multiple dossiers' do
let!(:dossier2) { create(:dossier, user: user, transfer: dossier_transfer, procedure: procedure) }
it { expect(subject.subject).to include("Vous avez une demande de transfert en attente.") }

5
spec/support/mailer.rb Normal file
View file

@ -0,0 +1,5 @@
# frozen_string_literal: true
def header_value(name, message)
message.header.fields.find { _1.name == name }.value
end