summaryrefslogtreecommitdiff
path: root/service/test/adapter/mail_service_test.py
blob: 8482203861da5d25de677cb19e6736d6113b68aa (plain)
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
import sys
import os
import unittest
from mock import patch
from mock import Mock

from app.adapter.mail_service import MailService


class MailboxCollision(Exception):
    pass


class MailboxException(Exception):
    pass


class TestMailService(unittest.TestCase):

    def setUp(self):
        self.mail_service = MailService()

    def test_request_mail_has_mailbox(self):
        with patch.object(self.mail_service, '_switch_mailbox', return_value=Mock()):
            mails = self.mail_service.mails({'tags': ['inbox']})
            self.assertIsNotNone(mails)

    def test_request_mail_has_mailbox(self):
        with patch.object(self.mail_service, '_switch_mailbox', return_value=None):
            mails = self.mail_service.mails({'tags': ['inbox']})
            self.assertEqual(mails, [])

    def test_switch_mailbox_special_tag(self):
        mailbox = Mock()
        with patch.object(self.mail_service.account, 'getMailbox', return_value=mailbox):
            new_mailbox = self.mail_service._switch_mailbox('sent')
            self.assertEqual(new_mailbox, mailbox)

    def test_switch_mailbox_custom_tag_exists(self):
        mailbox = Mock()
        with patch.object(self.mail_service.account, 'getMailbox', return_value=mailbox):
            returned_mailbox = self.mail_service._switch_mailbox('custom')
            self.assertEqual(mailbox, returned_mailbox)

    def test_switch_mailbox_custom_tag_not_exists(self):
        mailbox = Mock()
        with patch.object(self.mail_service.account, 'getMailbox', side_effect=MailboxException()):
            mailbox = self.mail_service._switch_mailbox('custom')
            self.assertIsNone(mailbox)

    def test_create_new_mailbox(self):
        with patch.object(self.mail_service.account, 'addMailbox', return_value=True) as addMailbox:
            self.assertTrue(self.mail_service._create_mailbox('teste'))

    def test_create_existing_mailbox(self):
        with patch.object(self.mail_service.account, 'addMailbox', side_effect=MailboxCollision()) as addMailbox:
            self.assertFalse(self.mail_service._create_mailbox('teste'))