예제 #1
0
def test_validation_field_errors():
    error = ValidationError({
        'name': ['Invalid name.'],
        'count': ['Invalid count.'],
    })
    response = exception_handler(error, None)
    errors = response.data['errors']
    assert errors == [
        {
            'status': '400',
            'code': 'invalid',
            'title': 'Invalid input.',
            'detail': 'Invalid name.',
            'source': {
                'parameter': 'name'
            },
        },
        {
            'status': '400',
            'code': 'invalid',
            'title': 'Invalid input.',
            'detail': 'Invalid count.',
            'source': {
                'parameter': 'count'
            },
        },
    ]
예제 #2
0
def test_http_not_found():
    error = Http404()
    response = exception_handler(error, None)
    errors = response.data['errors']
    assert errors == [
        {
            'status': '404',
            'code': 'not_found',
            'title': 'Not found.',
        },
    ]
예제 #3
0
def test_api_exc_string_w_code():
    error = APIException(code='custom_code')
    response = exception_handler(error, None)
    errors = response.data['errors']
    assert errors == [
        {
            'status': '500',
            'code': 'custom_code',
            'title': 'A server error occurred.',
        },
    ]
예제 #4
0
def test_http_permission_denied():
    error = PermissionDenied('Permission denied.')
    response = exception_handler(error, None)
    errors = response.data['errors']
    assert errors == [
        {
            'status': '403',
            'code': 'permission_denied',
            'title': 'You do not have permission to perform this action.',
        },
    ]
예제 #5
0
def test_api_exc_string_w_message():
    error = APIException('Custom error message.')
    response = exception_handler(error, None)
    errors = response.data['errors']
    assert errors == [
        {
            'status': '500',
            'code': 'error',
            'title': 'A server error occurred.',
            'detail': 'Custom error message.'
        },
    ]
예제 #6
0
def test_validation_errors_complex():
    error = ValidationError({
        'foo':
        'Foo error message.',
        'bar': [
            ErrorDetail('Bar conflict message.', code='conflict'),
            'Second bar message.'
        ],
        api_settings.NON_FIELD_ERRORS_KEY: ['Non field error message.'],
    })
    response = exception_handler(error, None)
    errors = response.data['errors']
    assert errors == [
        {
            'status': '400',
            'code': 'invalid',
            'title': 'Invalid input.',
            'detail': 'Foo error message.',
            'source': {
                'parameter': 'foo'
            },
        },
        {
            'status': '400',
            'code': 'conflict',
            'title': 'Invalid input.',
            'detail': 'Bar conflict message.',
            'source': {
                'parameter': 'bar'
            },
        },
        {
            'status': '400',
            'code': 'invalid',
            'title': 'Invalid input.',
            'detail': 'Second bar message.',
            'source': {
                'parameter': 'bar'
            },
        },
        {
            'status': '400',
            'code': 'invalid',
            'title': 'Invalid input.',
            'detail': 'Non field error message.',
        },
    ]
예제 #7
0
def test_api_exc_list():
    error = APIException([
        'First error message.',
        'Second error message.',
    ])
    response = exception_handler(error, None)
    errors = response.data['errors']
    assert errors == [
        {
            'status': '500',
            'code': 'error',
            'title': 'A server error occurred.',
            'detail': 'First error message.',
        },
        {
            'status': '500',
            'code': 'error',
            'title': 'A server error occurred.',
            'detail': 'Second error message.',
        },
    ]
예제 #8
0
def test_validation_non_field_errors():
    error = ValidationError({
        api_settings.NON_FIELD_ERRORS_KEY: [
            'First error message.',
            'Second error message.',
        ]
    })
    response = exception_handler(error, None)
    errors = response.data['errors']
    assert errors == [
        {
            'status': '400',
            'code': 'invalid',
            'title': 'Invalid input.',
            'detail': 'First error message.',
        },
        {
            'status': '400',
            'code': 'invalid',
            'title': 'Invalid input.',
            'detail': 'Second error message.',
        },
    ]