Ejemplo n.º 1
0
def test_200_for_good_validated_array_response():
    request = EnhancedDummyRequest(
        method='GET',
        path='/sample_array_response',
    )
    response = Response(
        body=simplejson.dumps([{
            "enum_value": "good_enum_value"
        }]),
        headers={'Content-Type': 'application/json; charset=UTF-8'},
    )

    _validate_against_tween(request,
                            response=response,
                            path_pattern='/sample_array_response')
Ejemplo n.º 2
0
def test_200_when_response_is_void_with_empty_response():
    request = EnhancedDummyRequest(
        method='GET',
        path='/sample/nonstring/{int_arg}/{float_arg}/{boolean_arg}',
        params={'required_arg': 'test'},
        matchdict={
            'int_arg': '1',
            'float_arg': '2.0',
            'boolean_arg': 'true'
        },
    )
    response = Response(body='{}')
    _validate_against_tween(
        request,
        response=response,
        path_pattern='/sample/nonstring/{int_arg}/{float_arg}/{boolean_arg}')
Ejemplo n.º 3
0
def test_response_validation_enabled_by_default():
    request = EnhancedDummyRequest(
        method='GET',
        path='/sample/path_arg1/resource',
        params={'required_arg': 'test'},
        matchdict={'path_arg': 'path_arg1'},
    )
    # Omit the logging_info key from the response. If response validation
    # occurs, we'll fail it.
    response = Response(
        body=simplejson.dumps({'raw_response': 'foo'}),
        headers={'Content-Type': 'application/json; charset=UTF-8'},
    )
    with pytest.raises(ResponseValidationError):
        _validate_against_tween(request,
                                response=response,
                                path_pattern='/sample/{path_arg}/resource')
Ejemplo n.º 4
0
def test_500_for_bad_validated_array_response():
    request = EnhancedDummyRequest(
        method='GET',
        path='/sample_array_response',
    )
    response = Response(
        body=simplejson.dumps([{
            "enum_value": "bad_enum_value"
        }]),
        headers={'Content-Type': 'application/json; charset=UTF-8'},
    )
    with pytest.raises(ResponseValidationError) as excinfo:
        _validate_against_tween(request,
                                response=response,
                                path_pattern='/sample_array_response')

    assert "'bad_enum_value' is not one of " in \
           str(excinfo.value)
Ejemplo n.º 5
0
def test_response_validation_context():
    request = EnhancedDummyRequest(
        method='GET',
        path='/sample/path_arg1/resource',
        params={'required_arg': 'test'},
        matchdict={'path_arg': 'path_arg1'},
    )
    # Omit the logging_info key from the response.
    response = Response(
        body=simplejson.dumps({'raw_response': 'foo'}),
        headers={'Content-Type': 'application/json; charset=UTF-8'},
    )

    with pytest.raises(CustomResponseValidationException):
        _validate_against_tween(
            request,
            response=response,
            path_pattern='/sample/{path_arg}/resource',
            **{'pyramid_swagger.validation_context_path': validation_ctx_path})
Ejemplo n.º 6
0
def test_200_when_response_is_void_with_none_response():
    request = EnhancedDummyRequest(
        method='GET',
        path='/sample/nonstring/{int_arg}/{float_arg}/{boolean_arg}',
        params={'required_arg': 'test'},
        matchdict={
            'int_arg': '1',
            'float_arg': '2.0',
            'boolean_arg': 'true'
        },
    )
    response = Response(
        body=simplejson.dumps(None),
        headers={'Content-Type': 'application/json; charset=UTF-8'},
    )
    _validate_against_tween(
        request,
        response=response,
        path_pattern='/sample/nonstring/{int_arg}/{float_arg}/{boolean_arg}')
Ejemplo n.º 7
0
def test_500_when_response_is_missing_required_field():

    request = EnhancedDummyRequest(
        method='GET',
        path='/sample/path_arg1/resource',
        params={'required_arg': 'test'},
        matchdict={'path_arg': 'path_arg1'},
    )

    # Omit the logging_info key from the response to induce failure
    response = Response(
        body=simplejson.dumps({'raw_response': 'foo'}),
        headers={'Content-Type': 'application/json; charset=UTF-8'},
    )

    with pytest.raises(ResponseValidationError) as excinfo:
        _validate_against_tween(request,
                                response=response,
                                path_pattern='/sample/{path_arg}/resource')

    assert "'logging_info' is a required property" in str(excinfo.value)
Ejemplo n.º 8
0
def test_500_when_response_arg_is_wrong_type():
    request = EnhancedDummyRequest(
        method='GET',
        path='/sample/path_arg1/resource',
        params={'required_arg': 'test'},
        matchdict={'path_arg': 'path_arg1'},
    )
    response = Response(
        body=simplejson.dumps({
            'raw_response': 1.0,  # <-- is supposed to be a string
            'logging_info': {
                'foo': 'bar'
            }
        }),
        headers={'Content-Type': 'application/json; charset=UTF-8'},
    )
    with pytest.raises(ResponseValidationError) as excinfo:
        _validate_against_tween(request,
                                response=response,
                                path_pattern='/sample/{path_arg}/resource')

    assert "1.0 is not of type " in str(excinfo.value)