Commit ad8570ce authored by Stan Hu's avatar Stan Hu

Merge branch 'fix-add-primary-email-to-emails-regression-new-user' into 'master'

Fix regression with logic to add user primary email to emails

See merge request gitlab-org/gitlab!85169
parents 761627fb 21210646
......@@ -282,7 +282,7 @@ class User < ApplicationRecord
after_update :username_changed_hook, if: :saved_change_to_username?
after_destroy :post_destroy_hook
after_destroy :remove_key_cache
after_save if: -> { saved_change_to_email? && confirmed? } do
after_save if: -> { (saved_change_to_email? || saved_change_to_confirmed_at?) && confirmed? } do
email_to_confirm = self.emails.find_by(email: self.email)
if email_to_confirm.present?
......
......@@ -1560,7 +1560,7 @@ RSpec.describe User do
end
it 'adds the confirmed primary email to emails' do
expect(user.emails.confirmed.map(&:email)).not_to include(user.email)
expect(user.emails.confirmed.map(&:email)).not_to include(user.unconfirmed_email)
user.confirm
......@@ -1619,14 +1619,23 @@ RSpec.describe User do
context 'when the email is changed but not confirmed' do
let(:user) { create(:user, email: 'primary@example.com') }
it 'does not add the new email to emails yet' do
before do
user.update!(email: 'new_primary@example.com')
end
it 'does not add the new email to emails yet' do
expect(user.unconfirmed_email).to eq('new_primary@example.com')
expect(user.email).to eq('primary@example.com')
expect(user).to be_confirmed
expect(user.emails.pluck(:email)).not_to include('new_primary@example.com')
end
it 'adds the new email to emails upon confirmation' do
user.confirm
expect(user.email).to eq('new_primary@example.com')
expect(user).to be_confirmed
expect(user.emails.pluck(:email)).to include('new_primary@example.com')
end
end
context 'when the user is created as not confirmed' do
......@@ -1636,6 +1645,11 @@ RSpec.describe User do
expect(user).not_to be_confirmed
expect(user.emails.pluck(:email)).not_to include('primary@example.com')
end
it 'adds the email to emails upon confirmation' do
user.confirm
expect(user.emails.pluck(:email)).to include('primary@example.com')
end
end
context 'when the user is created as confirmed' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment