Skip to content

Commit

Permalink
LG-12294: Fix asynchronous email delivery for aggregated sign-in email (
Browse files Browse the repository at this point in the history
#10421)

* Fix asynchronous email delivery for aggregated sign-in email

changelog: Upcoming Features, Sign In, Send single aggregated email notification for new device sign-in

* Add exclusion for current sync-only mailers

* Default synchronous_only to false
  • Loading branch information
aduth authored Apr 15, 2024
1 parent 122a095 commit 1a0ba9c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/services/user_alerts/alert_user_about_new_device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def self.send_alert(user:, disavowal_event:, disavowal_token:)
'sign_in_unsuccessful_2fa',
'sign_in_after_2fa',
],
).order(:created_at).includes(:device)
).order(:created_at).includes(:device).to_a

user.confirmed_email_addresses.each do |email_address|
mailer = UserMailer.with(user:, email_address:)
Expand Down
2 changes: 1 addition & 1 deletion spec/mailers/report_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)
end

it_behaves_like 'a system email'
it_behaves_like 'a system email', synchronous_only: true

it 'sends to the current email' do
expect(mail.to).to eq [email_address.email]
Expand Down
28 changes: 27 additions & 1 deletion spec/mailers/user_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'rails_helper'

RSpec.describe UserMailer, type: :mailer do
let(:user) { build(:user) }
let(:user) { create(:user) }
let(:email_address) { user.email_addresses.first }
let(:banned_email) { '[email protected]' }
let(:banned_email_address) { create(:email_address, email: banned_email, user: user) }
Expand Down Expand Up @@ -205,6 +205,32 @@
end
end

describe '#new_device_sign_in_before_2fa' do
let(:event) { create(:event, event_type: :sign_in_before_2fa, user:, device: create(:device)) }
subject(:mail) do
UserMailer.with(user:, email_address:).new_device_sign_in_before_2fa(
events: user.events.where(event_type: 'sign_in_before_2fa').includes(:device).to_a,
disavowal_token: 'token',
)
end

it_behaves_like 'a system email'
it_behaves_like 'an email that respects user email locale preference'
end

describe '#new_device_sign_in_after_2fa' do
let(:event) { create(:event, event_type: :sign_in_after_2fa, user:, device: create(:device)) }
subject(:mail) do
UserMailer.with(user:, email_address:).new_device_sign_in_after_2fa(
events: user.events.where(event_type: 'sign_in_after_2fa').includes(:device).to_a,
disavowal_token: 'token',
)
end

it_behaves_like 'a system email'
it_behaves_like 'an email that respects user email locale preference'
end

describe '#personal_key_regenerated' do
let(:mail) do
UserMailer.with(user: user, email_address: email_address).personal_key_regenerated
Expand Down
6 changes: 5 additions & 1 deletion spec/support/shared_examples_for_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.shared_examples 'a system email' do
RSpec.shared_examples 'a system email' do |synchronous_only: false|
it 'is from the default email' do
expect(mail.from).to eq [IdentityConfig.store.email_from]
expect(mail[:from].display_names).to eq [IdentityConfig.store.email_from_display_name]
Expand All @@ -10,6 +10,10 @@
# https://www.caniemail.com/features/image-svg/
expect(body).not_to have_css('img[src$=".svg"]')
end

it 'does not error when delivered asynchronously', unless: synchronous_only do
mail.deliver_later
end
end

# expects there to be a let(:user) in scope
Expand Down

0 comments on commit 1a0ba9c

Please sign in to comment.