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)
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')
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
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
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)
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