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_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_request_validation_with_parametrized_path_with_invalid_value(): """ Test that request validation finds and validates parametrized paths. Ensure that it does validation on the values. """ schema = SchemaFactory( paths={ '/get/{id}': { 'get': {'responses': {'200': {'description': 'Success'}}}, 'parameters': [ { 'name': 'id', 'in': PATH, 'description': 'The Primary Key', 'type': INTEGER, 'required': True, } ] }, } ) request = RequestFactory(url='http://www.example.com/get/abcd') with pytest.raises(ValidationError) as err: validate_request( request=request, schema=schema, ) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'path.id.type', )
def test_nullable_enum_with_null_values_strict(enum, value, monkeypatch): parameters = parameters_validator([ { 'name': 'id', 'in': PATH, 'description': 'id', 'type': [STRING, NUMBER, BOOLEAN], 'required': True, 'enum': enum, 'x-nullable': True }, ]) parameter_values = { 'id': value, } monkeypatch.setattr(os, 'environ', {FLEX_DISABLE_X_NULLABLE: '1'}) with pytest.raises(ValidationError) as err: validate_parameters(parameter_values, parameters, {}) assert_message_in_errors( MESSAGES['enum']['invalid'], err.value.detail, 'id.enum', )
def test_max_items_on_values_with_too_many_items(max_items, value): parameters = parameters_validator([ { 'name': 'id', 'in': PATH, 'description': 'id', 'type': ARRAY, 'required': True, 'maxItems': max_items, 'items': { 'type': STRING }, }, ]) parameter_values = { 'id': value, } with pytest.raises(ValidationError) as err: validate_parameters(parameter_values, parameters, {}) assert_message_in_errors( MESSAGES['max_items']['invalid'], err.value.detail, 'id.maxItems', )
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', )
def test_nullable_enum_with_invalid_values(enum, value): parameters = parameters_validator([ { 'name': 'id', 'in': PATH, 'description': 'id', 'type': [STRING, NUMBER, BOOLEAN], 'required': True, 'enum': enum, 'x-nullable': True }, ]) parameter_values = { 'id': value, } with pytest.raises(ValidationError) as err: validate_parameters(parameter_values, parameters, {}) assert_message_in_errors( MESSAGES['enum']['invalid'], err.value.detail, 'id.enum', )
def test_request_body_parameter_validation_with_invalid_value( user_post_schema): """ Test validating the request body with a valid post. """ request = RequestFactory( url='http://www.example.com/post/', content_type='application/json', body=json.dumps({ 'username': '******', 'email': 'test' }), method=POST, ) with pytest.raises(ValidationError) as err: validate_request( request=request, schema=user_post_schema, ) assert_message_in_errors( MESSAGES['format']['invalid_email'], err.value.detail, )
def test_parameter_schema_validation_for_invalid_value(value, error_key, message_key): parameters = parameters_validator([ { 'name': 'id', 'in': BODY, 'description': 'id', 'required': True, 'schema': { 'type': STRING, 'format': 'uuid' }, }, ]) parameter_values = { 'id': value, } with pytest.raises(ValidationError) as err: validate_parameters(parameter_values, parameters, context={}) assert_message_in_errors( MESSAGES[error_key][message_key], err.value.messages, 'id.{0}'.format(error_key), )
def test_request_header_validation(): schema = SchemaFactory(paths={ '/get/': { 'get': { 'responses': { 200: { 'description': "Success" } }, 'parameters': [{ 'name': 'Authorization', 'in': HEADER, 'type': INTEGER, }] }, }, }, ) request = RequestFactory( url='http://www.example.com/get/', headers={'Authorization': 'abc'}, ) with pytest.raises(ValidationError) as err: validate_request( request=request, schema=schema, ) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'method.parameters.headers.Authorization.type', )
def test_responses_are_validated(): """ Sanity check that the individual response objects are validated. """ context = {'deferred_references': set()} with pytest.raises(ValidationError) as err: definitions_validator( { 'responses': { 'WrongType': [], 'AlsoWrongType': None, }, }, context=context) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'responses.WrongType', ) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'responses.AlsoWrongType', )
def test_request_header_validation(): schema = SchemaFactory( paths={ '/get/': { 'get': { 'responses': {200: {'description': "Success"}}, 'parameters': [ { 'name': 'Authorization', 'in': HEADER, 'type': INTEGER, } ] }, }, }, ) request = RequestFactory( url='http://www.example.com/get/', headers={'Authorization': 'abc'}, ) with pytest.raises(ValidationError) as err: validate_request( request=request, schema=schema, ) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'method.parameters.headers.Authorization.type', )
def test_request_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/{id}/": { "parameters": [ {"name": "id", "in": PATH, "description": "id", "required": True, "type": STRING, "format": "uuid"}, {"name": "page", "in": QUERY, "type": INTEGER}, ], "get": {"responses": {200: {"description": "Success"}}}, } } ) request = RequestFactory(url="http://www.example.com/get/32/?page=abcd") with pytest.raises(ValidationError) as err: validate_request(request=request, schema=schema) assert_message_in_errors(MESSAGES["format"]["invalid_uuid"], err.value.detail, "method.parameters.path.id.format") assert_message_in_errors(MESSAGES["type"]["invalid"], err.value.detail, "query.page.type")
def test_parameter_items_validation_on_invalid_array(): parameters = parameters_validator([ { 'name': 'id', 'in': QUERY, 'description': 'id', 'items': { 'type': INTEGER, 'minimum': 0, }, 'type': ARRAY, }, ]) value = [1, 2, '3', -1, 4] parameter_values = { 'id': value, } with pytest.raises(ValidationError) as err: validate_parameters(parameter_values, parameters, context={}) assert_message_in_errors( MESSAGES['minimum']['invalid'], err.value.detail, 'id.items.type', ) assert_message_in_errors( MESSAGES['minimum']['invalid'], err.value.detail, 'id.items.minimum', )
def test_request_validation_with_invalid_operation_on_path(): """ Test that request validation detects request paths that are not declared in the schema. """ schema = SchemaFactory( paths={ '/post': { 'post': {}, }, }, ) request = RequestFactory(url='http://www.example.com/post') with pytest.raises(ValidationError) as err: validate_request( request=request, schema=schema, ) assert_message_in_errors( MESSAGES['request']['invalid_method'], err.value.detail, 'method', )
def test_unique_items_validation_with_duplicates(value): parameters = parameters_validator([ { 'name': 'id', 'in': PATH, 'description': 'id', 'type': ARRAY, 'required': True, 'uniqueItems': True, 'items': { 'type': [STRING, NUMBER, BOOLEAN] }, }, ]) parameter_values = { 'id': value, } with pytest.raises(ValidationError) as err: validate_parameters(parameter_values, parameters, {}) assert_message_in_errors( MESSAGES['unique_items']['invalid'], err.value.detail, 'id.uniqueItems', )
def test_invalid_api_call_with_polymorphism(): request_payload = """{ "events": [ { "eventType": "Impression", "timestamp": 12312312 } ] }""" responses.add(responses.POST, "http://test.com/poly/report", body="{}", status=200, content_type="application/json") response = requests.post("http://test.com/poly/report", json=json.loads(request_payload)) schema = load(os.path.join(BASE_DIR, 'schemas', 'polymorphism.yaml')) with pytest.raises(ValidationError) as err: validate_api_call(schema, raw_request=response.request, raw_response=response) assert_message_in_errors( MESSAGES['required']['required'], err.value.detail, )
def test_mimetype_with_invalid_values(value): with pytest.raises(ValidationError) as err: mimetype_validator(value) assert_message_in_errors( MESSAGES['mimetype']['invalid'], err.value.detail, )
def test_mimetype_with_invalid_value_in_multiple_values(): with pytest.raises(ValidationError) as err: mimetype_validator(['application/json', 'not-a-valid-mimetype']) assert_message_in_errors( MESSAGES['mimetype']['invalid'], err.value.detail, )
def test_multi_format_invalid_in_values(in_): parameter = ParameterFactory(**{"collectionFormat": MULTI, "in": in_}) with pytest.raises(ValidationError) as err: single_parameter_validator(parameter) assert_message_in_errors( MESSAGES["collection_format"]["invalid_based_on_in_value"], err.value.detail, "collectionFormat" )
def test_enum_with_empty_array_is_invalid(): with pytest.raises(ValidationError) as err: schema_validator({'enum': []}) assert_message_in_errors( MESSAGES['min_items']['invalid'], err.value.detail, 'enum.minItems', )
def test_in_must_be_one_of_valid_values(): with pytest.raises(ValidationError) as err: single_parameter_validator({'in': 'not-a-valid-in-value'}) assert_message_in_errors( MESSAGES['enum']['invalid'], err.value.detail, 'in.enum', )
def test_required_with_invalid_types(value): with pytest.raises(ValidationError) as err: schema_validator({'required': value}) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'required.type', )
def test_pattern_with_invalid_regex(): with pytest.raises(ValidationError) as err: schema_validator({'pattern': '(arrst'}) assert_message_in_errors( MESSAGES['pattern']['invalid_regex'], err.value.detail, 'pattern', )
def test_type_with_invalid_single_type(): with pytest.raises(ValidationError) as err: schema_validator({'type': 'not-a-valid-type'}) assert_message_in_errors( MESSAGES['enum']['invalid'], err.value.detail, 'type.enum', )
def test_min_length_for_invalid_types(value): with pytest.raises(ValidationError) as err: schema_validator({'minLength': value}) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'minLength.type', )
def test_mimetype_invalid_for_non_array_value(value): with pytest.raises(ValidationError) as err: mimetype_validator(value) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'type', )
def test_description_with_invalid_types(value): with pytest.raises(ValidationError) as err: single_parameter_validator({'description': value}) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'description.type', )
def test_properties_with_invalid_types(value): with pytest.raises(ValidationError) as err: schema_validator({'properties': value}) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'properties.type', )
def test_type_with_invalid_single_type(): with pytest.raises(ValidationError) as err: single_parameter_validator({'type': 'not-a-valid-type'}) assert_message_in_errors( MESSAGES['enum']['invalid'], err.value.detail, 'type.enum', )
def test_collection_format_with_invalid_value(): with pytest.raises(ValidationError) as err: single_parameter_validator({'collectionFormat': 'not-a-collection-format'}) assert_message_in_errors( MESSAGES['enum']['invalid'], err.value.detail, 'collectionFormat.enum', )
def test_type_with_invalid_types(value): with pytest.raises(ValidationError) as err: single_parameter_validator({'type': value}) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'type.type', )
def test_multiple_of_for_invalid_types(value): with pytest.raises(ValidationError) as err: schema_validator({'multipleOf': value}) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'multipleOf.type', )
def test_schema_for_invalid_types(value): with pytest.raises(ValidationError) as err: schema_validator(value) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'type', )
def test_multiple_of_invalid_for_negative_numbers(value): with pytest.raises(ValidationError) as err: schema_validator({'multipleOf': value}) assert_message_in_errors( MESSAGES['minimum']['invalid'], err.value.detail, 'multipleOf.minimum', )
def test_collection_format_with_invalid_types(value): with pytest.raises(ValidationError) as err: single_parameter_validator({'collectionFormat': value}) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'collectionFormat.type', )
def test_required_with_invalid_sub_types(): with pytest.raises(ValidationError) as err: schema_validator({'required': ['field-A', True, 'Field-B']}) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'required.type', )
def test_title_is_required(): data = {} with pytest.raises(ValidationError) as err: info_validator(data) assert_message_in_errors( MESSAGES['required']['required'], err.value.detail, 'required.title', )
def test_name_is_required(): context = {'deferred_references': set()} with pytest.raises(ValidationError) as err: single_parameter_validator({}, context=context) assert_message_in_errors( MESSAGES['required']['required'], err.value.detail, 'name', )
def test_that_when_type_is_array_items_is_required(): parameter = ParameterFactory(type=ARRAY) with pytest.raises(ValidationError) as err: single_parameter_validator(parameter) assert_message_in_errors( MESSAGES['items']['items_required_for_type_array'], err.value.detail, 'items', )
def test_responses_definitions_type_validation_for_invalid_types(value): context = {'deferred_references': set()} with pytest.raises(ValidationError) as err: definitions_validator({'responses': value}, context=context) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'responses', )
def test_ref_is_required(): ref_schema = {} with pytest.raises(ValidationError) as err: reference_object_validator(ref_schema) assert_message_in_errors( MESSAGES['required']['required'], err.value.detail, 'required.$ref', )
def test_exclusive_maximum_for_invalid_types(value): with pytest.raises(ValidationError) as err: schema_validator({ 'exclusiveMaximum': value, }) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'exclusiveMaximum.type', )
def test_ref_with_invalid_types(value): schema = {'$ref': value} with pytest.raises(ValidationError) as err: reference_object_validator(schema) assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, '$ref.type', )
def test_type_with_invalid_multiple_types(): with pytest.raises(ValidationError) as err: single_parameter_validator({ 'type': [STRING, 'not-a-valid-type', INTEGER, BOOLEAN], }) assert_message_in_errors( MESSAGES['enum']['invalid'], err.value.detail, 'type.enum', )