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,
    )
Ejemplo n.º 2
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_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'],
    )
Ejemplo n.º 4
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',
    )
Ejemplo n.º 5
0
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,
    )
Ejemplo n.º 7
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,
    )
Ejemplo n.º 8
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,
    )
Ejemplo n.º 9
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,
    )
Ejemplo n.º 10
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',
    )
Ejemplo n.º 11
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_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,
    )
Ejemplo n.º 13
0
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',
    )
Ejemplo n.º 14
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,
    )
Ejemplo n.º 15
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,
    )
Ejemplo n.º 16
0
def test_consumes_validation_for_valid_mimetype_from_operation_definition():
    """
    Test that when `consumes` is defined in an operation definition, that the
    local value is used in place of any global `consumes` definition.
    """
    request = RequestFactory(content_type='application/json')
    response = ResponseFactory(request=request)

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

    validators = construct_operation_validators(
        api_path='/get',
        path_definition=schema['paths']['/get'],
        operation_definition=schema['paths']['/get']['get'],
        context=schema,
    )
    validate_operation(response.request, validators)
Ejemplo n.º 17
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.
    """
    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,
        )
Ejemplo n.º 18
0
def test_consumes_validation_invalid_mimetype_from_global_definition():
    """
    Test that a request content_type that is in the global api consumes
    definitions is valid.
    """
    request = RequestFactory(content_type='application/json')
    response = ResponseFactory(request=request)

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

    validators = construct_operation_validators(
        api_path='/get',
        path_definition=schema['paths']['/get'],
        operation_definition=schema['paths']['/get']['get'],
        context=schema,
    )
    with pytest.raises(ValidationError):
        validate_operation(response.request, validators)
Ejemplo n.º 19
0
def test_consumes_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.
    """
    request = RequestFactory(content_type='application/xml')
    response = ResponseFactory(request=request)

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

    validators = construct_operation_validators(
        api_path='/get',
        path_definition=schema['paths']['/get'],
        operation_definition=schema['paths']['/get']['get'],
        context=schema,
    )
    with pytest.raises(ValidationError):
        validate_operation(response.request, validators)
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,
    )
Ejemplo n.º 21
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',
    )
Ejemplo n.º 22
0
def test_py2_invalid_json():
    body = '{"trailing comma not valid": [1,]}'
    response = ResponseFactory(
        content=body,
        content_type='application/json',
    )

    try:
        json.loads(body)
    except ValueError as e:
        expected_message = str(e)

    with pytest.raises(JSONDecodeError) as e:
        response.data
    assert str(e.value) == expected_message
Ejemplo n.º 23
0
def test_py3_invalid_json():
    body = '{"trailing comma not valid": [1,]}'
    response = ResponseFactory(
        content=body,
        content_type='application/json',
    )

    try:
        json.loads(body)
    except JSONDecodeError as e:
        expected_exception = e

    with pytest.raises(JSONDecodeError) as e:
        response.data
    assert e.value.msg == expected_exception.msg
Ejemplo n.º 24
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',
    )
Ejemplo n.º 25
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',
    )
Ejemplo n.º 26
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',
    )
Ejemplo n.º 27
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',
    )
Ejemplo n.º 28
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',
    )
Ejemplo n.º 29
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,
    )
Ejemplo n.º 30
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',
    )