summaryrefslogtreecommitdiff
path: root/service/test/unit/adapter/mailstore/test_leap_mail.py
blob: 3f117dbe21cc2027bd4dfd317de160948e3a471c (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 ThoughtWorks, Inc.
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
from mock import patch
from twisted.trial.unittest import TestCase

from pixelated.adapter.mailstore.leap_mailstore import LeapMail, AttachmentInfo


class TestLeapMail(TestCase):
    def test_leap_mail(self):
        mail = LeapMail('', 'INBOX', {'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'})

        self.assertEqual('test@example.test', mail.from_sender)
        self.assertEqual(['receiver@example.test'], mail.to)
        self.assertEqual('A test Mail', mail.subject)

    def test_email_addresses_in_to_are_split_into_a_list(self):
        mail = LeapMail('', 'INBOX', {'To': 'first@example.test,second@example.test'})

        self.assertEqual(['first@example.test', 'second@example.test'], mail.headers['To'])

    def test_email_addresses_in_cc_are_split_into_a_list(self):
        mail = LeapMail('', 'INBOX', {'Cc': 'first@example.test,second@example.test'})

        self.assertEqual(['first@example.test', 'second@example.test'], mail.headers['Cc'])

    def test_email_addresses_in_bcc_are_split_into_a_list(self):
        mail = LeapMail('', 'INBOX', {'Bcc': 'first@example.test,second@example.test'})

        self.assertEqual(['first@example.test', 'second@example.test'], mail.headers['Bcc'])

    def test_email_addresses_might_be_empty_array(self):
        mail = LeapMail('', 'INBOX', {'Cc': None})

        self.assertEqual([], mail.headers['Cc'])

    def test_as_dict(self):
        mail = LeapMail('doc id', 'INBOX', {'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test,receiver2@other.test'}, ('foo', 'bar'))
        self.maxDiff = None
        expected = {
            'header': {
                'from': 'test@example.test',
                'subject': 'A test Mail',
                'to': ['receiver@example.test', 'receiver2@other.test'],

            },
            'ident': 'doc id',
            'mailbox': 'inbox',
            'tags': {'foo', 'bar'},
            'status': [],
            'body': None,
            'textPlainBody': None,
            'security_casing': {
                'imprints': [{'state': 'no_signature_information'}],
                'locks': []
            },
            'replying': {'all': {'cc-field': [],
                                 'to-field': ['receiver@example.test',
                                              'test@example.test',
                                              'receiver2@other.test']},
                         'single': 'test@example.test'},
            'attachments': []
        }

        self.assertEqual(expected, mail.as_dict())

    def test_as_dict_with_body(self):
        body = 'some body content'
        mail = LeapMail('doc id', 'INBOX', {'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'}, ('foo', 'bar'), body=body)

        self.assertEqual(body, mail.as_dict()['body'])

    def test_as_dict_with_attachments(self):
        mail = LeapMail('doc id', 'INBOX', attachments=[AttachmentInfo('id', 'name', 'encoding')])

        self.assertEqual([{'ident': 'id', 'name': 'name', 'encoding': 'encoding'}],
                         mail.as_dict()['attachments'])

    def test_as_dict_headers_with_special_chars(self):
        expected_address = u'"\xc4lbert \xdcbr\xf6" <\xe4\xfc\xf6@example.mail>'
        expected_subject = u'H\xe4ll\xf6 W\xf6rld'
        mail = LeapMail('', 'INBOX',
                        {'From': '=?iso-8859-1?q?=22=C4lbert_=DCbr=F6=22_=3C=E4=FC=F6=40example=2Email=3E?=',
                         'To': '=?iso-8859-1?q?=22=C4lbert_=DCbr=F6=22_=3C=E4=FC=F6=40example=2Email=3E?=',
                         'Cc': '=?iso-8859-1?q?=22=C4lbert_=DCbr=F6=22_=3C=E4=FC=F6=40example=2Email=3E?=',
                         'Subject': '=?iso-8859-1?q?H=E4ll=F6_W=F6rld?='})

        self.assertEqual(expected_address, mail.as_dict()['header']['from'])
        self.assertEqual([expected_address], mail.as_dict()['header']['to'])
        self.assertEqual([expected_address], mail.as_dict()['header']['cc'])
        self.assertEqual(expected_subject, mail.as_dict()['header']['subject'])

    def test_as_dict_replying_with_special_chars(self):
        expected_address = u'"\xc4lbert \xdcbr\xf6" <\xe4\xfc\xf6@example.mail>'
        mail = LeapMail('', 'INBOX',
                        {'From': '=?iso-8859-1?q?=22=C4lbert_=DCbr=F6=22_=3C=E4=FC=F6=40example=2Email=3E?=',
                         'To': '=?iso-8859-1?q?=22=C4lbert_=DCbr=F6=22_=3C=E4=FC=F6=40example=2Email=3E?=',
                         'Cc': '=?iso-8859-1?q?=22=C4lbert_=DCbr=F6=22_=3C=E4=FC=F6=40example=2Email=3E?=',
                         'Subject': '=?iso-8859-1?q?H=E4ll=F6_W=F6rld?='})
        self.assertEqual([expected_address], mail.as_dict()['replying']['all']['to-field'])
        self.assertEqual([expected_address], mail.as_dict()['replying']['all']['cc-field'])
        self.assertEqual(expected_address, mail.as_dict()['replying']['single'])

    def test_reply_all_result_does_not_contain_own_address_in_to_with_spaces(self):
        my_address = 'myaddress@example.test'

        with patch('pixelated.adapter.mailstore.leap_mailstore.InputMail.FROM_EMAIL_ADDRESS', my_address):
            mail = LeapMail('', 'INBOX',
                            {'From': 'test@example.test',
                             'To': 'receiver@example.test, %s ' % my_address})
            expected_recipients = ['receiver@example.test', 'test@example.test']
            actual_recipients = mail.as_dict()['replying']['all']['to-field']
            expected_recipients.sort()
            actual_recipients.sort()

            self.assertEqual(expected_recipients, actual_recipients)

    def test_reply_all_result_does_not_contain_own_address_in_to_with_name(self):
        my_address = 'myaddress@example.test'

        with patch('pixelated.adapter.mailstore.leap_mailstore.InputMail.FROM_EMAIL_ADDRESS', my_address):
            mail = LeapMail('', 'INBOX',
                            {'From': 'test@example.test',
                             'To': 'receiver@example.test, Folker Bernitt <%s>' % my_address})

            expected_recipients = ['receiver@example.test', 'test@example.test']
            actual_recipients = mail.as_dict()['replying']['all']['to-field']
            expected_recipients.sort()
            actual_recipients.sort()

            self.assertEqual(expected_recipients, actual_recipients)

    # TODO: fix this test
    def test_reply_all_does_not_contain_own_address_in_to_field_with_different_encodings(self):
        my_address = 'myaddress@example.test'

        with patch('pixelated.adapter.mailstore.leap_mailstore.InputMail.FROM_EMAIL_ADDRESS', my_address):
            mail = LeapMail('', 'INBOX',
                            {'From': 'test@example.test',
                             'To': 'receiver@example.test, =?iso-8859-1?q?=C4lbert_=3Cmyaddress=40example=2Etest=3E?='})

            expected_recipients = [u'receiver@example.test', u'test@example.test']
            actual_recipients = mail.as_dict()['replying']['all']['to-field']
            expected_recipients.sort()
            actual_recipients.sort()

            self.assertEqual(expected_recipients, actual_recipients)

    def test_reply_all_result_does_not_contain_own_address_in_cc(self):
        my_address = 'myaddress@example.test'

        with patch('pixelated.adapter.mailstore.leap_mailstore.InputMail.FROM_EMAIL_ADDRESS', my_address):
            mail = LeapMail('', 'INBOX',
                            {'From': 'test@example.test',
                             'To': 'receiver@example.test',
                             'Cc': my_address})

            self.assertEqual([my_address], mail.as_dict()['replying']['all']['cc-field'])

    def test_reply_all_result_does_not_contain_own_address_if_sender(self):
        my_address = 'myaddress@example.test'

        with patch('pixelated.adapter.mailstore.leap_mailstore.InputMail.FROM_EMAIL_ADDRESS', my_address):
            mail = LeapMail('', 'INBOX',
                            {'From': 'myaddress@example.test',
                             'To': 'receiver@example.test'})

            self.assertEqual(['receiver@example.test'], mail.as_dict()['replying']['all']['to-field'])

    def test_reply_all_result_does_contain_own_address_if_only_recipient(self):
        my_address = 'myaddress@example.test'

        with patch('pixelated.adapter.mailstore.leap_mailstore.InputMail.FROM_EMAIL_ADDRESS', my_address):
            mail = LeapMail('', 'INBOX',
                            {'From': 'myaddress@example.test',
                             'To': 'myaddress@example.test'})

            self.assertEqual(['myaddress@example.test'], mail.as_dict()['replying']['all']['to-field'])

    def test_reply_result_swaps_sender_and_recipient_if_i_am_the_sender(self):
        my_address = 'myaddress@example.test'

        with patch('pixelated.adapter.mailstore.leap_mailstore.InputMail.FROM_EMAIL_ADDRESS', my_address):
            mail = LeapMail('', 'INBOX',
                            {'From': 'myaddress@example.test',
                             'To': 'recipient@example.test'})

            self.assertEqual('myaddress@example.test', mail.as_dict()['replying']['single'])

    def test_as_dict_with_mixed_encodings(self):
        subject = 'Another test with =?iso-8859-1?B?3G1s5Px0?= =?iso-8859-1?Q?s?='
        mail = LeapMail('', 'INBOX',
                        {'Subject': subject})

        self.assertEqual(u'Another test with Ümläüts', mail.as_dict()['header']['subject'])

    def test_raw_constructed_by_headers_and_body(self):
        body = 'some body content'
        mail = LeapMail('doc id', 'INBOX', {'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'}, ('foo', 'bar'), body=body)

        result = mail.raw

        expected_raw = 'To: receiver@example.test\nFrom: test@example.test\nSubject: A test Mail\n\nsome body content'
        self.assertEqual(expected_raw, result)

    def test_headers_none_recipients_are_converted_to_empty_array(self):
        mail = LeapMail('id', 'INBOX', {'To': None, 'Cc': None, 'Bcc': None})

        self.assertEquals([], mail.headers['To'])
        self.assertEquals([], mail.headers['Cc'])
        self.assertEquals([], mail.headers['Bcc'])

    def test_security_casing(self):
        # No Encryption, no Signature
        mail = LeapMail('id', 'INBOX', {})
        self.assertEqual({'locks': [], 'imprints': [{'state': 'no_signature_information'}]}, mail.security_casing)

        # Encryption
        mail = LeapMail('id', 'INBOX', {'X-Leap-Encryption': 'decrypted'})
        self.assertEqual([{'state': 'valid'}], mail.security_casing['locks'])

        mail = LeapMail('id', 'INBOX', {'X-Leap-Encryption': 'false'})
        self.assertEqual([], mail.security_casing['locks'])

        # Signature
        mail = LeapMail('id', 'INBOX', {'X-Leap-Signature': 'valid'})
        self.assertEqual([{'seal': {'validity': 'valid'}, 'state': 'valid'}], mail.security_casing['imprints'])

        mail = LeapMail('id', 'INBOX', {'X-Leap-Signature': 'invalid'})
        self.assertEqual([], mail.security_casing['imprints'])