summaryrefslogtreecommitdiff
path: root/tests/test_account.py
blob: 975f80ad8709f873f807802ae5d3a58ef3ed0a42 (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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# encoding=utf-8
# Copyright © 2017 Dylan Baker
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import logging
import unittest

from alot import account

from . import utilities

class _AccountTestClass(account.Account):
    """Implements stubs for ABC methods."""

    def send_mail(self, mail):
        pass


class TestAccount(unittest.TestCase):
    """Tests for the Account class."""

    def test_matches_address(self):
        """Tests address without aliases."""
        acct = _AccountTestClass(address="foo@example.com")
        self.assertTrue(acct.matches_address("foo@example.com"))
        self.assertFalse(acct.matches_address("bar@example.com"))

    def test_matches_address_with_aliases(self):
        """Tests address with aliases."""
        acct = _AccountTestClass(address="foo@example.com",
                                 aliases=['bar@example.com'])
        self.assertTrue(acct.matches_address("foo@example.com"))
        self.assertTrue(acct.matches_address("bar@example.com"))
        self.assertFalse(acct.matches_address("baz@example.com"))

    def test_matches_address_with_regex_aliases(self):
        """Tests address with regex aliases."""
        acct = _AccountTestClass(address="foo@example.com",
                                 alias_regexp=r'to\+.*@example.com')
        self.assertTrue(acct.matches_address("to+foo@example.com"))
        self.assertFalse(acct.matches_address("to@example.com"))


    def test_deprecated_encrypt_by_default(self):
        """Tests that deprecated values are still accepted."""
        for each in ['true', 'yes', '1']:
            acct = _AccountTestClass(address='foo@example.com',
                                     encrypt_by_default=each)
            self.assertEqual(acct.encrypt_by_default, 'all')
        for each in ['false', 'no', '0']:
            acct = _AccountTestClass(address='foo@example.com',
                                     encrypt_by_default=each)
            self.assertEqual(acct.encrypt_by_default, 'none')


class TestAddress(unittest.TestCase):

    """Tests for the Address class."""

    def test_from_string(self):
        addr = account.Address.from_string('user@example.com')
        self.assertEqual(addr.username, 'user')
        self.assertEqual(addr.domainname, 'example.com')

    def test_str(self):
        addr = account.Address('ušer', 'example.com')
        self.assertEqual(str(addr), 'ušer@example.com')

    def test_eq_unicode(self):
        addr = account.Address('ušer', 'example.com')
        self.assertEqual(addr, 'ušer@example.com')

    def test_eq_address(self):
        addr = account.Address('ušer', 'example.com')
        addr2 = account.Address('ušer', 'example.com')
        self.assertEqual(addr, addr2)

    def test_ne_unicode(self):
        addr = account.Address('ušer', 'example.com')
        self.assertNotEqual(addr, 'user@example.com')

    def test_ne_address(self):
        addr = account.Address('ušer', 'example.com')
        addr2 = account.Address('user', 'example.com')
        self.assertNotEqual(addr, addr2)

    def test_eq_unicode_case(self):
        addr = account.Address('UŠer', 'example.com')
        self.assertEqual(addr, 'ušer@example.com')

    def test_ne_unicode_case(self):
        addr = account.Address('ušer', 'example.com')
        self.assertEqual(addr, 'uŠer@example.com')

    def test_ne_address_case(self):
        addr = account.Address('ušer', 'example.com')
        addr2 = account.Address('uŠer', 'example.com')
        self.assertEqual(addr, addr2)

    def test_eq_address_case(self):
        addr = account.Address('UŠer', 'example.com')
        addr2 = account.Address('ušer', 'example.com')
        self.assertEqual(addr, addr2)

    def test_eq_unicode_case_sensitive(self):
        addr = account.Address('UŠer', 'example.com', case_sensitive=True)
        self.assertNotEqual(addr, 'ušer@example.com')

    def test_eq_address_case_sensitive(self):
        addr = account.Address('UŠer', 'example.com', case_sensitive=True)
        addr2 = account.Address('ušer', 'example.com')
        self.assertNotEqual(addr, addr2)

    def test_eq_str(self):
        addr = account.Address('user', 'example.com', case_sensitive=True)
        with self.assertRaises(TypeError):
            addr == 1  # pylint: disable=pointless-statement

    def test_ne_str(self):
        addr = account.Address('user', 'example.com', case_sensitive=True)
        with self.assertRaises(TypeError):
            addr != 1  # pylint: disable=pointless-statement

    def test_repr(self):
        addr = account.Address('user', 'example.com', case_sensitive=True)
        self.assertEqual(
            repr(addr),
            "Address('user', 'example.com', case_sensitive=True)")

    def test_domain_name_ne(self):
        addr = account.Address('user', 'example.com')
        self.assertNotEqual(addr, 'user@example.org')

    def test_domain_name_eq_case(self):
        addr = account.Address('user', 'example.com')
        self.assertEqual(addr, 'user@Example.com')

    def test_domain_name_ne_unicode(self):
        addr = account.Address('user', 'éxample.com')
        self.assertNotEqual(addr, 'user@example.com')

    def test_domain_name_eq_unicode(self):
        addr = account.Address('user', 'éxample.com')
        self.assertEqual(addr, 'user@Éxample.com')

    def test_domain_name_eq_case_sensitive(self):
        addr = account.Address('user', 'example.com', case_sensitive=True)
        self.assertEqual(addr, 'user@Example.com')

    def test_domain_name_eq_unicode_sensitive(self):
        addr = account.Address('user', 'éxample.com', case_sensitive=True)
        self.assertEqual(addr, 'user@Éxample.com')

    def test_cmp_empty(self):
        addr = account.Address('user', 'éxample.com')
        self.assertNotEqual(addr, '')


class TestSend(unittest.TestCase):

    @utilities.async_test
    async def test_logs_on_success(self):
        a = account.SendmailAccount(address="test@alot.dev", cmd="true")
        with self.assertLogs() as cm:
            await a.send_mail("some text")
        #self.assertIn(cm.output, "sent mail successfullya")
        self.assertIn("INFO:root:sent mail successfully", cm.output)

    @utilities.async_test
    async def test_failing_sendmail_command_is_noticed(self):
        a = account.SendmailAccount(address="test@alot.dev", cmd="false")
        with self.assertRaises(account.SendingMailFailed):
            with self.assertLogs(level=logging.ERROR):
                await a.send_mail("some text")