mirror of
https://github.com/ansible/awx.git
synced 2026-05-08 09:57:35 -02:30
Allow exception view to accept all valid HTTP methods.
This commit is contained in:
37
awx/main/tests/unit/test_views.py
Normal file
37
awx/main/tests/unit/test_views.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import pytest
|
||||||
|
import mock
|
||||||
|
|
||||||
|
# Django REST Framework
|
||||||
|
from rest_framework import exceptions
|
||||||
|
|
||||||
|
# AWX
|
||||||
|
from awx.main.views import ApiErrorView
|
||||||
|
|
||||||
|
|
||||||
|
HTTP_METHOD_NAMES = [
|
||||||
|
'get',
|
||||||
|
'post',
|
||||||
|
'put',
|
||||||
|
'patch',
|
||||||
|
'delete',
|
||||||
|
'head',
|
||||||
|
'options',
|
||||||
|
'trace',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def api_view_obj_fixture():
|
||||||
|
return ApiErrorView()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('method_name', HTTP_METHOD_NAMES)
|
||||||
|
def test_exception_view_allow_http_methods(method_name):
|
||||||
|
assert hasattr(ApiErrorView, method_name)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('method_name', HTTP_METHOD_NAMES)
|
||||||
|
def test_exception_view_raises_exception(api_view_obj_fixture, method_name):
|
||||||
|
request_mock = mock.MagicMock()
|
||||||
|
with pytest.raises(exceptions.APIException):
|
||||||
|
getattr(api_view_obj_fixture, method_name)(request_mock)
|
||||||
@@ -10,20 +10,32 @@ from django.utils.translation import ugettext_lazy as _
|
|||||||
from rest_framework import exceptions, permissions, views
|
from rest_framework import exceptions, permissions, views
|
||||||
|
|
||||||
|
|
||||||
|
def _force_raising_exception(view_obj, request, format=None):
|
||||||
|
raise view_obj.exception_class()
|
||||||
|
|
||||||
|
|
||||||
class ApiErrorView(views.APIView):
|
class ApiErrorView(views.APIView):
|
||||||
|
|
||||||
authentication_classes = []
|
authentication_classes = []
|
||||||
permission_classes = (permissions.AllowAny,)
|
permission_classes = (permissions.AllowAny,)
|
||||||
metadata_class = None
|
metadata_class = None
|
||||||
allowed_methods = ('GET', 'HEAD')
|
|
||||||
exception_class = exceptions.APIException
|
exception_class = exceptions.APIException
|
||||||
view_name = _('API Error')
|
view_name = _('API Error')
|
||||||
|
|
||||||
def get_view_name(self):
|
def get_view_name(self):
|
||||||
return self.view_name
|
return self.view_name
|
||||||
|
|
||||||
def get(self, request, format=None):
|
def finalize_response(self, request, response, *args, **kwargs):
|
||||||
raise self.exception_class()
|
response = super(ApiErrorView, self).finalize_response(request, response, *args, **kwargs)
|
||||||
|
try:
|
||||||
|
del response['Allow']
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
for method_name in ApiErrorView.http_method_names:
|
||||||
|
setattr(ApiErrorView, method_name, _force_raising_exception)
|
||||||
|
|
||||||
|
|
||||||
def handle_error(request, status=404, **kwargs):
|
def handle_error(request, status=404, **kwargs):
|
||||||
|
|||||||
Reference in New Issue
Block a user