mirror of
https://github.com/zulip/zulip.git
synced 2025-11-01 12:33:40 +00:00
rate_limit: Replace rate_limit with inlined rate limit checks.
This change incorporate should_rate_limit into rate_limit_user and rate_limit_request_by_ip. Note a slight behavior change to other callers to rate_limit_request_by_ip is made as we now check if the client is eligible to be exempted from rate limiting now, which was previously only done as a part of zerver.lib.rate_limiter.rate_limit. Now we mock zerver.lib.rate_limiter.RateLimitedUser instead of zerver.decorator.rate_limit_user in zerver.tests.test_decorators.RateLimitTestCase, because rate_limit_user will always be called but rate limit only happens the should_rate_limit check passes; we can continue to mock zerver.lib.rate_limiter.rate_limit_ip, because the decorated view functions call rate_limit_request_by_ip that calls rate_limit_ip when the should_rate_limit check passes. We need to mock zerver.decorator.rate_limit_user for SkipRateLimitingTest now because rate_limit has been removed. We don't need to mock RateLimitedUser in this case because we are only verifying that the skip_rate_limiting flag works. To ensure coverage in add_logging_data, a new test case is added to use a web_public_view (which decorates the view function with add_logging_data) with a new flag to check_rate_limit_public_or_user_views. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
committed by
Tim Abbott
parent
2aac1dc40a
commit
26a518267a
@@ -54,6 +54,9 @@ class InvalidZulipServerKeyError(InvalidZulipServerError):
|
||||
def rate_limit_remote_server(
|
||||
request: HttpRequest, remote_server: RemoteZulipServer, domain: str
|
||||
) -> None:
|
||||
if not should_rate_limit(request):
|
||||
return
|
||||
|
||||
try:
|
||||
RateLimitedRemoteZulipServer(remote_server, domain=domain).rate_limit_request(request)
|
||||
except RateLimited as e:
|
||||
@@ -98,8 +101,7 @@ def authenticated_remote_server_view(
|
||||
except JsonableError as e:
|
||||
raise UnauthorizedError(e.msg)
|
||||
|
||||
if should_rate_limit(request):
|
||||
rate_limit_remote_server(request, remote_server, domain="api_by_remote_server")
|
||||
rate_limit_remote_server(request, remote_server, domain="api_by_remote_server")
|
||||
return view_func(request, remote_server, *args, **kwargs)
|
||||
|
||||
return _wrapped_view_func
|
||||
|
||||
Reference in New Issue
Block a user