Browse Source

Rescue Notify badrequest errors (#1828)

pull/1833/head
Jack 1 year ago committed by GitHub
parent
commit
981e824652
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      app/mailers/devise_notify_mailer.rb
  2. 20
      spec/mailers/devise_notify_mailer_spec.rb

10
app/mailers/devise_notify_mailer.rb

@ -5,14 +5,18 @@ class DeviseNotifyMailer < Devise::Mailer
@notify_client ||= ::Notifications::Client.new(ENV["GOVUK_NOTIFY_API_KEY"])
end
def send_email(email, template_id, personalisation)
return true if intercept_send?(email)
def send_email(email_address, template_id, personalisation)
return true if intercept_send?(email_address)
notify_client.send_email(
email_address: email,
email_address:,
template_id:,
personalisation:,
)
rescue Notifications::Client::BadRequestError => e
Sentry.capture_exception(e)
true
end
def personalisation(record, token, url, username: false)

20
spec/mailers/devise_notify_mailer_spec.rb

@ -8,6 +8,11 @@ RSpec.describe DeviseNotifyMailer do
let(:name) { "test" }
let(:password) { "password" }
let(:role) { "data_coordinator" }
let(:bad_request_error) do
# rubocop:disable RSpec/VerifiedDoubles
Notifications::Client::BadRequestError.new(double(code: 200, body: ""))
# rubocop:enable RSpec/VerifiedDoubles
end
before do
allow(described_class).to receive(:new).and_return(devise_notify_mailer)
@ -39,6 +44,21 @@ RSpec.describe DeviseNotifyMailer do
User.create!(name:, organisation:, email:, password:, role:)
end
end
context "when notify mailer raises BadRequestError" do
before do
allow(notify_client).to receive(:send_email).and_raise(bad_request_error)
end
let(:domain) { Rails.application.credentials[:email_allowlist].first }
let(:email) { "test@#{domain}" }
it "does not raise an error" do
expect {
User.create!(name:, organisation:, email:, password:, role:)
}.not_to raise_error
end
end
end
context "when the rails environment is not staging" do

Loading…
Cancel
Save