/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: 2019-03-04 00:16:27 UTC
  • mfrom: (7293 work)
  • mto: This revision was merged to the branch mainline in revision 7318.
  • Revision ID: jelmer@jelmer.uk-20190304001627-v6u7o6pf97tukhek
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
Content-Disposition: inline
66
66
 
67
67
body
68
 
''' %  { 'version': _breezy_version, 'boundary': BOUNDARY }
 
68
''' % {'version': _breezy_version, 'boundary': BOUNDARY}
69
69
 
70
70
 
71
71
def final_newline_or_not(msg):
72
72
    if sys.version_info >= (2, 7, 6):
73
73
        # Some internals of python's email module changed in an (minor)
74
74
        # incompatible way: a final newline is appended in 2.7.6...
75
 
       msg += '\n'
 
75
        msg += '\n'
76
76
    return msg
77
77
 
78
78
 
95
95
d
96
96
e
97
97
 
98
 
--%(boundary)s--''' %  { 'boundary': BOUNDARY }
 
98
--%(boundary)s--''' % {'boundary': BOUNDARY}
99
99
    msg = final_newline_or_not(msg)
100
100
    return msg % (typ,)
101
101
 
123
123
        self.assertEqualDiff(simple_multipart_message(),
124
124
                             msg.as_string(BOUNDARY))
125
125
 
126
 
 
127
126
    def test_multipart_message_complex(self):
128
127
        msg = EmailMessage('from@from.com', 'to@to.com', 'subject', 'body')
129
128
        msg.add_inline_attachment(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-subtype')
131
130
                             msg.as_string(BOUNDARY))
132
131
 
133
132
    def test_headers_accept_unicode_and_utf8(self):
134
 
        for user in [ u'Pepe P\xe9rez <pperez@ejemplo.com>',
135
 
                'Pepe P\xc3\xa9red <pperez@ejemplo.com>' ]:
136
 
            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
137
136
 
138
137
            for header in ['From', 'To', 'Subject']:
139
138
                value = msg[header]
140
 
                value.encode('ascii') # no UnicodeDecodeError
 
139
                value.encode('ascii')  # no UnicodeDecodeError
141
140
 
142
141
    def test_headers_reject_8bit(self):
143
 
        for i in range(3): # from_address, to_address, subject
144
 
            x = [ b'"J. Random Developer" <jrandom@example.com>' ] * 3
 
142
        for i in range(3):  # from_address, to_address, subject
 
143
            x = [b'"J. Random Developer" <jrandom@example.com>'] * 3
145
144
            x[i] = b'Pepe P\xe9rez <pperez@ejemplo.com>'
146
145
            self.assertRaises(BzrBadParameterNotUnicode, EmailMessage, *x)
147
146
 
148
147
    def test_multiple_destinations(self):
149
 
        to_addresses = [ 'to1@to.com', 'to2@to.com', 'to3@to.com' ]
 
148
        to_addresses = ['to1@to.com', 'to2@to.com', 'to3@to.com']
150
149
        msg = EmailMessage('from@from.com', to_addresses, 'subject')
151
 
        self.assertContainsRe(msg.as_string(), 'To: ' +
152
 
                ', '.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 ^$
153
152
 
154
153
    def test_retrieving_headers(self):
155
154
        msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
156
155
        for header, value in [('From', 'from@from.com'), ('To', 'to@to.com'),
157
 
                ('Subject', 'subject')]:
 
156
                              ('Subject', 'subject')]:
158
157
            self.assertEqual(value, msg.get(header))
159
158
            self.assertEqual(value, msg[header])
160
159
        self.assertEqual(None, msg.get('Does-Not-Exist'))
192
191
        encoded = EmailMessage.address_to_encoded_header(address)
193
192
        self.assertEqual(address, encoded)
194
193
 
195
 
        address = u'Pepe P\xe9rez <pperez@ejemplo.com>' # unicode ok
 
194
        address = u'Pepe P\xe9rez <pperez@ejemplo.com>'  # unicode ok
196
195
        encoded = EmailMessage.address_to_encoded_header(address)
197
 
        self.assertTrue('pperez@ejemplo.com' in encoded) # addr must be unencoded
 
196
        # addr must be unencoded
 
197
        self.assertTrue('pperez@ejemplo.com' in encoded)
198
198
        self.assertEqual(address, decode(encoded))
199
199
 
200
 
        address = b'Pepe P\xe9rez <pperez@ejemplo.com>' # ISO-8859-1 not ok
 
200
        address = b'Pepe P\xe9rez <pperez@ejemplo.com>'  # ISO-8859-1 not ok
201
201
        self.assertRaises(BzrBadParameterNotUnicode,
202
 
                EmailMessage.address_to_encoded_header, address)
 
202
                          EmailMessage.address_to_encoded_header, address)
203
203
 
204
204
    def test_string_with_encoding(self):
205
205
        pairs = {
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'),
 
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'),
210
210
        }
211
211
        for string_, pair in pairs.items():
212
212
            self.assertEqual(pair, EmailMessage.string_with_encoding(string_))
223
223
 
224
224
        self.overrideAttr(SMTPConnection, 'send_email', send_as_append)
225
225
 
226
 
 
227
 
 
228
226
    def send_email(self, attachment=None, attachment_filename=None,
229
227
                   attachment_mime_subtype='plain'):
230
228
        class FakeConfig:
250
248
        self.assertMessage(complex_multipart_message('x-patch'))
251
249
 
252
250
    def test_send_simple(self):
253
 
          self.send_email()
254
 
          self.assertMessage(SIMPLE_MESSAGE_ASCII)
255
 
 
 
251
        self.send_email()
 
252
        self.assertMessage(SIMPLE_MESSAGE_ASCII)