Fix randomly failing spec.

Fix randomly failing spec.

diff --git a/spec/components/guardian_spec.rb b/spec/components/guardian_spec.rb
index 5a950fc..346265a 100644
--- a/spec/components/guardian_spec.rb
+++ b/spec/components/guardian_spec.rb
@@ -1560,8 +1560,12 @@ describe Guardian do
     end
 
     it 'should not allow an admin to grant admin access to a non real user' do
-      Discourse.system_user.update!(admin: false)
-      expect(Guardian.new(admin).can_grant_admin?(Discourse.system_user)).to be(false)
+      begin
+        Discourse.system_user.update!(admin: false)
+        expect(Guardian.new(admin).can_grant_admin?(Discourse.system_user)).to be(false)
+      ensure
+        Discourse.system_user.update!(admin: true)
+      end
     end
   end
 
@@ -1586,7 +1590,6 @@ describe Guardian do
     end
 
     it "should not allow an admin to revoke a no real user's admin access" do
-      Discourse.system_user.update!(admin: true)
       expect(Guardian.new(admin).can_revoke_admin?(Discourse.system_user)).to be(false)
     end
   end
@@ -1614,8 +1617,12 @@ describe Guardian do
     end
 
     it "should not allow an admin to grant moderation to a non real user" do
-      Discourse.system_user.update!(moderator: false)
-      expect(Guardian.new(admin).can_grant_moderation?(Discourse.system_user)).to be(false)
+      begin
+        Discourse.system_user.update!(moderator: false)
+        expect(Guardian.new(admin).can_grant_moderation?(Discourse.system_user)).to be(false)
+      ensure
+        Discourse.system_user.update!(moderator: true)
+      end
     end
   end
 
@@ -1646,7 +1653,6 @@ describe Guardian do
     end
 
     it "should not allow an admin to revoke moderation from a non real user" do
-      Discourse.system_user.update!(moderator: true)
       expect(Guardian.new(admin).can_revoke_moderation?(Discourse.system_user)).to be(false)
     end
   end

GitHub sha: 58f3a2e9

hmmm :thinking: do you remember why this fixed the heisentest?

Yea because Discourse.system_user is memoized discourse/discourse.rb at 4e1f25197da2b6e39e6f003f1c1229037486462c · discourse/discourse · GitHub

2 Likes

Aha, this is one hairy thing.

I wonder if this more explicit pattern is a bit easier to explain?

after do
   Discourse.reset_system_user_cache!
end
1 Like