1
# Copyright (C) 2007, 2009, 2011, 2014, 2016 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
from email.header import decode_header
20
except ImportError: # python < 3
21
from email.Header import decode_header
23
from .. import __version__ as _breezy_version
24
from ..email_message import EmailMessage
25
from ..errors import BzrBadParameterNotUnicode
26
from ..smtp_connection import SMTPConnection
33
User-Agent: Bazaar (%s)
37
_SIMPLE_MESSAGE = '''\
39
Content-Type: text/plain; charset="%%s"
40
Content-Transfer-Encoding: %%s
44
User-Agent: Bazaar (%s)
46
%%s''' % _breezy_version
48
SIMPLE_MESSAGE_ASCII = _SIMPLE_MESSAGE % ('us-ascii', '7bit', 'body')
49
SIMPLE_MESSAGE_UTF8 = _SIMPLE_MESSAGE % ('utf-8', 'base64', 'YsOzZHk=\n')
50
SIMPLE_MESSAGE_8BIT = _SIMPLE_MESSAGE % ('8-bit', 'base64', 'YvRkeQ==\n')
53
BOUNDARY = '=====123456=='
55
_MULTIPART_HEAD = '''\
56
Content-Type: multipart/mixed; boundary="%(boundary)s"
61
User-Agent: Bazaar (%(version)s)
65
Content-Type: text/plain; charset="us-ascii"
66
Content-Transfer-Encoding: 7bit
67
Content-Disposition: inline
70
''' % { 'version': _breezy_version, 'boundary': BOUNDARY }
73
def final_newline_or_not(msg):
74
if sys.version_info >= (2, 7, 6):
75
# Some internals of python's email module changed in an (minor)
76
# incompatible way: a final newline is appended in 2.7.6...
81
def simple_multipart_message():
82
msg = _MULTIPART_HEAD + '--%s--' % BOUNDARY
83
return final_newline_or_not(msg)
86
def complex_multipart_message(typ):
87
msg = _MULTIPART_HEAD + '''\
90
Content-Type: text/%%s; charset="us-ascii"; name="lines.txt"
91
Content-Transfer-Encoding: 7bit
92
Content-Disposition: inline
100
--%(boundary)s--''' % { 'boundary': BOUNDARY }
101
msg = final_newline_or_not(msg)
105
class TestEmailMessage(tests.TestCase):
107
def test_empty_message(self):
108
msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
109
self.assertEqualDiff(EMPTY_MESSAGE, msg.as_string())
111
def test_simple_message(self):
113
'body': SIMPLE_MESSAGE_ASCII,
114
u'b\xf3dy': SIMPLE_MESSAGE_UTF8,
115
'b\xc3\xb3dy': SIMPLE_MESSAGE_UTF8,
116
'b\xf4dy': SIMPLE_MESSAGE_8BIT,
118
for body, expected in pairs.items():
119
msg = EmailMessage('from@from.com', 'to@to.com', 'subject', body)
120
self.assertEqualDiff(expected, msg.as_string())
122
def test_multipart_message_simple(self):
123
msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
124
msg.add_inline_attachment('body')
125
self.assertEqualDiff(simple_multipart_message(),
126
msg.as_string(BOUNDARY))
129
def test_multipart_message_complex(self):
130
msg = EmailMessage('from@from.com', 'to@to.com', 'subject', 'body')
131
msg.add_inline_attachment(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-subtype')
132
self.assertEqualDiff(complex_multipart_message('x-subtype'),
133
msg.as_string(BOUNDARY))
135
def test_headers_accept_unicode_and_utf8(self):
136
for user in [ u'Pepe P\xe9rez <pperez@ejemplo.com>',
137
'Pepe P\xc3\xa9red <pperez@ejemplo.com>' ]:
138
msg = EmailMessage(user, user, user) # no exception raised
140
for header in ['From', 'To', 'Subject']:
142
str(value).decode('ascii') # no UnicodeDecodeError
144
def test_headers_reject_8bit(self):
145
for i in range(3): # from_address, to_address, subject
146
x = [ '"J. Random Developer" <jrandom@example.com>' ] * 3
147
x[i] = 'Pepe P\xe9rez <pperez@ejemplo.com>'
148
self.assertRaises(BzrBadParameterNotUnicode, EmailMessage, *x)
150
def test_multiple_destinations(self):
151
to_addresses = [ 'to1@to.com', 'to2@to.com', 'to3@to.com' ]
152
msg = EmailMessage('from@from.com', to_addresses, 'subject')
153
self.assertContainsRe(msg.as_string(), 'To: ' +
154
', '.join(to_addresses)) # re.M can't be passed, so no ^$
156
def test_retrieving_headers(self):
157
msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
158
for header, value in [('From', 'from@from.com'), ('To', 'to@to.com'),
159
('Subject', 'subject')]:
160
self.assertEqual(value, msg.get(header))
161
self.assertEqual(value, msg[header])
162
self.assertEqual(None, msg.get('Does-Not-Exist'))
163
self.assertEqual(None, msg['Does-Not-Exist'])
164
self.assertEqual('None', msg.get('Does-Not-Exist', 'None'))
166
def test_setting_headers(self):
167
msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
168
msg['To'] = 'to2@to.com'
169
msg['Cc'] = 'cc@cc.com'
170
self.assertEqual('to2@to.com', msg['To'])
171
self.assertEqual('cc@cc.com', msg['Cc'])
173
def test_address_to_encoded_header(self):
175
"""Convert a RFC2047-encoded string to a unicode string."""
176
return ' '.join([chunk.decode(encoding or 'ascii')
177
for chunk, encoding in decode_header(s)])
179
address = 'jrandom@example.com'
180
encoded = EmailMessage.address_to_encoded_header(address)
181
self.assertEqual(address, encoded)
183
address = 'J Random Developer <jrandom@example.com>'
184
encoded = EmailMessage.address_to_encoded_header(address)
185
self.assertEqual(address, encoded)
187
address = '"J. Random Developer" <jrandom@example.com>'
188
encoded = EmailMessage.address_to_encoded_header(address)
189
self.assertEqual(address, encoded)
191
address = u'Pepe P\xe9rez <pperez@ejemplo.com>' # unicode ok
192
encoded = EmailMessage.address_to_encoded_header(address)
193
self.assertTrue('pperez@ejemplo.com' in encoded) # addr must be unencoded
194
self.assertEqual(address, decode(encoded))
196
address = 'Pepe P\xc3\xa9red <pperez@ejemplo.com>' # UTF-8 ok
197
encoded = EmailMessage.address_to_encoded_header(address)
198
self.assertTrue('pperez@ejemplo.com' in encoded)
199
self.assertEqual(address, decode(encoded).encode('utf-8'))
201
address = 'Pepe P\xe9rez <pperez@ejemplo.com>' # ISO-8859-1 not ok
202
self.assertRaises(BzrBadParameterNotUnicode,
203
EmailMessage.address_to_encoded_header, address)
205
def test_string_with_encoding(self):
207
u'Pepe': ('Pepe', 'ascii'),
208
u'P\xe9rez': ('P\xc3\xa9rez', 'utf-8'),
209
'Perez': ('Perez', 'ascii'), # u'Pepe' == 'Pepe'
210
'P\xc3\xa9rez': ('P\xc3\xa9rez', 'utf-8'),
211
'P\xe8rez': ('P\xe8rez', '8-bit'),
213
for string_, pair in pairs.items():
214
self.assertEqual(pair, EmailMessage.string_with_encoding(string_))
217
class TestSend(tests.TestCase):
220
super(TestSend, self).setUp()
223
def send_as_append(_self, msg):
224
self.messages.append(msg.as_string(BOUNDARY))
226
self.overrideAttr(SMTPConnection, 'send_email', send_as_append)
230
def send_email(self, attachment=None, attachment_filename=None,
231
attachment_mime_subtype='plain'):
233
def get(self, option):
236
EmailMessage.send(FakeConfig(), 'from@from.com', 'to@to.com',
238
attachment=attachment,
239
attachment_filename=attachment_filename,
240
attachment_mime_subtype=attachment_mime_subtype)
242
def assertMessage(self, expected):
243
self.assertLength(1, self.messages)
244
self.assertEqualDiff(expected, self.messages[0])
246
def test_send_plain(self):
247
self.send_email(u'a\nb\nc\nd\ne\n', 'lines.txt')
248
self.assertMessage(complex_multipart_message('plain'))
250
def test_send_patch(self):
251
self.send_email(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-patch')
252
self.assertMessage(complex_multipart_message('x-patch'))
254
def test_send_simple(self):
256
self.assertMessage(SIMPLE_MESSAGE_ASCII)