15
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
18
from email.header import decode_header
23
20
from .. import __version__ as _breezy_version
24
21
from ..email_message import EmailMessage
67
64
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...
67
''' % {'version': _breezy_version, 'boundary': BOUNDARY}
70
def simple_multipart_message():
71
msg = _MULTIPART_HEAD + '--%s--\n' % BOUNDARY
81
def simple_multipart_message():
82
msg = _MULTIPART_HEAD + '--%s--' % BOUNDARY
83
return final_newline_or_not(msg)
86
75
def complex_multipart_message(typ):
87
76
msg = _MULTIPART_HEAD + '''\
111
100
def test_simple_message(self):
113
'body': SIMPLE_MESSAGE_ASCII,
102
b'body': SIMPLE_MESSAGE_ASCII,
114
103
u'b\xf3dy': SIMPLE_MESSAGE_UTF8,
115
'b\xc3\xb3dy': SIMPLE_MESSAGE_UTF8,
116
'b\xf4dy': SIMPLE_MESSAGE_8BIT,
104
b'b\xc3\xb3dy': SIMPLE_MESSAGE_UTF8,
105
b'b\xf4dy': SIMPLE_MESSAGE_8BIT,
118
107
for body, expected in pairs.items():
119
108
msg = EmailMessage('from@from.com', 'to@to.com', 'subject', body)
125
114
self.assertEqualDiff(simple_multipart_message(),
126
115
msg.as_string(BOUNDARY))
129
117
def test_multipart_message_complex(self):
130
118
msg = EmailMessage('from@from.com', 'to@to.com', 'subject', 'body')
131
119
msg.add_inline_attachment(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-subtype')
133
121
msg.as_string(BOUNDARY))
135
123
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
124
for user in [u'Pepe P\xe9rez <pperez@ejemplo.com>',
125
'Pepe P\xc3\xa9red <pperez@ejemplo.com>']:
126
msg = EmailMessage(user, user, user) # no exception raised
140
128
for header in ['From', 'To', 'Subject']:
141
129
value = msg[header]
142
str(value).decode('ascii') # no UnicodeDecodeError
130
value.encode('ascii') # no UnicodeDecodeError
144
132
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>'
133
for i in range(3): # from_address, to_address, subject
134
x = [b'"J. Random Developer" <jrandom@example.com>'] * 3
135
x[i] = b'Pepe P\xe9rez <pperez@ejemplo.com>'
148
136
self.assertRaises(BzrBadParameterNotUnicode, EmailMessage, *x)
150
138
def test_multiple_destinations(self):
151
to_addresses = [ 'to1@to.com', 'to2@to.com', 'to3@to.com' ]
139
to_addresses = ['to1@to.com', 'to2@to.com', 'to3@to.com']
152
140
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 ^$
141
self.assertContainsRe(msg.as_string(), 'To: '
142
+ ', '.join(to_addresses)) # re.M can't be passed, so no ^$
156
144
def test_retrieving_headers(self):
157
145
msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
158
146
for header, value in [('From', 'from@from.com'), ('To', 'to@to.com'),
159
('Subject', 'subject')]:
147
('Subject', 'subject')]:
160
148
self.assertEqual(value, msg.get(header))
161
149
self.assertEqual(value, msg[header])
162
150
self.assertEqual(None, msg.get('Does-Not-Exist'))
173
161
def test_address_to_encoded_header(self):
175
163
"""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)])
164
return ''.join([chunk.decode(encoding or 'ascii')
165
for chunk, encoding in decode_header(s)])
179
167
address = 'jrandom@example.com'
180
168
encoded = EmailMessage.address_to_encoded_header(address)
188
176
encoded = EmailMessage.address_to_encoded_header(address)
189
177
self.assertEqual(address, encoded)
191
address = u'Pepe P\xe9rez <pperez@ejemplo.com>' # unicode ok
179
address = u'Pepe P\xe9rez <pperez@ejemplo.com>' # unicode ok
192
180
encoded = EmailMessage.address_to_encoded_header(address)
193
self.assertTrue('pperez@ejemplo.com' in encoded) # addr must be unencoded
181
# addr must be unencoded
182
self.assertTrue('pperez@ejemplo.com' in encoded)
194
183
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
185
address = b'Pepe P\xe9rez <pperez@ejemplo.com>' # ISO-8859-1 not ok
202
186
self.assertRaises(BzrBadParameterNotUnicode,
203
EmailMessage.address_to_encoded_header, address)
187
EmailMessage.address_to_encoded_header, address)
205
189
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'),
191
u'Pepe': (b'Pepe', 'ascii'),
192
u'P\xe9rez': (b'P\xc3\xa9rez', 'utf-8'),
193
b'P\xc3\xa9rez': (b'P\xc3\xa9rez', 'utf-8'),
194
b'P\xe8rez': (b'P\xe8rez', '8-bit'),
213
196
for string_, pair in pairs.items():
214
197
self.assertEqual(pair, EmailMessage.string_with_encoding(string_))