aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_email_server.py
blob: 119b59d569ec92305dab79178429fa081d08f293 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import datetime
import logging
import unittest
from unittest import mock

import imapArch

log = logging.getLogger('test')


def create_test_message():
    with open('test/msg_test.eml', 'rb') as msg_file:
        msg_raw = msg_file.read()
        return [(b'13 (UID 13 RFC822 {3776}', msg_raw), b')']


class TestEmailServer(unittest.TestCase):
    @mock.patch.object(imapArch.imaplib, 'IMAP4_SSL', autospec=True)
    @mock.patch.object(imapArch.configparser, 'ConfigParser', autospec=True)
    def test_server_initialization(self, mock_configparser, mock_imapobj):
        mock_imapobj().login.return_value = ('OK', 'FAKED success')
        mock_imapobj().capability.return_value = (
            'OK', [b'IMAP4rev1 LITERAL+ UIDPLUS']
        )
        mock_configparser().items.return_value = [
            ("host", 'fakehost'),
            ('ssl', True),
            ('username', 'fakeuser'),
            ('password', 'veryverysecret')
        ]
        box = imapArch.EmailServer('fakeuser@fakehost', 'FakeArchive')
        mock_imapobj.assert_called_with(host='fakehost')
        mock_imapobj().login.assert_called_once_with('fakeuser', 'veryverysecret')
        mock_imapobj().capability.assert_called_once_with()
        self.assertEqual(mock_imapobj().features_present, (False, True))
        self.assertIsInstance(box, imapArch.EmailServer)


class TestFolder(unittest.TestCase):
    @mock.patch.object(imapArch.imaplib, 'IMAP4_SSL', autospec=True)
    def test_folder_initialization(self, mock_imapobj):
        mock_imapobj().list.return_value = ('OK',
                                            [b'(\\HasNoChildren) "/" Work/sociology-junk'])
        box = imapArch.imaplib.IMAP4_SSL()
        folder = imapArch.Folder(box, 'INBOX/FakeFolder')
        mock_imapobj().list.assert_called_once_with('INBOX/FakeFolder', '*')
        self.assertIsInstance(folder, imapArch.Folder)
        self.assertEqual(folder.folder_sep, '/')
        self.assertFalse(folder.selected)

    @mock.patch.object(imapArch.imaplib, 'IMAP4_SSL', autospec=True)
    def test_folder_select(self, mock_imapobj):
        mock_imapobj().list.return_value = ('OK',
                                            [b'(\\HasNoChildren) "/" Work/sociology-junk'])
        mock_imapobj().select.return_value = ('OK', 'FAKE success')
        box = imapArch.imaplib.IMAP4_SSL()
        folder = imapArch.Folder(box, 'INBOX/FakeFolder').select()
        mock_imapobj().list.assert_called_once_with('INBOX/FakeFolder', '*')
        mock_imapobj().select.assert_called_once_with('INBOX/FakeFolder')
        self.assertIsInstance(folder, imapArch.Folder)
        self.assertEqual(folder.folder_sep, '/')
        self.assertTrue(folder.selected)

    @mock.patch.object(imapArch.imaplib, 'IMAP4_SSL', autospec=True)
    def test_folder_emails_before(self, mock_imapobj):
        def fake_uid(cmd, param):
            if cmd == 'FETCH':
                return ('OK', create_test_message())
            elif cmd == 'SEARCH':
                return ('OK', [b'13'])

        mock_imapobj().list.return_value = ('OK',
                                            [b'(\\HasNoChildren) "/" Work/sociology-junk'])
        mock_imapobj().select.return_value = ('OK', 'FAKE success')
        mock_imapobj().uid.side_effect = fake_uid
        box = imapArch.imaplib.IMAP4_SSL()
        folder = imapArch.Folder(box, 'INBOX/FakeFolder').select()
        msgs = folder.emails_before('25-Feb-1948')
        self.assertIsInstance(msgs[0], imapArch.Message)

    @mock.patch.object(imapArch.imaplib, 'IMAP4_SSL', autospec=True)
    def test_folder_get_imapArch(self, mock_imapobj):
        mock_imapobj().list.return_value = ('OK',
                                            [b'(\\HasNoChildren) "/" Work/sociology-junk'])
        mock_imapobj().select.return_value = ('OK', 'FAKE success')
        box = imapArch.imaplib.IMAP4_SSL()
        folder = imapArch.Folder(box, 'INBOX/FakeFolder').select()

        mock_imapobj().uid.return_value = ('OK', create_test_message())
        box = imapArch.imaplib.IMAP4_SSL()
        msg = imapArch.Message(box, '10')

        arch_fld = folder.get_imapArch(msg, 'FakeArchive')
        self.assertEqual(arch_fld, 'FakeArchive/2001/INBOX/FakeFolder')


class TestMessage(unittest.TestCase):
    @mock.patch.object(imapArch.imaplib, 'IMAP4_SSL', autospec=True)
    def test_message_initialization(self, mock_imapobj):
        mock_imapobj().uid.return_value = ('OK', create_test_message())
        box = imapArch.imaplib.IMAP4_SSL()
        msg = imapArch.Message(box, '10')
        mock_imapobj().uid.assert_called_once_with('FETCH', '10 (RFC822)')
        self.assertIsInstance(msg, imapArch.Message)
        self.assertEqual(msg.uid, '10')
        self.assertEqual(msg.subject,
                         'TR: [dynnet] Evolution ofnetworks a temptative of bibliography')
        self.assertEqual(msg.date, datetime.datetime(2001, 7, 18, 17, 53, 30,
                                                     tzinfo=datetime.timezone(datetime.timedelta(0, 7200))))