Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
decorator: Extract public_json_view.
Zixuan James Lif54ecad6cd05
diff --git a/zerver/decorator.py b/zerver/decorator.pyindex 8e6d0a1ee01..e78077c9b15 100644--- a/zerver/decorator.py+++ b/zerver/decorator.py@@ -836,13 +836,11 @@ def process_as_post(return _wrapped_view_func-# Checks if the user is logged in. If not, return an error (the-# @login_required behavior of redirecting to a login page doesn't make-# sense for json views)-def authenticated_json_view(- view_func: Callable[Concatenate[HttpRequest, UserProfile, ParamT], HttpResponse],+def public_json_view(+ view_func: Callable[+ Concatenate[HttpRequest, Union[UserProfile, AnonymousUser], ParamT], HttpResponse+ ],skip_rate_limiting: bool = False,- allow_unauthenticated: bool = False,) -> Callable[Concatenate[HttpRequest, ParamT], HttpResponse]:@wraps(view_func)def _wrapped_view_func(@@ -855,9 +853,6 @@ def authenticated_json_view(rate_limit(request)if not request.user.is_authenticated:- if not allow_unauthenticated:- raise UnauthorizedError()-process_client(request,is_browser_view=True,@@ -865,6 +860,33 @@ def authenticated_json_view()return view_func(request, request.user, *args, **kwargs)+ # Fall back to authenticated_json_view if the user is authenticated.+ # Since we have done rate limiting earlier is no need to do it again.+ return authenticated_json_view(view_func, skip_rate_limiting=True)(request, *args, **kwargs)++ return _wrapped_view_func+++# Checks if the user is logged in. If not, return an error (the+# @login_required behavior of redirecting to a login page doesn't make+# sense for json views)+def authenticated_json_view(+ view_func: Callable[Concatenate[HttpRequest, UserProfile, ParamT], HttpResponse],+ skip_rate_limiting: bool = False,+) -> Callable[Concatenate[HttpRequest, ParamT], HttpResponse]:+ @wraps(view_func)+ def _wrapped_view_func(+ request: HttpRequest,+ /,+ *args: ParamT.args,+ **kwargs: ParamT.kwargs,+ ) -> HttpResponse:+ if not skip_rate_limiting:+ rate_limit(request)++ if not request.user.is_authenticated:+ raise UnauthorizedError()+user_profile = request.uservalidate_account_and_subdomain(request, user_profile)diff --git a/zerver/lib/rest.py b/zerver/lib/rest.pyindex a001041b525..c861ea2f0ce 100644--- a/zerver/lib/rest.py+++ b/zerver/lib/rest.py@@ -12,6 +12,7 @@ from zerver.decorator import (authenticated_rest_api_view,authenticated_uploads_api_view,process_as_post,+ public_json_view,)from zerver.lib.exceptions import MissingAuthenticationErrorfrom zerver.lib.request import RequestNotes@@ -150,8 +151,7 @@ def rest_dispatch(request: HttpRequest, **kwargs: Any) -> HttpResponse:):# For endpoints that support anonymous web access, we do that.# TODO: Allow /api calls when this is stable enough.- auth_kwargs = dict(allow_unauthenticated=True)- target_function = csrf_protect(authenticated_json_view(target_function, **auth_kwargs))+ target_function = csrf_protect(public_json_view(target_function))else:# Otherwise, throw an authentication error; our middleware# will generate the appropriate HTTP response.diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.pyindex b47abf5847a..0e799f8503a 100644--- a/zerver/tests/test_decorators.py+++ b/zerver/tests/test_decorators.py@@ -4,11 +4,12 @@ import reimport uuidfrom collections import defaultdictfrom functools import lru_cache-from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Sequence, Tuple+from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Sequence, Tuple, Unionfrom unittest import mock, skipUnlessimport orjsonfrom django.conf import settings+from django.contrib.auth.models import AnonymousUserfrom django.core.exceptions import ValidationErrorfrom django.http import HttpRequest, HttpResponsefrom django.utils.timezone import now as timezone_now@@ -27,6 +28,7 @@ from zerver.decorator import (authenticated_rest_api_view,authenticated_uploads_api_view,internal_notify_view,+ public_json_view,return_success_on_head_request,validate_api_key,webhook_view,@@ -1837,6 +1839,21 @@ class TestAuthenticatedJsonViewDecorator(ZulipTestCase):return self.client_post(r"/accounts/webathena_kerberos_login/", data)+class TestPublicJsonViewDecorator(ZulipTestCase):+ def test_access_public_json_view_when_logged_in(self) -> None:+ hamlet = self.example_user("hamlet")++ @public_json_view+ def public_view(+ request: HttpRequest, maybe_user_profile: Union[UserProfile, AnonymousUser]+ ) -> HttpResponse:+ self.assertEqual(maybe_user_profile, hamlet)+ return json_success(request)++ result = public_view(HostRequestMock(host="zulip.testserver", user_profile=hamlet))+ self.assert_json_success(result)++class TestZulipLoginRequiredDecorator(ZulipTestCase):def test_zulip_login_required_if_subdomain_is_invalid(self) -> None:self.login("hamlet")