diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 020235985fb..22245e32d82 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -522,6 +522,7 @@ en: confirmed: "Your email has been updated." please_continue: "Continue to %{site_name}" 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?" authorizing_old: title: "Thanks for confirming your current email address" diff --git a/lib/email_updater.rb b/lib/email_updater.rb index 71c57f05c82..1bda27bb27e 100644 --- a/lib/email_updater.rb +++ b/lib/email_updater.rb @@ -26,7 +26,11 @@ class EmailUpdater email = Email.downcase(email_input.strip) 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? args = { diff --git a/spec/components/email_updater_spec.rb b/spec/components/email_updater_spec.rb index b868e6c9a57..74ec89e2f2f 100644 --- a/spec/components/email_updater_spec.rb +++ b/spec/components/email_updater_spec.rb @@ -5,6 +5,17 @@ describe EmailUpdater do let(:old_email) { 'old.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 let(:user) { Fabricate(:user, email: old_email) } let(:updater) { EmailUpdater.new(user.guardian, user) }