From afacc70fbeb01f589ff246b1e19d966f53039462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 21 Mar 2016 19:36:26 +0100 Subject: [PATCH] improve error message when trying to change email address to one used by a staged user --- config/locales/server.en.yml | 1 + lib/email_updater.rb | 6 +++++- spec/components/email_updater_spec.rb | 11 +++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 020235985..22245e32d 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 71c57f05c..1bda27bb2 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 b868e6c9a..74ec89e2f 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) }