1
# Copyright (C) 2007, 2009, 2011, 2014, 2016 Canonical Ltd
1
# Copyright (C) 2007 Canonical Ltd
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
18
17
from email.Header import decode_header
20
19
from bzrlib import __version__ as _bzrlib_version
21
20
from bzrlib.email_message import EmailMessage
22
21
from bzrlib.errors import BzrBadParameterNotUnicode
23
22
from bzrlib.smtp_connection import SMTPConnection
24
from bzrlib import tests
23
from bzrlib.tests import TestCase
26
25
EMPTY_MESSAGE = '''\
27
26
From: from@from.com
67
66
''' % { 'version': _bzrlib_version, 'boundary': BOUNDARY }
70
def final_newline_or_not(msg):
71
if sys.version_info >= (2, 7, 6):
72
# Some internals of python's email module changed in an (minor)
73
# incompatible way: a final newline is appended in 2.7.6...
78
def simple_multipart_message():
79
msg = _MULTIPART_HEAD + '--%s--' % BOUNDARY
80
return final_newline_or_not(msg)
83
def complex_multipart_message(typ):
84
msg = _MULTIPART_HEAD + '''\
68
SIMPLE_MULTIPART_MESSAGE = _MULTIPART_HEAD + '--%s--' % BOUNDARY
70
COMPLEX_MULTIPART_MESSAGE = _MULTIPART_HEAD + '''\
87
73
Content-Type: text/%%s; charset="us-ascii"; name="lines.txt"
97
83
--%(boundary)s--''' % { 'boundary': BOUNDARY }
98
msg = final_newline_or_not(msg)
102
class TestEmailMessage(tests.TestCase):
86
class TestEmailMessage(TestCase):
104
88
def test_empty_message(self):
105
89
msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
116
100
msg = EmailMessage('from@from.com', 'to@to.com', 'subject', body)
117
101
self.assertEqualDiff(expected, msg.as_string())
119
def test_multipart_message_simple(self):
103
def test_multipart_message(self):
120
104
msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
121
105
msg.add_inline_attachment('body')
122
self.assertEqualDiff(simple_multipart_message(),
123
msg.as_string(BOUNDARY))
126
def test_multipart_message_complex(self):
106
self.assertEqualDiff(SIMPLE_MULTIPART_MESSAGE, msg.as_string(BOUNDARY))
127
108
msg = EmailMessage('from@from.com', 'to@to.com', 'subject', 'body')
128
109
msg.add_inline_attachment(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-subtype')
129
self.assertEqualDiff(complex_multipart_message('x-subtype'),
130
msg.as_string(BOUNDARY))
110
self.assertEqualDiff(COMPLEX_MULTIPART_MESSAGE % 'x-subtype',
111
msg.as_string(BOUNDARY))
132
113
def test_headers_accept_unicode_and_utf8(self):
133
114
for user in [ u'Pepe P\xe9rez <pperez@ejemplo.com>',
167
148
self.assertEqual('to2@to.com', msg['To'])
168
149
self.assertEqual('cc@cc.com', msg['Cc'])
153
def get(self, option):
158
def send_as_append(_self, msg):
159
messages.append(msg.as_string(BOUNDARY))
161
old_send_email = SMTPConnection.send_email
163
SMTPConnection.send_email = send_as_append
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',
171
EmailMessage.send(FakeConfig(), 'from@from.com', 'to@to.com',
172
'subject', 'body', u'a\nb\nc\nd\ne\n', 'lines.txt',
174
self.assertEqualDiff(COMPLEX_MULTIPART_MESSAGE % 'x-patch',
178
EmailMessage.send(FakeConfig(), 'from@from.com', 'to@to.com',
180
self.assertEqualDiff(SIMPLE_MESSAGE_ASCII , messages[0])
183
SMTPConnection.send_email = old_send_email
170
185
def test_address_to_encoded_header(self):
172
187
"""Convert a RFC2047-encoded string to a unicode string."""
188
203
address = u'Pepe P\xe9rez <pperez@ejemplo.com>' # unicode ok
189
204
encoded = EmailMessage.address_to_encoded_header(address)
190
self.assertTrue('pperez@ejemplo.com' in encoded) # addr must be unencoded
191
self.assertEqual(address, decode(encoded))
205
self.assert_('pperez@ejemplo.com' in encoded) # addr must be unencoded
206
self.assertEquals(address, decode(encoded))
193
208
address = 'Pepe P\xc3\xa9red <pperez@ejemplo.com>' # UTF-8 ok
194
209
encoded = EmailMessage.address_to_encoded_header(address)
195
self.assertTrue('pperez@ejemplo.com' in encoded)
196
self.assertEqual(address, decode(encoded).encode('utf-8'))
210
self.assert_('pperez@ejemplo.com' in encoded)
211
self.assertEquals(address, decode(encoded).encode('utf-8'))
198
213
address = 'Pepe P\xe9rez <pperez@ejemplo.com>' # ISO-8859-1 not ok
199
214
self.assertRaises(BzrBadParameterNotUnicode,
210
225
for string_, pair in pairs.items():
211
226
self.assertEqual(pair, EmailMessage.string_with_encoding(string_))
214
class TestSend(tests.TestCase):
217
super(TestSend, self).setUp()
220
def send_as_append(_self, msg):
221
self.messages.append(msg.as_string(BOUNDARY))
223
self.overrideAttr(SMTPConnection, 'send_email', send_as_append)
227
def send_email(self, attachment=None, attachment_filename=None,
228
attachment_mime_subtype='plain'):
230
def get(self, option):
233
EmailMessage.send(FakeConfig(), 'from@from.com', 'to@to.com',
235
attachment=attachment,
236
attachment_filename=attachment_filename,
237
attachment_mime_subtype=attachment_mime_subtype)
239
def assertMessage(self, expected):
240
self.assertLength(1, self.messages)
241
self.assertEqualDiff(expected, self.messages[0])
243
def test_send_plain(self):
244
self.send_email(u'a\nb\nc\nd\ne\n', 'lines.txt')
245
self.assertMessage(complex_multipart_message('plain'))
247
def test_send_patch(self):
248
self.send_email(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-patch')
249
self.assertMessage(complex_multipart_message('x-patch'))
251
def test_send_simple(self):
253
self.assertMessage(SIMPLE_MESSAGE_ASCII)