Allow exception view to accept all valid HTTP methods.

This commit is contained in:
Aaron Tan 2017-04-24 16:47:17 -04:00
parent 7d7d1daae7
commit 381b47201b
2 changed files with 52 additions and 3 deletions

View 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)

View File

@ -10,20 +10,32 @@ from django.utils.translation import ugettext_lazy as _
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):
authentication_classes = []
permission_classes = (permissions.AllowAny,)
metadata_class = None
allowed_methods = ('GET', 'HEAD')
exception_class = exceptions.APIException
view_name = _('API Error')
def get_view_name(self):
return self.view_name
def get(self, request, format=None):
raise self.exception_class()
def finalize_response(self, request, response, *args, **kwargs):
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):