Commit 73918efb authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'swellard-issue-6123' into 'master'

Fix duplicate 'Email has already been taken' message when creating a user

devise :validatable and validates uniqueness set on the user email field cause 2 validations to fire when creating a new user with an email address already in use, Issue 6123

@stanhu Thanks for your input on merge request 807, I'll close that one in favour of this as I have added tests

See merge request !817
parents de5c9b79 19e5b043
...@@ -137,7 +137,9 @@ class User < ActiveRecord::Base ...@@ -137,7 +137,9 @@ class User < ActiveRecord::Base
# Validations # Validations
# #
validates :name, presence: true validates :name, presence: true
validates :email, presence: true, email: { strict_mode: true }, uniqueness: true # Note that a 'uniqueness' and presence check is provided by devise :validatable for email. We do not need to
# duplicate that here as the validation framework will have duplicate errors in the event of a failure.
validates :email, presence: true, email: { strict_mode: true }
validates :notification_email, presence: true, email: { strict_mode: true } validates :notification_email, presence: true, email: { strict_mode: true }
validates :public_email, presence: true, email: { strict_mode: true }, allow_blank: true, uniqueness: true validates :public_email, presence: true, email: { strict_mode: true }, allow_blank: true, uniqueness: true
validates :bio, length: { maximum: 255 }, allow_blank: true validates :bio, length: { maximum: 255 }, allow_blank: true
......
...@@ -27,4 +27,25 @@ feature 'Users' do ...@@ -27,4 +27,25 @@ feature 'Users' do
user.reload user.reload
expect(user.reset_password_token).to be_nil expect(user.reset_password_token).to be_nil
end end
let!(:user) { create(:user, username: 'user1', name: 'User 1', email: 'user1@gitlab.com') }
scenario 'Should show one error if email is already taken' do
visit new_user_session_path
fill_in 'user_name', with: 'Another user name'
fill_in 'user_username', with: 'anotheruser'
fill_in 'user_email', with: user.email
fill_in 'user_password_sign_up', with: '12341234'
expect { click_button 'Sign up' }.to change { User.count }.by(0)
expect(page).to have_text('Email has already been taken')
expect(number_of_errors_on_page(page)).to be(1), 'errors on page:\n #{errors_on_page page}'
end
def errors_on_page(page)
page.find('#error_explanation').find('ul').all('li').map{ |item| item.text }.join("\n")
end
def number_of_errors_on_page(page)
page.find('#error_explanation').find('ul').all('li').count
end
end end
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