/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_email_message.py

Fix some conflict tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 2009, 2011, 2014, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
from email.Header import decode_header
 
17
import sys
 
18
try:
 
19
    from email.header import decode_header
 
20
except ImportError:  # python < 3
 
21
    from email.Header import decode_header
18
22
 
19
 
from bzrlib import __version__ as _bzrlib_version
20
 
from bzrlib.email_message import EmailMessage
21
 
from bzrlib.errors import BzrBadParameterNotUnicode
22
 
from bzrlib.smtp_connection import SMTPConnection
23
 
from bzrlib.tests import TestCase
 
23
from .. import __version__ as _breezy_version
 
24
from ..email_message import EmailMessage
 
25
from ..errors import BzrBadParameterNotUnicode
 
26
from ..sixish import text_type
 
27
from ..smtp_connection import SMTPConnection
 
28
from .. import tests
24
29
 
25
30
EMPTY_MESSAGE = '''\
26
31
From: from@from.com
28
33
To: to@to.com
29
34
User-Agent: Bazaar (%s)
30
35
 
31
 
''' % _bzrlib_version
 
36
''' % _breezy_version
32
37
 
33
38
_SIMPLE_MESSAGE = '''\
34
39
MIME-Version: 1.0
39
44
To: to@to.com
40
45
User-Agent: Bazaar (%s)
41
46
 
42
 
%%s''' % _bzrlib_version
 
47
%%s''' % _breezy_version
43
48
 
44
49
SIMPLE_MESSAGE_ASCII = _SIMPLE_MESSAGE % ('us-ascii', '7bit', 'body')
45
50
SIMPLE_MESSAGE_UTF8 = _SIMPLE_MESSAGE % ('utf-8', 'base64', 'YsOzZHk=\n')
63
68
Content-Disposition: inline
64
69
 
65
70
body
66
 
''' %  { 'version': _bzrlib_version, 'boundary': BOUNDARY }
67
 
 
68
 
SIMPLE_MULTIPART_MESSAGE = _MULTIPART_HEAD + '--%s--' % BOUNDARY
69
 
 
70
 
COMPLEX_MULTIPART_MESSAGE = _MULTIPART_HEAD + '''\
 
71
''' %  { 'version': _breezy_version, 'boundary': BOUNDARY }
 
72
 
 
73
 
 
74
def final_newline_or_not(msg):
 
75
    if sys.version_info >= (2, 7, 6):
 
76
        # Some internals of python's email module changed in an (minor)
 
77
        # incompatible way: a final newline is appended in 2.7.6...
 
78
       msg += '\n'
 
79
    return msg
 
80
 
 
81
 
 
82
def simple_multipart_message():
 
83
    msg = _MULTIPART_HEAD + '--%s--' % BOUNDARY
 
84
    return final_newline_or_not(msg)
 
85
 
 
86
 
 
87
def complex_multipart_message(typ):
 
88
    msg = _MULTIPART_HEAD + '''\
71
89
--%(boundary)s
72
90
MIME-Version: 1.0
73
91
Content-Type: text/%%s; charset="us-ascii"; name="lines.txt"
81
99
e
82
100
 
83
101
--%(boundary)s--''' %  { 'boundary': BOUNDARY }
84
 
 
85
 
 
86
 
class TestEmailMessage(TestCase):
 
102
    msg = final_newline_or_not(msg)
 
103
    return msg % (typ,)
 
104
 
 
105
 
 
106
class TestEmailMessage(tests.TestCase):
87
107
 
88
108
    def test_empty_message(self):
89
109
        msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
90
 
        self.assertEqualDiff(EMPTY_MESSAGE , msg.as_string())
 
110
        self.assertEqualDiff(EMPTY_MESSAGE, msg.as_string())
91
111
 
92
112
    def test_simple_message(self):
93
113
        pairs = {
94
 
            'body': SIMPLE_MESSAGE_ASCII,
 
114
            b'body': SIMPLE_MESSAGE_ASCII,
95
115
            u'b\xf3dy': SIMPLE_MESSAGE_UTF8,
96
 
            'b\xc3\xb3dy': SIMPLE_MESSAGE_UTF8,
97
 
            'b\xf4dy': SIMPLE_MESSAGE_8BIT,
 
116
            b'b\xc3\xb3dy': SIMPLE_MESSAGE_UTF8,
 
117
            b'b\xf4dy': SIMPLE_MESSAGE_8BIT,
98
118
        }
99
119
        for body, expected in pairs.items():
100
120
            msg = EmailMessage('from@from.com', 'to@to.com', 'subject', body)
101
121
            self.assertEqualDiff(expected, msg.as_string())
102
122
 
103
 
    def test_multipart_message(self):
 
123
    def test_multipart_message_simple(self):
104
124
        msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
105
125
        msg.add_inline_attachment('body')
106
 
        self.assertEqualDiff(SIMPLE_MULTIPART_MESSAGE, msg.as_string(BOUNDARY))
107
 
 
 
126
        self.assertEqualDiff(simple_multipart_message(),
 
127
                             msg.as_string(BOUNDARY))
 
128
 
 
129
 
 
130
    def test_multipart_message_complex(self):
108
131
        msg = EmailMessage('from@from.com', 'to@to.com', 'subject', 'body')
109
132
        msg.add_inline_attachment(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-subtype')
110
 
        self.assertEqualDiff(COMPLEX_MULTIPART_MESSAGE % 'x-subtype',
111
 
                msg.as_string(BOUNDARY))
 
133
        self.assertEqualDiff(complex_multipart_message('x-subtype'),
 
134
                             msg.as_string(BOUNDARY))
112
135
 
113
136
    def test_headers_accept_unicode_and_utf8(self):
114
137
        for user in [ u'Pepe P\xe9rez <pperez@ejemplo.com>',
148
171
        self.assertEqual('to2@to.com', msg['To'])
149
172
        self.assertEqual('cc@cc.com', msg['Cc'])
150
173
 
151
 
    def test_send(self):
152
 
        class FakeConfig:
153
 
            def get_user_option(self, option):
154
 
                return None
155
 
 
156
 
        messages = []
157
 
 
158
 
        def send_as_append(_self, msg):
159
 
            messages.append(msg.as_string(BOUNDARY))
160
 
 
161
 
        old_send_email = SMTPConnection.send_email
162
 
        try:
163
 
            SMTPConnection.send_email = send_as_append
164
 
 
165
 
            EmailMessage.send(FakeConfig(), 'from@from.com', 'to@to.com',
166
 
                    'subject', 'body', u'a\nb\nc\nd\ne\n', 'lines.txt')
167
 
            self.assertEqualDiff(COMPLEX_MULTIPART_MESSAGE % 'plain',
168
 
                    messages[0])
169
 
            messages[:] = []
170
 
 
171
 
            EmailMessage.send(FakeConfig(), 'from@from.com', 'to@to.com',
172
 
                    'subject', 'body', u'a\nb\nc\nd\ne\n', 'lines.txt',
173
 
                    'x-patch')
174
 
            self.assertEqualDiff(COMPLEX_MULTIPART_MESSAGE % 'x-patch',
175
 
                    messages[0])
176
 
            messages[:] = []
177
 
 
178
 
            EmailMessage.send(FakeConfig(), 'from@from.com', 'to@to.com',
179
 
                    'subject', 'body')
180
 
            self.assertEqualDiff(SIMPLE_MESSAGE_ASCII , messages[0])
181
 
            messages[:] = []
182
 
        finally:
183
 
            SMTPConnection.send_email = old_send_email
184
 
 
185
174
    def test_address_to_encoded_header(self):
186
175
        def decode(s):
187
176
            """Convert a RFC2047-encoded string to a unicode string."""
202
191
 
203
192
        address = u'Pepe P\xe9rez <pperez@ejemplo.com>' # unicode ok
204
193
        encoded = EmailMessage.address_to_encoded_header(address)
205
 
        self.assert_('pperez@ejemplo.com' in encoded) # addr must be unencoded
206
 
        self.assertEquals(address, decode(encoded))
 
194
        self.assertTrue('pperez@ejemplo.com' in encoded) # addr must be unencoded
 
195
        self.assertEqual(address, decode(encoded))
207
196
 
208
197
        address = 'Pepe P\xc3\xa9red <pperez@ejemplo.com>' # UTF-8 ok
209
198
        encoded = EmailMessage.address_to_encoded_header(address)
210
 
        self.assert_('pperez@ejemplo.com' in encoded)
211
 
        self.assertEquals(address, decode(encoded).encode('utf-8'))
 
199
        self.assertTrue('pperez@ejemplo.com' in encoded)
 
200
        self.assertEqual(address, decode(encoded).encode('utf-8'))
212
201
 
213
202
        address = 'Pepe P\xe9rez <pperez@ejemplo.com>' # ISO-8859-1 not ok
214
203
        self.assertRaises(BzrBadParameterNotUnicode,
216
205
 
217
206
    def test_string_with_encoding(self):
218
207
        pairs = {
219
 
                u'Pepe':        ('Pepe', 'ascii'),
220
 
                u'P\xe9rez':    ('P\xc3\xa9rez', 'utf-8'),
221
 
                'Perez':         ('Perez', 'ascii'), # u'Pepe' == 'Pepe'
222
 
                'P\xc3\xa9rez': ('P\xc3\xa9rez', 'utf-8'),
223
 
                'P\xe8rez':     ('P\xe8rez', '8-bit'),
 
208
                u'Pepe':        (b'Pepe', 'ascii'),
 
209
                u'P\xe9rez':    (b'P\xc3\xa9rez', 'utf-8'),
 
210
                b'Perez':        (b'Perez', 'ascii'), # u'Pepe' == 'Pepe'
 
211
                b'P\xc3\xa9rez': (b'P\xc3\xa9rez', 'utf-8'),
 
212
                b'P\xe8rez':     (b'P\xe8rez', '8-bit'),
224
213
        }
225
214
        for string_, pair in pairs.items():
226
215
            self.assertEqual(pair, EmailMessage.string_with_encoding(string_))
 
216
 
 
217
 
 
218
class TestSend(tests.TestCase):
 
219
 
 
220
    def setUp(self):
 
221
        super(TestSend, self).setUp()
 
222
        self.messages = []
 
223
 
 
224
        def send_as_append(_self, msg):
 
225
            self.messages.append(msg.as_string(BOUNDARY))
 
226
 
 
227
        self.overrideAttr(SMTPConnection, 'send_email', send_as_append)
 
228
 
 
229
 
 
230
 
 
231
    def send_email(self, attachment=None, attachment_filename=None,
 
232
                   attachment_mime_subtype='plain'):
 
233
        class FakeConfig:
 
234
            def get(self, option):
 
235
                return None
 
236
 
 
237
        EmailMessage.send(FakeConfig(), 'from@from.com', 'to@to.com',
 
238
                          'subject', 'body',
 
239
                          attachment=attachment,
 
240
                          attachment_filename=attachment_filename,
 
241
                          attachment_mime_subtype=attachment_mime_subtype)
 
242
 
 
243
    def assertMessage(self, expected):
 
244
        self.assertLength(1, self.messages)
 
245
        self.assertEqualDiff(expected, self.messages[0])
 
246
 
 
247
    def test_send_plain(self):
 
248
        self.send_email(u'a\nb\nc\nd\ne\n', 'lines.txt')
 
249
        self.assertMessage(complex_multipart_message('plain'))
 
250
 
 
251
    def test_send_patch(self):
 
252
        self.send_email(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-patch')
 
253
        self.assertMessage(complex_multipart_message('x-patch'))
 
254
 
 
255
    def test_send_simple(self):
 
256
          self.send_email()
 
257
          self.assertMessage(SIMPLE_MESSAGE_ASCII)
 
258