FIX: Avoid validation error when deleting users with locked trust level

FIX: Avoid validation error when deleting users with locked trust level

diff --git a/app/models/group_user.rb b/app/models/group_user.rb
index 7b8d865..8908498 100644
--- a/app/models/group_user.rb
+++ b/app/models/group_user.rb
@@ -60,6 +60,7 @@ class GroupUser < ActiveRecord::Base
 
   def recalculate_trust_level
     return if group.grant_trust_level.nil?
+    return if self.destroyed_by_association&.active_record == User # User is being destroyed, so don't try to recalculate
 
     Promotion.recalculate(user)
   end
diff --git a/spec/services/user_destroyer_spec.rb b/spec/services/user_destroyer_spec.rb
index 01e6e81..f1b37f7 100644
--- a/spec/services/user_destroyer_spec.rb
+++ b/spec/services/user_destroyer_spec.rb
@@ -378,7 +378,7 @@ describe UserDestroyer do
     end
 
     context 'user belongs to groups that grant trust level' do
-      let(:group) { Fabricate(:group, grant_trust_level: 2) }
+      let(:group) { Fabricate(:group, grant_trust_level: 4) }
 
       before do
         group.add(user)
@@ -391,6 +391,16 @@ describe UserDestroyer do
         }.to change { User.count }.by(-1)
       end
 
+      it 'can delete the user if they have a manual locked trust level and have no email' do
+        user.update(manual_locked_trust_level: 3)
+
+        UserEmail.where(user: user).delete_all
+        user.reload
+        expect {
+          UserDestroyer.new(admin).destroy(user)
+        }.to change { User.count }.by(-1)
+      end
+
       it 'can delete the user if they were to fall into another trust level and have no email' do
         g2 = Fabricate(:group, grant_trust_level: 1)
         g2.add(user)

GitHub sha: 407bb96a

1 Like