improve error message when trying to change email address to one used by a staged user

This commit is contained in:
Régis Hanol 2016-03-21 19:36:26 +01:00
parent 7dd89be741
commit afacc70fbe
3 changed files with 17 additions and 1 deletions

View File

@ -522,6 +522,7 @@ en:
confirmed: "Your email has been updated." confirmed: "Your email has been updated."
please_continue: "Continue to %{site_name}" please_continue: "Continue to %{site_name}"
error: "There was an error changing your email address. Perhaps the address is already in use?" error: "There was an error changing your email address. Perhaps the address is already in use?"
error_staged: "There was an error changing your email address. The address is already in use by a staged user."
already_done: "Sorry, this confirmation link is no longer valid. Perhaps your email was already changed?" already_done: "Sorry, this confirmation link is no longer valid. Perhaps your email was already changed?"
authorizing_old: authorizing_old:
title: "Thanks for confirming your current email address" title: "Thanks for confirming your current email address"

View File

@ -26,7 +26,11 @@ class EmailUpdater
email = Email.downcase(email_input.strip) email = Email.downcase(email_input.strip)
EmailValidator.new(attributes: :email).validate_each(self, :email, email) EmailValidator.new(attributes: :email).validate_each(self, :email, email)
errors.add(:base, I18n.t('change_email.error')) if User.find_by_email(email) if existing_user = User.find_by_email(email)
error_message = 'change_email.error'
error_message << '_staged' if existing_user.staged?
errors.add(:base, I18n.t(error_message))
end
if errors.blank? if errors.blank?
args = { args = {

View File

@ -5,6 +5,17 @@ describe EmailUpdater do
let(:old_email) { 'old.email@example.com' } let(:old_email) { 'old.email@example.com' }
let(:new_email) { 'new.email@example.com' } let(:new_email) { 'new.email@example.com' }
it "provides better error message when a staged user has the same email" do
Fabricate(:user, staged: true, email: new_email)
user = Fabricate(:user, email: old_email)
updater = EmailUpdater.new(user.guardian, user)
updater.change_to(new_email)
expect(updater.errors).to be_present
expect(updater.errors.messages[:base].first).to be I18n.t("change_email.error_staged")
end
context 'as a regular user' do context 'as a regular user' do
let(:user) { Fabricate(:user, email: old_email) } let(:user) { Fabricate(:user, email: old_email) }
let(:updater) { EmailUpdater.new(user.guardian, user) } let(:updater) { EmailUpdater.new(user.guardian, user) }