From 3512e071d315df889652550d7958cad7f4caae6f Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Mon, 25 Mar 2024 20:12:12 +0100 Subject: [PATCH] feat(mail): devise mailer respect user preferred host for any mail --- app/mailers/devise_user_mailer.rb | 11 +++++++++++ spec/mailers/devise_user_mailer_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/app/mailers/devise_user_mailer.rb b/app/mailers/devise_user_mailer.rb index a37931415..03ee64bcb 100644 --- a/app/mailers/devise_user_mailer.rb +++ b/app/mailers/devise_user_mailer.rb @@ -10,11 +10,22 @@ class DeviseUserMailer < Devise::Mailer include PriorityDeliveryConcern layout 'mailers/layout' + default from: "#{APPLICATION_NAME} <#{CONTACT_EMAIL}>" def template_paths ['devise_mailer'] end + # Note: this devise hook (like any callback) is called *after* the action, + # because we use mailers with Mailer.action_name() syntax + # instead of parameterized Mailer.with().action_name. + # So any action using Current must manually call `configure_defaults_for_user` + def initialize_from_record(record) + configure_defaults_for_user(record) + + super + end + def confirmation_instructions(record, token, opts = {}) configure_defaults_for_user(record) diff --git a/spec/mailers/devise_user_mailer_spec.rb b/spec/mailers/devise_user_mailer_spec.rb index 4f92bd4be..e04c04ff3 100644 --- a/spec/mailers/devise_user_mailer_spec.rb +++ b/spec/mailers/devise_user_mailer_spec.rb @@ -44,6 +44,26 @@ RSpec.describe DeviseUserMailer, type: :mailer do end end end + + context "reset password instructions" do + subject { described_class.reset_password_instructions(user, token) } + + context "legacy domain" do + it "respect preferred domain" do + expect(header_value("From", subject.message)).to include(CONTACT_EMAIL) + expect(subject.message.to_s).to include("#{ENV.fetch("APP_HOST_LEGACY")}/users/password") + end + end + + context "new domain" do + let(:user) { create(:user, preferred_domain: :demarches_gouv_fr) } + + it "respect preferred domain" do + expect(header_value("From", subject.message)).to include("@demarches.gouv.fr") + expect(subject.message.to_s).to include("#{ENV.fetch("APP_HOST")}/users/password") + end + end + end end def header_value(name, message)