Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[issue-4351] add logic to notify old user email of email change #4374

Merged
merged 10 commits into from
Sep 4, 2024
19 changes: 8 additions & 11 deletions app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,15 @@ def me
end

def update
[].tap do |update_email_user_ids|

super do |user|
if user.email_changed?
update_email_user_ids << user.id
user.update_attribute(:valid_email, true)
end
end
prev_email = user.email
email_changed = false
super do |user|
email_changed = user.email_changed?
end

update_email_user_ids.each do |user_id|
UserInfoChangedMailerWorker.perform_async(user_id, "email")
end
if email_changed
lcjohnso marked this conversation as resolved.
Show resolved Hide resolved
user.update(valid_email: true)
UserInfoChangedMailerWorker.perform_async(user.id, 'email', prev_email)
end
end

Expand Down
7 changes: 4 additions & 3 deletions app/mailers/user_info_changed_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
class UserInfoChangedMailer < ApplicationMailer
layout false

def user_info_changed(user, info)
def user_info_changed(user, info, previous_email=nil)
@user = user
@email_to = user.email
@recipient_emails = [user.email]

case info
when "email"
subject = "Your Zooniverse email address has been changed"
template = "email_changed"
@recipient_emails << previous_email if previous_email
when "password"
subject = "Your Zooniverse password has been changed"
template = "password_changed"
end

mail(to: @email_to, subject: subject, :template_name => template )
mail(to: @recipient_emails, subject: subject, template_name: template)
end
end
4 changes: 2 additions & 2 deletions app/workers/user_info_changed_mailer_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ class UserInfoChangedMailerWorker

attr_reader :user, :project

def perform(user_id, info)
def perform(user_id, info, previous_email=nil)
@user = User.find(user_id)
if @user && @user.receives_email?
UserInfoChangedMailer.user_info_changed(user, info).deliver
UserInfoChangedMailer.user_info_changed(user, info, previous_email).deliver
end
rescue ActiveRecord::RecordNotFound
nil
Expand Down
5 changes: 3 additions & 2 deletions spec/controllers/api/v1/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -473,13 +473,14 @@ def update_request

context "when changing email" do
let(:put_operations) { {users: {email: "[email protected]"}} }
let!(:user_email) { user.email }

after(:each) do
update_request
end

it "sends an email to the new address if user is valid" do
expect(UserInfoChangedMailerWorker).to receive(:perform_async).with(user.id, "email")
it "sends an email to the new and old address if user is valid" do
expect(UserInfoChangedMailerWorker).to receive(:perform_async).with(user.id, "email", user_email)
Tooyosi marked this conversation as resolved.
Show resolved Hide resolved
end

it "sets valid_email parameter to true" do
Expand Down
8 changes: 7 additions & 1 deletion spec/mailers/user_info_changed_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@
end

context "email address was changed" do
let(:mail) { UserInfoChangedMailer.user_info_changed(user, "email")}
prev_email = '[email protected]'
let(:mail) { UserInfoChangedMailer.user_info_changed(user, 'email', prev_email)}

it 'should have the correct subject' do
expect(mail.subject).to eq("Your Zooniverse email address has been changed")
end

it 'notifies the previous mail' do
expect(mail.to).to include(prev_email)
end
end
end
end
13 changes: 11 additions & 2 deletions spec/workers/user_info_changed_mailer_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
RSpec.describe UserInfoChangedMailerWorker do
let(:project) { create(:project) }
let(:user) { create(:user) }
let(:prev_email) { '[email protected]' }

it 'should deliver the mail' do
expect{ subject.perform(user.id, "password") }.to change{ ActionMailer::Base.deliveries.count }.by(1)
context 'when delivering an email' do
it 'delivers the mail' do
expect { subject.perform(user.id, 'password') }.to change { ActionMailer::Base.deliveries.count }.by(1)
zwolf marked this conversation as resolved.
Show resolved Hide resolved
end

it 'delivers to the right recipients' do
subject.perform(user.id, 'email', prev_email)
mail = ActionMailer::Base.deliveries.last
expect(mail.to).to eq([user.email, prev_email])
end
end

context "without a user" do
Expand Down
Loading