2013-05-19 17:06:24 +04:00
|
|
|
import copy
|
2024-09-07 14:21:18 +03:00
|
|
|
import unittest
|
2015-06-25 23:55:51 +03:00
|
|
|
|
2024-09-07 14:21:18 +03:00
|
|
|
from django import VERSION as DJANGO_VERSION
|
2012-10-08 20:10:50 +04:00
|
|
|
from django.test import TestCase
|
2015-06-25 23:55:51 +03:00
|
|
|
|
2012-10-08 20:10:50 +04:00
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework.decorators import api_view
|
|
|
|
from rest_framework.response import Response
|
2017-04-06 21:25:21 +03:00
|
|
|
from rest_framework.settings import APISettings, api_settings
|
2013-06-28 20:17:39 +04:00
|
|
|
from rest_framework.test import APIRequestFactory
|
2012-10-08 20:10:50 +04:00
|
|
|
from rest_framework.views import APIView
|
2013-05-19 17:06:24 +04:00
|
|
|
|
2013-06-28 20:17:39 +04:00
|
|
|
factory = APIRequestFactory()
|
2012-10-08 20:10:50 +04:00
|
|
|
|
2019-04-30 18:53:44 +03:00
|
|
|
JSON_ERROR = 'JSON parse error - Expecting value:'
|
2014-05-16 03:20:40 +04:00
|
|
|
|
2012-10-08 20:10:50 +04:00
|
|
|
|
|
|
|
class BasicView(APIView):
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'GET'})
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2015-07-30 17:26:42 +03:00
|
|
|
return Response({'method': 'POST', 'data': request.data})
|
2012-10-08 20:10:50 +04:00
|
|
|
|
|
|
|
|
2012-12-16 23:49:18 +04:00
|
|
|
@api_view(['GET', 'POST', 'PUT', 'PATCH'])
|
2012-10-08 20:10:50 +04:00
|
|
|
def basic_view(request):
|
|
|
|
if request.method == 'GET':
|
|
|
|
return {'method': 'GET'}
|
|
|
|
elif request.method == 'POST':
|
2015-07-30 17:26:42 +03:00
|
|
|
return {'method': 'POST', 'data': request.data}
|
2012-10-08 20:53:18 +04:00
|
|
|
elif request.method == 'PUT':
|
2015-07-30 17:26:42 +03:00
|
|
|
return {'method': 'PUT', 'data': request.data}
|
2012-12-16 23:49:18 +04:00
|
|
|
elif request.method == 'PATCH':
|
2015-07-30 17:26:42 +03:00
|
|
|
return {'method': 'PATCH', 'data': request.data}
|
2012-10-08 20:10:50 +04:00
|
|
|
|
|
|
|
|
2013-09-06 20:40:34 +04:00
|
|
|
class ErrorView(APIView):
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
raise Exception
|
|
|
|
|
|
|
|
|
2017-04-06 21:25:21 +03:00
|
|
|
def custom_handler(exc, context):
|
|
|
|
if isinstance(exc, SyntaxError):
|
|
|
|
return Response({'error': 'SyntaxError'}, status=400)
|
|
|
|
return Response({'error': 'UnknownError'}, status=500)
|
|
|
|
|
|
|
|
|
|
|
|
class OverridenSettingsView(APIView):
|
|
|
|
settings = APISettings({'EXCEPTION_HANDLER': custom_handler})
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
raise SyntaxError('request is invalid syntax')
|
|
|
|
|
|
|
|
|
2013-09-06 20:40:34 +04:00
|
|
|
@api_view(['GET'])
|
|
|
|
def error_view(request):
|
|
|
|
raise Exception
|
|
|
|
|
|
|
|
|
2012-10-11 14:21:50 +04:00
|
|
|
def sanitise_json_error(error_dict):
|
|
|
|
"""
|
|
|
|
Exact contents of JSON error messages depend on the installed version
|
|
|
|
of json.
|
|
|
|
"""
|
|
|
|
ret = copy.copy(error_dict)
|
2014-05-16 03:20:40 +04:00
|
|
|
chop = len(JSON_ERROR)
|
2012-10-11 14:21:50 +04:00
|
|
|
ret['detail'] = ret['detail'][:chop]
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2012-10-08 20:10:50 +04:00
|
|
|
class ClassBasedViewIntegrationTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.view = BasicView.as_view()
|
|
|
|
|
|
|
|
def test_400_parse_error(self):
|
|
|
|
request = factory.post('/', 'f00bar', content_type='application/json')
|
|
|
|
response = self.view(request)
|
|
|
|
expected = {
|
2014-05-16 03:20:40 +04:00
|
|
|
'detail': JSON_ERROR
|
2012-10-08 20:10:50 +04:00
|
|
|
}
|
2016-11-23 16:17:00 +03:00
|
|
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
|
|
|
assert sanitise_json_error(response.data) == expected
|
2012-10-08 20:10:50 +04:00
|
|
|
|
|
|
|
|
|
|
|
class FunctionBasedViewIntegrationTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.view = basic_view
|
|
|
|
|
|
|
|
def test_400_parse_error(self):
|
|
|
|
request = factory.post('/', 'f00bar', content_type='application/json')
|
|
|
|
response = self.view(request)
|
|
|
|
expected = {
|
2014-05-16 03:20:40 +04:00
|
|
|
'detail': JSON_ERROR
|
2012-10-08 20:10:50 +04:00
|
|
|
}
|
2016-11-23 16:17:00 +03:00
|
|
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
|
|
|
assert sanitise_json_error(response.data) == expected
|
2012-10-08 20:10:50 +04:00
|
|
|
|
2013-09-06 20:40:34 +04:00
|
|
|
|
|
|
|
class TestCustomExceptionHandler(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.DEFAULT_HANDLER = api_settings.EXCEPTION_HANDLER
|
|
|
|
|
2015-07-30 17:42:11 +03:00
|
|
|
def exception_handler(exc, request):
|
2013-09-06 20:40:34 +04:00
|
|
|
return Response('Error!', status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
api_settings.EXCEPTION_HANDLER = exception_handler
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
api_settings.EXCEPTION_HANDLER = self.DEFAULT_HANDLER
|
|
|
|
|
|
|
|
def test_class_based_view_exception_handler(self):
|
|
|
|
view = ErrorView.as_view()
|
|
|
|
|
|
|
|
request = factory.get('/', content_type='application/json')
|
|
|
|
response = view(request)
|
|
|
|
expected = 'Error!'
|
2016-11-23 16:17:00 +03:00
|
|
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
|
|
|
assert response.data == expected
|
2013-09-06 20:40:34 +04:00
|
|
|
|
|
|
|
def test_function_based_view_exception_handler(self):
|
|
|
|
view = error_view
|
|
|
|
|
|
|
|
request = factory.get('/', content_type='application/json')
|
|
|
|
response = view(request)
|
|
|
|
expected = 'Error!'
|
2016-11-23 16:17:00 +03:00
|
|
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
|
|
|
assert response.data == expected
|
2017-04-06 21:25:21 +03:00
|
|
|
|
|
|
|
|
|
|
|
class TestCustomSettings(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.view = OverridenSettingsView.as_view()
|
|
|
|
|
|
|
|
def test_get_exception_handler(self):
|
|
|
|
request = factory.get('/', content_type='application/json')
|
|
|
|
response = self.view(request)
|
|
|
|
assert response.status_code == 400
|
|
|
|
assert response.data == {'error': 'SyntaxError'}
|
2024-09-07 14:21:18 +03:00
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(DJANGO_VERSION >= (5, 1), 'Only for Django 5.1+')
|
|
|
|
class TestLoginRequiredMiddlewareCompat(TestCase):
|
|
|
|
def test_class_based_view_opted_out(self):
|
|
|
|
class_based_view = BasicView.as_view()
|
|
|
|
assert class_based_view.login_required is False
|
|
|
|
|
|
|
|
def test_function_based_view_opted_out(self):
|
|
|
|
assert basic_view.login_required is False
|