feat(mail): devise mailer respect user preferred host for any mail

This commit is contained in:
Colin Darie 2024-03-25 20:12:12 +01:00
parent d14fe83261
commit 3512e071d3
No known key found for this signature in database
GPG key ID: 8C76CADD40253590
2 changed files with 31 additions and 0 deletions

View file

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

View file

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