class TestHipchatClient(unittest.TestCase): def setUp(self): self.room_name = 'room_name' self.token = 'token' self.queue = Mock() self.client = HipChat(self.queue, self.token, self.room_name) def test_init(self): self.assertEqual(self.client.token, self.token) self.assertEqual(self.client.room_name, self.room_name) def test_get_rooms(self): """ Get rooms from Hipchat >>> get_rooms() [list of rooms] """ self.client.list_rooms = Mock(return_value={'rooms': [{'name': 'test', 'room_id': 1}, {'name': 'test1', 'room_id': 2}]}) rooms = self.client.get_rooms() self.client.list_rooms.assert_called_with() self.assertEqual(len(rooms), 2) def test_get_extended_room_information(self): """ Get room info >>> get_rooms('room_name') {'name': 'room_name', 'room_id': 'room id'} """ self.client.get_room_information = Mock(return_value={'name': 'test1', 'room_id': 2}) self.client.method = Mock(return_value={'room': {'name': 'test1', 'room_id': 2, 'participants': ['test']}}) room = self.client.get_extended_room_information('room_name') self.client.get_room_information.assert_called_with('room_name') self.client.method.assert_called_with('rooms/show', method='GET', parameters={'room_id': 2}) self.assertEqual(room.get('room_id'), 2) def test_get_room_information(self): """ Get room info >>> get_rooms('room_name') {'name': 'room_name', 'room_id': 'room id'} """ self.client.get_rooms = Mock(return_value=[{'name': 'test', 'room_id': 1}, {'name': 'test1', 'room_id': 2}]) room = self.client.get_room_information('test1') self.assertEqual(room.get('room_id'), 2) def test_get_participants(self): """ Get participants >> get_participants() [participants list] """ self.client.get_extended_room_information = Mock(return_value={'name': 'test1', 'room_id': 2, 'participants': ['test']}) participants = self.client.get_participants() self.client.get_extended_room_information.assert_called_with('room_name') self.assertEqual(len(participants), 1)
elif not participants and olark_state == "connected": print "Disconnecting from olark..." self.olark_client.disconnect() # wait to check again Hipchat participants time.sleep(self.sleep_delay) def stop(self): self.running = False if __name__ == '__main__': # create the queue queue = Queue.Queue(maxsize=0) # create clients for Olark and HipChat hipchat_client = HipChat(queue, config.HIPCHAT_TOKEN, config.HIPCHAT_ROOMNAME) hipchat_client.start() olark_client = Olark(queue, config.OLARK_USERNAME, config.OLARK_PASSWORD, config.HIPCHAT_ROOMNAME) # create the watch dog client = OlarkHipchat(config.HIPCHAT_SLEEPDELAY, hipchat_client, olark_client) client.start() # CTRL + C -> quit the application def handler(signum, frame): client.stop() try: olark_client.abort() except: pass
def setUp(self): self.room_name = 'room_name' self.token = 'token' self.queue = Mock() self.client = HipChat(self.queue, self.token, self.room_name)