From 584c47a1f8398adaca242822aba9919932fedaed Mon Sep 17 00:00:00 2001 From: Stuart Owen Date: Thu, 21 Nov 2024 13:51:48 +0000 Subject: [PATCH] an enhanced MailDeliveryJob that propagates smtp_settings #2070 --- app/jobs/enhanced_mail_delivery_job.rb | 10 +++++++ config/application.rb | 2 +- .../jobs/enhanced_mail_delivery_job_test.rb | 29 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 app/jobs/enhanced_mail_delivery_job.rb create mode 100644 test/unit/jobs/enhanced_mail_delivery_job_test.rb diff --git a/app/jobs/enhanced_mail_delivery_job.rb b/app/jobs/enhanced_mail_delivery_job.rb new file mode 100644 index 0000000000..a4df82534b --- /dev/null +++ b/app/jobs/enhanced_mail_delivery_job.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class EnhancedMailDeliveryJob < ActionMailer::MailDeliveryJob + # makes sure any changes to the smtp and host settings are picked up without having to restart delayed job + before_perform do + # make sure the SMTP,site_base_host configuration is in sync with current SEEK settings + Seek::Config.smtp_propagate + Seek::Config.site_base_host_propagate + end +end diff --git a/config/application.rb b/config/application.rb index 1dc9b15b5d..de2a33c666 100644 --- a/config/application.rb +++ b/config/application.rb @@ -72,7 +72,7 @@ class Application < Rails::Application config.i18n.load_path += Dir[Rails.root.join('config', 'locales', 'overrides', '**', '*.{rb,yml}')] unless Rails.env.test? config.active_record.belongs_to_required_by_default = false - config.action_mailer.delivery_job = 'ActionMailer::MailDeliveryJob' # Can remove after updating defaults + config.action_mailer.delivery_job = 'EnhancedMailDeliveryJob' # sets the configured SMTP settngs before each run config.action_mailer.preview_path = "#{Rails.root}/test/mailers/previews" # For some reason it is looking in spec/ by default end end diff --git a/test/unit/jobs/enhanced_mail_delivery_job_test.rb b/test/unit/jobs/enhanced_mail_delivery_job_test.rb new file mode 100644 index 0000000000..aec9d0ac17 --- /dev/null +++ b/test/unit/jobs/enhanced_mail_delivery_job_test.rb @@ -0,0 +1,29 @@ +require 'test_helper' +require 'minitest/mock' + +class EnhancedMailDeliveryJobTest < ActiveSupport::TestCase + test 'perform' do + with_config_value(:email_enabled, true) do + + assert_enqueued_jobs(1, only: EnhancedMailDeliveryJob, queue: QueueNames::MAILERS) do + Mailer.test_email('fred@email.com').deliver_later + end + + smtp_propagate_called = false + site_base_host_propagate_called = false + + Seek::Config.stub :smtp_propagate, ->{smtp_propagate_called = true} do + Seek::Config.stub :site_base_host_propagate, ->{site_base_host_propagate_called = true} do + assert_emails 1 do + assert_performed_jobs(1, only: EnhancedMailDeliveryJob) do + Mailer.test_email('fred@email.com').deliver_later + end + end + end + end + + assert smtp_propagate_called + assert site_base_host_propagate_called + end + end +end \ No newline at end of file