FIX: Allow users to add emails which were deleted before

FIX: Allow users to add emails which were deleted before

diff --git a/app/models/email_log.rb b/app/models/email_log.rb
index 4ca1559..6cfbca4 100644
--- a/app/models/email_log.rb
+++ b/app/models/email_log.rb
@@ -6,8 +6,10 @@ class EmailLog < ActiveRecord::Base
     admin_login
     confirm_new_email
     confirm_old_email
+    confirm_old_email_add
     forgot_password
     notify_old_email
+    notify_old_email_add
     signup
     signup_after_approval
   }
diff --git a/lib/email_updater.rb b/lib/email_updater.rb
index 23f0a5c..bac085b 100644
--- a/lib/email_updater.rb
+++ b/lib/email_updater.rb
@@ -54,7 +54,7 @@ class EmailUpdater
       change_req.new_email = email
     end
 
-    if change_req.change_state.blank?
+    if change_req.change_state.blank? || change_req.change_state == EmailChangeRequest.states[:complete]
       change_req.change_state = if @user.staff?
         # Staff users must confirm their old email address first.
         EmailChangeRequest.states[:authorizing_old]
diff --git a/spec/components/email_updater_spec.rb b/spec/components/email_updater_spec.rb
index 046e8b0..3476f48 100644
--- a/spec/components/email_updater_spec.rb
+++ b/spec/components/email_updater_spec.rb
@@ -168,6 +168,25 @@ describe EmailUpdater do
           expect(@change_req.change_state).to eq(EmailChangeRequest.states[:complete])
         end
       end
+
+      context 'that was deleted before' do
+        it 'works' do
+          Jobs.expects(:enqueue).once.with(:critical_user_email, has_entries(type: :notify_old_email_add, to_address: old_email))
+          updater.confirm(@change_req.new_email_token.token)
+          expect(user.reload.user_emails.pluck(:email)).to contain_exactly(old_email, new_email)
+
+          user.user_emails.where(email: new_email).delete_all
+          expect(user.reload.user_emails.pluck(:email)).to contain_exactly(old_email)
+
+          Jobs.expects(:enqueue).once.with(:critical_user_email, has_entries(type: :confirm_new_email, to_address: new_email))
+          updater.change_to(new_email, add: true)
+          @change_req = user.email_change_requests.first
+
+          Jobs.expects(:enqueue).once.with(:critical_user_email, has_entries(type: :notify_old_email_add, to_address: old_email))
+          updater.confirm(@change_req.new_email_token.token)
+          expect(user.reload.user_emails.pluck(:email)).to contain_exactly(old_email, new_email)
+        end
+      end
     end
   end
 

GitHub sha: b7e70850