Skip to content

Commit

Permalink
+outlook test box, improve flag test
Browse files Browse the repository at this point in the history
  • Loading branch information
ikvk committed Apr 22, 2020
1 parent 60fe5a8 commit 9aa8bc9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 29 deletions.
4 changes: 2 additions & 2 deletions tests/test_actions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest

import imap_tools
from tests.utils import MailboxTestCase, test_mailbox_name_set, get_test_mailbox
from tests.utils import MailboxTestCase, TEST_MAILBOX_NAME_SET, get_test_mailbox


class ActionTest(MailboxTestCase):
Expand All @@ -10,7 +10,7 @@ class ActionTest(MailboxTestCase):
@classmethod
def setUpClass(cls):
# clear temp folders
for test_mailbox_name in test_mailbox_name_set:
for test_mailbox_name in TEST_MAILBOX_NAME_SET:
mailbox = get_test_mailbox(test_mailbox_name)
mailbox.folder.set(mailbox.folder_test_temp1)
for_del_1 = [msg.uid for msg in mailbox.fetch()]
Expand Down
6 changes: 3 additions & 3 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import unittest

from imap_tools import MailBox
from tests.utils import get_test_mailbox_config, test_mailbox_name_set
from tests.utils import get_test_mailbox_config, TEST_MAILBOX_NAME_SET


class ConnectionTest(unittest.TestCase):
def test_connection(self):
# simple
for test_mailbox_name in test_mailbox_name_set:
for test_mailbox_name in TEST_MAILBOX_NAME_SET:
config = get_test_mailbox_config(test_mailbox_name)
mailbox = MailBox(config['host'])
self.assertIs(type(mailbox), MailBox)
Expand All @@ -16,7 +16,7 @@ def test_connection(self):
logout_result = mailbox.logout()
self.assertEqual(logout_result[0], 'BYE')
# with
for test_mailbox_name in test_mailbox_name_set:
for test_mailbox_name in TEST_MAILBOX_NAME_SET:
config = get_test_mailbox_config(test_mailbox_name)
with MailBox(config['host']).login(config['email'], config['password']) as mailbox:
self.assertIs(type(mailbox), MailBox)
Expand Down
12 changes: 4 additions & 8 deletions tests/test_folders.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import unittest

from tests.utils import MailboxTestCase, test_mailbox_name_set, get_test_mailbox
from tests.utils import MailboxTestCase, TEST_MAILBOX_NAME_SET, get_test_mailbox


class FoldersTest(MailboxTestCase):
@classmethod
def setUpClass(cls):
# delete temp new folders
for test_mailbox_name in test_mailbox_name_set:
for test_mailbox_name in TEST_MAILBOX_NAME_SET:
mailbox = get_test_mailbox(test_mailbox_name)
for del_folder in (mailbox.folder_test_new, mailbox.folder_test_new1):
if mailbox.folder.exists(del_folder):
mailbox.folder.delete(del_folder)

def test_folders(self):
for mailbox_name, mailbox in self.mailbox_set.items():
if mailbox_name == 'MAIL_RU':
continue
# LIST
folder_list = mailbox.folder.list(mailbox.folder_test)
self.assertEqual(
set([i['name'] for i in folder_list]),
{mailbox.folder_test_base, mailbox.folder_test_temp1, mailbox.folder_test_temp2}
)
check_folder_set = {mailbox.folder_test_base, mailbox.folder_test_temp1, mailbox.folder_test_temp2}
self.assertTrue(check_folder_set.issubset(set([i['name'] for i in folder_list])))
for folder in folder_list:
self.assertIs(type(folder['flags']), str)
self.assertIs(type(folder['delim']), str)
Expand Down
7 changes: 6 additions & 1 deletion tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from tests.utils import MailboxTestCase

from tests.data import MESSAGE_ATTRIBUTES
from imap_tools import MailMessage
from imap_tools import MailMessage, MessageFlags


class MessageTest(MailboxTestCase):
Expand All @@ -13,6 +13,7 @@ def test_live(self):
none_type = type(None)
for mailbox in self.mailbox_set.values():
mailbox.folder.set(mailbox.folder_test_base)
answered_and_flagged_cnt = 0
for message in mailbox.fetch():
self.assertIn(type(message.uid), (str, none_type))
self.assertIs(type(message.subject), str)
Expand All @@ -35,12 +36,16 @@ def test_live(self):
self.assertIs(type(message.flags), tuple)
for i in message.flags:
self.assertIs(type(i), str)
if {MessageFlags.ANSWERED, MessageFlags.FLAGGED}.issubset(message.flags):
answered_and_flagged_cnt += 1

for att in message.attachments:
self.assertIs(type(att.filename), str)
self.assertIs(type(att.content_type), str)
self.assertIs(type(att.payload), bytes)

self.assertTrue(answered_and_flagged_cnt >= 1)

def test_attributes(self):
msg_attr_set = {'subject', 'from_', 'to', 'cc', 'bcc', 'reply_to', 'date', 'date_str', 'text', 'html',
'headers', 'from_values', 'to_values', 'cc_values', 'bcc_values', 'reply_to_values'}
Expand Down
28 changes: 13 additions & 15 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import configparser
from imap_tools import MailBox

test_mailbox_name_set = {'YANDEX', 'ZIMBRA', 'MAIL_RU'} # YANDEX, MAIL_RU, GOOGLE, ZIMBRA
# YANDEX, MAIL_RU, GOOGLE, ZIMBRA, OUTLOOK
TEST_MAILBOX_NAME_SET = {'YANDEX', 'ZIMBRA', 'MAIL_RU', 'OUTLOOK'}


def get_test_mailbox_config(mailbox_name: str) -> dict:
Expand All @@ -17,42 +18,39 @@ def get_test_mailbox_config(mailbox_name: str) -> dict:
email=config[mailbox_name]['email'],
password=config[mailbox_name]['password'],
path_separator=config[mailbox_name]['path_separator'],
test_folder=config[mailbox_name]['test_folder'],
)


def get_test_mailbox(mailbox_name: str):
# get config
config = get_test_mailbox_config(mailbox_name)

# add class attributes for pycharm code analyzer
# add test attributes to MailBox
class MailBoxTestEx(MailBox):
def __init__(self, *args):
super().__init__(*args)
self.folder_test = 'test'
self.folder_test_base = 'test{}base'
self.folder_test_temp1 = 'test{}temp1'
self.folder_test_temp2 = 'test{}temp2'
self.folder_test_new = 'test{}new'
self.folder_test_new1 = 'test{}new1'
test_folder = config['test_folder']
path_separator = config['path_separator']
self.folder_test = '{}'.format(test_folder)
self.folder_test_base = '{}{}base'.format(test_folder, path_separator)
self.folder_test_temp1 = '{}{}temp1'.format(test_folder, path_separator)
self.folder_test_temp2 = '{}{}temp2'.format(test_folder, path_separator)
self.folder_test_new = '{}{}new'.format(test_folder, path_separator)
self.folder_test_new1 = '{}{}new1'.format(test_folder, path_separator)

# create mailbox instance
mailbox = MailBoxTestEx(config['host'])
# connect
mailbox.login(config['email'], config['password'])
# set test folder paths
mailbox.folder_test_base = mailbox.folder_test_base.format(config['path_separator'])
mailbox.folder_test_temp1 = mailbox.folder_test_temp1.format(config['path_separator'])
mailbox.folder_test_temp2 = mailbox.folder_test_temp2.format(config['path_separator'])
mailbox.folder_test_new = mailbox.folder_test_new.format(config['path_separator'])
mailbox.folder_test_new1 = mailbox.folder_test_new1.format(config['path_separator'])
# done
return mailbox


class MailboxTestCase(unittest.TestCase):
def setUp(self):
self.mailbox_set = dict()
for test_mailbox_name in test_mailbox_name_set:
for test_mailbox_name in TEST_MAILBOX_NAME_SET:
self.mailbox_set[test_mailbox_name] = get_test_mailbox(test_mailbox_name)

def tearDown(self):
Expand Down

0 comments on commit 9aa8bc9

Please sign in to comment.