/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_email_message.py

  • Committer: Jelmer Vernooij
  • Date: 2017-07-23 22:06:41 UTC
  • mfrom: (6738 trunk)
  • mto: This revision was merged to the branch mainline in revision 6739.
  • Revision ID: jelmer@jelmer.uk-20170723220641-69eczax9bmv8d6kk
Merge trunk, address review comments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
import sys
18
 
from email.header import decode_header
 
18
from email.Header import decode_header
19
19
 
20
20
from .. import __version__ as _breezy_version
21
21
from ..email_message import EmailMessage
64
64
Content-Disposition: inline
65
65
 
66
66
body
67
 
''' % {'version': _breezy_version, 'boundary': BOUNDARY}
 
67
''' %  { 'version': _breezy_version, 'boundary': BOUNDARY }
 
68
 
 
69
 
 
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...
 
74
       msg += '\n'
 
75
    return msg
68
76
 
69
77
 
70
78
def simple_multipart_message():
71
 
    msg = _MULTIPART_HEAD + '--%s--\n' % BOUNDARY
72
 
    return msg
 
79
    msg = _MULTIPART_HEAD + '--%s--' % BOUNDARY
 
80
    return final_newline_or_not(msg)
73
81
 
74
82
 
75
83
def complex_multipart_message(typ):
86
94
d
87
95
e
88
96
 
89
 
--%(boundary)s--
90
 
''' % {'boundary': BOUNDARY}
 
97
--%(boundary)s--''' %  { 'boundary': BOUNDARY }
 
98
    msg = final_newline_or_not(msg)
91
99
    return msg % (typ,)
92
100
 
93
101
 
95
103
 
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())
99
107
 
100
108
    def test_simple_message(self):
101
109
        pairs = {
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,
106
114
        }
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))
116
124
 
 
125
 
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))
122
131
 
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
127
136
 
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
131
140
 
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)
137
146
 
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 ^$
143
152
 
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):
162
171
        def decode(s):
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)])
166
175
 
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)
178
187
 
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))
 
192
 
 
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'))
184
197
 
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)
188
201
 
189
202
    def test_string_with_encoding(self):
190
203
        pairs = {
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'),
195
209
        }
196
210
        for string_, pair in pairs.items():
197
211
            self.assertEqual(pair, EmailMessage.string_with_encoding(string_))
208
222
 
209
223
        self.overrideAttr(SMTPConnection, 'send_email', send_as_append)
210
224
 
 
225
 
 
226
 
211
227
    def send_email(self, attachment=None, attachment_filename=None,
212
228
                   attachment_mime_subtype='plain'):
213
229
        class FakeConfig:
233
249
        self.assertMessage(complex_multipart_message('x-patch'))
234
250
 
235
251
    def test_send_simple(self):
236
 
        self.send_email()
237
 
        self.assertMessage(SIMPLE_MESSAGE_ASCII)
 
252
          self.send_email()
 
253
          self.assertMessage(SIMPLE_MESSAGE_ASCII)
 
254