FIX: Email domain whitelist prevented user anonymizing

FIX: Email domain whitelist prevented user anonymizing

FIX: Secondary email addresses were not deleted during anonymizing

diff --git a/app/services/user_anonymizer.rb b/app/services/user_anonymizer.rb
index d87c7e6..ae48025 100644
--- a/app/services/user_anonymizer.rb
+++ b/app/services/user_anonymizer.rb
@@ -26,7 +26,6 @@ class UserAnonymizer
 
       @user.reload
       @user.password = SecureRandom.hex
-      @user.email = "#{@user.username}@anonymized.invalid"
       @user.name = SiteSetting.full_name_required ? @user.username : nil
       @user.date_of_birth = nil
       @user.title = nil
@@ -37,7 +36,8 @@ class UserAnonymizer
         @user.registration_ip_address = @opts[:anonymize_ip]
       end
 
-      @user.save
+      @user.save!
+      @user.primary_email.update_attribute(:email, "#{@user.username}@anonymized.invalid")
 
       options = @user.user_option
       options.email_always = false
@@ -45,7 +45,7 @@ class UserAnonymizer
       options.email_digests = false
       options.email_private_messages = false
       options.email_direct = false
-      options.save
+      options.save!
 
       if profile = @user.user_profile
         profile.update(location: nil, website: nil, bio_raw: nil, bio_cooked: nil,
@@ -61,6 +61,7 @@ class UserAnonymizer
       @user.instagram_user_info.try(:destroy)
       @user.user_open_ids.find_each { |x| x.destroy }
       @user.api_key.try(:destroy)
+      @user.user_emails.secondary.destroy_all
 
       @user_history = log_action
     end
@@ -101,6 +102,6 @@ class UserAnonymizer
       history_details[:details] = "username: #{@prev_username}"
     end
 
-    UserHistory.create(history_details)
+    UserHistory.create!(history_details)
   end
 end
diff --git a/spec/services/user_anonymizer_spec.rb b/spec/services/user_anonymizer_spec.rb
index 9d43e6c..6f1e348 100644
--- a/spec/services/user_anonymizer_spec.rb
+++ b/spec/services/user_anonymizer_spec.rb
@@ -24,7 +24,7 @@ describe UserAnonymizer do
 
   describe "make_anonymous" do
     let(:original_email) { "edward@example.net" }
-    let(:user) { Fabricate(:user, username: "edward", email: original_email) }
+    let(:user) { Fabricate(:user_single_email, username: "edward", email: original_email) }
     let(:another_user) { Fabricate(:evil_trout) }
     subject(:make_anonymous) { described_class.make_anonymous(user, admin) }
 
@@ -33,11 +33,25 @@ describe UserAnonymizer do
       expect(user.reload.username).to match(/^anon\d{3,}$/)
     end
 
-    it "changes email address" do
+    it "changes the primary email address" do
       make_anonymous
       expect(user.reload.email).to eq("#{user.username}@anonymized.invalid")
     end
 
+    it "changes the primary email address when there is an email domain whitelist" do
+      SiteSetting.email_domains_whitelist = 'example.net|wayne.com|discourse.org'
+
+      make_anonymous
+      expect(user.reload.email).to eq("#{user.username}@anonymized.invalid")
+    end
+
+    it "deletes secondary email addresses" do
+      Fabricate(:secondary_email, user: user, email: "secondary_email@example.com")
+
+      make_anonymous
+      expect(user.reload.secondary_emails).to be_blank
+    end
+
     it "turns off all notifications" do
       user.user_option.update_columns(
         email_always: true

GitHub sha: 1e989295