def aiohttp_app(aiohttp_api_spec_dir):
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    options = {"validate_responses": True}
    app.add_api('swagger_simple.yaml', validate_responses=True, pass_context_arg_name='request_ctx', options=options)
    return app
def test_basic_auth_secure(oauth_requests, aiohttp_api_spec_dir,
                           aiohttp_client):
    # Create the app and run the test_app testcase below.
    app = AioHttpApp(__name__,
                     port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.add_api('openapi_secure.yaml')
    app_client = yield from aiohttp_client(app.app)

    post_hello = yield from app_client.post('/v1.0/greeting/jsantos')
    assert post_hello.status == 401

    username = '******'
    password = username  # check fake_basic_auth
    basic_header = 'Basic ' + base64.b64encode(
        (username + ':' + password).encode('ascii')).decode('ascii')
    headers = {'Authorization': basic_header}
    post_hello = yield from app_client.post('/v1.0/greeting/jsantos',
                                            headers=headers)

    assert (yield from post_hello.read()) == b"{'greeting': 'Hello jsantos'}"

    broken_header = 'Basic ' + base64.b64encode(
        (username + ':' + password[:-1]).encode('ascii')).decode('ascii')
    headers = {'Authorization': broken_header}
    no_auth = yield from app_client.post('/v1.0/greeting/jsantos',
                                         headers=headers)
    assert no_auth.status == 401, "Wrong header should result into Unauthorized"
    assert no_auth.content_type == 'application/problem+json'
Beispiel #3
0
 def aiohttp_app_async_def(aiohttp_api_spec_dir):
     app = AioHttpApp(__name__,
                      port=5001,
                      specification_dir=aiohttp_api_spec_dir,
                      debug=True)
     app.add_api('swagger_simple_async_def.yaml', validate_responses=True)
     return app
def test_basic_auth_secure(oauth_requests, aiohttp_api_spec_dir, aiohttp_client):
    # Create the app and run the test_app testcase below.
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.add_api('openapi_secure.yaml')
    app_client = yield from aiohttp_client(app.app)

    post_hello = yield from app_client.post('/v1.0/greeting/jsantos')
    assert post_hello.status == 401

    username= '******'
    password = username  # check fake_basic_auth
    basic_header = 'Basic ' + base64.b64encode((username + ':' + password).encode('ascii')).decode('ascii')
    headers = {'Authorization': basic_header}
    post_hello = yield from app_client.post(
        '/v1.0/greeting/jsantos',
        headers=headers
    )

    assert (yield from post_hello.read()) == b"{'greeting': 'Hello jsantos'}"

    broken_header = 'Basic ' + base64.b64encode((username + ':' + password[:-1]).encode('ascii')).decode('ascii')
    headers = {'Authorization': broken_header}
    no_auth = yield from app_client.post(
        '/v1.0/greeting/jsantos',
        headers=headers
    )
    assert no_auth.status == 401, "Wrong header should result into Unauthorized"
    assert no_auth.content_type == 'application/problem+json'
Beispiel #5
0
def aiohttp_app(problem_api_spec_dir):
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=problem_api_spec_dir,
                     debug=True)
    options = {"validate_responses": True}
    app.add_api('openapi.yaml', validate_responses=True, pass_context_arg_name='request_ctx', options=options)
    return app
Beispiel #6
0
    async def test_swagger_json_behind_proxy(simple_api_spec_dir, aiohttp_client):
        """ Verify the swagger.json file is returned with base_path updated
            according to X-Forwarded-Path header. """
        app = AioHttpApp(__name__, port=5001,
                         specification_dir=simple_api_spec_dir,
                         debug=True)
        api = app.add_api('swagger.yaml')

        aio = app.app
        reverse_proxied = XPathForwarded()
        aio.middlewares.append(reverse_proxied.middleware)

        app_client = await aiohttp_client(app.app)
        headers = {'X-Forwarded-Path': '/behind/proxy'}

        swagger_ui = await app_client.get('/v1.0/ui/', headers=headers)
        assert swagger_ui.status == 200
        assert b'url = "/behind/proxy/v1.0/swagger.json"' in (
            await swagger_ui.read()
        )

        swagger_json = await app_client.get('/v1.0/swagger.json', headers=headers)
        assert swagger_json.status == 200
        assert swagger_json.headers.get('Content-Type') == 'application/json'
        json_ = await swagger_json.json()

        assert api.specification.raw['basePath'] == '/v1.0', \
            "Original specifications should not have been changed"

        assert json_.get('basePath') == '/behind/proxy/v1.0', \
            "basePath should contains original URI"

        json_['basePath'] = api.specification.raw['basePath']
        assert api.specification.raw == json_, \
            "Only basePath should have been updated"
Beispiel #7
0
def test_app_run_default_port(web_run_app_mock, aiohttp_api_spec_dir):
    app = AioHttpApp(__name__,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.run()
    assert web_run_app_mock.call_args_list == [
        mock.call(app.app, port=5000, host='0.0.0.0', access_log=None)
    ]
Beispiel #8
0
def test_app_with_empty_base_path_and_only_one_api(aiohttp_api_spec_dir):
    spec_dir = '..' / aiohttp_api_spec_dir.relative_to(TEST_FOLDER)
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=spec_dir,
                     debug=True,
                     only_one_api=True)
    api = app.add_api('swagger_empty_base_path.yaml')
    assert api is app.app
Beispiel #9
0
def test_app_run_debug(web_run_app_mock, aiohttp_api_spec_dir):
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir)
    app.add_api('swagger_simple.yaml')
    app.run(debug=True)
    assert web_run_app_mock.call_args_list == [
        mock.call(app.app, port=5001, host='0.0.0.0', access_log=None)
    ]
Beispiel #10
0
def test_app_run_server_error(web_run_app_mock, aiohttp_api_spec_dir):
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir)

    with pytest.raises(Exception) as exc_info:
        app.run(server='other')

    assert exc_info.value.args == ('Server other not recognized',)
Beispiel #11
0
def test_app_run_default_port(web_run_app_mock, aiohttp_api_spec_dir):
    app = AioHttpApp(__name__,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.run()
    assert web_run_app_mock.call_args_list == [
        mock.call(app.app, port=5000, host='0.0.0.0', access_log=None)
    ]
Beispiel #12
0
async def test_secure_app(oauth_aiohttp_client, aiohttp_api_spec_dir,
                          aiohttp_client, spec):
    """
    Test common authentication method between Swagger 2 and OpenApi 3
    """
    app = AioHttpApp(__name__,
                     port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.add_api(spec)
    app_client = await aiohttp_client(app.app)

    response = await app_client.get('/v1.0/all_auth')
    assert response.status == 401
    assert response.content_type == 'application/problem+json'

    response = await app_client.get('/v1.0/all_auth',
                                    headers={'Authorization': 'Bearer 100'})
    assert response.status == 200
    assert (await response.json()) == {
        "scope": ['myscope'],
        "uid": 'test-user'
    }

    response = await app_client.get('/v1.0/all_auth',
                                    headers={'authorization': 'Bearer 100'})
    assert response.status == 200, "Authorization header in lower case should be accepted"
    assert (await response.json()) == {
        "scope": ['myscope'],
        "uid": 'test-user'
    }

    response = await app_client.get('/v1.0/all_auth',
                                    headers={'AUTHORIZATION': 'Bearer 100'})
    assert response.status == 200, "Authorization header in upper case should be accepted"
    assert (await response.json()) == {
        "scope": ['myscope'],
        "uid": 'test-user'
    }

    basic_header = 'Basic ' + base64.b64encode(b'username:username').decode(
        'ascii')
    response = await app_client.get('/v1.0/all_auth',
                                    headers={'Authorization': basic_header})
    assert response.status == 200
    assert (await response.json()) == {"uid": 'username'}

    basic_header = 'Basic ' + base64.b64encode(b'username:wrong').decode(
        'ascii')
    response = await app_client.get('/v1.0/all_auth',
                                    headers={'Authorization': basic_header})
    assert response.status == 401, "Wrong password should trigger unauthorized"
    assert response.content_type == 'application/problem+json'

    response = await app_client.get('/v1.0/all_auth',
                                    headers={'X-API-Key': '{"foo": "bar"}'})
    assert response.status == 200
    assert (await response.json()) == {"foo": "bar"}
Beispiel #13
0
def test_app_run_server_error(web_run_app_mock, aiohttp_api_spec_dir):
    app = AioHttpApp(__name__,
                     port=5001,
                     specification_dir=aiohttp_api_spec_dir)

    with pytest.raises(Exception) as exc_info:
        app.run(server='other')

    assert exc_info.value.args == ('Server other not recognized', )
def aiohttp_app(aiohttp_api_spec_dir):
    app = AioHttpApp(__name__,
                     port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.add_api('swagger_simple.yaml',
                validate_responses=True,
                pass_context_arg_name='request_ctx')
    return app
Beispiel #15
0
def test_app_run(web_run_app_mock, aiohttp_api_spec_dir):
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.run(use_default_access_log=True)
    logger = logging.getLogger('connexion.aiohttp_app')
    assert web_run_app_mock.call_args_list == [
        mock.call(app.app, port=5001, host='0.0.0.0', access_log=logger)
    ]
Beispiel #16
0
def test_app_with_empty_base_path_and_only_one_api(aiohttp_api_spec_dir):
    spec_dir = '..' / aiohttp_api_spec_dir.relative_to(TEST_FOLDER)
    app = AioHttpApp(__name__,
                     port=5001,
                     specification_dir=spec_dir,
                     debug=True,
                     only_one_api=True)
    api = app.add_api('swagger_empty_base_path.yaml')
    assert api is app.app
Beispiel #17
0
def test_swagger_ui_index(aiohttp_api_spec_dir, aiohttp_client):
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.add_api('swagger_simple.yaml')

    app_client = yield from aiohttp_client(app.app)
    swagger_ui = yield from app_client.get('/v1.0/ui/index.html')
    assert swagger_ui.status == 200
    assert b'url = "/v1.0/swagger.json"' in (yield from swagger_ui.read())
def test_swagger_ui_index(aiohttp_api_spec_dir, test_client):
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.add_api('swagger_simple.yaml')

    app_client = yield from test_client(app.app)
    swagger_ui = yield from app_client.get('/v1.0/ui/index.html')
    assert swagger_ui.status == 200
    assert b'url = "/v1.0/swagger.json"' in (yield from swagger_ui.read())
def test_no_swagger_ui_config_json(aiohttp_api_spec_dir, aiohttp_client):
    """ Verify the swagger-ui-config.json file is not returned when the swagger_ui_config option not passed to app. """
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.add_api('swagger_simple.yaml')

    app_client = yield from aiohttp_client(app.app)
    swagger_ui_config_json = yield from app_client.get('/v1.0/ui/swagger-ui-config.json')
    assert swagger_ui_config_json.status == 404
Beispiel #20
0
def test_app_run(web_run_app_mock, aiohttp_api_spec_dir):
    app = AioHttpApp(__name__,
                     port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.run(use_default_access_log=True)
    logger = logging.getLogger('connexion.aiohttp_app')
    assert web_run_app_mock.call_args_list == [
        mock.call(app.app, port=5001, host='0.0.0.0', access_log=logger)
    ]
def test_no_swagger_json(aiohttp_api_spec_dir, test_client):
    """ Verify the swagger.json file is not returned when set to False when creating app. """
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     swagger_json=False,
                     debug=True)
    api = app.add_api('swagger_simple.yaml')

    app_client = yield from test_client(app.app)
    swagger_json = yield from app_client.get('/v1.0/swagger.json')  # type: flask.Response
    assert swagger_json.status == 404
def test_app_with_relative_path(aiohttp_api_spec_dir, test_client):
    # Create the app with a relative path and run the test_app testcase below.
    app = AioHttpApp(__name__, port=5001,
                     specification_dir='..' /
                                       aiohttp_api_spec_dir.relative_to(TEST_FOLDER),
                     debug=True)
    app.add_api('swagger_simple.yaml')
    app_client = yield from test_client(app.app)
    get_bye = yield from app_client.get('/v1.0/bye/jsantos')
    assert get_bye.status == 200
    assert (yield from get_bye.read()) == b'Goodbye jsantos'
Beispiel #23
0
def test_app_with_relative_path(aiohttp_api_spec_dir, aiohttp_client):
    # Create the app with a relative path and run the test_app testcase below.
    app = AioHttpApp(__name__, port=5001,
                     specification_dir='..' /
                                       aiohttp_api_spec_dir.relative_to(TEST_FOLDER),
                     debug=True)
    app.add_api('swagger_simple.yaml')
    app_client = yield from aiohttp_client(app.app)
    get_bye = yield from app_client.get('/v1.0/bye/jsantos')
    assert get_bye.status == 200
    assert (yield from get_bye.read()) == b'Goodbye jsantos'
Beispiel #24
0
async def test_async_secure(aiohttp_api_spec_dir, aiohttp_client):
    app = AioHttpApp(__name__,
                     port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.add_api('openapi_secure.yaml', pass_context_arg_name='request')
    app_client = await aiohttp_client(app.app)

    response = await app_client.get('/v1.0/async_auth')
    assert response.status == 401
    assert response.content_type == 'application/problem+json'

    bearer_header = 'Bearer {"scope": ["myscope"], "uid": "test-user"}'
    response = await app_client.get('/v1.0/async_auth',
                                    headers={'Authorization': bearer_header})
    assert response.status == 200
    assert (await response.json()) == {
        "scope": ['myscope'],
        "uid": 'test-user'
    }

    bearer_header = 'Bearer {"scope": ["myscope", "other_scope"], "uid": "test-user"}'
    response = await app_client.get('/v1.0/async_auth',
                                    headers={'Authorization': bearer_header})
    assert response.status == 403, "async_scope_validation should deny access if scopes are not strictly the same"

    basic_header = 'Basic ' + base64.b64encode(b'username:username').decode(
        'ascii')
    response = await app_client.get('/v1.0/async_auth',
                                    headers={'Authorization': basic_header})
    assert response.status == 200
    assert (await response.json()) == {"uid": 'username'}

    basic_header = 'Basic ' + base64.b64encode(b'username:wrong').decode(
        'ascii')
    response = await app_client.get('/v1.0/async_auth',
                                    headers={'Authorization': basic_header})
    assert response.status == 401, "Wrong password should trigger unauthorized"
    assert response.content_type == 'application/problem+json'

    response = await app_client.get('/v1.0/all_auth',
                                    headers={'X-API-Key': '{"foo": "bar"}'})
    assert response.status == 200
    assert (await response.json()) == {"foo": "bar"}

    bearer_header = 'Bearer {"scope": ["myscope"], "uid": "test-user"}'
    response = await app_client.get('/v1.0/async_bearer_auth',
                                    headers={'Authorization': bearer_header})
    assert response.status == 200
    assert (await response.json()) == {
        "scope": ['myscope'],
        "uid": 'test-user'
    }
Beispiel #25
0
def test_pythonic_path_param(aiohttp_api_spec_dir, aiohttp_client):
    app = AioHttpApp(__name__,
                     port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.add_api('openapi_simple.yaml', pythonic_params=True)

    app_client = yield from aiohttp_client(app.app)
    pythonic = yield from app_client.get('/v1.0/pythonic/100')
    assert pythonic.status == 200
    j = yield from pythonic.json()
    assert j['id_'] == 100
Beispiel #26
0
async def test_swagger_ui_index(aiohttp_api_spec_dir, aiohttp_client):
    app = AioHttpApp(__name__,
                     port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.add_api('openapi_secure.yaml')

    app_client = await aiohttp_client(app.app)
    swagger_ui = await app_client.get('/v1.0/ui/index.html')
    assert swagger_ui.status == 200
    assert b'url: "/v1.0/openapi.json"' in (await swagger_ui.read())
    assert b'swagger-ui-config.json' not in (await swagger_ui.read())
async def test_bearer_secure(aiohttp_api_spec_dir, aiohttp_client):
    """
    Test authentication method specific to OpenApi 3
    """
    app = AioHttpApp(__name__, port=5001, specification_dir=aiohttp_api_spec_dir, debug=True)
    app.add_api('openapi_secure.yaml')
    app_client = await aiohttp_client(app.app)

    bearer_header = 'Bearer {"scope": ["myscope"], "uid": "test-user"}'
    response = await app_client.get('/v1.0/bearer_auth', headers={'Authorization': bearer_header})
    assert response.status == 200
    assert (await response.json()) == {"scope": ['myscope'], "uid": 'test-user'}
Beispiel #28
0
def test_app_with_empty_base_path_error(aiohttp_api_spec_dir):
    spec_dir = '..' / aiohttp_api_spec_dir.relative_to(TEST_FOLDER)
    app = AioHttpApp(__name__,
                     port=5001,
                     specification_dir=spec_dir,
                     debug=True)
    with pytest.raises(ConnexionException) as exc_info:
        app.add_api('swagger_empty_base_path.yaml')

    assert exc_info.value.args == (
        "aiohttp doesn't allow to set empty base_path ('/'), "
        "use non-empty instead, e.g /api", )
Beispiel #29
0
def test_app_with_empty_base_path_error(aiohttp_api_spec_dir):
    spec_dir = '..' / aiohttp_api_spec_dir.relative_to(TEST_FOLDER)
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=spec_dir,
                     debug=True)
    with pytest.raises(ConnexionException) as exc_info:
        app.add_api('swagger_empty_base_path.yaml')

    assert exc_info.value.args == (
        "aiohttp doesn't allow to set empty base_path ('/'), "
        "use non-empty instead, e.g /api",
    )
def test_swagger_json(aiohttp_api_spec_dir, test_client):
    """ Verify the swagger.json file is returned for default setting passed to app. """
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    api = app.add_api('swagger_simple.yaml')

    app_client = yield from test_client(app.app)
    swagger_json = yield from app_client.get('/v1.0/swagger.json')
    json_ = yield from swagger_json.read()

    assert swagger_json.status == 200
    assert api.specification == json.loads(json_)
def test_swagger_ui_index_with_config(aiohttp_api_spec_dir, aiohttp_client):
    swagger_ui_config = {"displayOperationId": True}
    options = {"swagger_ui_config": swagger_ui_config}
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     options=options,
                     debug=True)
    app.add_api('openapi_secure.yaml')

    app_client = yield from aiohttp_client(app.app)
    swagger_ui = yield from app_client.get('/v1.0/ui/index.html')
    assert swagger_ui.status == 200
    assert b'configUrl: "swagger-ui-config.json"' in (yield from swagger_ui.read())
def test_swagger_ui_static(aiohttp_api_spec_dir, test_client):
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.add_api('swagger_simple.yaml')

    app_client = yield from test_client(app.app)
    swagger_ui = yield from app_client.get('/v1.0/ui/lib/swagger-oauth.js')
    assert swagger_ui.status == 200

    app_client = yield from test_client(app.app)
    swagger_ui = yield from app_client.get('/v1.0/ui/swagger-ui.min.js')
    assert swagger_ui.status == 200
Beispiel #33
0
def test_swagger_ui_static(aiohttp_api_spec_dir, aiohttp_client):
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.add_api('swagger_simple.yaml')

    app_client = yield from aiohttp_client(app.app)
    swagger_ui = yield from app_client.get('/v1.0/ui/lib/swagger-oauth.js')
    assert swagger_ui.status == 200

    app_client = yield from aiohttp_client(app.app)
    swagger_ui = yield from app_client.get('/v1.0/ui/swagger-ui.min.js')
    assert swagger_ui.status == 200
Beispiel #34
0
def test_swagger_yaml(aiohttp_api_spec_dir, aiohttp_client):
    """ Verify the swagger.yaml file is returned for default setting passed to app. """
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    api = app.add_api('swagger_simple.yaml')

    app_client = yield from aiohttp_client(app.app)
    spec_response = yield from app_client.get('/v1.0/swagger.yaml')
    data_ = yield from spec_response.read()

    assert spec_response.status == 200
    assert api.specification.raw == yaml.load(data_)
Beispiel #35
0
async def test_swagger_json(aiohttp_api_spec_dir, aiohttp_client):
    """ Verify the swagger.json file is returned for default setting passed to app. """
    app = AioHttpApp(__name__,
                     port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.add_api('datetime_support.yaml')

    app_client = await aiohttp_client(app.app)
    swagger_json = await app_client.get('/v1.0/openapi.json')
    spec_data = await swagger_json.json()

    def get_value(data, path):
        for part in path.split('.'):
            data = data.get(part)
            assert data, f"No data in part '{part}' of '{path}'"
        return data

    example = get_value(
        spec_data,
        'paths./datetime.get.responses.200.content.application/json.schema.example.value'
    )
    assert example in [
        '2000-01-23T04:56:07.000008+00:00',  # PyYAML 5.3
        '2000-01-23T04:56:07.000008Z'
    ]
    example = get_value(
        spec_data,
        'paths./date.get.responses.200.content.application/json.schema.example.value'
    )
    assert example == '2000-01-23'
    example = get_value(
        spec_data,
        'paths./uuid.get.responses.200.content.application/json.schema.example.value'
    )
    assert example == 'a7b8869c-5f24-4ce0-a5d1-3e44c3663aa9'

    resp = await app_client.get('/v1.0/datetime')
    assert resp.status == 200
    json_data = await resp.json()
    assert json_data == {'value': '2000-01-02T03:04:05.000006Z'}

    resp = await app_client.get('/v1.0/date')
    assert resp.status == 200
    json_data = await resp.json()
    assert json_data == {'value': '2000-01-02'}

    resp = await app_client.get('/v1.0/uuid')
    assert resp.status == 200
    json_data = await resp.json()
    assert json_data == {'value': 'e7ff66d0-3ec2-4c4e-bed0-6e4723c24c51'}
Beispiel #36
0
async def test_no_swagger_yaml(aiohttp_api_spec_dir, aiohttp_client):
    """ Verify the swagger.json file is not returned when set to False when creating app. """
    options = {"swagger_json": False}
    app = AioHttpApp(__name__,
                     port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     options=options,
                     debug=True)
    app.add_api('swagger_simple.yaml')

    app_client = await aiohttp_client(app.app)
    spec_response = await app_client.get('/v1.0/swagger.yaml'
                                         )  # type: flask.Response
    assert spec_response.status == 404
Beispiel #37
0
async def test_swagger_json(aiohttp_api_spec_dir, aiohttp_client):
    """ Verify the swagger.json file is returned for default setting passed to app. """
    app = AioHttpApp(__name__,
                     port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    api = app.add_api('swagger_simple.yaml')

    app_client = await aiohttp_client(app.app)
    swagger_json = await app_client.get('/v1.0/swagger.json')

    assert swagger_json.status == 200
    json_ = await swagger_json.json()
    assert api.specification.raw == json_
Beispiel #38
0
def test_app_get_root_path_invalid(sys_modules_mock, aiohttp_api_spec_dir):
    with pytest.raises(RuntimeError) as exc_info:
        AioHttpApp('error__',
                   port=5001,
                   specification_dir=aiohttp_api_spec_dir)

    assert exc_info.value.args == ("Invalid import name 'error__'", )
def test_swagger_ui_config_json(aiohttp_api_spec_dir, aiohttp_client):
    """ Verify the swagger-ui-config.json file is returned for swagger_ui_config option passed to app. """
    swagger_ui_config = {"displayOperationId": True}
    options = {"swagger_ui_config": swagger_ui_config}
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     options=options,
                     debug=True)
    api = app.add_api('swagger_simple.yaml')

    app_client = yield from aiohttp_client(app.app)
    swagger_ui_config_json = yield from app_client.get('/v1.0/ui/swagger-ui-config.json')
    json_ = yield from swagger_ui_config_json.read()

    assert swagger_ui_config_json.status == 200
    assert swagger_ui_config == json.loads(json_)
Beispiel #40
0
def test_app_add_two_apis_error_with_only_one_api(aiohttp_api_spec_dir):
    spec_dir = '..' / aiohttp_api_spec_dir.relative_to(TEST_FOLDER)
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=spec_dir,
                     debug=True,
                     only_one_api=True)
    app.add_api('swagger_empty_base_path.yaml')

    with pytest.raises(ConnexionException) as exc_info:
        app.add_api('swagger_empty_base_path.yaml')

    assert exc_info.value.args == (
        "an api was already added, "
        "create a new app with 'only_one_api=False' "
        "to add more than one api",
    )
Beispiel #41
0
async def test_swagger_ui(aiohttp_api_spec_dir, aiohttp_client):
    app = AioHttpApp(__name__,
                     port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.add_api('swagger_simple.yaml')

    app_client = await aiohttp_client(app.app)
    swagger_ui = await app_client.get('/v1.0/ui')
    assert swagger_ui.status == 200
    assert swagger_ui.url.path == '/v1.0/ui/'
    assert b'url = "/v1.0/swagger.json"' in (await swagger_ui.read())

    swagger_ui = await app_client.get('/v1.0/ui/')
    assert swagger_ui.status == 200
    assert b'url = "/v1.0/swagger.json"' in (await swagger_ui.read())
Beispiel #42
0
def test_secure_app(oauth_requests, aiohttp_api_spec_dir, aiohttp_client):
    # Create the app and run the test_app testcase below.
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.add_api('swagger_secure.yaml')
    app_client = yield from aiohttp_client(app.app)

    post_hello = yield from app_client.post('/v1.0/greeting/jsantos')
    assert post_hello.status == 401

    headers = {'Authorization': 'Bearer 100'}
    post_hello = yield from app_client.post(
        '/v1.0/greeting/jsantos',
        headers=headers
    )

    assert post_hello.status == 200
    assert (yield from post_hello.read()) == b'{"greeting":"Hello jsantos"}'

    headers = {'authorization': 'Bearer 100'}
    post_hello = yield from app_client.post(
        '/v1.0/greeting/jsantos',
        headers=headers
    )

    assert post_hello.status == 200, "Authorization header in lower case should be accepted"
    assert (yield from post_hello.read()) == b'{"greeting":"Hello jsantos"}'

    headers = {'AUTHORIZATION': 'Bearer 100'}
    post_hello = yield from app_client.post(
        '/v1.0/greeting/jsantos',
        headers=headers
    )

    assert post_hello.status == 200, "Authorization header in upper case should be accepted"
    assert (yield from post_hello.read()) == b'{"greeting":"Hello jsantos"}'

    no_authorization = yield from app_client.post(
        '/v1.0/greeting/jsantos',
    )

    assert no_authorization.status == 401
    assert no_authorization.content_type == 'application/problem+json'
def test_secure_app(oauth_requests, aiohttp_api_spec_dir, test_client):
    # Create the app and run the test_app testcase below.
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.add_api('swagger_secure.yaml')
    app_client = yield from test_client(app.app)

    post_hello = yield from app_client.post('/v1.0/greeting/jsantos')
    assert post_hello.status == 401

    headers = {'Authorization': 'Bearer 100'}
    post_hello = yield from app_client.post(
        '/v1.0/greeting/jsantos',
        headers=headers
    )

    assert post_hello.status == 200
    assert (yield from post_hello.read()) == b'{"greeting":"Hello jsantos"}'
def test_middlewares(aiohttp_api_spec_dir, test_client):
    @asyncio.coroutine
    def middleware(app, handler):
        @asyncio.coroutine
        def middleware_handler(request):
            response = (yield from handler(request))
            response.body += b' middleware'
            return response

        return middleware_handler

    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True, middlewares=[middleware])
    app.add_api('swagger_simple.yaml')
    app_client = yield from test_client(app.app)
    get_bye = yield from app_client.get('/v1.0/bye/jsantos')
    assert get_bye.status == 200
    assert (yield from get_bye.read()) == b'Goodbye jsantos middleware'
def test_auth_all_paths(oauth_requests, aiohttp_api_spec_dir, test_client):
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True, auth_all_paths=True)
    app.add_api('swagger_secure.yaml')

    app_client = yield from test_client(app.app)

    headers = {'Authorization': 'Bearer 100'}
    get_inexistent_endpoint = yield from app_client.get(
        '/v1.0/does-not-exist-valid-token',
        headers=headers
    )
    assert get_inexistent_endpoint.status == 404
    assert get_inexistent_endpoint.content_type == 'application/problem+json'

    get_inexistent_endpoint = yield from app_client.get(
        '/v1.0/does-not-exist-no-token'
    )
    assert get_inexistent_endpoint.status == 401
    assert get_inexistent_endpoint.content_type == 'application/problem+json'
def test_no_swagger_ui(aiohttp_api_spec_dir, test_client):
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     swagger_ui=False, debug=True)
    app.add_api('swagger_simple.yaml')

    app_client = yield from test_client(app.app)
    swagger_ui = yield from app_client.get('/v1.0/ui/')
    assert swagger_ui.status == 404

    app2 = AioHttpApp(__name__, port=5001,
                      specification_dir=aiohttp_api_spec_dir,
                      debug=True)
    app2.add_api('swagger_simple.yaml', swagger_ui=False)
    app2_client = yield from test_client(app.app)
    swagger_ui2 = yield from app2_client.get('/v1.0/ui/')
    assert swagger_ui2.status == 404
def aiohttp_app(aiohttp_api_spec_dir):
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir,
                     debug=True)
    app.add_api('swagger_simple.yaml', validate_responses=True)
    return app
Beispiel #48
0
def test_app_get_root_path(aiohttp_api_spec_dir):
    app = AioHttpApp(__name__, port=5001,
                     specification_dir=aiohttp_api_spec_dir)
    assert app.get_root_path().endswith('connexion/tests/aiohttp') == True
Beispiel #49
0
def test_app_get_root_path_not_in_sys_modules(sys_modules_mock, aiohttp_api_spec_dir):
    app = AioHttpApp('connexion', port=5001,
                     specification_dir=aiohttp_api_spec_dir)
    assert app.get_root_path().endswith('/connexion') == True