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.

api: Fix require_post decorator not returning 405 error body.

shanukunff6921b438be
diff --git a/zerver/decorator.py b/zerver/decorator.py
index 8b38cda77e0..5be49ef68b6 100644
--- a/zerver/decorator.py
+++ b/zerver/decorator.py
@@ -13,16 +13,10 @@ from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth import login as django_login
from django.contrib.auth.decorators import user_passes_test as django_user_passes_test
from django.contrib.auth.models import AnonymousUser
-from django.http import (
- HttpRequest,
- HttpResponse,
- HttpResponseNotAllowed,
- HttpResponseRedirect,
- QueryDict,
-)
+from django.http import HttpRequest, HttpResponse, HttpResponseRedirect, QueryDict
from django.http.multipartparser import MultiPartParser
from django.shortcuts import resolve_url
-from django.template.response import SimpleTemplateResponse
+from django.template.response import SimpleTemplateResponse, TemplateResponse
from django.utils.timezone import now as timezone_now
from django.utils.translation import ugettext as _
from django.views.decorators.csrf import csrf_exempt
@@ -44,7 +38,7 @@ from zerver.lib.logging_util import log_to_file
from zerver.lib.queue import queue_json_publish
from zerver.lib.rate_limiter import RateLimitedUser
from zerver.lib.request import REQ, has_request_variables
-from zerver.lib.response import json_error, json_success, json_unauthorized
+from zerver.lib.response import json_error, json_method_not_allowed, json_success, json_unauthorized
from zerver.lib.subdomains import get_subdomain, user_matches_subdomain
from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime
from zerver.lib.types import ViewFuncT
@@ -104,7 +98,10 @@ def require_post(func: ViewFuncT) -> ViewFuncT:
err_method = request.method
logging.warning('Method Not Allowed (%s): %s', err_method, request.path,
extra={'status_code': 405, 'request': request})
- return HttpResponseNotAllowed(["POST"])
+ if request.error_format == 'JSON':
+ return json_method_not_allowed(["POST"])
+ else:
+ return TemplateResponse(request, "404.html", context={'status_code': 405}, status=405)
return func(request, *args, **kwargs)
return cast(ViewFuncT, wrapper) # https://github.com/python/mypy/issues/1927
diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.py
index b1d7f459eeb..9ea828a4cf1 100644
--- a/zerver/tests/test_decorators.py
+++ b/zerver/tests/test_decorators.py
@@ -1527,6 +1527,39 @@ class TestHumanUsersOnlyDecorator(ZulipTestCase):
result = self.api_delete(default_bot, endpoint)
self.assert_json_error(result, "This endpoint does not accept bot requests.")
+class TestAuthenticatedRequirePostDecorator(ZulipTestCase):
+ def test_authenticated_html_post_view_with_get_request(self) -> None:
+ self.login('hamlet')
+ with mock.patch('logging.warning') as mock_warning:
+ result = self.client_get(r'/accounts/register/', {'stream': 'Verona'})
+ self.assertEqual(result.status_code, 405)
+ mock_warning.assert_called_once() # Check we logged the Mock Not Allowed
+ self.assertEqual(mock_warning.call_args_list[0][0],
+ ('Method Not Allowed (%s): %s', 'GET', '/accounts/register/'))
+
+ with mock.patch('logging.warning') as mock_warning:
+ result = self.client_get(r'/accounts/logout/', {'stream': 'Verona'})
+ self.assertEqual(result.status_code, 405)
+ mock_warning.assert_called_once() # Check we logged the Mock Not Allowed
+ self.assertEqual(mock_warning.call_args_list[0][0],
+ ('Method Not Allowed (%s): %s', 'GET', '/accounts/logout/'))
+
+ def test_authenticated_json_post_view_with_get_request(self) -> None:
+ self.login('hamlet')
+ with mock.patch('logging.warning') as mock_warning:
+ result = self.client_get(r'/api/v1/dev_fetch_api_key', {'stream': 'Verona'})
+ self.assertEqual(result.status_code, 405)
+ mock_warning.assert_called_once() # Check we logged the Mock Not Allowed
+ self.assertEqual(mock_warning.call_args_list[0][0],
+ ('Method Not Allowed (%s): %s', 'GET', '/api/v1/dev_fetch_api_key'))
+
+ with mock.patch('logging.warning') as mock_warning:
+ result = self.client_get(r'/json/remotes/server/register', {'stream': 'Verona'})
+ self.assertEqual(result.status_code, 405)
+ mock_warning.assert_called_once() # Check we logged the Mock Not Allowed
+ self.assertEqual(mock_warning.call_args_list[0][0],
+ ('Method Not Allowed (%s): %s', 'GET', '/json/remotes/server/register'))
+
class TestAuthenticatedJsonPostViewDecorator(ZulipTestCase):
def test_authenticated_json_post_view_if_everything_is_correct(self) -> None:
user = self.example_user('hamlet')