-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correct the mailer to always send
to
as an array
- Loading branch information
Showing
3 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |