diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2018-04-21 22:04:37 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2018-04-21 22:04:37 +0200 |
commit | 26daa1452153ba503f8b5b941f1574275346ecfd (patch) | |
tree | 0b823e74fc9f7ec4f9f169f5f69b9f94e0fe94b0 | |
parent | 687199091e4739cfab40089a2d585a18835ab812 (diff) | |
download | imapArch-26daa1452153ba503f8b5b941f1574275346ecfd.tar.gz |
Attempts to start testing
-rw-r--r-- | .gitignore | 1 | ||||
-rwxr-xr-x | archive_folder.py | 32 | ||||
-rw-r--r-- | test/test_email_server.py | 17 | ||||
-rw-r--r-- | test/test_purge_obsolete.py | 43 |
4 files changed, 38 insertions, 55 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/archive_folder.py b/archive_folder.py index 2b3ba14..e321dfe 100755 --- a/archive_folder.py +++ b/archive_folder.py @@ -27,23 +27,15 @@ log = logging.getLogger('imapArch') class ServerError(IOError): pass - class FolderError(IOError): pass - class MessageError(IOError): pass Capas = collections.namedtuple('Capas', ['MOVE', 'UIDPLUS']) -def get_config(): - config = configparser.ConfigParser() - config.read(os.path.expanduser('~/.config/imap_archiver.cfg')) - return config - - class Message(object): """Abstraction over one email message.""" def __init__(self, client, uid): @@ -194,10 +186,17 @@ class Folder(object): class EmailServer(object): def __init__(self, serverKey=None, archive_root=None): - config = get_config() + config = self.get_config() acc_name = serverKey if serverKey is not None \ else config['general']['account'] - self.cfg = dict(config.items(acc_name)) + if config.has_section(acc_name): + self.cfg = dict(config.items(acc_name)) + else: + self.cfg = { + 'host': 'localhost', + 'username': 'unknown', + 'password': 'verysecret' + } self.archive_root = archive_root self.__box = self.__login(**self.cfg) @@ -215,6 +214,15 @@ class EmailServer(object): box.features_present = Capas._make(['MOVE' in capas, 'UIDPLUS' in capas]) log.debug('features_present = %s', box.features_present) + @staticmethod + @functools.lru_cache() + def get_config(): + # In case the configuration file is missing, only empty list will be + # returned + config = configparser.ConfigParser() + config.read(os.path.expanduser('~/.config/imap_archiver.cfg')) + return config + def archive_folder(self, folder_name, before_date): # type: (str, datetime.date) -> None """ @@ -270,8 +278,8 @@ if __name__ == '__main__': args = argp.parse_args() - if args.cmd == 'list': - config = get_config() + if args.cmd == 'servers': + config = EmailServer.get_config() sects = set(config.keys()) - {'DEFAULT', 'general'} print('Available servers:\n%s' % tuple(sects)) else: diff --git a/test/test_email_server.py b/test/test_email_server.py new file mode 100644 index 0000000..3be9824 --- /dev/null +++ b/test/test_email_server.py @@ -0,0 +1,17 @@ +import unittest +from unittest import mock + +import archive_folder + + +class TestEmailServer(unittest.TestCase): + + # Ok, data = box.login(username, password) + # Ok, data = box.capability() + + @mock.patch.object(archive_folder.imaplib, 'IMAP4_SSL', autospec=True) + def test_server_initialization(self, mock_imapobj): + mock_imapobj.login.return_value = ('OK', 'FAKED success') + host = 'fakehost' + box = archive_folder.EmailServer(host, 'FakeArchive') + mock_imapobj.assert_called_with(host) diff --git a/test/test_purge_obsolete.py b/test/test_purge_obsolete.py deleted file mode 100644 index c4d1ec9..0000000 --- a/test/test_purge_obsolete.py +++ /dev/null @@ -1,43 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import division -import unittest - -class SimpleDateTest(unittest.TestCase): - def setUp(self): - from purgeObsolete import SimpleDate - - def runInnerTest(self): - threeMonthAgo = date.today() - JD3MAgo = GregorianCalendar(threeMonthAgo.year, - threeMonthAgo.month-1, - threeMonthAgo.day,0,0) - formatter = SimpleDateFormat("yyyy-MM-dd",Locale.US); - JD3MAgoStr = formatter.format(JD3MAgo.getTime()) - self.assertEqual(str(threeMonthAgo),JD3MAgoStr) - - def runTest(self): - pass - -class ArchivedStoreTest(unittest.TestCase): - def setUp(self): - from purgeObsolete import ArchivedStore - self.store = ArchivedStore(paramServer="zimbra") - - def tearDown(self): - self.store.store.close() - - def testConnect(self): - folderList = self.store.store.getDefaultFolder().list("*") - self.assertEqual(len(folderList)>0,True) - - def testOpeningFolder(self): - from purgeObsolete import ArchivedFolder - folder = ArchivedFolder(self.store,"INBOX/bugzilla/xgl") - self.assertEqual(folder.folder.getMessageCount() > 0,True) - - def runTest(self): - self.testConnect() - self.testOpeningFolder() - -if __name__=="__main__": - unittest.main() |