def test_get_room_id(self): """Test whether room id is returned""" setup_http_server() client = GitterClient(api_token='aaa', ssl_verify=True) room_id = client.get_room_id('testapicomm/community') self.assertEqual(room_id, GITTER_ROOM_ID)
def test_room_id_not_found(self): """Test whether room id returned is None when room id is not found """ setup_http_server() client = GitterClient(api_token='aaa', ssl_verify=True) self.assertIsNone(client.get_room_id('testapicomm/community_not'))
def test_rate_limit_error(self): """Test if a rate limit error is raised when rate is exhausted""" wait = 2 reset = int(time.time() + wait) rate_limit_headers = { 'X-RateLimit-Remaining': '0', 'X-RateLimit-Reset': reset } setup_http_server(rate_limit_headers=rate_limit_headers) client = GitterClient('aaaa', max_items=1, sleep_for_rate=False) room_id = client.get_room_id('testapicomm/community') with self.assertRaises(RateLimitError): _ = [m for m in client.message_page(room_id, before_id=None)]
def test_init(self): """Check attributes initialization""" client = GitterClient(api_token='aaa', ssl_verify=True) self.assertIsInstance(client, GitterClient) self.assertEqual(client.api_token, 'aaa') self.assertTrue(client.ssl_verify) self.assertFalse(client.sleep_for_rate) self.assertEqual(client.min_rate_to_sleep, MIN_RATE_LIMIT) client = GitterClient(api_token='aaa', sleep_for_rate=True, min_rate_to_sleep=1, ssl_verify=False) self.assertIsInstance(client, GitterClient) self.assertEqual(client.api_token, 'aaa') self.assertFalse(client.ssl_verify) self.assertTrue(client.sleep_for_rate) self.assertEqual(client.min_rate_to_sleep, 1)
def test_message(self): """Test whether messages are fetched""" setup_http_server() client = GitterClient(api_token='aaa', ssl_verify=True) messages = [ m for m in client.message_page(GITTER_ROOM_ID, before_id=None) ] self.assertEqual(len(messages), 6) # Check requests expected = { 'limit': ['100'], } self.assertEqual(httpretty.last_request().querystring, expected) self.assertEqual(httpretty.last_request().headers["Authorization"], 'Bearer aaa')
def test_sleep_for_rate(self): """Test if the clients sleeps when the rate limit is reached""" wait = 10 reset = int(time.time() + wait) rate_limit_headers = { 'X-RateLimit-Remaining': '0', 'X-RateLimit-Reset': reset } setup_http_server(rate_limit_headers=rate_limit_headers) client = GitterClient('aaaa', max_items=2, min_rate_to_sleep=5, sleep_for_rate=True) room_id = client.get_room_id('testapicomm/community') _ = [m for m in client.message_page(room_id, before_id=None)] after = float(time.time()) self.assertTrue(reset >= after)
def test_sanitize_for_archive(self): """Test whether the sanitize method works properly""" url = "https://api.gitter.im/v1/testapicomm/community" headers = {GitterClient.HAUTHORIZATION: 'Bearer aaaa'} payload = { 'limit': 10, 'beforeId': '123', } s_url, s_headers, s_payload = GitterClient.sanitize_for_archive( url, copy.deepcopy(headers), payload) headers.pop(GitterClient.HAUTHORIZATION) self.assertEqual(url, s_url) self.assertEqual(headers, s_headers) self.assertEqual(payload, s_payload)