/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: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 2009, 2011, 2014, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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
 
from bzrlib import __version__ as _bzrlib_version
21
 
from bzrlib.email_message import EmailMessage
22
 
from bzrlib.errors import BzrBadParameterNotUnicode
23
 
from bzrlib.smtp_connection import SMTPConnection
24
 
from bzrlib import tests
 
20
from .. import __version__ as _breezy_version
 
21
from ..email_message import EmailMessage
 
22
from ..errors import BzrBadParameterNotUnicode
 
23
from ..smtp_connection import SMTPConnection
 
24
from .. import tests
25
25
 
26
26
EMPTY_MESSAGE = '''\
27
27
From: from@from.com
29
29
To: to@to.com
30
30
User-Agent: Bazaar (%s)
31
31
 
32
 
''' % _bzrlib_version
 
32
''' % _breezy_version
33
33
 
34
34
_SIMPLE_MESSAGE = '''\
35
35
MIME-Version: 1.0
40
40
To: to@to.com
41
41
User-Agent: Bazaar (%s)
42
42
 
43
 
%%s''' % _bzrlib_version
 
43
%%s''' % _breezy_version
44
44
 
45
45
SIMPLE_MESSAGE_ASCII = _SIMPLE_MESSAGE % ('us-ascii', '7bit', 'body')
46
46
SIMPLE_MESSAGE_UTF8 = _SIMPLE_MESSAGE % ('utf-8', 'base64', 'YsOzZHk=\n')
64
64
Content-Disposition: inline
65
65
 
66
66
body
67
 
''' %  { 'version': _bzrlib_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'
 
67
''' % {'version': _breezy_version, 'boundary': BOUNDARY}
 
68
 
 
69
 
 
70
def simple_multipart_message():
 
71
    msg = _MULTIPART_HEAD + '--%s--\n' % BOUNDARY
75
72
    return msg
76
73
 
77
74
 
78
 
def simple_multipart_message():
79
 
    msg = _MULTIPART_HEAD + '--%s--' % BOUNDARY
80
 
    return final_newline_or_not(msg)
81
 
 
82
 
 
83
75
def complex_multipart_message(typ):
84
76
    msg = _MULTIPART_HEAD + '''\
85
77
--%(boundary)s
94
86
d
95
87
e
96
88
 
97
 
--%(boundary)s--''' %  { 'boundary': BOUNDARY }
98
 
    msg = final_newline_or_not(msg)
 
89
--%(boundary)s--
 
90
''' % {'boundary': BOUNDARY}
99
91
    return msg % (typ,)
100
92
 
101
93
 
103
95
 
104
96
    def test_empty_message(self):
105
97
        msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
106
 
        self.assertEqualDiff(EMPTY_MESSAGE , msg.as_string())
 
98
        self.assertEqualDiff(EMPTY_MESSAGE, msg.as_string())
107
99
 
108
100
    def test_simple_message(self):
109
101
        pairs = {
110
 
            'body': SIMPLE_MESSAGE_ASCII,
 
102
            b'body': SIMPLE_MESSAGE_ASCII,
111
103
            u'b\xf3dy': SIMPLE_MESSAGE_UTF8,
112
 
            'b\xc3\xb3dy': SIMPLE_MESSAGE_UTF8,
113
 
            'b\xf4dy': SIMPLE_MESSAGE_8BIT,
 
104
            b'b\xc3\xb3dy': SIMPLE_MESSAGE_UTF8,
 
105
            b'b\xf4dy': SIMPLE_MESSAGE_8BIT,
114
106
        }
115
107
        for body, expected in pairs.items():
116
108
            msg = EmailMessage('from@from.com', 'to@to.com', 'subject', body)
122
114
        self.assertEqualDiff(simple_multipart_message(),
123
115
                             msg.as_string(BOUNDARY))
124
116
 
125
 
 
126
117
    def test_multipart_message_complex(self):
127
118
        msg = EmailMessage('from@from.com', 'to@to.com', 'subject', 'body')
128
119
        msg.add_inline_attachment(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-subtype')
130
121
                             msg.as_string(BOUNDARY))
131
122
 
132
123
    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
 
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
136
127
 
137
128
            for header in ['From', 'To', 'Subject']:
138
129
                value = msg[header]
139
 
                str(value).decode('ascii') # no UnicodeDecodeError
 
130
                value.encode('ascii')  # no UnicodeDecodeError
140
131
 
141
132
    def test_headers_reject_8bit(self):
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>'
 
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>'
145
136
            self.assertRaises(BzrBadParameterNotUnicode, EmailMessage, *x)
146
137
 
147
138
    def test_multiple_destinations(self):
148
 
        to_addresses = [ 'to1@to.com', 'to2@to.com', 'to3@to.com' ]
 
139
        to_addresses = ['to1@to.com', 'to2@to.com', 'to3@to.com']
149
140
        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 ^$
 
141
        self.assertContainsRe(msg.as_string(), 'To: '
 
142
                              + ', '.join(to_addresses))  # re.M can't be passed, so no ^$
152
143
 
153
144
    def test_retrieving_headers(self):
154
145
        msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
155
146
        for header, value in [('From', 'from@from.com'), ('To', 'to@to.com'),
156
 
                ('Subject', 'subject')]:
 
147
                              ('Subject', 'subject')]:
157
148
            self.assertEqual(value, msg.get(header))
158
149
            self.assertEqual(value, msg[header])
159
150
        self.assertEqual(None, msg.get('Does-Not-Exist'))
170
161
    def test_address_to_encoded_header(self):
171
162
        def decode(s):
172
163
            """Convert a RFC2047-encoded string to a unicode string."""
173
 
            return ' '.join([chunk.decode(encoding or 'ascii')
174
 
                             for chunk, encoding in decode_header(s)])
 
164
            return ''.join([chunk.decode(encoding or 'ascii')
 
165
                            for chunk, encoding in decode_header(s)])
175
166
 
176
167
        address = 'jrandom@example.com'
177
168
        encoded = EmailMessage.address_to_encoded_header(address)
185
176
        encoded = EmailMessage.address_to_encoded_header(address)
186
177
        self.assertEqual(address, encoded)
187
178
 
188
 
        address = u'Pepe P\xe9rez <pperez@ejemplo.com>' # unicode ok
189
 
        encoded = EmailMessage.address_to_encoded_header(address)
190
 
        self.assert_('pperez@ejemplo.com' in encoded) # addr must be unencoded
191
 
        self.assertEquals(address, decode(encoded))
192
 
 
193
 
        address = 'Pepe P\xc3\xa9red <pperez@ejemplo.com>' # UTF-8 ok
194
 
        encoded = EmailMessage.address_to_encoded_header(address)
195
 
        self.assert_('pperez@ejemplo.com' in encoded)
196
 
        self.assertEquals(address, decode(encoded).encode('utf-8'))
197
 
 
198
 
        address = 'Pepe P\xe9rez <pperez@ejemplo.com>' # ISO-8859-1 not ok
 
179
        address = u'Pepe P\xe9rez <pperez@ejemplo.com>'  # unicode ok
 
180
        encoded = EmailMessage.address_to_encoded_header(address)
 
181
        # addr must be unencoded
 
182
        self.assertTrue('pperez@ejemplo.com' in encoded)
 
183
        self.assertEqual(address, decode(encoded))
 
184
 
 
185
        address = b'Pepe P\xe9rez <pperez@ejemplo.com>'  # ISO-8859-1 not ok
199
186
        self.assertRaises(BzrBadParameterNotUnicode,
200
 
                EmailMessage.address_to_encoded_header, address)
 
187
                          EmailMessage.address_to_encoded_header, address)
201
188
 
202
189
    def test_string_with_encoding(self):
203
190
        pairs = {
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'),
 
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'),
209
195
        }
210
196
        for string_, pair in pairs.items():
211
197
            self.assertEqual(pair, EmailMessage.string_with_encoding(string_))
222
208
 
223
209
        self.overrideAttr(SMTPConnection, 'send_email', send_as_append)
224
210
 
225
 
 
226
 
 
227
211
    def send_email(self, attachment=None, attachment_filename=None,
228
212
                   attachment_mime_subtype='plain'):
229
213
        class FakeConfig:
249
233
        self.assertMessage(complex_multipart_message('x-patch'))
250
234
 
251
235
    def test_send_simple(self):
252
 
          self.send_email()
253
 
          self.assertMessage(SIMPLE_MESSAGE_ASCII)
254
 
 
 
236
        self.send_email()
 
237
        self.assertMessage(SIMPLE_MESSAGE_ASCII)