def test_response_validation_with_parametrized_path():
    """
    Test that request validation finds and validates parametrized paths.
    """
    schema = SchemaFactory(
        paths={
            '/get/{id}': {
                'get': {'responses': {200: {'description': 'Success'}}},
                'parameters': [
                    {
                        'name': 'id',
                        'in': PATH,
                        'description': 'The Primary Key',
                        'type': INTEGER,
                        'required': True,
                    }
                ]
            },
        }
    )

    response = ResponseFactory(url='http://www.example.com/get/1234')

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
Beispiel #2
0
def test_response_validation_with_invalid_operation_on_path():
    schema = SchemaFactory(
        produces=['application/json'],
        paths={
            '/get': {
                'get': {
                    'responses': {
                        '200': {
                            'description': 'Success'
                        }
                    },
                }
            },
        },
    )

    response = ResponseFactory(
        url='http://www.example.com/get',
        content_type=None,
    )

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
def test_response_parameter_validation():
    """
    Test that request validation does parameter validation.  This is largely a
    smoke test to ensure that parameter validation is wired into request
    validation correctly.
    """
    schema = SchemaFactory(paths={
        '/get': {
            'get': {
                'responses': {
                    '200': {
                        'description': 'Success'
                    }
                },
            },
        },
    }, )

    response = ResponseFactory(url='http://www.example.com/get',
                               status_code=301)

    with pytest.raises(ValidationError) as err:
        validate_response(
            response=response,
            request_method='get',
            schema=schema,
        )

    assert 'status_code' in err.value.messages[0]
    assert_error_message_equal(
        err.value.messages[0]['status_code'][0],
        MESSAGES['response']['invalid_status_code'],
    )
def test_produces_validation_is_noop_when_produces_not_declared():
    """
    Test that the `produces` validation is a noop when no content types are
    declared.
    """
    response = ResponseFactory(
        content_type='application/json',
        url='http://www.example.com/get',
    )

    schema = SchemaFactory(paths={
        '/get': {
            'get': {
                'responses': {
                    '200': {
                        'description': 'Success'
                    }
                }
            }
        },
    }, )

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
Beispiel #5
0
def validate_api_call(schema, raw_request, raw_response):
    """
    Validate the request/response cycle of an api call against a swagger
    schema.  Request/Response objects from the `requests` and `urllib` library
    are supported.
    """
    request = normalize_request(raw_request)
    response = normalize_response(raw_response)

    with ErrorCollection() as errors:
        try:
            validate_request(
                request=request,
                schema=schema,
            )
        except ValidationError as err:
            errors['request'].add_error(err.messages or getattr(err, 'detail'))
            return

        try:
            validate_response(
                response=response,
                request_method=request.method,
                schema=schema,
            )
        except ValidationError as err:
            errors['response'].add_error(err.messages
                                         or getattr(err, 'detail'))
def test_response_validation_with_invalid_method():
    """
    Test that response validation detects not defined method.
    """
    schema = SchemaFactory(
        paths={
            '/get': {
                GET: {'responses': {'200': {'description': 'Success'}}},
            },
        }
    )

    response = ResponseFactory(url='http://www.example.com/get')

    with pytest.raises(ValidationError) as err:
        validate_response(
            response=response,
            request_method=POST,
            schema=schema,
        )

    assert_message_in_errors(
        MESSAGES['request']['invalid_method'],
        err.value.detail,
        'method',
    )
def test_produces_validation_invalid_mimetype_from_global_definition():
    """
    Test that a response content_type that is in the global api produces
    definitions is valid.
    """
    response = ResponseFactory(
        content_type='application/json',
        url='http://www.example.com/get',
    )

    schema = SchemaFactory(
        produces=['application/xml'],
        paths={
            '/get': {
                'get': {
                    'responses': {
                        '200': {
                            'description': 'Success'
                        }
                    }
                }
            },
        },
    )

    with pytest.raises(ValidationError):
        validate_response(
            response=response,
            request_method='get',
            schema=schema,
        )
Beispiel #8
0
def test_produces_validation_for_invalid_mimetype_from_operation_definition():
    """
    Test the situation when the operation definition has overridden the global
    allowed mimetypes, that that the local value is used for validation.
    """
    response = ResponseFactory(
        content_type='application/xml',
        url='http://www.example.com/get',
    )

    schema = SchemaFactory(
        produces=['application/xml'],
        paths={
            '/get': {'get': {
                'responses': {200: {'description': 'Success'}},
                'produces': ['application/json'],
            }},
        },
    )

    with pytest.raises(ValidationError) as err:
        validate_response(
            response=response,
            request_method='get',
            schema=schema,
        )

    assert_message_in_errors(
        MESSAGES['content_type']['invalid'],
        err.value.detail,
        'body.produces',
    )
Beispiel #9
0
def test_basic_response_body_schema_validation_with_invalid_value():
    schema = SchemaFactory(
        paths={
            '/get': {
                'get': {
                    'responses': {
                        200: {
                            'description': 'Success',
                            'schema': {'type': INTEGER},
                        }
                    },
                },
            },
        },
    )

    response = ResponseFactory(
        url='http://www.example.com/get',
        status_code=200,
        content_type='application/json',
        content=json.dumps('not-an-integer'),
    )

    with pytest.raises(ValidationError) as err:
        validate_response(
            response=response,
            request_method='get',
            schema=schema,
        )

    assert_message_in_errors(
        MESSAGES['type']['invalid'],
        err.value.detail,
        'body.schema.type',
    )
def test_response_paramater_uses_default():
    """
    Test that a `default` key is used if one exists and no matching response is found.

    See http://swagger.io/specification/#responsesObject.
    """

    schema = SchemaFactory(
        paths={
            '/get': {
                'get': {
                    'responses': {
                        200: {'description': 'Success'},
                        'default': {'description': 'Unexpected error.'}
                    },
                },
            },
        },
    )

    response = ResponseFactory(url='http://www.example.com/get', status_code=301)

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
def test_response_content_type_validation_ignores_parameters():
    schema = SchemaFactory(
        produces=['application/json'],
        paths={
            '/get': {
                'get': {
                    'responses': {
                        '200': {
                            'description': 'Success'
                        }
                    },
                }
            },
        },
    )

    response = ResponseFactory(
        url='http://www.example.com/get',
        content_type='application/json; charset=UTF-8',
    )

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
Beispiel #12
0
def validate_api_call(schema, raw_request, raw_response):
    """
    Validate the request/response cycle of an api call against a swagger
    schema.  Request/Response objects from the `requests` and `urllib` library
    are supported.
    """
    request = normalize_request(raw_request)
    response = normalize_response(raw_response)

    with ErrorCollection() as errors:
        try:
            validate_request(
                request=request,
                schema=schema,
            )
        except ValidationError as err:
            errors['request'].add_error(err.messages or getattr(err, 'detail'))
            return

        try:
            validate_response(
                response=response,
                request_method=request.method,
                schema=schema,
            )
        except ValidationError as err:
            errors['response'].add_error(err.messages or getattr(err, 'detail'))
Beispiel #13
0
def test_response_validation_with_parametrized_path():
    """
    Test that request validation finds and validates parametrized paths.
    """
    schema = SchemaFactory(
        paths={
            '/get/{id}': {
                'get': {
                    'responses': {
                        200: {
                            'description': 'Success'
                        }
                    }
                },
                'parameters': [{
                    'name': 'id',
                    'in': PATH,
                    'description': 'The Primary Key',
                    'type': INTEGER,
                    'required': True,
                }]
            },
        })

    response = ResponseFactory(url='http://www.example.com/get/1234')

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
Beispiel #14
0
def test_response_validation_with_valid_path_and_base_path():
    """
    Test that response validation is able to match api paths even when there is
    a base path.
    """
    schema = SchemaFactory(basePath='/api/v1',
                           paths={
                               '/get': {
                                   'get': {
                                       'responses': {
                                           200: {
                                               'description': 'Success'
                                           }
                                       }
                                   },
                               },
                           })

    response = ResponseFactory(url='http://www.example.com/api/v1/get')

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
def test_response_parameter_validation():
    """
    Test that request validation does parameter validation.  This is largely a
    smoke test to ensure that parameter validation is wired into request
    validation correctly.
    """
    from django.core.exceptions import ValidationError

    schema = SchemaFactory(
        paths={
            '/get': {
                'get': {
                    'responses': {200: {'description': 'Success'}},
                },
            },
        },
    )

    response = ResponseFactory(url='http://www.example.com/get', status_code=301)

    with pytest.raises(ValidationError) as err:
        validate_response(
            response,
            paths=schema['paths'],
            base_path=schema.get('base_path', ''),
            context=schema,
            inner=True,
        )

    assert 'response' in err.value.messages[0]
    assert_error_message_equal(
        err.value.messages[0]['response'][0],
        MESSAGES['response']['invalid_status_code'],
    )
def test_response_validation_with_parametrized_path_and_base_path_slash_end():
    """
    Test that request validation finds and validates parametrized paths even
    when the api has a base path being /.
    """
    schema = SchemaFactory(basePath='/api/v1/',
                           paths={
                               '/get/{id}': {
                                   'get': {
                                       'responses': {
                                           '200': {
                                               'description': 'Success'
                                           }
                                       }
                                   },
                                   'parameters': [{
                                       'name': 'id',
                                       'in': PATH,
                                       'description': 'The Primary Key',
                                       'type': INTEGER,
                                       'required': True,
                                   }]
                               },
                           })

    response = ResponseFactory(url='http://www.example.com/api/v1/get/1234')

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
def test_response_parameter_with_yaml():
    """
    Test that when flex is asked to load YAML file, a response value that was
    an integer, is converted to a string
    """
    schema = SchemaFactory(
        paths={
            '/get': {
                'get': {
                    'responses': {
                        200: {'description': 'Success'},
                        404: {'description': 'Failure'},
                        'default': {'description': 'Unexpected error.'}
                    },
                },
            },
        },
    )

    response = ResponseFactory(url='http://www.example.com/get',
                               status_code=404)

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
Beispiel #18
0
def test_produces_validation_for_valid_mimetype_from_operation_definition():
    """
    Test that when `produces` is defined in an operation definition, that the
    local value is used in place of any global `produces` definition.
    """
    response = ResponseFactory(
        content_type='application/json',
        url='http://www.example.com/get',
    )

    schema = SchemaFactory(
        produces=['application/xml'],
        paths={
            '/get': {'get': {
                'responses': {200: {'description': 'Success'}},
                'produces': ['application/json'],
            }},
        },
    )

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
def test_response_content_type_validation_when_no_content_type_specified():
    schema = SchemaFactory(
        produces=['application/json'],
        paths={
            '/get': {
                'get': {
                    'responses': {
                        '200': {
                            'description': 'Success'
                        }
                    },
                }
            },
        },
    )

    response = ResponseFactory(
        url='http://www.example.com/get',
        content_type=None,
    )

    # this is considered valid currently, but may change
    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
def test_response_validation_with_invalid_operation_on_path():
    """
    Test that response validation detects request paths that are not declared
    in the schema.
    """
    from django.core.exceptions import ValidationError

    schema = SchemaFactory(
        paths={
            '/post': {
                'post': None,
            },
        },
    )

    response = ResponseFactory(url='http://www.example.com/post')

    with pytest.raises(ValidationError) as err:
        validate_response(
            response,
            paths=schema['paths'],
            base_path=schema.get('base_path', ''),
            context=schema,
            inner=True,
        )

    assert 'request' in err.value.messages[0]
    assert_error_message_equal(
        err.value.messages[0]['request'][0],
        MESSAGES['request']['invalid_method'],
    )
Beispiel #21
0
def test_basic_response_body_schema_validation_with_invalid_value():
    schema = SchemaFactory(
        paths={
            '/get': {
                'get': {
                    'responses': {
                        200: {
                            'description': 'Success',
                            'schema': {'type': INTEGER},
                        }
                    },
                },
            },
        },
    )

    response = ResponseFactory(
        url='http://www.example.com/get',
        status_code=200,
        content_type='application/json',
        content=json.dumps('not-an-integer'),
    )

    with pytest.raises(ValidationError) as err:
        validate_response(
            response=response,
            request_method='get',
            schema=schema,
        )

    assert_message_in_errors(
        MESSAGES['type']['invalid'],
        err.value.detail,
        'body.schema.type',
    )
def test_response_header_validation_for_non_strings(type_, value):
    schema = SchemaFactory(
        paths={
            '/get': {
                'get': {
                    'responses': {'200': {
                        'description': "Success",
                        'headers': {
                            'Foo': {'type': type_},
                        }
                    }},
                },
            },
        },
    )

    response = ResponseFactory(
        url='http://www.example.com/get',
        headers={'Foo': value},
    )

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
Beispiel #23
0
def test_produces_validation_for_invalid_mimetype_from_operation_definition():
    """
    Test the situation when the operation definition has overridden the global
    allowed mimetypes, that that the local value is used for validation.
    """
    from django.core.exceptions import ValidationError
    response = ResponseFactory(content_type='application/xml')

    schema = SchemaFactory(
        produces=['application/xml'],
        paths={
            '/get': {'get': {
                'responses': {200: {'description': 'Success'}},
                'produces': ['application/json'],
            }},
        },
    )

    with pytest.raises(ValidationError):
        validate_response(
            response,
            operation_definition=schema['paths']['/get']['get'],
            context=schema,
            inner=True,
        )
Beispiel #24
0
def test_basic_response_body_schema_validation_with_nullable_value():
    """
    Ensure objects marked with x-nullable: true attribute are treated as nullable.
    """
    schema = SchemaFactory(paths={
        '/get': {
            'get': {
                'responses': {
                    '200': {
                        'description': 'Success',
                        'schema': {
                            'type': INTEGER,
                            'x-nullable': True
                        },
                    }
                },
            },
        },
    }, )

    response = ResponseFactory(
        url='http://www.example.com/get',
        status_code=200,
        content_type='application/json',
        content=json.dumps(None),
    )

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
Beispiel #25
0
def test_response_header_validation_for_non_strings(type_, value):
    schema = SchemaFactory(paths={
        '/get': {
            'get': {
                'responses': {
                    '200': {
                        'description': "Success",
                        'headers': {
                            'Foo': {
                                'type': type_
                            },
                        }
                    }
                },
            },
        },
    }, )

    response = ResponseFactory(
        url='http://www.example.com/get',
        headers={'Foo': value},
    )

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
def test_response_parameter_validation():
    """
    Test that request validation does parameter validation.  This is largely a
    smoke test to ensure that parameter validation is wired into request
    validation correctly.
    """
    schema = SchemaFactory(
        paths={
            '/get': {
                'get': {
                    'responses': {200: {'description': 'Success'}},
                },
            },
        },
    )

    response = ResponseFactory(url='http://www.example.com/get', status_code=301)

    with pytest.raises(ValidationError) as err:
        validate_response(
            response=response,
            request_method='get',
            schema=schema,
        )

    assert 'status_code' in err.value.messages[0]
    assert_error_message_equal(
        err.value.messages[0]['status_code'][0],
        MESSAGES['response']['invalid_status_code'],
    )
def test_response_paramater_uses_default():
    """
    Test that a `default` key is used if one exists and no matching response is found.

    See http://swagger.io/specification/#responsesObject.
    """

    schema = SchemaFactory(paths={
        '/get': {
            'get': {
                'responses': {
                    '200': {
                        'description': 'Success'
                    },
                    'default': {
                        'description': 'Unexpected error.'
                    }
                },
            },
        },
    }, )

    response = ResponseFactory(url='http://www.example.com/get',
                               status_code=301)

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
Beispiel #28
0
def validate_api_response(schema, raw_response, request_method='get'):
    """
    Validate the response of an api call against a swagger schema.
    """
    response = None
    if raw_response is not None:
        response = normalize_response(raw_response)

    if response is not None:
        validate_response(response=response,
                          request_method=request_method,
                          schema=schema)
Beispiel #29
0
def test_basic_response_body_schema_validation_with_type_mismatch():
    """
    Ensure that when the expected response type is an object, and some other
    type is provided, that schema validation does not break since internally it
    will try to pull keys off of the value.
    """
    schema = SchemaFactory(paths={
        '/get': {
            'get': {
                'responses': {
                    '200': {
                        'description': 'Success',
                        'schema': {
                            'type': OBJECT,
                            'required': ['id', 'name'],
                            'properties': {
                                'id': {
                                    'type': INTEGER
                                },
                                'name': {
                                    'type': STRING
                                },
                            },
                        },
                    }
                },
            },
        },
    }, )

    response = ResponseFactory(
        url='http://www.example.com/get',
        status_code=200,
        content_type='application/json',
        content=json.dumps([{
            'id': 3,
            'name': 'Bob'
        }]),
    )

    with pytest.raises(ValidationError) as err:
        validate_response(
            response=response,
            request_method='get',
            schema=schema,
        )

    assert_message_in_errors(
        MESSAGES['type']['invalid'],
        err.value.detail,
        'body.schema.type',
    )
def test_response_parameter_validation():
    """
    Test that request validation does parameter validation.  This is largely a
    smoke test to ensure that parameter validation is wired into request
    validation correctly.
    """
    schema = SchemaFactory(paths={"/get": {"get": {"responses": {200: {"description": "Success"}}}}})

    response = ResponseFactory(url="http://www.example.com/get", status_code=301)

    with pytest.raises(ValidationError) as err:
        validate_response(response=response, request_method="get", schema=schema)

    assert "status_code" in err.value.messages[0]
    assert_error_message_equal(err.value.messages[0]["status_code"][0], MESSAGES["response"]["invalid_status_code"])
Beispiel #31
0
def test_response_validation_with_parameter_as_reference_for_invalid_value():
    """
    Test that request validation finds and validates parametrized paths when
    the parameter is a reference.  Ensure that it detects invalid types.
    """
    schema = SchemaFactory(
        paths={
            '/get/{id}': {
                'get': {
                    'responses': {
                        '200': {
                            'description': 'Success'
                        }
                    }
                },
                'parameters': [
                    {
                        '$ref': '#/parameters/id'
                    },
                ]
            },
        },
        parameters={
            'id': {
                'name': 'id',
                'in': PATH,
                'description': 'The Primary Key',
                'type': INTEGER,
                'required': True,
            }
        },
    )

    response = ResponseFactory(url='http://www.example.com/get/abc')

    with pytest.raises(ValidationError) as err:
        validate_response(
            response=response,
            request_method='get',
            schema=schema,
        )

    assert_message_in_errors(
        MESSAGES['type']['invalid'],
        err.value.detail,
        'path.id.type',
    )
Beispiel #32
0
def test_basic_response_body_schema_validation_with_type_mismatch():
    """
    Ensure that when the expected response type is an object, and some other
    type is provided, that schema validation does not break since internally it
    will try to pull keys off of the value.
    """
    schema = SchemaFactory(
        paths={
            '/get': {
                'get': {
                    'responses': {
                        200: {
                            'description': 'Success',
                            'schema': {
                                'type': OBJECT,
                                'required': ['id', 'name'],
                                'properties': {
                                    'id': {'type': INTEGER},
                                    'name': {'type': STRING},
                                },
                            },
                        }
                    },
                },
            },
        },
    )

    response = ResponseFactory(
        url='http://www.example.com/get',
        status_code=200,
        content_type='application/json',
        content=json.dumps([{'id': 3, 'name': 'Bob'}]),
    )

    with pytest.raises(ValidationError) as err:
        validate_response(
            response=response,
            request_method='get',
            schema=schema,
        )

    assert_message_in_errors(
        MESSAGES['type']['invalid'],
        err.value.detail,
        'body.schema.type',
    )
def test_request_header_validation():
    from django.core.exceptions import ValidationError

    schema = SchemaFactory(
        paths={
            '/get/': {
                'get': {
                    'responses': {200: {'description': "Success"}},
                    'parameters': [
                        {
                            'name': 'Authorization',
                            'in': HEADER,
                            'type': INTEGER,
                        }
                    ]
                },
            },
        },
    )

    response = ResponseFactory(
        url='http://www.example.com/get/',
        request__headers={'Authorization': 'abc'},
    )

    with pytest.raises(ValidationError) as err:
        validate_response(
            response,
            paths=schema['paths'],
            base_path=schema.get('base_path', ''),
            context=schema,
            inner=True,
        )

    assert 'request' in err.value.messages[0]
    assert 'parameters' in err.value.messages[0]['request'][0][0]
    assert 'headers' in err.value.messages[0]['request'][0][0]['parameters'][0]
    assert 'Authorization' in err.value.messages[0]['request'][0][0]['parameters'][0]['headers'][0]
    assert 'type' in err.value.messages[0]['request'][0][0]['parameters'][0]['headers'][0]['Authorization'][0]
    assert_error_message_equal(
        err.value.messages[0]['request'][0][0]['parameters'][0]['headers'][0]['Authorization'][0]['type'][0],
        MESSAGES['type']['invalid'],
    )
Beispiel #34
0
def test_produces_validation_valid_mimetype_from_global_definition():
    """
    Test that a response content_type that is in the global api produces
    definitions is valid.
    """
    response = ResponseFactory(content_type='application/json')

    schema = SchemaFactory(
        produces=['application/json'],
        paths={
            '/get': {'get': {'responses': {200: {'description': 'Success'}}}},
        },
    )

    validate_response(
        response,
        operation_definition=schema['paths']['/get']['get'],
        context=schema,
    )
def test_response_validation_with_valid_path():
    """
    Test that response validation is able to match api paths.
    """
    schema = SchemaFactory(
        paths={
            '/get': {
                'get': {'responses': {200: {'description': 'Success'}}},
            },
        }
    )

    response = ResponseFactory(url='http://www.example.com/get')

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
def test_response_validation_with_valid_path_and_base_path():
    """
    Test that response validation is able to match api paths even when there is
    a base path.
    """
    schema = SchemaFactory(
        basePath='/api/v1',
        paths={
            '/get': {
                'get': {'responses': {'200': {'description': 'Success'}}},
            },
        }
    )

    response = ResponseFactory(url='http://www.example.com/api/v1/get')

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
Beispiel #37
0
def test_produces_validation_is_noop_when_produces_not_declared():
    """
    Test that the `produces` validation is a noop when no content types are
    declared.
    """
    response = ResponseFactory(
        content_type='application/json',
        url='http://www.example.com/get',
    )

    schema = SchemaFactory(
        paths={
            '/get': {'get': {'responses': {200: {'description': 'Success'}}}},
        },
    )

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
Beispiel #38
0
def test_response_reference():
    schema = SchemaFactory(
            paths={
                '/get': {
                    'get': {'responses': {
                            '200':
                                { '$ref': "#/responses/NumberResponse"},
                            }
                    }
                },
            },
            definitions={
                'Number': {
                    'type': INTEGER,
                },
            },
            responses={
                'NumberResponse':
                {'description': 'Success', 'schema': {'$ref': '#/definitions/Number'}},
            },
    )

    response = ResponseFactory(
            url='http://www.example.com/get',
            status_code=200,
            content_type='application/json',
            content=json.dumps(None),
    )

    with pytest.raises(ValidationError) as err:
        validate_response(
                response=response,
                request_method='get',
                schema=schema,
        )
    assert_message_in_errors(
            MESSAGES['type']['invalid'],
            err.value.detail,
            'body.schema.type',
    )
Beispiel #39
0
def test_basic_response_body_schema_validation_with_invalid_value():
    from django.core.exceptions import ValidationError
    schema = SchemaFactory(
        paths={
            '/get': {
                'get': {
                    'responses': {
                        200: {
                            'description': 'Success',
                            'schema': {'type': INTEGER},
                        }
                    },
                },
            },
        },
    )

    response = ResponseFactory(
        url='http://www.example.com/get',
        status_code=200,
        content_type='application/json',
        content=json.dumps('not-an-integer'),
    )

    with pytest.raises(ValidationError) as err:
        validate_response(
            response,
            schema['paths']['/get']['get'],
            context=schema,
            inner=True,
        )

    assert 'body' in err.value.messages[0]
    assert 'schema' in err.value.messages[0]['body'][0]
    assert 'type' in err.value.messages[0]['body'][0]['schema'][0]
    assert_error_message_equal(
        err.value.messages[0]['body'][0]['schema'][0]['type'][0],
        MESSAGES['type']['invalid'],
    )
def test_response_validation_with_invalid_path():
    """
    Test that request validation detects request paths that are not declared
    in the schema.
    """
    schema = SchemaFactory()
    assert not schema['paths']

    response = ResponseFactory(url='http://www.example.com/not-an-api-path')

    with pytest.raises(ValidationError) as err:
        validate_response(
            response=response,
            request_method='get',
            schema=schema,
        )

    assert_message_in_errors(
        MESSAGES['path']['no_matching_paths_found'],
        err.value.detail,
        'path',
    )
def test_response_validation_with_parameter_as_reference_for_invalid_value():
    """
    Test that request validation finds and validates parametrized paths when
    the parameter is a reference.  Ensure that it detects invalid types.
    """
    schema = SchemaFactory(
        paths={
            '/get/{id}': {
                'get': {'responses': {200: {'description': 'Success'}}},
                'parameters': [
                    {'$ref': '#/parameters/id'},
                ]
            },
        },
        parameters={
            'id': {
                'name': 'id',
                'in': PATH,
                'description': 'The Primary Key',
                'type': INTEGER,
                'required': True,
            }
        },
    )

    response = ResponseFactory(url='http://www.example.com/get/abc')

    with pytest.raises(ValidationError) as err:
        validate_response(
            response=response,
            request_method='get',
            schema=schema,
        )

    assert_message_in_errors(
        MESSAGES['type']['invalid'],
        err.value.detail,
        'path.id.type',
    )
Beispiel #42
0
def test_produces_validation_valid_mimetype_from_global_definition():
    """
    Test that a response content_type that is in the global api produces
    definitions is valid.
    """
    response = ResponseFactory(
        content_type='application/json',
        url='http://www.example.com/get',
    )

    schema = SchemaFactory(
        produces=['application/json'],
        paths={
            '/get': {'get': {'responses': {200: {'description': 'Success'}}}},
        },
    )

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
def test_response_content_type_validation_ignores_parameters():
    schema = SchemaFactory(
        produces=['application/json'],
        paths={
            '/get': {
                'get': {
                    'responses': {'200': {'description': 'Success'}},
                }
            },
        },
    )

    response = ResponseFactory(
        url='http://www.example.com/get',
        content_type='application/json; charset=UTF-8',
    )

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
def test_response_header_validation():
    from django.core.exceptions import ValidationError

    schema = SchemaFactory(
        paths={
            '/get': {
                'get': {
                    'responses': {200: {
                        'description': "Success",
                        'headers': {
                            'Foo': {'type': INTEGER},
                        }
                    }},
                },
            },
        },
    )

    response = ResponseFactory(
        url='http://www.example.com/get',
        headers={'Foo': 'abc'},
    )

    with pytest.raises(ValidationError) as err:
        validate_response(
            response,
            operation_definition=schema['paths']['/get']['get'],
            context=schema,
            inner=True,
        )

    assert 'body' in err.value.messages[0]
    assert 'headers' in err.value.messages[0]['body'][0]
    assert 'Foo' in err.value.messages[0]['body'][0]['headers'][0]
    assert 'type' in err.value.messages[0]['body'][0]['headers'][0]['Foo'][0]
    assert_error_message_equal(
        err.value.messages[0]['body'][0]['headers'][0]['Foo'][0]['type'][0],
        MESSAGES['type']['invalid'],
    )
Beispiel #45
0
def test_response_validation_with_invalid_path():
    """
    Test that request validation detects request paths that are not declared
    in the schema.
    """
    schema = SchemaFactory()
    assert not schema['paths']

    response = ResponseFactory(url='http://www.example.com/not-an-api-path')

    with pytest.raises(ValidationError) as err:
        validate_response(
            response=response,
            request_method='get',
            schema=schema,
        )

    assert_message_in_errors(
        MESSAGES['path']['no_matching_paths_found'],
        err.value.detail,
        'path',
    )
def test_response_validation_with_invalid_operation_on_path():
    schema = SchemaFactory(
        produces=['application/json'],
        paths={
            '/get': {
                'get': {
                    'responses': {'200': {'description': 'Success'}},
                }
            },
        },
    )

    response = ResponseFactory(
        url='http://www.example.com/get',
        content_type=None,
    )

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
Beispiel #47
0
def test_produces_validation_for_valid_mimetype_from_operation_definition():
    """
    Test that when `produces` is defined in an operation definition, that the
    local value is used in place of any global `produces` definition.
    """
    response = ResponseFactory(content_type='application/json')

    schema = SchemaFactory(
        produces=['application/xml'],
        paths={
            '/get': {'get': {
                'responses': {200: {'description': 'Success'}},
                'produces': ['application/json'],
            }},
        },
    )

    validate_response(
        response,
        operation_definition=schema['paths']['/get']['get'],
        context=schema,
    )
Beispiel #48
0
def test_response_validation_with_parametrized_path_and_invalid_value():
    """
    Test that request validation does type checking on path parameters.  Ensure
    that the value in the path is validated.
    """
    schema = SchemaFactory(
        paths={
            '/get/{id}': {
                'get': {
                    'responses': {
                        200: {
                            'description': 'Success'
                        }
                    }
                },
                'parameters': [{
                    'name': 'id',
                    'in': PATH,
                    'description': 'The Primary Key',
                    'type': INTEGER,
                    'required': True,
                }]
            },
        })

    response = ResponseFactory(url='http://www.example.com/get/abc')

    with pytest.raises(ValidationError) as err:
        validate_response(
            response=response,
            request_method='get',
            schema=schema,
        )

    assert_message_in_errors(
        MESSAGES['type']['invalid'],
        err.value.detail,
        'path.id.type',
    )
Beispiel #49
0
def test_response_header_validation():
    """
    Test basic validation of response headers.
    """
    schema = SchemaFactory(paths={
        '/get': {
            'get': {
                'responses': {
                    '200': {
                        'description': "Success",
                        'headers': {
                            'Foo': {
                                'type': INTEGER
                            },
                        }
                    }
                },
            },
        },
    }, )

    response = ResponseFactory(
        url='http://www.example.com/get',
        headers={'Foo': 'abc'},
    )

    with pytest.raises(ValidationError) as err:
        validate_response(
            response=response,
            request_method='get',
            schema=schema,
        )

    assert_message_in_errors(
        MESSAGES['type']['invalid'],
        err.value.detail,
        'body.headers.Foo.type',
    )
def test_response_content_type_validation_when_no_content_type_specified():
    schema = SchemaFactory(
        produces=['application/json'],
        paths={
            '/get': {
                'get': {
                    'responses': {'200': {'description': 'Success'}},
                }
            },
        },
    )

    response = ResponseFactory(
        url='http://www.example.com/get',
        content_type=None,
    )

    # this is considered valid currently, but may change
    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
Beispiel #51
0
def test_produces_validation_invalid_mimetype_from_global_definition():
    """
    Test that a response content_type that is in the global api produces
    definitions is valid.
    """
    from django.core.exceptions import ValidationError

    response = ResponseFactory(content_type='application/json')

    schema = SchemaFactory(
        produces=['application/xml'],
        paths={
            '/get': {'get': {'responses': {200: {'description': 'Success'}}}},
        },
    )

    with pytest.raises(ValidationError):
        validate_response(
            response,
            operation_definition=schema['paths']['/get']['get'],
            context=schema,
            inner=True,
        )
Beispiel #52
0
def test_response_validation_with_valid_path():
    """
    Test that response validation is able to match api paths.
    """
    schema = SchemaFactory(paths={
        '/get': {
            'get': {
                'responses': {
                    '200': {
                        'description': 'Success'
                    }
                }
            },
        },
    })

    response = ResponseFactory(url='http://www.example.com/get')

    validate_response(
        response=response,
        request_method='get',
        schema=schema,
    )
def test_response_validation_with_parametrized_path_and_invalid_value():
    """
    Test that request validation does type checking on path parameters.  Ensure
    that the value in the path is validated.
    """
    schema = SchemaFactory(
        paths={
            '/get/{id}': {
                'get': {'responses': {200: {'description': 'Success'}}},
                'parameters': [
                    {
                        'name': 'id',
                        'in': PATH,
                        'description': 'The Primary Key',
                        'type': INTEGER,
                        'required': True,
                    }
                ]
            },
        }
    )

    response = ResponseFactory(url='http://www.example.com/get/abc')

    with pytest.raises(ValidationError) as err:
        validate_response(
            response=response,
            request_method='get',
            schema=schema,
        )

    assert_message_in_errors(
        MESSAGES['type']['invalid'],
        err.value.detail,
        'path.id.type',
    )
Beispiel #54
0
def test_response_body_schema_validation_with_items_as_reference():
    """
    Ensure that when the expected response type is an object, and some other
    type is provided, that schema validation does not break since internally it
    will try to pull keys off of the value.
    """
    schema = SchemaFactory(
        definitions={
            'User': {
                'properties': {
                    'id': {
                        'required': True,
                        'type': INTEGER,
                    },
                    'name': {
                        'required': True,
                        'enum': ('bob', 'joe'),
                    },
                },
            },
            'UserList': {
                'type': OBJECT,
                'properties': {
                    'results': {
                        'type': ARRAY,
                        'items':{
                            '$ref': 'User',
                        },
                        'required': True,
                    },
                },
            },
        },
        paths={
            '/get': {
                'get': {
                    'responses': {
                        200: {
                            'description': 'Success',
                            'schema': {
                                '$ref': 'UserList',
                            },
                        }
                    },
                },
            },
        },
    )

    response = ResponseFactory(
        url='http://www.example.com/get',
        status_code=200,
        content_type='application/json',
        content=json.dumps({'results': [{'id': 3, 'name': 'billy'}]}),
    )

    with pytest.raises(ValidationError) as err:
        validate_response(
            response=response,
            request_method='get',
            schema=schema,
        )

    assert_message_in_errors(
        MESSAGES['enum']['invalid'],
        err.value.detail,
        'body.schema.enum',
    )