Exemplo n.º 1
0
def _init_api(
        mock_api_app_config,
        mock_api_spec,
        plugin=None,  # noqa: F811
        init_server=False,
        init_apps=False,
        init_swagger=False,
        generate_mode=False):
    api.clear()
    if generate_mode:
        api.setup(generate_mode=True)
    api.init_empty_spec(api_version=mock_api_spec['info']['version'],
                        title=mock_api_spec['info']['title'],
                        description=mock_api_spec['info']['description'])
    server_config = mock_api_app_config.server
    if init_server:
        api.register_server_config(server_config)
    if init_apps:
        apps = [mock_api_app_config]
        if plugin:
            apps.append(plugin)
        api.register_apps(apps)
    if init_swagger:
        api.static_spec = deepcopy(api.spec)
        api.enable_swagger(mock_api_app_config.server, web.Application())
Exemplo n.º 2
0
def test_load_api_file(mock_api_spec):  # noqa: F811
    api.clear()
    file_name = f"/tmp/{str(uuid.uuid4())}.json"
    with open(file_name, 'w') as fp:
        json.dump(mock_api_spec, fp)
    api.load_api_file(file_name)
    assert api.spec == mock_api_spec
    assert api.static_spec == mock_api_spec
    assert id(api.spec) != id(api.static_spec)
    os.remove(file_name)
Exemplo n.º 3
0
def test_init_empty_spec(mock_api_spec):  # noqa: F811
    api.clear()
    api.init_empty_spec(api_version=mock_api_spec['info']['version'],
                        title=mock_api_spec['info']['title'],
                        description=mock_api_spec['info']['description'])
    assert api.spec == {
        'openapi': api.OPEN_API_VERSION,
        'info': mock_api_spec['info'],
        'paths': {}
    }
    assert api.static_spec == api.spec
Exemplo n.º 4
0
def test_generate_mode(mock_api_app_config, mock_api_spec, mock_plugin_config,
                       mock_app_api_generated):  # noqa: F811
    api.clear()
    _init_api(mock_api_app_config,
              mock_api_spec,
              plugin=mock_plugin_config,
              init_server=True,
              init_apps=True,
              generate_mode=True)
    generated_spec = deepcopy(mock_api_spec)
    generated_spec['paths'].update(mock_app_api_generated)
    assert api.spec == generated_spec
Exemplo n.º 5
0
async def _setup(
        monkeypatch,
        mock_app_config,  # noqa: F811
        mock_plugin_config,  # noqa: F811
        aiohttp_client,  # noqa: F811
        streams=None,
        enabled_groups: Optional[List[str]] = None):
    stream_event = MockResult("ok: ok")
    monkeypatch.setattr(MockStreamManager, 'test_payload', stream_event)
    monkeypatch.setattr(MockEventHandler, 'test_track_ids', None)

    api.clear()
    if enabled_groups is None:
        enabled_groups = []
    await start_test_server(mock_app_config, mock_plugin_config, streams,
                            enabled_groups)
    return await aiohttp_client(hopeit.server.web.web_server)
Exemplo n.º 6
0
def test_api_disabled(mock_api_app_config):  # noqa: F811
    api.clear()
    api.register_server_config(mock_api_app_config.server)
    assert api.spec is None
    apps = [mock_api_app_config]
    app = web.Application()
    api.register_apps(apps)
    api.enable_swagger(mock_api_app_config.server, app)
    assert api.spec is None
    route = app_route_name(mock_api_app_config.app,
                           event_name='mock-app-api-get')
    handler = api.add_route('get', route, _test_handler_get)
    assert handler is _test_handler_get
    route = app_route_name(mock_api_app_config.app,
                           event_name='mock-app-noapi')
    handler = api.add_route('get', route, _test_handler_get)
    assert handler is _test_handler_get
Exemplo n.º 7
0
def _setup(
        monkeypatch,
        loop,
        mock_app_config,  # noqa: F811
        mock_plugin_config,  # noqa: F811
        aiohttp_server,  # noqa: F811
        aiohttp_client,  # noqa: F811
        streams=None):
    stream_event = MockResult("ok: ok")
    monkeypatch.setattr(MockStreamManager, 'test_payload', stream_event)
    monkeypatch.setattr(MockEventHandler, 'test_track_ids', None)

    api.clear()
    loop.run_until_complete(
        start_test_server(mock_app_config, mock_plugin_config, aiohttp_server,
                          streams))
    return loop.run_until_complete(aiohttp_client(
        hopeit.server.web.web_server))
Exemplo n.º 8
0
def test_save_api_file(mock_api_spec):  # noqa: F811
    api.clear()
    file_name = f"/tmp/{str(uuid.uuid4())}.json"
    api.spec = mock_api_spec
    api.static_spec = deepcopy(mock_api_spec)
    api.save_api_file(file_name, api_version=mock_api_spec['info']['version'])
    with open(file_name, 'r') as fp:
        check_spec = json.load(fp)
    assert check_spec == mock_api_spec

    with pytest.raises(APIError):
        api.static_spec['info']['title'] = 'Simulating API Change'
        api.save_api_file(file_name,
                          api_version=mock_api_spec['info']['version'])

    api.save_api_file(file_name, api_version='9.9.9')
    with open(file_name, 'r') as fp:
        check_spec = json.load(fp)
    assert check_spec == mock_api_spec
    assert check_spec['info']['version'] == '9.9.9'
    os.remove(file_name)