def test_fetch_from_empty_cache(self):
        """Test if there are not any message returned when the cache is empty"""

        cache = Cache(self.tmp_path)
        tlg = Telegram(TELEGRAM_BOT, TELEGRAM_TOKEN, cache=cache)
        cached_messages = [msg for msg in tlg.fetch_from_cache()]
        self.assertEqual(len(cached_messages), 0)
Exemple #2
0
    def test_initialization(self):
        """Test whether attributes are initializated"""

        origin = 'https://telegram.org/' + TELEGRAM_BOT

        tlg = Telegram(TELEGRAM_BOT, TELEGRAM_TOKEN, tag='test')

        self.assertEqual(tlg.bot, 'mybot')
        self.assertEqual(tlg.origin, origin)
        self.assertEqual(tlg.tag, 'test')
        self.assertIsNone(tlg.client)
        self.assertTrue(tlg.ssl_verify)

        # When tag is empty or None it will be set to
        # the value in url
        tlg = Telegram(TELEGRAM_BOT, TELEGRAM_TOKEN, ssl_verify=False)
        self.assertEqual(tlg.bot, TELEGRAM_BOT)
        self.assertEqual(tlg.origin, origin)
        self.assertEqual(tlg.tag, origin)
        self.assertFalse(tlg.ssl_verify)

        tlg = Telegram(TELEGRAM_BOT, TELEGRAM_TOKEN, tag='')
        self.assertEqual(tlg.bot, TELEGRAM_BOT)
        self.assertEqual(tlg.origin, origin)
        self.assertEqual(tlg.tag, origin)
Exemple #3
0
    def test_fetch_from_offset(self):
        """Test whether it fetches and parses messages from the given offset"""

        http_requests = setup_http_server()

        tlg = Telegram(TELEGRAM_BOT, TELEGRAM_TOKEN)
        messages = [msg for msg in tlg.fetch(offset=319280321)]

        self.assertEqual(len(messages), 1)

        msg = messages[0]
        self.assertEqual(msg['data']['message']['message_id'], 34)
        self.assertEqual(msg['origin'], 'https://telegram.org/' + TELEGRAM_BOT)
        self.assertEqual(msg['uuid'],
                         '2e61e72b64c9084f3c5a36671c3119641c3ae42f')
        self.assertEqual(msg['updated_on'], 1467370372.0)
        self.assertEqual(msg['offset'], 319280321)
        self.assertEqual(msg['category'], 'message')
        self.assertEqual(msg['tag'], 'https://telegram.org/' + TELEGRAM_BOT)

        # Check requests
        expected = [{'offset': ['319280321']}, {'offset': ['319280322']}]

        self.assertEqual(len(http_requests), len(expected))

        for i in range(len(expected)):
            self.assertDictEqual(http_requests[i].querystring, expected[i])
    def test_fetch_from_non_set_cache(self):
        """Test if a error is raised when the cache was not set"""

        tlg = Telegram(TELEGRAM_BOT, TELEGRAM_TOKEN)

        with self.assertRaises(CacheError):
            _ = [msg for msg in tlg.fetch_from_cache()]
    def test_fetch_by_chats(self):
        """Test if it returns only those messages that belong to the given chats"""

        _ = setup_http_server()

        tlg = Telegram(TELEGRAM_BOT, TELEGRAM_TOKEN)

        chats = [8, -1]
        messages = [msg for msg in tlg.fetch(chats=chats)]

        self.assertEqual(len(messages), 3)

        expected = [(31, '5a5457aec04237ac3fab30031e84c745a3bdd157', 1467289325.0, 319280318),
                    (33, '9d03eeea7e3186ca8e5c150b4cbf18c8283cca9d', 1467289371.0, 319280320),
                    (34, '2e61e72b64c9084f3c5a36671c3119641c3ae42f', 1467370372.0, 319280321)]

        for x in range(len(messages)):
            message = messages[x]
            self.assertEqual(message['data']['message']['message_id'], expected[x][0])
            self.assertEqual(message['origin'], 'https://telegram.org/' + TELEGRAM_BOT)
            self.assertEqual(message['uuid'], expected[x][1])
            self.assertEqual(message['updated_on'], expected[x][2])
            self.assertEqual(message['offset'], expected[x][3])
            self.assertEqual(message['category'], 'message')
            self.assertEqual(message['tag'], 'https://telegram.org/' + TELEGRAM_BOT)

        # Empty list of chats will return no messages
        chats = []
        messages = [msg for msg in tlg.fetch(chats=chats)]

        self.assertEqual(len(messages), 0)
Exemple #6
0
 def setUp(self):
     super().setUp()
     self.backend_write_archive = Telegram(TELEGRAM_BOT,
                                           TELEGRAM_TOKEN,
                                           archive=self.archive)
     self.backend_read_archive = Telegram(TELEGRAM_BOT,
                                          "another-token",
                                          archive=self.archive)
Exemple #7
0
    def test_fetch_empty(self):
        """Test whether it works when there are no messages to fetch"""

        http_requests = setup_http_server()

        tlg = Telegram(TELEGRAM_BOT, TELEGRAM_TOKEN)
        messages = [msg for msg in tlg.fetch(offset=319280324)]

        self.assertEqual(len(messages), 0)

        # Check requests
        self.assertEqual(len(http_requests), 1)

        self.assertDictEqual(http_requests[0].querystring,
                             {'offset': ['319280324']})
Exemple #8
0
    def test_fetch(self):
        """Test whether a list of messages is returned"""

        http_requests = setup_http_server()

        tlg = Telegram(TELEGRAM_BOT, TELEGRAM_TOKEN)
        messages = [msg for msg in tlg.fetch(offset=None)]

        expected = [(31, '5a5457aec04237ac3fab30031e84c745a3bdd157',
                     1467289325.0, 319280318),
                    (32, '16a59e93e919174fcd4e70e5b3289201c1016c72',
                     1467289329.0, 319280319),
                    (33, '9d03eeea7e3186ca8e5c150b4cbf18c8283cca9d',
                     1467289371.0, 319280320),
                    (34, '2e61e72b64c9084f3c5a36671c3119641c3ae42f',
                     1467370372.0, 319280321)]

        self.assertEqual(len(messages), len(expected))

        for x in range(len(messages)):
            message = messages[x]
            self.assertEqual(message['data']['message']['message_id'],
                             expected[x][0])
            self.assertEqual(message['origin'],
                             'https://telegram.org/' + TELEGRAM_BOT)
            self.assertEqual(message['uuid'], expected[x][1])
            self.assertEqual(message['updated_on'], expected[x][2])
            self.assertEqual(message['offset'], expected[x][3])
            self.assertEqual(message['category'], 'message')
            self.assertEqual(message['tag'],
                             'https://telegram.org/' + TELEGRAM_BOT)

        # Check requests
        expected = [{
            'offset': ['1']
        }, {
            'offset': ['319280321']
        }, {
            'offset': ['319280322']
        }]

        self.assertEqual(len(http_requests), len(expected))

        for i in range(len(expected)):
            self.assertDictEqual(http_requests[i].querystring, expected[i])
Exemple #9
0
    def test_fetch_from_cache(self):
        """Test whether the cache works"""

        http_requests = setup_http_server()

        # First, we fetch the messages from the server,
        # storing them in a cache
        cache = Cache(self.tmp_path)
        tlg = Telegram(TELEGRAM_BOT, TELEGRAM_TOKEN, cache=cache)

        messages = [msg for msg in tlg.fetch()]
        self.assertEqual(len(http_requests), 3)

        # Now, we get the messages from the cache.
        # The contents should be the same and there won't be
        # any new request to the server
        cached_messages = [msg for msg in tlg.fetch_from_cache()]
        self.assertEqual(len(cached_messages), len(messages))

        expected = [(31, '5a5457aec04237ac3fab30031e84c745a3bdd157',
                     1467289325.0, 319280318),
                    (32, '16a59e93e919174fcd4e70e5b3289201c1016c72',
                     1467289329.0, 319280319),
                    (33, '9d03eeea7e3186ca8e5c150b4cbf18c8283cca9d',
                     1467289371.0, 319280320),
                    (34, '2e61e72b64c9084f3c5a36671c3119641c3ae42f',
                     1467370372.0, 319280321)]

        self.assertEqual(len(cached_messages), len(expected))

        for x in range(len(cached_messages)):
            message = cached_messages[x]
            self.assertEqual(message['data']['message']['message_id'],
                             expected[x][0])
            self.assertEqual(message['origin'],
                             'https://telegram.org/' + TELEGRAM_BOT)
            self.assertEqual(message['uuid'], expected[x][1])
            self.assertEqual(message['updated_on'], expected[x][2])
            self.assertEqual(message['offset'], expected[x][3])
            self.assertEqual(message['category'], 'message')
            self.assertEqual(message['tag'],
                             'https://telegram.org/' + TELEGRAM_BOT)

        # No more requests were sent
        self.assertEqual(len(http_requests), 3)
    def test_parse_messages(self):
        """Test whether the method parses a raw file"""

        body_msgs = read_file('data/telegram_messages.json')
        body_msgs_empty = read_file('data/telegram_messages_empty.json')

        messages = Telegram.parse_messages(body_msgs)
        result = [msg for msg in messages]

        self.assertEqual(len(result), 3)
        self.assertEqual(result[0]['message']['message_id'], 31)
        self.assertEqual(result[1]['message']['message_id'], 32)
        self.assertEqual(result[2]['message']['message_id'], 33)

        messages = Telegram.parse_messages(body_msgs_empty)
        result = [msg for msg in messages]

        self.assertEqual(len(result), 0)
Exemple #11
0
    def test_fetch_from_offset(self):
        """Test whether it fetches and parses messages from the given offset"""

        http_requests = setup_http_server()

        tlg = Telegram(TELEGRAM_BOT, TELEGRAM_TOKEN)
        messages = [msg for msg in tlg.fetch(offset=319280321)]

        expected = [(34, '2e61e72b64c9084f3c5a36671c3119641c3ae42f',
                     1467370372.0, 319280321),
                    (35, '563e8e9d3f477cbfd04eef0af3a72af51ab6d72b',
                     1467370373.0, 319280322),
                    (35, '563e8e9d3f477cbfd04eef0af3a72af51ab6d72b',
                     1467370374.0, 319280323)]

        self.assertEqual(len(messages), len(expected))

        for x in range(len(messages)):
            message = messages[x]
            self.assertEqual(message['data']['message']['message_id'],
                             expected[x][0])
            self.assertEqual(message['origin'],
                             'https://telegram.org/' + TELEGRAM_BOT)
            self.assertEqual(message['uuid'], expected[x][1])
            self.assertEqual(message['updated_on'], expected[x][2])
            self.assertEqual(message['offset'], expected[x][3])
            self.assertEqual(message['category'], 'message')
            self.assertEqual(message['tag'],
                             'https://telegram.org/' + TELEGRAM_BOT)

        # Check requests
        expected = [{'offset': ['319280321']}, {'offset': ['319280324']}]

        self.assertEqual(len(http_requests), len(expected))

        for i in range(len(expected)):
            self.assertDictEqual(http_requests[i].querystring, expected[i])
Exemple #12
0
    def test_search_fields(self):
        """Test whether the search_fields is properly set"""

        setup_http_server()

        tlg = Telegram(TELEGRAM_BOT, TELEGRAM_TOKEN)
        messages = [msg for msg in tlg.fetch(offset=None)]

        message = messages[0]
        self.assertEqual(tlg.metadata_id(message['data']),
                         message['search_fields']['item_id'])
        self.assertEqual(message['data']['message']['chat']['title'],
                         'Testing Telegram Bots')
        self.assertEqual(message['data']['message']['chat']['title'],
                         message['search_fields']['chat_name'])
        self.assertEqual(message['data']['message']['chat']['id'], -1)
        self.assertEqual(message['data']['message']['chat']['id'],
                         message['search_fields']['chat_id'])

        message = messages[1]
        self.assertEqual(tlg.metadata_id(message['data']),
                         message['search_fields']['item_id'])
        self.assertEqual(message['data']['message']['chat']['title'],
                         'A new group')
        self.assertEqual(message['data']['message']['chat']['title'],
                         message['search_fields']['chat_name'])
        self.assertEqual(message['data']['message']['chat']['id'], -2)
        self.assertEqual(message['data']['message']['chat']['id'],
                         message['search_fields']['chat_id'])

        message = messages[2]
        self.assertEqual(tlg.metadata_id(message['data']),
                         message['search_fields']['item_id'])
        self.assertEqual(message['data']['message']['chat']['title'],
                         'Testing Telegram Bots')
        self.assertEqual(message['data']['message']['chat']['title'],
                         message['search_fields']['chat_name'])
        self.assertEqual(message['data']['message']['chat']['id'], -1)
        self.assertEqual(message['data']['message']['chat']['id'],
                         message['search_fields']['chat_id'])

        message = messages[3]
        self.assertEqual(tlg.metadata_id(message['data']),
                         message['search_fields']['item_id'])
        self.assertNotIn('title', message['data']['message']['chat'])
        self.assertEqual(message['data']['message']['chat']['id'], 8)
        self.assertEqual(message['data']['message']['chat']['id'],
                         message['search_fields']['chat_id'])
Exemple #13
0
    def test_has_resuming(self):
        """Test if it returns True when has_resuming is called"""

        self.assertEqual(Telegram.has_resuming(), True)
Exemple #14
0
 def setUp(self):
     super().setUp()
     self.backend = Telegram(TELEGRAM_BOT,
                             TELEGRAM_TOKEN,
                             archive=self.archive)
Exemple #15
0
    def test_has_caching(self):
        """Test if it returns False when has_caching is called"""

        self.assertEqual(Telegram.has_caching(), False)