when a primary email is replaced and added to the secondary emails list,

make sure it stays confirmed
This commit is contained in:
Brett Walker 2017-09-13 15:02:27 +02:00
parent b2d5379161
commit 85d2bf778a
4 changed files with 50 additions and 4 deletions

View File

@ -526,8 +526,11 @@ class User < ActiveRecord::Base
def update_emails_with_primary_email
primary_email_record = emails.find_by(email: email)
if primary_email_record
Emails::DestroyService.new(self, email: email).execute
Emails::CreateService.new(self, email: email_was).execute
Emails::DestroyService.new(self).execute(primary_email_record)
# the original primary email was confirmed, and we want that to carry over. We don't
# have access to the original confirmation values at this point, so just set confirmed_at
Emails::CreateService.new(self, email: email_was).execute(confirmed_at: confirmed_at_was)
end
end

View File

@ -1,7 +1,7 @@
module Emails
class CreateService < ::Emails::BaseService
def execute
@user.emails.create(email: @email)
def execute(options = {})
@user.emails.create({email: @email}.merge(options))
end
end
end

View File

@ -379,6 +379,44 @@ describe User do
user.update_attributes!(email: 'shawnee.ritchie@denesik.com')
end
end
describe '#update_emails_with_primary_email' do
before do
@user = create(:user, email: 'primary@example.com').tap do |user|
user.skip_reconfirmation!
end
@secondary = create :email, email: 'secondary@example.com', user: @user
@user.reload
end
it 'gets called when email updated' do
expect(@user).to receive(:update_emails_with_primary_email)
@user.update_attributes!(email: 'new_primary@example.com')
end
it 'does not add old primary to secondary emails' do
@user.update_attributes!(email: 'new_primary@example.com')
@user.reload
expect(@user.emails.count).to eq 1
expect(@user.emails.first.email).to eq @secondary.email
end
it 'adds old primary to secondary emails if secondary is becoming a primary' do
@user.update_attributes!(email: @secondary.email)
@user.reload
expect(@user.emails.count).to eq 1
expect(@user.emails.first.email).to eq 'primary@example.com'
end
it 'transfers old confirmation values into new secondary' do
org_user = @user
@user.update_attributes!(email: @secondary.email)
@user.reload
expect(@user.emails.count).to eq 1
expect(@user.emails.first.confirmed_at).not_to eq nil
end
end
end
describe '#update_tracked_fields!', :clean_gitlab_redis_shared_state do

View File

@ -12,6 +12,11 @@ describe Emails::CreateService do
expect(Email.where(opts)).not_to be_empty
end
it 'creates an email with additional attributes' do
expect { service.execute(confirmation_token: 'abc') }.to change { Email.count }.by(1)
expect(Email.where(opts).first.confirmation_token).to eq 'abc'
end
it 'has the right user association' do
service.execute