def make_response(loop, method, url, data=None, content_type='text/plain', charset='utf-8'): if LooseVersion(aiohttp_version) >= LooseVersion('3.3.0'): response = ClientResponse(method, URL(url), writer=mock.Mock(), continue100=None, timer=None, request_info=mock.Mock(), traces=[], loop=loop, session=mock.Mock()) elif LooseVersion(aiohttp_version) >= LooseVersion('3.1.0'): response = ClientResponse(method, URL(url), writer=mock.Mock(), continue100=None, timer=None, request_info=mock.Mock(), auto_decompress=True, traces=[], loop=loop, session=mock.Mock()) else: response = ClientResponse(method, URL(url), writer=mock.Mock(), continue100=None, timer=None, request_info=mock.Mock(), auto_decompress=True) response._post_init(loop, mock.Mock()) def side_effect(*args, **kwargs): fut = loop.create_future() fut.set_result(str(data).encode(charset)) return fut if LooseVersion(aiohttp_version) >= LooseVersion('3.3.0'): response._headers = { 'Content-Type': '%s; charset=%s' % (content_type, charset) } else: response.headers = { 'Content-Type': '%s; charset=%s' % (content_type, charset) } content = response.content = mock.Mock() if data: content.read.side_effect = side_effect return response
async def test_authenticate(): st = AsyncSpaceTrackClient('identity', 'wrongpassword') loop = asyncio.get_event_loop() response = ClientResponse( 'post', URL('https://www.space-track.org/ajaxauth/login')) # aiohttp 2.2 uses session try: response._post_init(loop) except TypeError: response._post_init(loop, st.session) response.status = 200 response.json = Mock() async def mock_post(url, data): response.json.return_value = asyncio.Future() if data['password'] == 'wrongpassword': response.json.return_value.set_result({'Login': '******'}) elif data['password'] == 'unknownresponse': # Space-Track doesn't respond like this, but make sure anything # other than {'Login': '******'} doesn't raise AuthenticationError response.json.return_value.set_result({'Login': '******'}) else: response.json.return_value.set_result('') return response with st, patch.object(st.session, 'post', mock_post): with pytest.raises(AuthenticationError): await st.authenticate() assert response.json.call_count == 1 st.password = '******' await st.authenticate() # This shouldn't make a HTTP request since we're already authenticated. await st.authenticate() assert response.json.call_count == 2 st = AsyncSpaceTrackClient('identity', 'unknownresponse') with st, patch.object(st.session, 'post', mock_post): await st.authenticate() response.close()
async def test_authenticate(): st = AsyncSpaceTrackClient('identity', 'wrongpassword') loop = asyncio.get_event_loop() response = ClientResponse( 'post', 'https://www.space-track.org/ajaxauth/login') response._post_init(loop) response.status = 200 response.json = Mock() async def mock_post(url, data): response.json.return_value = asyncio.Future() if data['password'] == 'wrongpassword': response.json.return_value.set_result({'Login': '******'}) elif data['password'] == 'unknownresponse': # Space-Track doesn't respond like this, but make sure anything # other than {'Login': '******'} doesn't raise AuthenticationError response.json.return_value.set_result({'Login': '******'}) else: response.json.return_value.set_result('') return response with st, patch.object(st.session, 'post', mock_post): with pytest.raises(AuthenticationError): await st.authenticate() assert response.json.call_count == 1 st.password = '******' await st.authenticate() # This shouldn't make a HTTP request since we're already authenticated. await st.authenticate() assert response.json.call_count == 2 st = AsyncSpaceTrackClient('identity', 'unknownresponse') with st, patch.object(st.session, 'post', mock_post): await st.authenticate() response.close()
def make_response(loop, method, url, data=None, content_type='text/plain', charset='utf-8'): response = ClientResponse(method, URL(url)) response._post_init(loop, mock.Mock()) def side_effect(*args, **kwargs): fut = loop.create_future() fut.set_result(str(data).encode(charset)) return fut response.headers = { 'Content-Type': '%s; charset=%s' % (content_type, charset) } content = response.content = mock.Mock() if data: content.read.side_effect = side_effect return response
async def test_generic_request(): def mock_authenticate(self): result = asyncio.Future() result.set_result(None) return result def mock_download_predicate_data(self, class_): result = asyncio.Future() data = [ { 'Default': '0000-00-00 00:00:00', 'Extra': '', 'Field': 'PUBLISH_EPOCH', 'Key': '', 'Null': 'NO', 'Type': 'datetime' }, { 'Default': '', 'Extra': '', 'Field': 'TLE_LINE1', 'Key': '', 'Null': 'NO', 'Type': 'char(71)' }, { 'Default': '', 'Extra': '', 'Field': 'TLE_LINE2', 'Key': '', 'Null': 'NO', 'Type': 'char(71)' } ] result.set_result(data) return result st = AsyncSpaceTrackClient('identity', 'password') loop = asyncio.get_event_loop() response = ClientResponse( 'get', 'https://www.space-track.org/basicspacedata/query/class' '/tle_publish/format/tle') response._post_init(loop) tle = ( '1 25544U 98067A 08264.51782528 -.00002182 00000-0 -11606-4 0 2927\r\n' '2 25544 51.6416 247.4627 0006703 130.5360 325.0288 15.72125391563537\r\n') normalised_tle = tle.replace('\r\n', '\n') response.status = 200 response.text = Mock() response.text.return_value = asyncio.Future() response.text.return_value.set_result(tle) mock_get = asyncio.Future() mock_get.set_result(response) patch_authenticate = patch.object( AsyncSpaceTrackClient, 'authenticate', mock_authenticate) patch_download_predicate_data = patch.object( AsyncSpaceTrackClient, '_download_predicate_data', mock_download_predicate_data) patch_get = patch.object(st.session, 'get', return_value=mock_get) with patch_authenticate, patch_download_predicate_data, patch_get: assert await st.tle_publish(format='tle') == normalised_tle response.close() response = ClientResponse( 'get', 'https://www.space-track.org/basicspacedata/query/class' '/tle_publish') response._post_init(loop) response.status = 200 response.json = Mock() response.json.return_value = asyncio.Future() response.json.return_value.set_result({'a': 5}) mock_get = asyncio.Future() mock_get.set_result(response) patch_get = patch.object(st.session, 'get', return_value=mock_get) with patch_authenticate, patch_download_predicate_data, patch_get: result = await st.tle_publish() assert result['a'] == 5 response.close() st.close()