15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
from email.header import decode_header
18
from email.Header import decode_header
20
20
from .. import __version__ as _breezy_version
21
21
from ..email_message import EmailMessage
64
64
Content-Disposition: inline
67
''' % {'version': _breezy_version, 'boundary': BOUNDARY}
67
''' % { 'version': _breezy_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...
70
78
def simple_multipart_message():
71
msg = _MULTIPART_HEAD + '--%s--\n' % BOUNDARY
79
msg = _MULTIPART_HEAD + '--%s--' % BOUNDARY
80
return final_newline_or_not(msg)
75
83
def complex_multipart_message(typ):
96
104
def test_empty_message(self):
97
105
msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
98
self.assertEqualDiff(EMPTY_MESSAGE, msg.as_string())
106
self.assertEqualDiff(EMPTY_MESSAGE , msg.as_string())
100
108
def test_simple_message(self):
102
b'body': SIMPLE_MESSAGE_ASCII,
110
'body': SIMPLE_MESSAGE_ASCII,
103
111
u'b\xf3dy': SIMPLE_MESSAGE_UTF8,
104
b'b\xc3\xb3dy': SIMPLE_MESSAGE_UTF8,
105
b'b\xf4dy': SIMPLE_MESSAGE_8BIT,
112
'b\xc3\xb3dy': SIMPLE_MESSAGE_UTF8,
113
'b\xf4dy': SIMPLE_MESSAGE_8BIT,
107
115
for body, expected in pairs.items():
108
116
msg = EmailMessage('from@from.com', 'to@to.com', 'subject', body)
114
122
self.assertEqualDiff(simple_multipart_message(),
115
123
msg.as_string(BOUNDARY))
117
126
def test_multipart_message_complex(self):
118
127
msg = EmailMessage('from@from.com', 'to@to.com', 'subject', 'body')
119
128
msg.add_inline_attachment(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-subtype')
121
130
msg.as_string(BOUNDARY))
123
132
def test_headers_accept_unicode_and_utf8(self):
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
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
128
137
for header in ['From', 'To', 'Subject']:
129
138
value = msg[header]
130
value.encode('ascii') # no UnicodeDecodeError
139
str(value).decode('ascii') # no UnicodeDecodeError
132
141
def test_headers_reject_8bit(self):
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>'
142
for i in range(3): # from_address, to_address, subject
143
x = [ '"J. Random Developer" <jrandom@example.com>' ] * 3
144
x[i] = 'Pepe P\xe9rez <pperez@ejemplo.com>'
136
145
self.assertRaises(BzrBadParameterNotUnicode, EmailMessage, *x)
138
147
def test_multiple_destinations(self):
139
to_addresses = ['to1@to.com', 'to2@to.com', 'to3@to.com']
148
to_addresses = [ 'to1@to.com', 'to2@to.com', 'to3@to.com' ]
140
149
msg = EmailMessage('from@from.com', to_addresses, 'subject')
141
self.assertContainsRe(msg.as_string(), 'To: '
142
+ ', '.join(to_addresses)) # re.M can't be passed, so no ^$
150
self.assertContainsRe(msg.as_string(), 'To: ' +
151
', '.join(to_addresses)) # re.M can't be passed, so no ^$
144
153
def test_retrieving_headers(self):
145
154
msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
146
155
for header, value in [('From', 'from@from.com'), ('To', 'to@to.com'),
147
('Subject', 'subject')]:
156
('Subject', 'subject')]:
148
157
self.assertEqual(value, msg.get(header))
149
158
self.assertEqual(value, msg[header])
150
159
self.assertEqual(None, msg.get('Does-Not-Exist'))
161
170
def test_address_to_encoded_header(self):
163
172
"""Convert a RFC2047-encoded string to a unicode string."""
164
return ''.join([chunk.decode(encoding or 'ascii')
165
for chunk, encoding in decode_header(s)])
173
return ' '.join([chunk.decode(encoding or 'ascii')
174
for chunk, encoding in decode_header(s)])
167
176
address = 'jrandom@example.com'
168
177
encoded = EmailMessage.address_to_encoded_header(address)
176
185
encoded = EmailMessage.address_to_encoded_header(address)
177
186
self.assertEqual(address, encoded)
179
address = u'Pepe P\xe9rez <pperez@ejemplo.com>' # unicode ok
180
encoded = EmailMessage.address_to_encoded_header(address)
181
# addr must be unencoded
188
address = u'Pepe P\xe9rez <pperez@ejemplo.com>' # unicode ok
189
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))
193
address = 'Pepe P\xc3\xa9red <pperez@ejemplo.com>' # UTF-8 ok
194
encoded = EmailMessage.address_to_encoded_header(address)
182
195
self.assertTrue('pperez@ejemplo.com' in encoded)
183
self.assertEqual(address, decode(encoded))
196
self.assertEqual(address, decode(encoded).encode('utf-8'))
185
address = b'Pepe P\xe9rez <pperez@ejemplo.com>' # ISO-8859-1 not ok
198
address = 'Pepe P\xe9rez <pperez@ejemplo.com>' # ISO-8859-1 not ok
186
199
self.assertRaises(BzrBadParameterNotUnicode,
187
EmailMessage.address_to_encoded_header, address)
200
EmailMessage.address_to_encoded_header, address)
189
202
def test_string_with_encoding(self):
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'),
204
u'Pepe': ('Pepe', 'ascii'),
205
u'P\xe9rez': ('P\xc3\xa9rez', 'utf-8'),
206
'Perez': ('Perez', 'ascii'), # u'Pepe' == 'Pepe'
207
'P\xc3\xa9rez': ('P\xc3\xa9rez', 'utf-8'),
208
'P\xe8rez': ('P\xe8rez', '8-bit'),
196
210
for string_, pair in pairs.items():
197
211
self.assertEqual(pair, EmailMessage.string_with_encoding(string_))