15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
from email.header import decode_header
19
from email.header import decode_header
20
except ImportError: # python < 3
21
from email.Header import decode_header
20
23
from .. import __version__ as _breezy_version
21
24
from ..email_message import EmailMessage
22
25
from ..errors import BzrBadParameterNotUnicode
23
from ..sixish import PY3, text_type
24
26
from ..smtp_connection import SMTPConnection
25
27
from .. import tests
65
67
Content-Disposition: inline
68
''' % {'version': _breezy_version, 'boundary': BOUNDARY}
70
''' % { 'version': _breezy_version, 'boundary': BOUNDARY }
71
73
def final_newline_or_not(msg):
72
74
if sys.version_info >= (2, 7, 6):
73
75
# Some internals of python's email module changed in an (minor)
74
76
# incompatible way: a final newline is appended in 2.7.6...
109
111
def test_simple_message(self):
111
b'body': SIMPLE_MESSAGE_ASCII,
113
'body': SIMPLE_MESSAGE_ASCII,
112
114
u'b\xf3dy': SIMPLE_MESSAGE_UTF8,
113
b'b\xc3\xb3dy': SIMPLE_MESSAGE_UTF8,
114
b'b\xf4dy': SIMPLE_MESSAGE_8BIT,
115
'b\xc3\xb3dy': SIMPLE_MESSAGE_UTF8,
116
'b\xf4dy': SIMPLE_MESSAGE_8BIT,
116
118
for body, expected in pairs.items():
117
119
msg = EmailMessage('from@from.com', 'to@to.com', 'subject', body)
123
125
self.assertEqualDiff(simple_multipart_message(),
124
126
msg.as_string(BOUNDARY))
126
129
def test_multipart_message_complex(self):
127
130
msg = EmailMessage('from@from.com', 'to@to.com', 'subject', 'body')
128
131
msg.add_inline_attachment(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-subtype')
130
133
msg.as_string(BOUNDARY))
132
135
def test_headers_accept_unicode_and_utf8(self):
133
for user in [u'Pepe P\xe9rez <pperez@ejemplo.com>',
134
'Pepe P\xc3\xa9red <pperez@ejemplo.com>']:
135
msg = EmailMessage(user, user, user) # no exception raised
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
137
140
for header in ['From', 'To', 'Subject']:
138
141
value = msg[header]
139
value.encode('ascii') # no UnicodeDecodeError
142
str(value).decode('ascii') # no UnicodeDecodeError
141
144
def test_headers_reject_8bit(self):
142
for i in range(3): # from_address, to_address, subject
143
x = [b'"J. Random Developer" <jrandom@example.com>'] * 3
144
x[i] = b'Pepe P\xe9rez <pperez@ejemplo.com>'
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>'
145
148
self.assertRaises(BzrBadParameterNotUnicode, EmailMessage, *x)
147
150
def test_multiple_destinations(self):
148
to_addresses = ['to1@to.com', 'to2@to.com', 'to3@to.com']
151
to_addresses = [ 'to1@to.com', 'to2@to.com', 'to3@to.com' ]
149
152
msg = EmailMessage('from@from.com', to_addresses, 'subject')
150
self.assertContainsRe(msg.as_string(), 'To: '
151
+ ', '.join(to_addresses)) # re.M can't be passed, so no ^$
153
self.assertContainsRe(msg.as_string(), 'To: ' +
154
', '.join(to_addresses)) # re.M can't be passed, so no ^$
153
156
def test_retrieving_headers(self):
154
157
msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
155
158
for header, value in [('From', 'from@from.com'), ('To', 'to@to.com'),
156
('Subject', 'subject')]:
159
('Subject', 'subject')]:
157
160
self.assertEqual(value, msg.get(header))
158
161
self.assertEqual(value, msg[header])
159
162
self.assertEqual(None, msg.get('Does-Not-Exist'))
170
173
def test_address_to_encoded_header(self):
172
175
"""Convert a RFC2047-encoded string to a unicode string."""
174
return ''.join([chunk.decode(encoding or 'ascii')
175
for chunk, encoding in decode_header(s)])
177
# Cope with python2 stripping whitespace.
178
# https://bugs.python.org/issue1467619
179
return ' '.join([chunk.decode(encoding or 'ascii')
180
for chunk, encoding in decode_header(s)])
176
return ' '.join([chunk.decode(encoding or 'ascii')
177
for chunk, encoding in decode_header(s)])
182
179
address = 'jrandom@example.com'
183
180
encoded = EmailMessage.address_to_encoded_header(address)
191
188
encoded = EmailMessage.address_to_encoded_header(address)
192
189
self.assertEqual(address, encoded)
194
address = u'Pepe P\xe9rez <pperez@ejemplo.com>' # unicode ok
195
encoded = EmailMessage.address_to_encoded_header(address)
196
# addr must be unencoded
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)
197
198
self.assertTrue('pperez@ejemplo.com' in encoded)
198
self.assertEqual(address, decode(encoded))
199
self.assertEqual(address, decode(encoded).encode('utf-8'))
200
address = b'Pepe P\xe9rez <pperez@ejemplo.com>' # ISO-8859-1 not ok
201
address = 'Pepe P\xe9rez <pperez@ejemplo.com>' # ISO-8859-1 not ok
201
202
self.assertRaises(BzrBadParameterNotUnicode,
202
EmailMessage.address_to_encoded_header, address)
203
EmailMessage.address_to_encoded_header, address)
204
205
def test_string_with_encoding(self):
206
u'Pepe': (b'Pepe', 'ascii'),
207
u'P\xe9rez': (b'P\xc3\xa9rez', 'utf-8'),
208
b'P\xc3\xa9rez': (b'P\xc3\xa9rez', 'utf-8'),
209
b'P\xe8rez': (b'P\xe8rez', '8-bit'),
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'),
211
213
for string_, pair in pairs.items():
212
214
self.assertEqual(pair, EmailMessage.string_with_encoding(string_))