Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
rate_limit: Handle the case of request.user being a RemoteZulipServer.
Mateusz Manderad247db37a5dd
diff --git a/zerver/decorator.py b/zerver/decorator.pyindex ece35c01705..91b4dfa9d6e 100644--- a/zerver/decorator.py+++ b/zerver/decorator.py@@ -789,7 +789,7 @@ def rate_limit(domain: str='api_by_user') -> Callable[[ViewFuncT], ViewFuncT]:user = request.user- if isinstance(user, AnonymousUser): # nocoverage+ if isinstance(user, AnonymousUser) or isinstance(user, RemoteZulipServer):# We can only rate-limit logged-in users for now.# We also only support rate-limiting authenticated# views right now.diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.pyindex 014c0cad0d6..0a8ebc183d4 100644--- a/zerver/tests/test_decorators.py+++ b/zerver/tests/test_decorators.py@@ -10,6 +10,7 @@ from django.conf import settingsfrom django.contrib.auth.models import AnonymousUserfrom django.core.exceptions import ValidationErrorfrom django.http import HttpRequest, HttpResponse+from django.utils.timezone import now as timezone_nowfrom zerver.decorator import (api_key_only_webhook_view,@@ -77,6 +78,7 @@ from zerver.lib.validator import ()from zerver.lib.webhooks.common import UnexpectedWebhookEventTypefrom zerver.models import Realm, UserProfile, get_realm, get_user+from zilencer.models import RemoteZulipServerclass DecoratorTestCase(ZulipTestCase):@@ -706,6 +708,35 @@ class RateLimitTestCase(ZulipTestCase):self.assertTrue(rate_limit_mock.called)+ def test_rate_limiting_skipped_if_remote_server(self) -> None:+ server_uuid = "1234-abcd"+ server = RemoteZulipServer(uuid=server_uuid,+ api_key="magic_secret_api_key",+ hostname="demo.example.com",+ last_updated=timezone_now())++ class Client:+ name = 'external'++ class Request:+ client = Client()+ META = {'REMOTE_ADDR': '3.3.3.3'}+ user = server++ req = Request()++ def f(req: Any) -> str:+ return 'some value'++ f = rate_limit()(f)++ with self.settings(RATE_LIMITING=True):+ with mock.patch('zerver.decorator.rate_limit_user') as rate_limit_mock:+ with self.errors_disallowed():+ self.assertEqual(f(req), 'some value')++ self.assertFalse(rate_limit_mock.called)+class ValidatorTestCase(ZulipTestCase):def test_check_string(self) -> None:x: Any = "hello"