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

Conversation

Tooyosi
Copy link
Contributor

@Tooyosi Tooyosi commented Aug 7, 2024

Describe your change here.

Review checklist

  • First, the most important one: is this PR small enough that you can actually review it? Feel free to just reject a branch if the changes are hard to review due to the length of the diff.
  • If there are any migrations, will they the previous version of the app work correctly after they've been run (e.g. the don't remove columns still known about by ActiveRecord).
  • If anything changed with regards to the public API, are those changes also documented in the apiary.apib file?
  • Are all the changes covered by tests? Think about any possible edge cases that might be left untested.

@yuenmichelle1 yuenmichelle1 removed their request for review August 8, 2024 15:40
Copy link
Member

@zwolf zwolf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple things to address here. Adding an additional test for "when changing email / also sends an email to the old address" would confirm behavior, so I suggest adding that also.

@user = user
@email_to = user.email
@recipient_emails = [user.email]
@recipient_emails << previous_email if previous_email.present? && info == 'email'
Copy link
Member

@zwolf zwolf Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previous_email.present? can be shortened to just if previous_email"

Also I think this line can be moved to inside the when "email" block in the case statement below as they both have the same condition.

super do |user|
if user.email_changed?
user.update(valid_email: true)
UserInfoChangedMailerWorker.perform_async(user.id, 'email', prev_email)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will reintroduce the race condition that the tap/array-with-a-single-id thing seeks to avoid. The save in update happens after the block is yielded, so when the worker is called before the super block is finished, the user hasn't been updated in the db and the email will go to the previous address.

Moving the worker call outside of the super block should be sufficient, assuming that user.email_changed? is still accurate after the save. It's possible that "changed?" info is lost after the transaction, though, and perhaps that's why the update_email_user_ids.each exists. Either way, it's gotta be outside the super block in order to ensure that the new address is saved while you pass along the old one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get now. The user.email_changed? is not very accurate outside the save so i'm passing it through a variable now

@Tooyosi
Copy link
Contributor Author

Tooyosi commented Aug 16, 2024

Couple things to address here. Adding an additional test for "when changing email / also sends an email to the old address" would confirm behavior, so I suggest adding that also.

I captured this behaviour in the sends an email to the new and old address if user is valid case

@zwolf
Copy link
Member

zwolf commented Aug 29, 2024

This behavior looks good. I think if you're introducing changes to the interfaces for both UserInfoChangedMailer and UserInfoChangedMailerWorker that you should add a test for each that includes the previous_email being passed around. This will prove that the worker gets it in its list of arguments during the update and that the mailer is being passed an array with both emails in it.

@@ -21,10 +21,15 @@
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)}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RSpec/EmptyLineAfterFinalLet: Add an empty line after the last let.
RSpec/DescribedClass: Use described_class instead of UserInfoChangedMailer.
Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

@@ -4,8 +4,17 @@
let(:project) { create(:project) }
let(:user) { create(:user) }

it 'should deliver the mail' do
expect{ subject.perform(user.id, "password") }.to change{ ActionMailer::Base.deliveries.count }.by(1)
context 'email delivery' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a good idea, if just to shut the hound up later

Copy link
Member

@zwolf zwolf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more small comment, non blocking. LGTM! :shipit:

@@ -3,9 +3,18 @@
RSpec.describe UserInfoChangedMailerWorker do
let(:project) { create(:project) }
let(:user) { create(:user) }
let(:prev_pass) { '[email protected]' }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should this be prev_email here and below?

@@ -4,8 +4,17 @@
let(:project) { create(:project) }
let(:user) { create(:user) }

it 'should deliver the mail' do
expect{ subject.perform(user.id, "password") }.to change{ ActionMailer::Base.deliveries.count }.by(1)
context 'email delivery' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a good idea, if just to shut the hound up later

@Tooyosi Tooyosi merged commit 022f1de into master Sep 4, 2024
8 checks passed
@Tooyosi Tooyosi deleted the 4351-change-user-email-notifications branch September 4, 2024 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants