Пример #1
0
def test_type_casting_errors(rf, router):
    request = rf.get('/add-numbers?a=foo&b=8')
    request.api_info = APIInfo(
        router.get_path('/add-numbers').get_operation('get'))
    with pytest.raises(ErroneousParameters) as ei:
        read_parameters(request, capture_errors=True)
    assert 'a' in ei.value.errors
    assert 'b' in ei.value.parameters
Пример #2
0
def test_malformed_complex_parameter():
    request = make_request_for_operation(
        doc.get_path('/complex-parameter').get_operation('get'),
        query_string=urlencode({
            'coordinates': '{{{{{{{{{{{{{it\'s so cold and miserable',
        }),
    )
    with pytest.raises(ErroneousParameters) as ei:
        read_parameters(request, capture_errors=True)
    assert isinstance(ei.value.errors['coordinates'], InvalidComplexContent)
Пример #3
0
def test_complex_parameter_fails_validation():
    request = make_request_for_operation(
        doc.get_path('/complex-parameter').get_operation('get'),
        query_string=urlencode({
            'coordinates': json.dumps({'lat': 8, 'long': 'hoi there'}),
        }),
    )
    with pytest.raises(ErroneousParameters) as ei:
        read_parameters(request, capture_errors=True)
    assert isinstance(ei.value.errors['coordinates'], ValidationError)
Пример #4
0
def test_parameter_cascade(rf, router):
    request = rf.get('/cascade-parameters?a=7&b=10')
    request.api_info = APIInfo(
        router.get_path('/cascade-parameters').get_operation('get'))
    assert read_parameters(request) == {'a': 7, 'b': 10}
    request = rf.get('/cascade-parameter-override?a=yylmao')
    request.api_info = APIInfo(
        router.get_path('/cascade-parameter-override').get_operation('get'))
    assert read_parameters(request) == {
        'a': 'yylmao'
    }  # this would fail in the typecast if override didn't work
Пример #5
0
def test_header_parameter(rf, router):
    # Too bad there isn't a "requests"-like interface for testing that didn't
    # work by creating a `WSGIRequest` environment... Would be more truthful to test with something like that.
    request = rf.get('/header-parameter?blep=foo', HTTP_TOKEN='foo')
    request.api_info = APIInfo(
        router.get_path('/header-parameter').get_operation('get'))
    assert read_parameters(request)['token'] == 'foo'
Пример #6
0
def test_text_body_type(rf):
    apidoc = APIDefinition.from_data(yaml.safe_load(JSONIFY_DOC))
    operation = apidoc.get_path('/jsonify').get_operation('post')
    request = rf.post('/jsonify', 'henlo worl', content_type='text/plain')
    request.api_info = APIInfo(operation=operation)
    params = read_parameters(request, {})
    assert params['text'] == 'henlo worl'
Пример #7
0
def test_schema_refs(rf, doc_version):
    router = get_router(f'{doc_version}/schema-refs.yaml')
    request = rf.post('/cat',
                      json.dumps(lil_bub),
                      content_type='application/json')
    request.api_info = APIInfo(router.get_path('/cat').get_operation('post'))
    params = read_parameters(request, {})
    assert params['cat'] == lil_bub
Пример #8
0
def test_polymorphism(rf, doc_version, object):
    router = get_router(f'{doc_version}/schema-refs.yaml')
    request = rf.post('/pet',
                      json.dumps(object),
                      content_type='application/json')
    request.api_info = APIInfo(router.get_path('/pet').get_operation('post'))
    params = read_parameters(request, {})
    assert params['pet'] == object
Пример #9
0
def test_complex_parameter():
    coords_obj = {'lat': 8, 'long': 7}
    request = make_request_for_operation(
        doc.get_path('/complex-parameter').get_operation('get'),
        query_string=urlencode({
            'coordinates': json.dumps(coords_obj),
        }),
    )
    params = read_parameters(request)
    assert params == {'coordinates': coords_obj}
Пример #10
0
def test_files(rf, router):
    request = rf.post('/upload', {
        'file': ContentFile(b'foo', name='foo.txt'),
    })
    request.api_info = APIInfo(
        router.get_path('/upload').get_operation('post'))
    parameters = read_parameters(request)
    if OpenAPI3BodyParameter.name in parameters:  # Peel into the body parameter
        parameters = parameters[OpenAPI3BodyParameter.name]
    assert isinstance(parameters['file'], UploadedFile)
Пример #11
0
def test_label_parameter():
    request = make_request_for_operation(
        doc.get_path('/single/{thing}/data{format}').get_operation('get'))
    params = read_parameters(request, {
        'thing': 'blorp',
        'format': '.json',
    })
    assert params == {
        'thing': 'blorp',
        'format': 'json',  # Notice the missing dot
    }
Пример #12
0
def test_label_array_parameter():
    request = make_request_for_operation(
        doc.get_path('/array/{thing}/data{format}').get_operation('get'))
    params = read_parameters(request, {
        'thing': 'blorp',
        'format': '.json.yaml.xml.pdf',
    })
    assert params == {
        'thing': 'blorp',
        'format': ['json', 'yaml', 'xml', 'pdf'],  # An eldritch monstrosity
    }
Пример #13
0
def test_label_object_parameter():
    request = make_request_for_operation(
        doc.get_path('/object/{thing}/data{format}').get_operation('get'))
    params = read_parameters(request, {
        'thing': 'blorp',
        'format': '.some,body,once,told',
    })
    assert params == {
        'thing': 'blorp',
        'format': {
            'some': 'body',
            'once': 'told'
        },
    }
Пример #14
0
    def dispatch(self, request, **kwargs):
        try:
            operation = self.path.get_operation(request.method)
        except InvalidOperation:
            return self.http_method_not_allowed(request, **kwargs)
        request.api_info = APIInfo(
            operation=operation,
            router=self.router,
        )
        params = dict(
            (snake_case(name), value)
            for (name, value)
            in read_parameters(request, kwargs, capture_errors=True).items()
        )
        handler = request.api_info.router.get_handler(operation.id)
        try:
            response = handler(request, **params)
        except ExceptionalResponse as er:
            response = er.response

        return self.transform_response(response)
Пример #15
0
def test_parameter_ref(rf, router):
    request = rf.get('/parameter-reference?age=86')
    request.api_info = APIInfo(
        router.get_path('/parameter-reference').get_operation('get'))
    assert read_parameters(request) == {'age': 86}
Пример #16
0
def test_parameters_ref(rf, router):
    # /parameters-reference refers the entire parameters object from parameter-reference, so the test is equivalent
    request = rf.get('/parameters-reference?age=86')
    request.api_info = APIInfo(
        router.get_path('/parameters-reference').get_operation('get'))
    assert read_parameters(request) == {'age': 86}
Пример #17
0
def test_invalid_collection_format(rf, router):
    request = rf.get('/invalid-collection-format?blep=foo')
    request.api_info = APIInfo(
        router.get_path('/invalid-collection-format').get_operation('get'))
    with pytest.raises((NotImplementedError, ImproperlyConfigured)):
        read_parameters(request)
Пример #18
0
def test_required(rf, router):
    request = rf.get('/greet')
    request.api_info = APIInfo(router.get_path('/greet').get_operation('get'))
    with pytest.raises(ErroneousParameters) as ei:
        read_parameters(request)
    assert isinstance(ei.value.errors['greetee'], MissingParameter)
Пример #19
0
def test_default(rf, router):
    request = rf.get('/greet?greetee=doggo')
    request.api_info = APIInfo(router.get_path('/greet').get_operation('get'))
    parameters = read_parameters(request)
    assert parameters == {'greeting': 'henlo', 'greetee': 'doggo'}
Пример #20
0
def test_multi(rf, router):
    request = rf.get('/multiple-tags?tag=a&tag=b&tag=c')
    request.api_info = APIInfo(
        router.get_path('/multiple-tags').get_operation('get'))
    parameters = read_parameters(request)
    assert parameters['tag'] == ['a', 'b', 'c']