def test_non_field_error(self):
     """Test ``custom_exception_handler`` for non field errors."""
     error_msg = fake.sentence()
     exception = ValidationError({'non_field_errors': [error_msg]})
     response = custom_exception_handler(exception)
     self.assertEqual(response.data['code'], 'validation_error')
     self.assertEqual(response.data['detail'], error_msg)
 def test_permission_denied(self):
     """Test ``custom_exception_handler`` for ``PermissionDenied``."""
     exception = CorePermissionDenied()
     response = custom_exception_handler(exception)
     self.assertEqual(response.data['code'], PermissionDenied.default_code)
     self.assertEqual(response.data['detail'],
                      PermissionDenied.default_detail)
 def test_field_error(self):
     """Test ``custom_exception_handler`` for field errors."""
     field_name = fake.word()
     field_error = [fake.sentence()]
     exception = ValidationError({field_name: field_error})
     response = custom_exception_handler(exception)
     self.assertEqual(response.data['code'], 'validation_error')
     self.assertEqual(response.data['validation_errors'][0]['field'],
                      field_name)
     self.assertEqual(response.data['validation_errors'][0]['errors'],
                      field_error)
    def test_custom_code_error(self):
        """Test ``custom_exception_handler`` for ``CustomAPIException``.

        All exceptions that are inherited from ``CustomAPIException`` already
        have proper detail format.

        """
        exception = CustomCodeError()
        response = custom_exception_handler(exception)
        self.assertEqual(response.data['code'], exception.code)
        self.assertEqual(response.data['detail'], exception.message)
    def test_validation_error_for_list(self):
        """Test ``custom_exception_handler`` for ValidationError (list).

        If ValidationError detail is a list, method will add to detail the
        message from list.

        """
        error_msg = fake.word()
        exception = ValidationError(error_msg)
        response = custom_exception_handler(exception)
        self.assertEqual(response.data['code'], 'validation_error')
        self.assertEqual(response.data['detail'], error_msg)
    def test_default_code_error(self):
        """Test ``custom_exception_handler`` for ``APIException``.

        All exceptions that are inherited from ``APIException`` (e.g. any
        native DRF exception like ``NotAuthenticated``) and not
        ``ValidationError`` are parsed using 'default_code' and
        'default_detail'.

        """
        exception = DefaultCodeError()
        response = custom_exception_handler(exception)
        self.assertEqual(response.data['code'], exception.default_code)
        self.assertEqual(response.data['detail'], exception.default_detail)
    def test_non_field_and_field_error(self):
        """Test ``custom_exception_handler`` for non field and field errors."""
        non_field_error_msg = fake.sentence()
        field_name = fake.word()
        field_error = [fake.sentence()]

        exception = ValidationError({
            'non_field_errors': [non_field_error_msg],
            field_name: field_error
        })

        response = custom_exception_handler(exception)
        self.assertEqual(response.data['code'], 'validation_error')
        self.assertEqual(response.data['detail'], non_field_error_msg)
        self.assertEqual(response.data['validation_errors'][0]['field'],
                         field_name)
        self.assertEqual(response.data['validation_errors'][0]['errors'],
                         field_error)
 def test_http_404(self):
     """Test ``custom_exception_handler`` for HTTP 404 error."""
     exception = Http404()
     response = custom_exception_handler(exception)
     self.assertEqual(response.data['code'], NotFound.default_code)
     self.assertEqual(response.data['detail'], NotFound.default_detail)