From b39c7869f911f4a66fe2ff36fe39b22b1b86d037 Mon Sep 17 00:00:00 2001 From: Jack S Date: Fri, 4 Aug 2023 16:06:14 +0100 Subject: [PATCH] Only send required confirmation emails --- app/mailers/devise_notify_mailer.rb | 8 ++++---- spec/requests/users_controller_spec.rb | 7 +++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/mailers/devise_notify_mailer.rb b/app/mailers/devise_notify_mailer.rb index 0edf70ef0..043e13550 100644 --- a/app/mailers/devise_notify_mailer.rb +++ b/app/mailers/devise_notify_mailer.rb @@ -35,17 +35,17 @@ class DeviseNotifyMailer < Devise::Mailer end def confirmation_instructions(record, token, _opts = {}) - username = record.email if email_changed?(record) - username = record.unconfirmed_email if someone_else_changed_email?(record) send_email_changed_to_old_email(record) send_email_changed_to_new_email(record, token) else send_confirmation_email(record.unconfirmed_email, record, token, record.unconfirmed_email) + send_confirmation_email(record.email, record, token, record.unconfirmed_email) end + else + send_confirmation_email(record.email, record, token, record.email) end - send_confirmation_email(record.email, record, token, username) end def intercept_send?(email) @@ -83,7 +83,7 @@ class DeviseNotifyMailer < Devise::Mailer link = "#{user_confirmation_url}?confirmation_token=#{token}" send_email( - record.email, + record.unconfirmed_email, User::FOR_NEW_EMAIL_CHANGED_BY_OTHER_USER_TEMPLATE_ID, { new_email: record.unconfirmed_email, diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index 157d86bfd..2b2f5a98d 100644 --- a/spec/requests/users_controller_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -1720,6 +1720,8 @@ RSpec.describe UsersController, type: :request do expect(notify_client).to receive(:send_email).with(email_address: other_user.email, template_id: User::CONFIRMABLE_TEMPLATE_ID, personalisation:).once expect(notify_client).to receive(:send_email).with(email_address: new_email, template_id: User::CONFIRMABLE_TEMPLATE_ID, personalisation:).once + expect(notify_client).not_to receive(:send_email) + patch "/users/#{other_user.id}", headers:, params: end @@ -1729,7 +1731,6 @@ RSpec.describe UsersController, type: :request do end it "sends a new flow emails" do - expect(notify_client).to receive(:send_email).with( email_address: other_user.email, template_id: User::FOR_OLD_EMAIL_CHANGED_BY_OTHER_USER_TEMPLATE_ID, @@ -1740,7 +1741,7 @@ RSpec.describe UsersController, type: :request do ).once expect(notify_client).to receive(:send_email).with( - email_address: other_user.email, + email_address: new_email, template_id: User::FOR_NEW_EMAIL_CHANGED_BY_OTHER_USER_TEMPLATE_ID, personalisation: { new_email:, @@ -1749,6 +1750,8 @@ RSpec.describe UsersController, type: :request do }, ).once + expect(notify_client).not_to receive(:send_email) + patch "/users/#{other_user.id}", headers:, params: end end