Пример #1
0
def main():
    config = loadConfig('settings.conf')

    HOST = config.get('IMAP', 'HOST')
    USERNAME = config.get('IMAP', 'USERNAME')
    FOLDER = config.get('IMAP', 'FOLDER')
    PASSWORD = getpass.getpass()

    imap_client = ImapClient(HOST,
                             USERNAME,
                             PASSWORD,
                             folder=FOLDER,
                             )
    email_addrs = sorted(list(imap_client.get_email_addresses()))

    for addr in email_addrs:
        print(addr)
    def test_zip_with_date(self):
        filename = 'test.zip'

        expected = 'test_e3b0c4.zip'
        actual = ImapClient._unique_filename(filename, date='1-17-18')

        assert expected == actual
        self.mock_sha256.return_value.update.assert_has_calls([
            mock.call(b''),
            mock.call(b'1-17-18'),
        ])
        self.mock_sha256.return_value.hexdigest.assert_called_once_with()
    def test_tar_gz_no_from_addr(self):
        filename = 'test.tar.gz'

        expected = 'test_e3b0c4.tar.gz'
        actual = ImapClient._unique_filename(filename)

        assert expected == actual
        self.mock_sha256.return_value.update.assert_has_calls([
            mock.call(b''),
            mock.call(b''),
        ])
        self.mock_sha256.return_value.hexdigest.assert_called_once_with()
    def test_zip_with_from_addr(self):
        filename = 'test.zip'

        expected = 'test_e3b0c4.zip'
        actual = ImapClient._unique_filename(filename,
                                             from_addr='*****@*****.**')

        assert expected == actual
        self.mock_sha256.return_value.update.assert_has_calls([
            mock.call(b'*****@*****.**'),
            mock.call(b''),
        ])
        self.mock_sha256.return_value.hexdigest.assert_called_once_with()
    def setup_method(self):
        self.imap4_patcher = mock.patch('imap_client.IMAP4')
        self.mock_imap4 = self.imap4_patcher.start()

        self.imap4_ssl_patcher = mock.patch('imap_client.IMAP4_SSL')
        self.mock_imap4_ssl = self.imap4_ssl_patcher.start()

        self.fetch_messages_patcher = mock.patch(
            'imap_client.ImapClient.fetch_messages')
        self.mock_fetch_messages = self.fetch_messages_patcher.start()

        self._login_patcher = mock.patch('imap_client.ImapClient._login')
        self.mock_login = self._login_patcher.start()

        self._select_patcher = mock.patch('imap_client.ImapClient._select')
        self.mock_select = self._select_patcher.start()

        self.imap_client = ImapClient(
            'test_host',
            'test_username',
            'test_password',
        )
    def test_filename_includes_spaces_tar_gz(self):
        filename = 'john doe smith.tar.gz'

        expected = 'john_doe_smith_e3b0c4.tar.gz'
        actual = ImapClient._unique_filename(filename,
                                             from_addr='*****@*****.**',
                                             date='1-17-18')

        assert expected == actual
        self.mock_sha256.return_value.update.assert_has_calls([
            mock.call(b'*****@*****.**'),
            mock.call(b'1-17-18'),
        ])
        self.mock_sha256.return_value.hexdigest.assert_called_once_with()
    def test_quoted_printable_encoded_filename_tar_gz(self):
        filename = '=?UTF-8?Q?john=5Fdoe=5Fsmith.tar.gz?='

        expected = 'john_doe_smith_e3b0c4.tar.gz'
        actual = ImapClient._unique_filename(filename,
                                             from_addr='*****@*****.**',
                                             date='1-17-18')

        assert expected == actual
        self.mock_sha256.return_value.update.assert_has_calls([
            mock.call(b'*****@*****.**'),
            mock.call(b'1-17-18'),
        ])
        self.mock_sha256.return_value.hexdigest.assert_called_once_with()
    def test_b64_encoded_filename_tar_gz(self):
        filename = '=?UTF-8?b?dGVzdC50YXIuZ3oK?='

        expected = 'test_e3b0c4.tar.gz'
        actual = ImapClient._unique_filename(filename,
                                             from_addr='*****@*****.**',
                                             date='1-17-18')

        assert expected == actual
        self.mock_sha256.return_value.update.assert_has_calls([
            mock.call(b'*****@*****.**'),
            mock.call(b'1-17-18'),
        ])
        self.mock_sha256.return_value.hexdigest.assert_called_once_with()
class TestGetEmailAddresses(object):
    def setup_method(self):
        self.imap4_patcher = mock.patch('imap_client.IMAP4')
        self.mock_imap4 = self.imap4_patcher.start()

        self.imap4_ssl_patcher = mock.patch('imap_client.IMAP4_SSL')
        self.mock_imap4_ssl = self.imap4_ssl_patcher.start()

        self.fetch_messages_patcher = mock.patch(
            'imap_client.ImapClient.fetch_messages')
        self.mock_fetch_messages = self.fetch_messages_patcher.start()

        self._login_patcher = mock.patch('imap_client.ImapClient._login')
        self.mock_login = self._login_patcher.start()

        self._select_patcher = mock.patch('imap_client.ImapClient._select')
        self.mock_select = self._select_patcher.start()

        self.imap_client = ImapClient(
            'test_host',
            'test_username',
            'test_password',
        )

    def teardown_method(self):
        self.fetch_messages_patcher.stop()
        self.imap4_patcher.stop()
        self.imap4_ssl_patcher.stop()
        self._login_patcher.stop()
        self._select_patcher.stop()

    def test_get_email_addresses(self):
        self.mock_fetch_messages.return_value = [
            {
                'From': 'John Doe <*****@*****.**>'
            },
            {
                'From': '[email protected] <*****@*****.**>'
            },
            {
                'From': '<*****@*****.**>'
            },
        ]

        expected = set(['*****@*****.**', '*****@*****.**'])
        actual = self.imap_client.get_email_addresses()

        assert expected == actual
Пример #10
0
def main():
    config = loadConfig('settings.conf')
    HOST = config.get('IMAP', 'HOST')
    USERNAME = config.get('IMAP', 'USERNAME')
    FOLDER = config.get('IMAP', 'FOLDER')
    FILENAME = config.get('RESULTS', 'FILENAME')
    PASSWORD = getpass.getpass()

    LOCAL_DIRECTORY = config.get('ATTACHMENTS', 'LOCAL_DIRECTORY')

    imap_client = ImapClient(
        HOST,
        USERNAME,
        PASSWORD,
        folder=FOLDER,
    )
    imap_client.download_attachements(directory=LOCAL_DIRECTORY)
    imap_client.logout()

    print('Finished downloading attachments')
    print()

    print('Starting decompressing archives')
    decompress_archives(LOCAL_DIRECTORY)
    print('Finished decompressing archives')
    print()

    run_code = input('Run code now? (y/N) ')
    if run_code.lower() in ('y', 'yes'):
        results = run_all()

        output_results_to_stdout(results)

        if FILENAME:
            output_results_to_file(FILENAME, results)

        for result in results:
            if result.has_failures:
                result.write_failures_to_file()

    print()
    print('All Done')