FIX: Destroy invites of anonymized emails (#13404)

FIX: Destroy invites of anonymized emails (#13404)

Anonymizing a user changed their email address, destroyed all associated InvitedUser records, but did not destroy the invites associated to user’s email.

diff --git a/app/jobs/regular/anonymize_user.rb b/app/jobs/regular/anonymize_user.rb
index 1113476..c078706 100644
--- a/app/jobs/regular/anonymize_user.rb
+++ b/app/jobs/regular/anonymize_user.rb
@@ -16,6 +16,7 @@ module Jobs
     def make_anonymous
       anonymize_ips(@anonymize_ip) if @anonymize_ip
 
+      Invite.where(email: @prev_email).destroy_all
       InvitedUser.where(user_id: @user_id).destroy_all
       EmailToken.where(user_id: @user_id).destroy_all
       EmailLog.where(user_id: @user_id).delete_all
diff --git a/spec/services/user_anonymizer_spec.rb b/spec/services/user_anonymizer_spec.rb
index 1307990..a150d1e 100644
--- a/spec/services/user_anonymizer_spec.rb
+++ b/spec/services/user_anonymizer_spec.rb
@@ -368,4 +368,17 @@ describe UserAnonymizer do
 
   end
 
+  describe "anonymize_emails" do
+    it "destroys all associated invites" do
+      invite = Fabricate(:invite, email: 'test@example.com')
+      user = invite.redeem
+
+      Jobs.run_immediately!
+      described_class.make_anonymous(user, admin)
+
+      expect(user.email).not_to eq('test@example.com')
+      expect(Invite.exists?(id: invite.id)).to eq(false)
+    end
+  end
+
 end

GitHub sha: c893b20298aa7af3f9303cef1cc71a96d62b4db5

This commit appears in #13404 which was approved by eviltrout. It was merged by udan11.