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.

rate_limit: Improve dummy request objects in RateLimitTestCase.

Mateusz Mandera934bdb9651a2
diff --git a/zerver/decorator.py b/zerver/decorator.py
index bf9a4f10dfe..ece35c01705 100644
--- a/zerver/decorator.py
+++ b/zerver/decorator.py
@@ -796,6 +796,7 @@ def rate_limit(domain: str='api_by_user') -> Callable[[ViewFuncT], ViewFuncT]:
# TODO: implement per-IP non-authed rate limiting
return func(request, *args, **kwargs)
+ assert isinstance(user, UserProfile)
rate_limit_user(request, user, domain)
return func(request, *args, **kwargs)
diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.py
index d24200f78e0..014c0cad0d6 100644
--- a/zerver/tests/test_decorators.py
+++ b/zerver/tests/test_decorators.py
@@ -7,6 +7,7 @@ from unittest import mock
import orjson
from django.conf import settings
+from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import ValidationError
from django.http import HttpRequest, HttpResponse
@@ -619,6 +620,7 @@ class RateLimitTestCase(ZulipTestCase):
class Request:
client = Client()
META = {'REMOTE_ADDR': '127.0.0.1'}
+ user = AnonymousUser()
req = Request()
@@ -641,6 +643,7 @@ class RateLimitTestCase(ZulipTestCase):
class Request:
client = Client()
META = {'REMOTE_ADDR': '3.3.3.3'}
+ user = AnonymousUser()
req = Request()
@@ -664,7 +667,7 @@ class RateLimitTestCase(ZulipTestCase):
class Request:
client = Client()
META = {'REMOTE_ADDR': '3.3.3.3'}
- user = 'stub' # any non-None value here exercises the correct code path
+ user = self.example_user("hamlet")
req = Request()
@@ -687,7 +690,7 @@ class RateLimitTestCase(ZulipTestCase):
class Request:
client = Client()
META = {'REMOTE_ADDR': '3.3.3.3'}
- user = 'stub' # any non-None value here exercises the correct code path
+ user = self.example_user("hamlet")
req = Request()