feat(mail): devise confirmation instructions respect user preferred host
This commit is contained in:
parent
863784a1a9
commit
d14fe83261
4 changed files with 70 additions and 6 deletions
26
app/mailers/concerns/mailer_headers_configurable_concern.rb
Normal file
26
app/mailers/concerns/mailer_headers_configurable_concern.rb
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
module MailerHeadersConfigurableConcern
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
included do
|
||||||
|
def configure_defaults_for_user(user)
|
||||||
|
I18n.locale = user.locale
|
||||||
|
|
||||||
|
if user.preferred_domain_demarches_gouv_fr?
|
||||||
|
Current.application_name = "demarches.gouv.fr"
|
||||||
|
Current.host = ENV.fetch("APP_HOST")
|
||||||
|
Current.contact_email = "contact@demarches.gouv.fr"
|
||||||
|
Current.no_reply_email = NO_REPLY_EMAIL.sub("demarches-simplifiees.fr", "demarches.gouv.fr") # rubocop:disable DS/ApplicationName
|
||||||
|
else
|
||||||
|
Current.application_name = APPLICATION_NAME
|
||||||
|
Current.host = ENV["APP_HOST_LEGACY"] || ENV.fetch("APP_HOST") # _LEGACY is optional, fallbagck to default when unset
|
||||||
|
Current.contact_email = CONTACT_EMAIL
|
||||||
|
Current.no_reply_email = NO_REPLY_EMAIL
|
||||||
|
end
|
||||||
|
|
||||||
|
from = "#{Current.application_name} <#{Current.contact_email}>"
|
||||||
|
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
|
||||||
|
end
|
||||||
|
end
|
|
@ -5,6 +5,7 @@ class DeviseUserMailer < Devise::Mailer
|
||||||
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
|
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
|
||||||
include MailerDolistConcern
|
include MailerDolistConcern
|
||||||
include MailerMonitoringConcern
|
include MailerMonitoringConcern
|
||||||
|
include MailerHeadersConfigurableConcern
|
||||||
include BalancedDeliveryConcern
|
include BalancedDeliveryConcern
|
||||||
include PriorityDeliveryConcern
|
include PriorityDeliveryConcern
|
||||||
|
|
||||||
|
@ -15,7 +16,10 @@ class DeviseUserMailer < Devise::Mailer
|
||||||
end
|
end
|
||||||
|
|
||||||
def confirmation_instructions(record, token, opts = {})
|
def confirmation_instructions(record, token, opts = {})
|
||||||
opts[:from] = NO_REPLY_EMAIL
|
configure_defaults_for_user(record)
|
||||||
|
|
||||||
|
opts[:from] = Current.no_reply_email
|
||||||
|
opts[:reply_to] = Current.no_reply_email
|
||||||
@procedure = opts[:procedure_after_confirmation] || nil
|
@procedure = opts[:procedure_after_confirmation] || nil
|
||||||
@prefill_token = opts[:prefill_token]
|
@prefill_token = opts[:prefill_token]
|
||||||
super
|
super
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
class Current < ActiveSupport::CurrentAttributes
|
class Current < ActiveSupport::CurrentAttributes
|
||||||
attribute :user, :request_id, :browser,
|
attribute :application_base_url
|
||||||
:host, :application_name, :contact_email,
|
attribute :application_name
|
||||||
:application_base_url
|
attribute :browser
|
||||||
|
attribute :contact_email
|
||||||
|
attribute :host
|
||||||
|
attribute :no_reply_email
|
||||||
|
attribute :request_id
|
||||||
|
attribute :user
|
||||||
end
|
end
|
||||||
|
|
|
@ -16,8 +16,37 @@ RSpec.describe DeviseUserMailer, type: :mailer do
|
||||||
|
|
||||||
context 'when perform_later is called' do
|
context 'when perform_later is called' do
|
||||||
it 'enqueues email in default queue for high priority delivery' do
|
it 'enqueues email in default queue for high priority delivery' do
|
||||||
expect { subject.deliver_later }.to have_enqueued_job.on_queue(Rails.application.config.action_mailer.deliver_later_queue_name)
|
expect { subject.deliver_later }.to have_enqueued_job.on_queue(Rails.application.config.action_mailer.deliver_later_queue_name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'headers for user' do
|
||||||
|
context "confirmation email" do
|
||||||
|
subject { described_class.confirmation_instructions(user, token, opts = {}) }
|
||||||
|
|
||||||
|
context "legacy domain" do
|
||||||
|
it "respect preferred domain" do
|
||||||
|
expect(header_value("From", subject.message)).to eq(NO_REPLY_EMAIL)
|
||||||
|
expect(header_value("Reply-To", subject.message)).to eq(NO_REPLY_EMAIL)
|
||||||
|
expect(subject.message.to_s).to include("#{ENV.fetch("APP_HOST_LEGACY")}/users/confirmation")
|
||||||
|
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 eq("Ne pas répondre <ne-pas-repondre@demarches.gouv.fr>")
|
||||||
|
expect(header_value("Reply-To", subject.message)).to eq("Ne pas répondre <ne-pas-repondre@demarches.gouv.fr>")
|
||||||
|
expect(subject.message.to_s).to include("#{ENV.fetch("APP_HOST")}/users/confirmation")
|
||||||
|
expect(subject.message.to_s).to include("//#{ENV.fetch("APP_HOST")}/assets/mailer/republique")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def header_value(name, message)
|
||||||
|
message.header.fields.find { _1.name == name }.value
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue