From 5bf580b6ac1fce80b16d8ace8a3b6f133797569e Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Tue, 26 Mar 2024 18:37:25 +0100 Subject: [PATCH] feat(mail): link with hosts for recipient for notification mailer --- .../mailer_headers_configurable_concern.rb | 3 ++- app/mailers/notification_mailer.rb | 3 +++ spec/mailers/notification_mailer_spec.rb | 15 ++++++++++++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/mailers/concerns/mailer_headers_configurable_concern.rb b/app/mailers/concerns/mailer_headers_configurable_concern.rb index 03c07491a..b5d1c1731 100644 --- a/app/mailers/concerns/mailer_headers_configurable_concern.rb +++ b/app/mailers/concerns/mailer_headers_configurable_concern.rb @@ -27,7 +27,8 @@ module MailerHeadersConfigurableConcern end self.class.default from: from, reply_to: from - self.class.default_url_options = { host: Current.host } + self.class.default_url_options[:host] = Current.host + Rails.application.routes.default_url_options[:host] = Current.host # link generated by url helpers in tags substitutions self.class.asset_host = Current.application_base_url end diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index dab4dfaab..70f5b4091 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -33,6 +33,8 @@ class NotificationMailer < ApplicationMailer return end + configure_defaults_for_user(@dossier.user) + @subject = "Votre dossier rempli par le mandataire #{@dossier.mandataire_first_name} #{@dossier.mandataire_last_name} a été mis à jour" @email = @dossier.individual.email @logo_url = procedure_logo_url(@dossier.procedure) @@ -78,6 +80,7 @@ class NotificationMailer < ApplicationMailer def set_dossier @dossier = params[:dossier] + configure_defaults_for_user(@dossier.user) if @dossier.skip_user_notification_email? mail.perform_deliveries = false diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb index 3ab227b22..0af291a71 100644 --- a/spec/mailers/notification_mailer_spec.rb +++ b/spec/mailers/notification_mailer_spec.rb @@ -73,8 +73,8 @@ RSpec.describe NotificationMailer, type: :mailer do end it 'renders the actions' do - expect(mail.body).to have_link('Consulter mon dossier', href: dossier_url(dossier)) - expect(mail.body).to have_link('J’ai une question', href: messagerie_dossier_url(dossier)) + expect(mail.body).to have_link('Consulter mon dossier', href: dossier_url(dossier, host: ENV.fetch("APP_HOST_LEGACY"))) + expect(mail.body).to have_link('J’ai une question', href: messagerie_dossier_url(dossier, host: ENV.fetch("APP_HOST_LEGACY"))) end context 'when the template body contains tags' do @@ -85,7 +85,16 @@ RSpec.describe NotificationMailer, type: :mailer do end it 'replaces link tags with a clickable link' do - expect(mail.body).to have_link(dossier_url(dossier)) + expect(mail.body).to have_link(dossier_url(dossier, host: ENV.fetch("APP_HOST_LEGACY"))) + end + + context "when user has preferred domain" do + let(:user) { create(:user, preferred_domain: :demarches_gouv_fr) } + + it do + expect(mail.body).to have_link(dossier_url(dossier, host: ENV.fetch("APP_HOST"))) + expect(header_value("From", mail)).to include("@demarches.gouv.fr") + end end end