Skip to content

Commit

Permalink
Correct the mailer to always send to as an array
Browse files Browse the repository at this point in the history
  • Loading branch information
elken committed Dec 13, 2024
1 parent 9c2df9c commit 0cef2b6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/mailers/email_to_provider_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def notify(message_class, submission)
message = message_class.new(submission.latest_version.application)
set_template(message.template)
set_personalisation(**message.contents)
mail(to: message.recipient)
mail(to: Array(message.recipient))
end
end
# :nocov:
Expand Down
14 changes: 13 additions & 1 deletion config/initializers/govuk_notify.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
ActionMailer::Base.add_delivery_method :govuk_notify, GovukNotifyRails::Delivery,
api_key: ENV.fetch('GOVUK_NOTIFY_API_KEY', nil)
api_key: ENV.fetch("GOVUK_NOTIFY_API_KEY", nil)

class MailDeliveryJobWrapper < ActionMailer::MailDeliveryJob
def serialize
super.tap do |args|
args["arguments"][3] = Array(args["arguments"][3])
end
end
end

# Set this so we ensure that the `to` list we pass to
# govuk_notify_rails is always an array
Rails.application.config.action_mailer.delivery_job = "MailDeliveryJobWrapper"
37 changes: 37 additions & 0 deletions spec/jobs/mail_delivery_job_wrapper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "rails_helper"

# rubocop:disable Rails/ApplicationMailer, Rails/I18nLocaleTexts
RSpec.describe MailDeliveryJobWrapper do
before do
ActionMailer::Base.add_delivery_method :govuk_notify, GovukNotifyRails::Delivery,
api_key: "fake-test-key"
end

it "ensures 'to' is always an array" do
mailer_class = Class.new(ActionMailer::Base) do
default delivery_method: :govuk_notify

def test_email
mail(
to: "invalid@email.",
subject: "Test",
body: "Test content",
)
end
end

mail = mailer_class.test_email
job = described_class.set(queue: :mailers).perform_later(
mailer_class.name,
"test_email",
"deliver_now",
mail.to,
{},
)

serialized_job = job.serialize
args = serialized_job["arguments"]
expect(args[3]).to be_an(Array)
end
end
# rubocop:enable Rails/ApplicationMailer, Rails/I18nLocaleTexts

0 comments on commit 0cef2b6

Please sign in to comment.