Example #1
0
 def test_missing_filter_action(self):
     config_filename = '.maildaemon.config.tmp'
     with open(config_filename, 'w') as cfg_file:
         print(r'''{"filters": {"empty": {}}}''', file=cfg_file)
     with self.assertRaises(ValueError):
         load_config(config_filename)
     os.remove(config_filename)
Example #2
0
 def test_missing_domain(self):
     config_filename = '.maildaemon.config.tmp'
     with open(config_filename, 'w') as cfg_file:
         print(
             r'''{"connections": {"missing-protocol": {"protocol": "IMAP"}}}''',
             file=cfg_file)
     with self.assertRaises(ValueError):
         load_config(config_filename)
     os.remove(config_filename)
Example #3
0
class Tests(unittest.TestCase):

    config = load_config(_TEST_CONFIG_PATH)

    def test_connection(self):
        conns = {
            'test-imap': self.config['connections']['test-imap'],
            'test-imap-ssl': self.config['connections']['test-imap-ssl'],
            'test-pop': self.config['connections']['test-pop'],
            'test-pop-ssl': self.config['connections']['test-pop-ssl']
        }
        connections = ConnectionGroup.from_dict(conns)
        daemons = DaemonGroup(connections, [])
        self.assertEqual(len(daemons), 4)

    def test_run(self):
        conns = {
            'test-imap': self.config['connections']['test-imap'],
            'test-imap-ssl': self.config['connections']['test-imap-ssl'],
            'test-pop': self.config['connections']['test-pop'],
            'test-pop-ssl': self.config['connections']['test-pop-ssl']
        }
        connections = ConnectionGroup.from_dict(conns)
        daemons = DaemonGroup(connections, [])
        self.assertEqual(len(daemons), 4)
Example #4
0
 def test_completeness(self):
     cfg = load_config(_TEST_CONFIG_PATH)
     self.assertIn('test-imap', cfg['connections'], msg=cfg)
     section = cfg['connections']['test-imap']
     self.assertIn('domain', section, msg=cfg)
     self.assertIn('ssl', section, msg=cfg)
     self.assertIn('port', section, msg=cfg)
     self.assertIn('login', section, msg=cfg)
     self.assertIn('password', section, msg=cfg)
Example #5
0
class Tests(unittest.TestCase):

    config = load_config(_TEST_CONFIG_PATH)

    def test_construct(self):
        def func1(_: str):
            return True

        def func2(_: t.Any):
            return

        conn = None  # type: Connection
        connections = [conn]
        msg_filter = MessageFilter(connections, [[('aa', func1)]], [func2])
        self.assertIsNotNone(msg_filter)

    @unittest.skipUnless(
        os.environ.get('TEST_COMM') or os.environ.get('CI'),
        'skipping test that requires server connection')
    def test_from_config(self):
        connection = IMAPConnection.from_dict(
            self.config['connections']['test-imap'])
        filter_ = MessageFilter.from_dict(
            self.config['filters']['facebook-notification'],
            {'test-imap': connection})
        self.assertIsNotNone(filter_)

    @unittest.skipUnless(
        os.environ.get('TEST_COMM') or os.environ.get('CI'),
        'skipping test that requires server connection')
    def test_if_applies(self):
        connection = IMAPCache.from_dict(
            self.config['connections']['test-imap'])
        connection.connect()
        ids = connection.retrieve_message_ids()
        msg = connection.retrieve_message(ids[0])
        connection.disconnect()
        _LOG.debug('%s', msg.from_address)
        _LOG.debug('%s', msg.subject)

        filter_ = MessageFilter.from_dict(
            self.config['filters']['facebook-notification'],
            {'test-imap': connection})
        result = filter_.applies_to(msg)
        _LOG.debug('Does filter apply? %s', result)
        self.assertIsInstance(result, bool, msg=(filter_, msg))
        self.assertFalse(result)

        filter_ = MessageFilter.from_dict(
            self.config['filters']['test-message'], {'test-imap': connection})
        result = filter_.applies_to(msg)
        _LOG.debug('Does filter apply? %s', result)
        self.assertIsInstance(result, bool, msg=(filter_, msg))
        self.assertTrue(result)
Example #6
0
class Tests(unittest.TestCase):

    config = load_config(_TEST_CONFIG_PATH)

    def test_update(self):
        for connection_name in ['test-pop', 'test-pop-ssl']:
            with self.subTest(msg=connection_name):
                connection = POPCache.from_dict(
                    self.config['connections'][connection_name])
                connection.connect()
                connection.update()
                connection.disconnect()
                self.assertIn('INBOX', connection.folders, msg=connection)
                self.assertGreater(len(connection.folders['INBOX'].messages),
                                   0,
                                   msg=connection)
Example #7
0
 def test_from_email_message(self):
     config = load_config(_TEST_CONFIG_PATH)
     folder = 'INBOX'
     connection = IMAPConnection.from_dict(
         config['connections']['test-imap-ssl'])
     connection.connect()
     connection.open_folder(folder)
     ids = connection.retrieve_message_ids()
     messages_data = connection.retrieve_messages_parts(
         ids[:1], ['BODY.PEEK[]'])
     connection.disconnect()
     _, body = messages_data[0]
     message = Message(email.message_from_bytes(body), connection, folder,
                       1)
     _LOG.debug('%s', message.from_address)
     _LOG.debug('%s', message.subject)
     self.assertGreater(len(message.from_address), 0)
     self.assertGreater(len(str(message.subject)), 0)
class Tests(unittest.TestCase):

    config = load_config(_TEST_CONFIG_PATH)

    def test_connect(self):
        smtp = SMTPConnection.from_dict(
            self.config['connections']['test-smtp'])
        smtp.connect()
        smtp.disconnect()

    def test_connect_ssl(self):
        smtp = SMTPConnection.from_dict(
            self.config['connections']['test-smtp-ssl'])
        smtp.connect()
        smtp.disconnect()

    def test_send_message(self):
        with _HERE.joinpath('message1.txt').open() as email_file:
            message = email.message_from_file(email_file)

        smtp = SMTPConnection.from_dict(
            self.config['connections']['test-smtp'])
        smtp.connect()
        for _ in range(5):
            smtp.send_message(message)
        message['To'] = '*****@*****.**'
        for _ in range(5):
            smtp.send_message(message)
        smtp.disconnect()

    def test_send_message_ssl(self):
        with _HERE.joinpath('message2.txt').open() as email_file:
            message = email.message_from_file(email_file)

        smtp = SMTPConnection.from_dict(
            self.config['connections']['test-smtp-ssl'])
        smtp.connect()
        for _ in range(5):
            smtp.send_message(message)
        message['To'] = '*****@*****.**'
        for _ in range(5):
            smtp.send_message(message)
        smtp.disconnect()
class Tests(unittest.TestCase):

    config = load_config(_TEST_CONFIG_PATH)

    def test_connection(self):
        conns = {'test-imap': self.config['connections']['test-imap'],
                 'test-imap-ssl': self.config['connections']['test-imap-ssl'],
                 'test-pop-ssl': self.config['connections']['test-pop-ssl']}
        connections = ConnectionGroup.from_dict(conns)
        self.assertEqual(len(connections), 3)
        connections.connect_all()
        connections.disconnect_all()

    def test_purge_dead(self):
        conns = {'test-imap-ssl': self.config['connections']['test-imap-ssl'],
                 'test-pop': self.config['connections']['test-pop'],
                 'test-pop-ssl': self.config['connections']['test-pop-ssl']}
        connections = ConnectionGroup.from_dict(conns)
        self.assertEqual(len(connections), 3)
        connections.connect_all()
        connections.purge_dead()
        connections.disconnect_all()
Example #10
0
class Tests(unittest.TestCase):

    config = load_config(_TEST_CONFIG_PATH)

    def test_update_folders(self):
        for connection_name in ['test-imap', 'test-imap-ssl']:
            with self.subTest(msg=connection_name):
                c = IMAPCache.from_dict(self.config['connections'][connection_name])
                c.connect()
                c.update_folders()
                # folder = c.folders['']
                # c.delete_folder(folder)  # TODO: implement IMAP folder deletion
                c.update_folders()
                c.disconnect()

    def test_update(self):
        for connection_name in ['test-imap', 'test-imap-ssl']:
            with self.subTest(msg=connection_name):
                # import time; time.sleep(2)
                c = IMAPCache.from_dict(self.config['connections'][connection_name])
                c.connect()
                # c.update()  # TODO: there's some cryptic error in msg id 12 in INBOX
                c.disconnect()
Example #11
0
class Tests(unittest.TestCase):

    config = load_config(_TEST_CONFIG_PATH)

    def test_retrieve_message_ids(self):
        for connection_name in ['test-pop', 'test-pop-ssl']:
            with self.subTest(msg=connection_name):
                connection = POPConnection.from_dict(
                    self.config['connections'][connection_name])
                connection.connect()
                ids = connection.retrieve_message_ids()
                alive = connection.is_alive()
                connection.disconnect()
                self.assertIsInstance(ids, list, msg=connection)
                self.assertTrue(alive, msg=connection)

    def test_retrieve_message_lines(self):
        for connection_name in ['test-pop', 'test-pop-ssl']:
            with self.subTest(msg=connection_name):
                connection = POPConnection.from_dict(
                    self.config['connections'][connection_name])
                connection.connect()
                lines = connection.retrieve_message_lines(1)
                self.assertGreater(len(lines), 0, msg=connection)
Example #12
0
class Tests(unittest.TestCase):

    config = load_config(_TEST_CONFIG_PATH)

    def test_retrieve_folders(self):
        for connection_name in ['test-imap', 'test-imap-ssl']:
            with self.subTest(msg=connection_name):
                connection = IMAPConnection.from_dict(
                    self.config['connections'][connection_name])
                connection.connect()
                folders = connection.retrieve_folders()
                connection.disconnect()
                self.assertGreater(len(folders), 0, msg=connection)
                self.assertIn('INBOX', folders, msg=connection)

    def test_retrieve_message_ids(self):
        for connection_name in ['test-imap', 'test-imap-ssl']:
            with self.subTest(msg=connection_name):
                connection = IMAPConnection.from_dict(
                    self.config['connections'][connection_name])
                connection.connect()
                connection.open_folder('INBOX')
                ids = connection.retrieve_message_ids('INBOX')
                connection.disconnect()
                self.assertIsInstance(ids, list, msg=type(ids))
                for id_ in ids:
                    self.assertIsInstance(id_, int, msg=ids)

    def test_retrieve_messages_parts(self):
        for connection_name in ['test-imap', 'test-imap-ssl']:
            with self.subTest(msg=connection_name):
                connection = IMAPConnection.from_dict(
                    self.config['connections'][connection_name])
                connection.connect()
                connection.open_folder()
                ids = connection.retrieve_message_ids()
                msgs1 = connection.retrieve_messages_parts(
                    ids[:2], ['UID', 'ENVELOPE'])
                msgs2 = connection.retrieve_messages_parts(
                    ids[:2], ['BODY.PEEK[]'])
                connection.close_folder()
                alive = connection.is_alive()
                connection.disconnect()
                for env, msg in msgs1:
                    # print('uid+envelope', len(env), len(msg) if isinstance(msg, bytes) else msg)
                    self.assertGreater(len(env), 0, msg=msgs1)
                    self.assertIsNone(msg, msg=msgs1)
                for env, msg in msgs2:
                    # print('body', len(env), len(msg) if isinstance(msg, bytes) else msg)
                    self.assertGreater(len(env), 0, msg=msgs2)
                    self.assertGreater(len(msg), 0, msg=msgs2)
                self.assertTrue(alive, msg=connection)

    def test_delete_message(self):
        connection = IMAPConnection.from_dict(
            self.config['connections']['test-imap-ssl'])
        connection.connect()
        ids = connection.retrieve_message_ids()
        connection.delete_message(ids[-1], 'INBOX')
        connection.purge_deleted_messages()
        # with self.assertRaises(RuntimeError):
        #     connection.delete_message(ids[-1], 'INBOX')
        connection.delete_message(ids[-2], 'INBOX', purge_immediately=True)
        connection.disconnect()

    @unittest.skip('long')
    def test_timeout(self):

        c = IMAPConnection.from_dict(self.config['connections']['gmail-imap'])

        _LOG.debug('sleeping for 5m20s...')
        time.sleep(5 * 60 + 20)
        _LOG.debug('finished sleeping')

        c.connect()
        # works after 1m, 2m, 5m, 5m15s
        # doesn't work after 5m20s, 5m30s, 6m, 7m, 9m, 10m, 15m

    @unittest.skip('long')
    def test_timeout_after_connect(self):

        c = IMAPConnection.from_dict(self.config['connections']['gmail-imap'])
        c.connect()

        _LOG.debug('sleeping for 5 minutes...')
        time.sleep(5 * 60)
        _LOG.debug('finished sleeping')

        self.assertTrue(c.is_alive())

        _LOG.debug('sleeping for 5.5 minutes...')
        time.sleep(5.5 * 60)
        _LOG.debug('finished sleeping')

        self.assertFalse(c.is_alive())
Example #13
0
 def test_load_concrete_connections(self):
     cfg = load_config(_TEST_CONFIG_PATH)
     self.assertIn('connections', cfg, msg=cfg)
     self.assertGreater(len(cfg['connections']), 0, msg=cfg)
     self.assertIn('test-imap', cfg['connections'], msg=cfg)
     self.assertIn('test-pop', cfg['connections'], msg=cfg)
Example #14
0
 def test_load_basic(self):
     cfg = load_config(_TEST_CONFIG_PATH)
     self.assertIn('connections', cfg, msg=cfg)
     self.assertGreater(len(cfg['connections']), 0, msg=cfg)