예제 #1
0
def test_validation_error() -> None:
    # Default code
    response = exception_handler(
        exceptions.ValidationError("I did not like your input."))
    assert response is not None
    assert response.status_code == status.HTTP_400_BAD_REQUEST
    assert response.data == {
        "type": "validation_error",
        "code": "invalid_input",  # Default code for `validation_error`
        "detail": "I did not like your input.",
        "attr": None,
    }

    # Custom code
    response = exception_handler(
        exceptions.ValidationError("I did not like your input.",
                                   code="ugly_input"))
    assert response is not None
    assert response.status_code == status.HTTP_400_BAD_REQUEST
    assert response.data == {
        "type": "validation_error",
        "code": "ugly_input",  # Default code for `validation_error`
        "detail": "I did not like your input.",
        "attr": None,
    }
예제 #2
0
def test_permission_denied_exception(res_permission_denied) -> None:
    response = exception_handler(exceptions.PermissionDenied())
    assert response is not None
    assert response.status_code == status.HTTP_403_FORBIDDEN
    assert response.data == res_permission_denied

    # Test Django base exception too
    response = exception_handler(PermissionDenied())
    assert response is not None
    assert response.status_code == status.HTTP_403_FORBIDDEN
    assert response.data == res_permission_denied
예제 #3
0
def test_not_found_exception(res_not_found) -> None:
    response = exception_handler(exceptions.NotFound())
    assert response is not None
    assert response.status_code == status.HTTP_404_NOT_FOUND
    assert response.data == res_not_found

    # Test Django base exception too
    response = exception_handler(Http404())
    assert response is not None
    assert response.status_code == status.HTTP_404_NOT_FOUND
    assert response.data == res_not_found
예제 #4
0
def test_not_found_exception_in_debug(settings, res_not_found) -> None:
    settings.DEBUG = True

    # Same as normal, since exception is an APIException instance
    response = exception_handler(exceptions.NotFound())
    assert response is not None
    assert response.status_code == status.HTTP_404_NOT_FOUND
    assert response.data == res_not_found

    # Test Django base 404 exception too
    response = exception_handler(Http404())
    assert response.status_code == status.HTTP_404_NOT_FOUND
    assert response.data == res_not_found
예제 #5
0
def test_throttled_exception() -> None:
    response = exception_handler(exceptions.Throttled(62))
    assert response is not None
    assert response.status_code == status.HTTP_429_TOO_MANY_REQUESTS
    assert response.data == {
        "type": "throttled_error",
        "code": "throttled",
        "detail": "Request was throttled. Expected available in 62 seconds.",
        "attr": None,
    }
예제 #6
0
def test_python_exception_with_enabled_in_debug(res_server_error, settings,
                                                monkeypatch) -> None:
    settings.DEBUG = True
    monkeypatch.setattr(api_settings, "ENABLE_IN_DEBUG", True)

    # Handled by exceptions_hog since `ENABLE_IN_DEBUG` is `True`
    response = exception_handler(TypeError())
    assert response is not None
    assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
    assert response.data == res_server_error
예제 #7
0
def test_not_acceptable_exception() -> None:
    response = exception_handler(exceptions.NotAcceptable())
    assert response is not None
    assert response.status_code == status.HTTP_406_NOT_ACCEPTABLE
    assert response.data == {
        "type": "invalid_request",
        "code": "not_acceptable",
        "detail": "Could not satisfy the request Accept header.",
        "attr": None,
    }
예제 #8
0
def test_unsupported_media_type_exception() -> None:
    response = exception_handler(
        exceptions.UnsupportedMediaType("application/xml"))
    assert response is not None
    assert response.status_code == status.HTTP_415_UNSUPPORTED_MEDIA_TYPE
    assert response.data == {
        "type": "invalid_request",
        "code": "unsupported_media_type",
        "detail": 'Unsupported media type "application/xml" in request.',
        "attr": None,
    }
예제 #9
0
def test_drf_exception_in_debug(settings) -> None:
    settings.DEBUG = True

    # Exception is handled as usual with the exceptions_hog handler
    response = exception_handler(exceptions.Throttled(28))
    assert response is not None
    assert response.status_code == status.HTTP_429_TOO_MANY_REQUESTS
    assert response.data == {
        "type": "throttled_error",
        "code": "throttled",
        "detail": "Request was throttled. Expected available in 28 seconds.",
        "attr": None,
    }
예제 #10
0
def test_protected_error() -> None:
    response = exception_handler(
        ProtectedError("Resource 'Hedgehog' has dependencies.",
                       protected_objects=[1]))
    assert response is not None
    assert response.status_code == status.HTTP_409_CONFLICT
    assert response.data == {
        "type": "invalid_request",
        "code": "protected_error",
        "detail": "Requested operation cannot be completed because"
        " a related object is protected.",
        "attr": None,
    }
예제 #11
0
def test_python_exception_in_debug(settings) -> None:
    settings.DEBUG = True
    # Not handled, since not APIException instance
    response = exception_handler(TypeError())
    assert response is None
예제 #12
0
def test_type_error(res_server_error) -> None:
    response = exception_handler(TypeError())
    assert response is not None
    assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
    assert response.data == res_server_error
예제 #13
0
def test_not_implemented_error(res_server_error) -> None:
    response = exception_handler(
        NotImplementedError("This function is not implemented"))
    assert response is not None
    assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
    assert response.data == res_server_error