Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

test_external: Refactor mock.patch to assertLogs.

palash60f9f87c5de0
diff --git a/zerver/tests/test_external.py b/zerver/tests/test_external.py
index 1cae60ea550..5d35b50b7d5 100644
--- a/zerver/tests/test_external.py
+++ b/zerver/tests/test_external.py
@@ -118,16 +118,17 @@ class RateLimitTests(ZulipTestCase):
self.assert_json_success(result)
- @mock.patch('zerver.lib.rate_limiter.logger.warning')
- def test_hit_ratelimiterlockingexception(self, mock_warn: mock.MagicMock) -> None:
+ def test_hit_ratelimiterlockingexception(self) -> None:
user = self.example_user('cordelia')
RateLimitedUser(user).clear_history()
with mock.patch('zerver.lib.rate_limiter.RedisRateLimiterBackend.incr_ratelimit',
side_effect=RateLimiterLockingException):
- result = self.send_api_message(user, "some stuff")
- self.assertEqual(result.status_code, 429)
- mock_warn.assert_called_with(
- "Deadlock trying to incr_ratelimit for %s",
- f"RateLimitedUser:{user.id}:api_by_user",
+ with self.assertLogs("zerver.lib.rate_limiter", level="WARNING") as m:
+ result = self.send_api_message(user, "some stuff")
+ self.assertEqual(result.status_code, 429)
+ self.assertEqual(
+ m.output,
+ ["WARNING:zerver.lib.rate_limiter:Deadlock trying to incr_ratelimit for {}".format(
+ f"RateLimitedUser:{user.id}:api_by_user")]
)