diff --git a/app/models/user.rb b/app/models/user.rb index 965bc0c22..e0ca51a9d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -132,7 +132,8 @@ class User < ApplicationRecord end def send_devise_notification(notification, *) - devise_mailer.with(account: Current.account).send(notification, self, *).deliver_later + account = Current.account || accounts.first + devise_mailer.with(account: account).send(notification, self, *).deliver_later end def set_password_and_uid diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index f9eef4ef1..1afa21367 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -283,6 +283,33 @@ RSpec.describe User do end end + describe '#send_devise_notification' do + let(:account) { create(:account, locale: 'pt_BR') } + let(:recipient) { create(:user, account: account) } + let(:mailer_double) { double(reset_password_instructions: double(deliver_later: nil)) } # rubocop:disable RSpec/VerifiedDoubles + + before do + Current.reset + allow(Devise::Mailer).to receive(:with).and_return(mailer_double) + end + + it 'falls back to the user account when Current.account is nil' do + recipient.send_reset_password_instructions + + expect(Devise::Mailer).to have_received(:with).with(account: account) + end + + it 'prefers Current.account when it is set' do + other_account = create(:account) + create(:account_user, user: recipient, account: other_account) + Current.account = other_account + + recipient.send_reset_password_instructions + + expect(Devise::Mailer).to have_received(:with).with(account: other_account) + end + end + describe 'destroy' do it 'nullifies scheduled messages author when user has sent scheduled messages' do account = create(:account)