Example #1
0
def test_NetboxApiOperation_parse_params():
    spec = ResolvingParser(str(here / 'data' / 'openapi-2.yaml'))
    nb = AIONetbox(host='http://localhost',
                   api_key='11',
                   spec=spec,
                   session=SessionMock())
    op = NetboxApiOperation('users', 'users_list',
                            nb.config['users']['users_list'], nb)

    assert ({}, {}, {}) == op.parse_params({})

    params = {'name': 'foobar', 'bad_param': True}
    parsed = op.parse_params(params)

    assert ({}, {}, {'name': 'foobar'}) == parsed

    op = NetboxApiOperation('users', 'users_read',
                            nb.config['users']['users_read'], nb)

    params = {'userId': 1}
    parsed = op.parse_params(params)

    assert ({'userId': 1}, {}, {}) == parsed

    with pytest.raises(MissingRequiredParam):
        op.parse_params({})
Example #2
0
async def test_NetboxApiOperation_request_cf(mnbro):
    spec = ResolvingParser(str(here / 'data' / 'openapi-2.yaml'))
    nb = AIONetbox(host='http://localhost',
                   api_key='11',
                   spec=spec,
                   session=SessionMock())
    nb.request = asynctest.CoroutineMock(return_value=ResponseMock())
    op = NetboxApiOperation('users', 'users_list',
                            nb.config['users']['users_list'], nb)

    mnbro.from_response.return_value.next = None

    response_schema = spec.specification['paths']['/users']['get'][
        'responses']['200']['schema']
    op.parse_params = MagicMock(return_value=({}, {}, {'name': 'test'}))
    op.build_url = MagicMock(return_value='http://test/users')

    await op.request(name='test', cf_hello='world', extra='ignore')

    nb.request.assert_called_with(method='get',
                                  url='http://test/users',
                                  query_params={
                                      'name': 'test',
                                      'cf_hello': 'world'
                                  },
                                  body={})

    mnbro.from_response.assert_called_with(data={}, **response_schema)
Example #3
0
async def test_AIONetbox_get_session_key():
    spec = ResolvingParser(str(here / 'data' / 'openapi-2.yaml'))
    nb = AIONetbox(host='http://localhost',
                   api_key='11',
                   spec=spec,
                   session=SessionMock())
    nb.request = asynctest.CoroutineMock(return_value=ResponseMock())

    await nb.get_session_key('test')
Example #4
0
def test_NetboxApi():
    spec = ResolvingParser(str(here / 'data' / 'openapi-2.yaml'))
    nb = AIONetbox(host='http://localhost',
                   api_key='11',
                   spec=spec,
                   session=SessionMock())
    api = NetboxApi('users', nb.config.get('users'), nb)

    assert repr(api) == 'Netbox.users'
Example #5
0
def test_NetboxApiOperation_build_url():
    spec = ResolvingParser(str(here / 'data' / 'openapi-2.yaml'))
    nb = AIONetbox(host='http://localhost',
                   api_key='11',
                   spec=spec,
                   session=SessionMock())
    op = NetboxApiOperation('users', 'users_read', {}, nb)

    assert 'http://localhost/v1/test' == op.build_url('/test')
Example #6
0
def test_NetboxApiOperation():
    spec = ResolvingParser(str(here / 'data' / 'openapi-2.yaml'))
    nb = AIONetbox(host='http://localhost',
                   api_key='11',
                   spec=spec,
                   session=SessionMock())
    op = NetboxApiOperation('users', 'users_read', {}, nb)

    assert repr(op) == 'Netbox.users.users_read'
Example #7
0
def test_AIONetbox_parse_spec():
    a = AIONetbox('http://localhost', '1122')
    spec = ResolvingParser(str(here / 'data' / 'openapi-2.yaml'))

    assert {} == a.parse_spec(None)
    assert {} == a.parse_spec({})
    assert '_orig' in a.parse_spec(spec)
    assert 'users' in a.parse_spec(spec)
    assert 'fake' not in a.parse_spec(spec)
    assert 'users_list' in a.parse_spec(spec).get('users')
Example #8
0
def test_NetboxApiOperation_parse_params_body():
    spec = ResolvingParser(str(here / 'data' / 'openapi-2.yaml'))
    nb = AIONetbox(host='http://localhost',
                   api_key='11',
                   spec=spec,
                   session=SessionMock())
    op = NetboxApiOperation('users', 'users_create',
                            nb.config['users']['users_create'], nb)

    params = {'data': {'foobar': 'bar', 'baz': True}}
    parsed = op.parse_params(params)

    assert ({}, {'foobar': 'bar', 'baz': True}, {}) == parsed
Example #9
0
async def test_AIONetbox_get_session_key_fail():
    spec = ResolvingParser(str(here / 'data' / 'openapi-2.yaml'))
    nb = AIONetbox(host='http://localhost',
                   api_key='11',
                   spec=spec,
                   session=SessionMock())

    resp = ResponseMock()
    nb.request = asynctest.CoroutineMock(return_value=resp)
    resp.json = asynctest.CoroutineMock(side_effect=[Exception])

    key = await nb.get_session_key('test')

    assert key is None
Example #10
0
async def test_NetboxApiOperation_request_delete():
    spec = ResolvingParser(str(here / 'data' / 'openapi-2.yaml'))
    nb = AIONetbox(host='http://localhost',
                   api_key='11',
                   spec=spec,
                   session=SessionMock())
    nb.request = asynctest.CoroutineMock(return_value=ResponseMock())

    op = NetboxApiOperation('users', 'users_delete',
                            nb.config['users']['users_delete'], nb)

    results = await op.request(userId=42)

    assert results
Example #11
0
def test_AIONetbox_attr(mnba):

    spec = ResolvingParser(str(here / 'data' / 'openapi-2.yaml'))
    nb = AIONetbox(host='http://localhost',
                   api_key='11',
                   spec=spec,
                   session=SessionMock())

    nb.users
    nb.users

    mnba.assert_called_once()
    mnba.assert_called_with('users', nb.config['users'], nb)

    with pytest.raises(AttributeError):
        nb.fake_tag
Example #12
0
def test_NetboxApi_attr(mnbaop):
    spec = ResolvingParser(str(here / 'data' / 'openapi-2.yaml'))
    nb = AIONetbox(host='http://localhost',
                   api_key='11',
                   spec=spec,
                   session=SessionMock())

    api = NetboxApi('users', nb.config.get('users'), nb)

    api.users_read
    api.users_read

    mnbaop.assert_called_once()
    mnbaop.assert_called_with('users', 'users_read',
                              nb.config['users']['users_read'], nb)

    with pytest.raises(AttributeError):
        api.fake_method
Example #13
0
async def test_NetboxApiOperation_request(mnbro):
    spec = ResolvingParser(str(here / 'data' / 'openapi-2.yaml'))
    nb = AIONetbox(host='http://localhost',
                   api_key='11',
                   spec=spec,
                   session=SessionMock())
    nb.request = asynctest.CoroutineMock(return_value=ResponseMock())
    op = NetboxApiOperation('users', 'users_read',
                            nb.config['users']['users_read'], nb)

    response_schema = spec.specification['paths']['/users/{userId}']['get'][
        'responses']['200']['schema']
    op.parse_params = MagicMock(return_value=({}, {}, {}))
    op.build_url = MagicMock()

    await op.request(userId=42)

    mnbro.from_response.assert_called_with(data={}, **response_schema)
Example #14
0
async def test_NetboxApiOperation_request_pagination(mnbro):
    spec = ResolvingParser(str(here / 'data' / 'openapi-2.yaml'))
    nb = AIONetbox(host='http://localhost',
                   api_key='11',
                   spec=spec,
                   session=SessionMock())
    nb.request = asynctest.CoroutineMock(return_value=ResponseMock())
    op = NetboxApiOperation('users', 'users_list',
                            nb.config['users']['users_list'], nb)

    page0 = MagicMock()
    page1 = MagicMock()

    page0.next = 'http://test/users?limit=50&offset=50'
    page0.results = ['1', '2', '3']
    page1.next = None
    page1.results = ['one', 'two', 'three']

    mnbro.from_response.side_effect = [page0, page1]

    op.parse_params = MagicMock(return_value=({}, {}, {'name': 'test'}))
    op.build_url = MagicMock(return_value='http://test/users')

    results = await op.request(name='test')

    nb.request.assert_any_call(method='get',
                               url='http://test/users',
                               query_params={'name': 'test'},
                               body={})

    nb.request.assert_any_call(method='get',
                               url='http://test/users',
                               query_params={
                                   'name': 'test',
                                   'limit': '50',
                                   'offset': '50'
                               },
                               body={})

    assert ['1', '2', '3', 'one', 'two', 'three'] == results.results
Example #15
0
def test_NetboxResponseObject_from_invalid_response():
    spec = ResolvingParser(str(here / 'data' / 'openapi-2.yaml'))
    schema = spec.specification['paths']['/users/{userId}']['get'][
        'responses']['200']['schema']

    data = {
        'id':
        1,
        'name':
        'Marco Ceppi',
        'cars': [{
            'make': 'MG',
            'model': 'BGT',
            'year': 1974,
        }, {
            'model': 'TR6',
            'year': 1974,
        }]
    }

    with pytest.raises(InvalidResponse):
        NetboxResponseObject.from_response(data=data, **schema)