Beispiel #1
0
def app():
    cnx_app = App(__name__,
                  port=5001,
                  specification_dir=SPEC_FOLDER,
                  debug=True)
    cnx_app.add_api('api.yaml', validate_responses=True)
    return cnx_app
Beispiel #2
0
def test_handle_add_operation_error_debug(simple_api_spec_dir):
    app = App(__name__, specification_dir=simple_api_spec_dir, debug=True)
    app.api_cls = type('AppTest', (app.api_cls,), {})
    app.api_cls.add_operation = mock.MagicMock(side_effect=Exception('operation error!'))
    api = app.add_api('swagger.yaml', resolver=lambda oid: (lambda foo: 'bar'))
    assert app.api_cls.add_operation.called
    assert api.resolver.resolve_function_from_operation_id('faux')('bah') == 'bar'
Beispiel #3
0
def test_app_with_resolver(simple_api_spec_dir, spec):
    from specific.resolver import Resolver
    resolver = Resolver()
    app = App(__name__, port=5001,
              specification_dir='..' / simple_api_spec_dir.relative_to(TEST_FOLDER),
              resolver=resolver)
    api = app.add_api(spec)
    assert api.resolver is resolver
Beispiel #4
0
def test_swagger_json_app(simple_api_spec_dir, spec):
    """ Verify the spec json file is returned for default setting passed to app. """
    app = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
    app.add_api(spec)
    app_client = app.app.test_client()
    url = '/v1.0/{spec}'
    url = url.format(spec=spec.replace("yaml", "json"))
    spec_json = app_client.get(url)  # type: flask.Response
    assert spec_json.status_code == 200
Beispiel #5
0
def test_no_swagger_json_api(simple_api_spec_dir, spec):
    """ Verify the spec json file is not returned when set to False when adding api. """
    app = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
    app.add_api(spec, options={"serve_spec": False})

    app_client = app.app.test_client()
    url = '/v1.0/{spec}'.format(spec=spec.replace("yaml", "json"))
    swagger_json = app_client.get(url)  # type: flask.Response
    assert swagger_json.status_code == 404
Beispiel #6
0
def test_app_with_relative_path(simple_api_spec_dir, spec):
    # Create the app with a relative path and run the test_app testcase below.
    app = App(__name__, port=5001,
              specification_dir='..' / simple_api_spec_dir.relative_to(TEST_FOLDER),
              debug=True)
    app.add_api(spec)

    app_client = app.app.test_client()
    get_bye = app_client.get('/v1.0/bye/jsantos')  # type: flask.Response
    assert get_bye.status_code == 200
    assert get_bye.data == b'Goodbye jsantos'
Beispiel #7
0
def test_using_all_fields_in_path_item(simple_api_spec_dir):
    """Test that specific will try to add an endpoint only on http methods.

    test also that each http methods has its own endpoint.
    """
    app = App(__name__, specification_dir=simple_api_spec_dir)
    app.add_api('openapi.yaml')

    test_methods = set()
    for rule in app.app.url_map.iter_rules():
        if rule.rule != "/v1.0/add_operation_on_http_methods_only":
            continue
        test_methods.update({method.lower() for method in rule.methods})
    assert set(test_methods) == METHODS
Beispiel #8
0
def test_app_with_different_array_parser(simple_api_spec_dir):
    from specific.decorators.array_parsing import FirstValueArrayParser
    app = App(__name__, port=5001,
              specification_dir='..' / simple_api_spec_dir.relative_to(TEST_FOLDER),
              options={"array_parser_class": FirstValueArrayParser},
              debug=True)
    app.add_api('swagger.yaml')

    app_client = app.app.test_client()
    resp = app_client.get(
        '/v1.0/test_array_csv_query_param?items=a,b,c&items=d,e,f'
    )  # type: flask.Response
    assert resp.status_code == 200
    j = json.loads(resp.get_data(as_text=True))
    assert j == ['a', 'b', 'c']
Beispiel #9
0
def build_app_from_fixture(api_spec_folder,
                           spec_file='openapi.yaml',
                           **kwargs):
    debug = True
    if 'debug' in kwargs:
        debug = kwargs['debug']
        del (kwargs['debug'])

    cnx_app = App(__name__,
                  port=5001,
                  specification_dir=FIXTURES_FOLDER / api_spec_folder,
                  debug=debug)

    cnx_app.add_api(spec_file, **kwargs)
    cnx_app._spec_file = spec_file
    return cnx_app
Beispiel #10
0
def test_validator_map(json_validation_spec_dir, spec):
    def validate_type(validator, types, instance, schema):
        types = _utils.ensure_list(types)
        errors = Draft4RequestValidator.VALIDATORS['type'](validator, types,
                                                           instance, schema)
        for e in errors:
            yield e

        if 'string' in types and 'minLength' not in schema:
            errors = Draft4RequestValidator.VALIDATORS['minLength'](validator,
                                                                    1,
                                                                    instance,
                                                                    schema)
            for e in errors:
                yield e

    MinLengthRequestValidator = extend(Draft4RequestValidator,
                                       {'type': validate_type})

    class MyRequestBodyValidator(RequestBodyValidator):
        def __init__(self, *args, **kwargs):
            super(MyRequestBodyValidator,
                  self).__init__(*args,
                                 validator=MinLengthRequestValidator,
                                 **kwargs)

    validator_map = {'body': MyRequestBodyValidator}

    app = App(__name__, specification_dir=json_validation_spec_dir)
    app.add_api(spec, validate_responses=True, validator_map=validator_map)
    app_client = app.app.test_client()

    res = app_client.post(
        '/v1.0/minlength',
        data=json.dumps({'foo': 'bar'}),
        content_type='application/json')  # type: flask.Response
    assert res.status_code == 200

    res = app_client.post(
        '/v1.0/minlength',
        data=json.dumps({'foo': ''}),
        content_type='application/json')  # type: flask.Response
    assert res.status_code == 400
Beispiel #11
0
def test_dict_as_yaml_path(simple_api_spec_dir, spec):
    openapi_yaml_path = simple_api_spec_dir / spec

    with openapi_yaml_path.open(mode='rb') as openapi_yaml:
        contents = openapi_yaml.read()
        try:
            openapi_template = contents.decode()
        except UnicodeDecodeError:
            openapi_template = contents.decode('utf-8', 'replace')

        openapi_string = jinja2.Template(openapi_template).render({})
        specification = yaml.load(openapi_string, ExtendedSafeLoader)  # type: dict

    app = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
    app.add_api(specification)

    app_client = app.app.test_client()
    url = '/v1.0/{spec}'.format(spec=spec.replace("yaml", "json"))
    swagger_json = app_client.get(url)  # type: flask.Response
    assert swagger_json.status_code == 200
Beispiel #12
0
def test_no_swagger_ui(simple_api_spec_dir, spec):
    options = {"swagger_ui": False}
    app = App(__name__, port=5001, specification_dir=simple_api_spec_dir,
              options=options, debug=True)
    app.add_api(spec)

    app_client = app.app.test_client()
    swagger_ui = app_client.get('/v1.0/ui/')  # type: flask.Response
    assert swagger_ui.status_code == 404

    app2 = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
    app2.add_api(spec, options={"swagger_ui": False})
    app2_client = app2.app.test_client()
    swagger_ui2 = app2_client.get('/v1.0/ui/')  # type: flask.Response
    assert swagger_ui2.status_code == 404
Beispiel #13
0
def test_handle_add_operation_error(simple_api_spec_dir):
    app = App(__name__, specification_dir=simple_api_spec_dir)
    app.api_cls = type('AppTest', (app.api_cls,), {})
    app.api_cls.add_operation = mock.MagicMock(side_effect=Exception('operation error!'))
    with pytest.raises(Exception):
        app.add_api('swagger.yaml', resolver=lambda oid: (lambda foo: 'bar'))
Beispiel #14
0
def test_add_api_with_function_resolver_function_is_wrapped(simple_api_spec_dir, spec):
    app = App(__name__, specification_dir=simple_api_spec_dir)
    api = app.add_api(spec, resolver=lambda oid: (lambda foo: 'bar'))
    assert api.resolver.resolve_function_from_operation_id('faux')('bah') == 'bar'