def test_secured_api_keys(self): hosts = F.hosts(self.client.app_id) config1 = SearchConfig(F.get_app_id(), F.get_api_key()) config1.hosts = hosts client1 = F.decide(SearchClient.create_with_config(config1)) index1 = F.index(client1, self._testMethodName) index2 = F.index(client1, '{}_dev'.format(self._testMethodName)) index1.save_object({'objectID': 'one'}).wait() index2.save_object({'objectID': 'one'}).wait() api_key = self.client.generate_secured_api_key( os.environ['ALGOLIA_SEARCH_KEY_1'], { "validUntil": int(round(time.time())) + (60 * 10), # + 10 min "restrictIndices": index1.name }) config2 = SearchConfig(F.get_app_id(), api_key) config2.hosts = hosts client2 = F.decide(SearchClient.create_with_config(config2)) index1_search = client2.init_index(index1.name) index2_search = client2.init_index(index2.name) index1_search.search('') with self.assertRaises(RequestException) as _: index2_search.search('')
def test_async_session(self): app_id = Factory.get_app_id() api_key = Factory.get_api_key() client = SearchClient.create(app_id, api_key) import asyncio result = asyncio.get_event_loop().run_until_complete( asyncio.gather(client.list_api_keys_async())) self.assertIsInstance(result, list) asyncio.get_event_loop().run_until_complete( asyncio.gather(client.close())) self.assertTrue(client._transporter_async._requester._session.closed)
def test_dns_timeout(self): config = SearchConfig(F.get_app_id(), F.get_api_key()) config.hosts = HostsCollection([ Host('algolia.biz', 10), Host('{}-1.algolianet.com'.format(F.get_app_id())), Host('{}-2.algolianet.com'.format(F.get_app_id())), Host('{}-3.algolianet.com'.format(F.get_app_id())) ]) client = SearchClient.create_with_config(config) client.list_indices() # We test that the first Host `algolia.biz` is down. self.assertFalse(config.hosts.read()[0].up) self.assertTrue(config.hosts.read()[1].up) self.assertTrue(config.hosts.read()[2].up) self.assertTrue(config.hosts.read()[3].up)
def test_async_session(self): app_id = Factory.get_app_id() api_key = Factory.get_api_key() client = SearchClient.create(app_id, api_key) import asyncio result = asyncio.get_event_loop().run_until_complete( asyncio.gather(client.list_api_keys_async()) ) self.assertIsInstance(result, list) asyncio.get_event_loop().run_until_complete( asyncio.gather(client.close()) ) self.assertTrue( client._transporter_async._requester._session.closed )