def test_nested_many_serializer_field(self): data = { 'name': 'test', 'value': 'valid', 'children': [{ 'name': 'ok' }, { 'name': 'invalid' }], } serializer = TestSerializer(data=data) try: serializer.is_valid(raise_exception=True) except Exception as exc: resp = handlers.error_list_exception_handler(exc, {}) assert resp.status_code == 400 assert resp.data == { 'errors': [ { 'code': 'invalid', 'message': 'bad name', 'source': '/children/1/name', }, ] }
def test_not_found(self): exc = Http404() resp = handlers.error_list_exception_handler(exc, {}) assert resp.status_code == 404 assert resp.data == { 'errors': [ { 'code': 'not_found', 'message': 'Not found.', }, ] }
def test_permission_denied(self): exc = PermissionDenied() resp = handlers.error_list_exception_handler(exc, {}) assert resp.status_code == 403 assert resp.data == { 'errors': [ { 'code': 'permission_denied', 'message': 'You do not have permission to perform this action.', }, ] }
def test_validation_error(self): exc = serializers.ValidationError('value error') resp = handlers.error_list_exception_handler(exc, {}) assert resp.status_code == 400 assert resp.data == { 'errors': [ { 'code': 'invalid', 'message': 'value error', }, ] } exc = serializers.ValidationError('value error', code='wrong_value') resp = handlers.error_list_exception_handler(exc, {}) assert resp.status_code == 400 assert resp.data == { 'errors': [ { 'code': 'wrong_value', 'message': 'value error', }, ] }
def test_field_error(self): data = {'name': 'valid'} serializer = TestSerializer(data=data) try: serializer.is_valid(raise_exception=True) except Exception as exc: resp = handlers.error_list_exception_handler(exc, {}) assert resp.status_code == 400 assert resp.data == { 'errors': [ { 'code': 'required', 'message': 'This field is required.', 'source': '/value', }, ] }
def test_validation_error_with_array(self): data = {'name': 'valid', 'value': 'valid', 'array': [None]} serializer = TestSerializer(data=data) try: serializer.is_valid(raise_exception=True) except Exception as exc: resp = handlers.error_list_exception_handler(exc, {}) assert resp.status_code == 400 assert resp.data == { 'errors': [ { 'code': 'null', 'message': 'This field may not be null.', 'source': '/array/0', }, ] }
def test_validation_error_detail_field(self): data = {'name': 'valid', 'value': 'valid', 'detail': 'none'} serializer = TestSerializer(data=data) try: serializer.is_valid(raise_exception=True) except Exception as exc: resp = handlers.error_list_exception_handler(exc, {}) assert resp.status_code == 400 assert resp.data == { 'errors': [ { 'code': 'invalid', 'message': 'cant use none', 'source': '/detail', }, ] }
def test_validation_error_detail_field_camelcase_renderer(self): data = {'name': 'valid', 'value': 'valid', 'underscore_field': 'none'} serializer = TestSerializer(data=data) try: serializer.is_valid(raise_exception=True) except Exception as exc: context = {'view': views.APIView(), 'request': None} context['view'].perform_content_negotiation = \ MagicMock(return_value=(CamelCaseJSONRenderer(), 'application/json')) resp = handlers.error_list_exception_handler(exc, context) assert resp.status_code == 400 assert resp.data == { 'errors': [ { 'code': 'invalid', 'message': 'cant use none', 'source': '/underscoreField', }, ] }