Example #1
0
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_parameters_definitions_type_validation_for_invalid_types(value):
    context = {'deferred_references': set()}
    with pytest.raises(ValidationError) as err:
        definitions_validator({'parameters': value}, context=context)

    assert_message_in_errors(
        MESSAGES['type']['invalid'],
        err.value.detail,
        'parameters',
    )
Example #3
0
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',
    )
Example #4
0
def test_deferred_references_are_validated():
    deferred_references = set()
    context = {"deferred_references": deferred_references}

    definitions = {
        "definitions": {"SomeReference": {"type": OBJECT, "properties": {"address": {"$ref": "#/definitions/Address"}}}}
    }
    with pytest.raises(ValidationError) as err:
        definitions_validator(definitions, context=context)

    assert_message_in_errors(MESSAGES["reference"]["undefined"], err.value.detail, "definitions.Address")
Example #5
0
def test_references_end_up_in_deferred_referrences():
    deferred_references = set()
    context = {"deferred_references": deferred_references}

    definitions = {
        "definitions": {
            "SomeReference": {"type": OBJECT, "properties": {"address": {"$ref": "#/definitions/Address"}}},
            "Address": {"type": STRING},
        }
    }
    definitions_validator(definitions, context=context)
    assert "#/definitions/Address" in deferred_references
def test_parameters_definitions_are_not_required():
    context = {'deferred_references': set()}
    try:
        definitions_validator({}, context=context)
    except ValidationError as err:
        errors = err.detail
    else:
        errors = {}

    assert_path_not_in_errors(
        'parameters',
        errors,
    )
Example #7
0
def test_responses_definitions_are_not_required():
    context = {'deferred_references': set()}
    try:
        definitions_validator({}, context=context)
    except ValidationError as err:
        errors = err.detail
    else:
        errors = {}

    assert_path_not_in_errors(
        'responses',
        errors,
    )
def test_single_parameter_type_validation(value):
    context = {'deferred_references': set()}

    with pytest.raises(ValidationError) as err:
        definitions_validator({
            'parameters': {
                'SomeParameter': value,
            },
        },
                              context=context)

    assert_message_in_errors(
        MESSAGES['type']['invalid'],
        err.value.detail,
        'parameters.SomeParameter',
    )
Example #9
0
def SchemaFactory(**kwargs):
    raw_schema = RawSchemaFactory(**kwargs)

    context = {'deferred_references': set()}
    definitions = definitions_validator(raw_schema, context=context)

    swagger_schema = swagger_schema_validator(raw_schema, context=definitions)

    return swagger_schema
Example #10
0
def test_responses_definitions_with_valid_response_definition():
    context = {'deferred_references': set()}
    response_definition = ResponseDefinitionFactory()
    try:
        definitions_validator({
            'responses': {
                'SomeResponse': response_definition,
            },
        }, context=context)
    except ValidationError as err:
        errors = err.detail
    else:
        errors = {}

    assert_path_not_in_errors(
        'responses',
        errors,
    )
Example #11
0
def SchemaFactory(**kwargs):
    raw_schema = RawSchemaFactory(**kwargs)

    context = {'deferred_references': set()}
    definitions = definitions_validator(raw_schema, context=context)

    swagger_schema = swagger_schema_validator(raw_schema, context=definitions)

    return swagger_schema
Example #12
0
def parse(raw_schema):
    context = {
        'deferred_references': set(),
    }
    swagger_definitions = definitions_validator(raw_schema, context=context)

    swagger_schema = swagger_schema_validator(
        raw_schema,
        context=swagger_definitions,
    )
    return swagger_schema
Example #13
0
def test_responses_definitions_with_valid_response_definition():
    context = {'deferred_references': set()}
    response_definition = ResponseDefinitionFactory()
    try:
        definitions_validator(
            {
                'responses': {
                    'SomeResponse': response_definition,
                },
            },
            context=context)
    except ValidationError as err:
        errors = err.detail
    else:
        errors = {}

    assert_path_not_in_errors(
        'responses',
        errors,
    )
Example #14
0
def test_basic_valid_parameter():
    context = {'deferred_references': set()}
    raw_parameter = ParameterFactory()
    try:
        definitions_validator(
            {
                'parameters': {
                    'SomeParameter': raw_parameter,
                },
            },
            context=context)
    except ValidationError as err:
        errors = err.detail
    else:
        errors = {}

    assert_path_not_in_errors(
        'parameters',
        errors,
    )
Example #15
0
def parse(raw_schema):
    context = {
        'deferred_references': set(),
    }
    swagger_definitions = definitions_validator(raw_schema, context=context)

    swagger_schema = swagger_schema_validator(
        raw_schema,
        context=swagger_definitions,
    )
    return swagger_schema
Example #16
0
def test_references_end_up_in_deferred_referrences():
    deferred_references = set()
    context = {'deferred_references': deferred_references}

    definitions = {
        'definitions': {
            'SomeReference': {
                'type': OBJECT,
                'properties': {
                    'address': {
                        '$ref': '#/definitions/Address',
                    }
                }
            },
            'Address': {
                'type': STRING,
            }
        }
    }
    definitions_validator(definitions, context=context)
    assert '#/definitions/Address' in deferred_references
Example #17
0
def test_references_end_up_in_external_deferred_references():
    deferred_references = set()
    context = {'deferred_references': deferred_references}

    definitions = {
        'definitions': {
            'SomeReference': {
                'type': OBJECT,
                'properties': {
                    'address': {
                        '$ref': 'jsonschemas/ext.json#',
                    }
                }
            },
            'Address': {
                'type': STRING,
            }
        }
    }
    definitions_validator(definitions, context=context, base_path=DIR)
    assert 'jsonschemas/ext.json#' in deferred_references
Example #18
0
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',
    )
Example #19
0
def test_deferred_references_are_validated():
    deferred_references = set()
    context = {'deferred_references': deferred_references}

    definitions = {
        'definitions': {
            'SomeReference': {
                'type': OBJECT,
                'properties': {
                    'address': {
                        '$ref': '#/definitions/Address',
                    }
                }
            },
        }
    }
    with pytest.raises(ValidationError) as err:
        definitions_validator(definitions, context=context)

    assert_message_in_errors(
        MESSAGES['reference']['undefined'],
        err.value.detail,
        'definitions.Address',
    )