def test_fetch_from_date(self):
        """Test when fetching messages from a given date"""

        setup_http_server(from_date=True)
        from_date = datetime.datetime(2020,
                                      5,
                                      3,
                                      0,
                                      0,
                                      tzinfo=dateutil.tz.tzutc())

        backend = RocketChat(url='https://open.rocket.chat',
                             user_id='123user',
                             api_token='aaa',
                             channel='testapichannel')

        messages = [m for m in backend.fetch(from_date=from_date)]
        self.assertEqual(len(messages), 1)

        message = messages[0]
        self.assertEqual(message['data']['_id'], 'p5dQSb48W25EimhJK')
        self.assertEqual(message['origin'],
                         'https://open.rocket.chat/testapichannel')
        self.assertEqual(message['uuid'],
                         'a3b2a4c195e8b6a155cf5eddb2b8f79a13f836dd')
        self.assertEqual(message['updated_on'], 1588491123.587)
        self.assertEqual(message['category'], 'message')
        self.assertEqual(message['tag'],
                         'https://open.rocket.chat/testapichannel')
        self.assertEqual(message['data']['msg'], 'Test message 2')
        self.assertEqual(message['data']['u']['username'], 'animesh_username1')
        self.assertEqual(message['data']['u']['name'], 'Animesh Kumar')
        self.assertEqual(message['data']['channel_info']['_id'],
                         'wyJHNAtuPGnQCT5xP')
        self.assertEqual(message['data']['channel_info']['usersCount'], 2)
    def test_fetch_empty(self):
        """Test whether an empty list is returned when there are no messages"""

        setup_http_server(no_message=True)

        backend = RocketChat(url='https://open.rocket.chat',
                             user_id='123user',
                             api_token='aaa',
                             channel='testapichannel')
        messages = [m for m in backend.fetch()]
        self.assertListEqual(messages, [])
 def setUp(self):
     super().setUp()
     self.backend_write_archive = RocketChat(url='https://open.rocket.chat',
                                             user_id='123user',
                                             api_token='aaa',
                                             channel='testapichannel',
                                             max_items=5,
                                             archive=self.archive)
     self.backend_read_archive = RocketChat(url='https://open.rocket.chat',
                                            user_id='123user',
                                            api_token='aaa',
                                            channel='testapichannel',
                                            max_items=5,
                                            archive=self.archive)
    def test_search_fields_messages(self):
        """Test whether the search_fields is properly set"""

        setup_http_server()

        backend = RocketChat(url='https://open.rocket.chat',
                             user_id='123user',
                             api_token='aaa',
                             channel='testapichannel')
        messages = [m for m in backend.fetch()]

        message = messages[0]
        self.assertEqual(message['search_fields']['item_id'],
                         backend.metadata_id(message['data']))
        self.assertEqual(message['search_fields']['channel_id'],
                         'wyJHNAtuPGnQCT5xP')
        self.assertEqual(message['search_fields']['channel_name'],
                         'testapichannel')
    def test_fetch_messages(self):
        """Test whether a list of messages is returned"""

        setup_http_server()

        backend = RocketChat(url='https://open.rocket.chat',
                             user_id='123user',
                             api_token='aaa',
                             channel='testapichannel')
        messages = [m for m in backend.fetch()]

        self.assertEqual(len(messages), 4)

        message = messages[0]
        self.assertEqual(message['data']['_id'], '4AwA2eJQ7xBgPZ4mv')
        self.assertEqual(message['origin'],
                         'https://open.rocket.chat/testapichannel')
        self.assertEqual(message['uuid'],
                         '888b6c9a728c267435cee1d5fe8f5dbe446614a2')
        self.assertEqual(message['updated_on'], 1588404686.164)
        self.assertEqual(message['category'], 'message')
        self.assertEqual(message['tag'],
                         'https://open.rocket.chat/testapichannel')
        self.assertEqual(message['data']['msg'], 'Test message 1')
        self.assertEqual(message['data']['u']['username'], 'animesh_username1')
        self.assertEqual(message['data']['u']['name'], 'Animesh Kumar')
        self.assertListEqual(message['data']['replies'], ["567user"])
        self.assertEqual(message['data']['channel_info']['_id'],
                         'wyJHNAtuPGnQCT5xP')
        self.assertEqual(message['data']['channel_info']['lastMessage']['msg'],
                         'Test message 2')
        self.assertEqual(message['data']['channel_info']['usersCount'], 2)

        message = messages[1]
        self.assertEqual(message['data']['_id'], 'zofFonHMq5M3tdGyu')
        self.assertEqual(message['origin'],
                         'https://open.rocket.chat/testapichannel')
        self.assertEqual(message['uuid'],
                         '6fa3f6a18491b9bcb3309f12e3a2b1cd654980c3')
        self.assertEqual(message['updated_on'], 1588404716.711)
        self.assertEqual(message['category'], 'message')
        self.assertEqual(message['tag'],
                         'https://open.rocket.chat/testapichannel')
        self.assertEqual(message['data']['msg'], 'Test reply 1')
        self.assertEqual(message['data']['u']['username'], 'animesh_username2')
        self.assertEqual(message['data']['u']['name'], 'Animesh Kumar Singh')
        self.assertEqual(message['data']['channel_info']['_id'],
                         'wyJHNAtuPGnQCT5xP')
        self.assertEqual(message['data']['channel_info']['lastMessage']['msg'],
                         'Test message 2')
        self.assertEqual(message['data']['channel_info']['usersCount'], 2)

        message = messages[2]
        self.assertEqual(message['data']['_id'], 'WnWSwiD877xRpqcMb')
        self.assertEqual(message['origin'],
                         'https://open.rocket.chat/testapichannel')
        self.assertEqual(message['uuid'],
                         '3b3afa62a63766ebeb70e3c8951c2c4b42f34767')
        self.assertEqual(message['updated_on'], 1588404716.714)
        self.assertEqual(message['category'], 'message')
        self.assertEqual(message['tag'],
                         'https://open.rocket.chat/testapichannel')
        self.assertEqual(message['data']['msg'], 'Test reply 1 edited')
        self.assertEqual(message['data']['u']['username'], 'animesh_username2')
        self.assertEqual(message['data']['u']['name'], 'Animesh Kumar Singh')
        self.assertEqual(message['data']['editedBy']['username'],
                         'animesh_username1')
        self.assertEqual(message['data']['channel_info']['_id'],
                         'wyJHNAtuPGnQCT5xP')
        self.assertEqual(message['data']['channel_info']['usersCount'], 2)

        message = messages[3]
        self.assertEqual(message['data']['_id'], 'p5dQSb48W25EimhJK')
        self.assertEqual(message['origin'],
                         'https://open.rocket.chat/testapichannel')
        self.assertEqual(message['uuid'],
                         'a3b2a4c195e8b6a155cf5eddb2b8f79a13f836dd')
        self.assertEqual(message['updated_on'], 1588491123.587)
        self.assertEqual(message['category'], 'message')
        self.assertEqual(message['tag'],
                         'https://open.rocket.chat/testapichannel')
        self.assertEqual(message['data']['msg'], 'Test message 2')
        self.assertEqual(message['data']['u']['username'], 'animesh_username1')
        self.assertEqual(message['data']['u']['name'], 'Animesh Kumar')
        self.assertEqual(message['data']['channel_info']['_id'],
                         'wyJHNAtuPGnQCT5xP')
        self.assertEqual(message['data']['channel_info']['usersCount'], 2)
    def test_has_resuming(self):
        """Test if it returns True when has_resuming is called"""

        self.assertEqual(RocketChat.has_resuming(), True)
    def test_initialization(self):
        """Test whether attributes are initialized"""

        backend = RocketChat(url='https://chat.example.com',
                             user_id='123user',
                             api_token='aaa',
                             channel='testapichannel',
                             tag='test')

        self.assertEqual(backend.url, 'https://chat.example.com')
        self.assertEqual(backend.user_id, '123user')
        self.assertEqual(backend.api_token, "aaa")
        self.assertEqual(backend.channel, "testapichannel")
        self.assertEqual(backend.min_rate_to_sleep, MIN_RATE_LIMIT)
        self.assertIsNone(backend.client)
        self.assertFalse(backend.sleep_for_rate)
        self.assertTrue(backend.ssl_verify)
        self.assertEqual(backend.origin,
                         'https://chat.example.com/testapichannel')
        self.assertEqual(backend.tag, 'test')
        self.assertEqual(backend.max_items, MAX_ITEMS)

        # When tag is empty or None it will be set to
        # the value in URL
        backend = RocketChat(url="https://chat.example.com",
                             user_id="123user",
                             api_token='aaa',
                             channel='testapichannel',
                             tag=None)
        self.assertEqual(backend.origin,
                         'https://chat.example.com/testapichannel')
        self.assertEqual(backend.tag,
                         'https://chat.example.com/testapichannel')

        backend = RocketChat(url="https://chat.example.com",
                             user_id="123user",
                             api_token='aaa',
                             channel='testapichannel',
                             tag='')
        self.assertEqual(backend.origin,
                         'https://chat.example.com/testapichannel')
        self.assertEqual(backend.tag,
                         'https://chat.example.com/testapichannel')

        backend = RocketChat(url='https://chat.example.com',
                             user_id='123user',
                             api_token='aaa',
                             channel='testapichannel',
                             tag='',
                             sleep_for_rate=True,
                             ssl_verify=False,
                             max_items=20,
                             min_rate_to_sleep=1)
        self.assertEqual(backend.origin,
                         'https://chat.example.com/testapichannel')
        self.assertEqual(backend.tag,
                         'https://chat.example.com/testapichannel')
        self.assertFalse(backend.ssl_verify)
        self.assertTrue(backend.sleep_for_rate)
        self.assertEqual(backend.max_items, 20)
        self.assertEqual(backend.min_rate_to_sleep, 1)