/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: 2018-05-06 11:48:54 UTC
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180506114854-h4qd9ojaqy8wxjsd
Move .mailmap to root.

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
try:
 
19
    from email.header import decode_header
 
20
except ImportError:  # python < 3
 
21
    from email.Header import decode_header
19
22
 
20
23
from .. import __version__ as _breezy_version
21
24
from ..email_message import EmailMessage
22
25
from ..errors import BzrBadParameterNotUnicode
23
 
from ..sixish import PY3, text_type
24
26
from ..smtp_connection import SMTPConnection
25
27
from .. import tests
26
28
 
65
67
Content-Disposition: inline
66
68
 
67
69
body
68
 
''' % {'version': _breezy_version, 'boundary': BOUNDARY}
 
70
''' %  { 'version': _breezy_version, 'boundary': BOUNDARY }
69
71
 
70
72
 
71
73
def final_newline_or_not(msg):
72
74
    if sys.version_info >= (2, 7, 6):
73
75
        # Some internals of python's email module changed in an (minor)
74
76
        # incompatible way: a final newline is appended in 2.7.6...
75
 
        msg += '\n'
 
77
       msg += '\n'
76
78
    return msg
77
79
 
78
80
 
95
97
d
96
98
e
97
99
 
98
 
--%(boundary)s--''' % {'boundary': BOUNDARY}
 
100
--%(boundary)s--''' %  { 'boundary': BOUNDARY }
99
101
    msg = final_newline_or_not(msg)
100
102
    return msg % (typ,)
101
103
 
108
110
 
109
111
    def test_simple_message(self):
110
112
        pairs = {
111
 
            b'body': SIMPLE_MESSAGE_ASCII,
 
113
            'body': SIMPLE_MESSAGE_ASCII,
112
114
            u'b\xf3dy': SIMPLE_MESSAGE_UTF8,
113
 
            b'b\xc3\xb3dy': SIMPLE_MESSAGE_UTF8,
114
 
            b'b\xf4dy': SIMPLE_MESSAGE_8BIT,
 
115
            'b\xc3\xb3dy': SIMPLE_MESSAGE_UTF8,
 
116
            'b\xf4dy': SIMPLE_MESSAGE_8BIT,
115
117
        }
116
118
        for body, expected in pairs.items():
117
119
            msg = EmailMessage('from@from.com', 'to@to.com', 'subject', body)
123
125
        self.assertEqualDiff(simple_multipart_message(),
124
126
                             msg.as_string(BOUNDARY))
125
127
 
 
128
 
126
129
    def test_multipart_message_complex(self):
127
130
        msg = EmailMessage('from@from.com', 'to@to.com', 'subject', 'body')
128
131
        msg.add_inline_attachment(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-subtype')
130
133
                             msg.as_string(BOUNDARY))
131
134
 
132
135
    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
 
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
136
139
 
137
140
            for header in ['From', 'To', 'Subject']:
138
141
                value = msg[header]
139
 
                value.encode('ascii')  # no UnicodeDecodeError
 
142
                str(value).decode('ascii') # no UnicodeDecodeError
140
143
 
141
144
    def test_headers_reject_8bit(self):
142
 
        for i in range(3):  # from_address, to_address, subject
143
 
            x = [b'"J. Random Developer" <jrandom@example.com>'] * 3
144
 
            x[i] = b'Pepe P\xe9rez <pperez@ejemplo.com>'
 
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>'
145
148
            self.assertRaises(BzrBadParameterNotUnicode, EmailMessage, *x)
146
149
 
147
150
    def test_multiple_destinations(self):
148
 
        to_addresses = ['to1@to.com', 'to2@to.com', 'to3@to.com']
 
151
        to_addresses = [ 'to1@to.com', 'to2@to.com', 'to3@to.com' ]
149
152
        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 ^$
 
153
        self.assertContainsRe(msg.as_string(), 'To: ' +
 
154
                ', '.join(to_addresses)) # re.M can't be passed, so no ^$
152
155
 
153
156
    def test_retrieving_headers(self):
154
157
        msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
155
158
        for header, value in [('From', 'from@from.com'), ('To', 'to@to.com'),
156
 
                              ('Subject', 'subject')]:
 
159
                ('Subject', 'subject')]:
157
160
            self.assertEqual(value, msg.get(header))
158
161
            self.assertEqual(value, msg[header])
159
162
        self.assertEqual(None, msg.get('Does-Not-Exist'))
170
173
    def test_address_to_encoded_header(self):
171
174
        def decode(s):
172
175
            """Convert a RFC2047-encoded string to a unicode string."""
173
 
            if PY3:
174
 
                return ''.join([chunk.decode(encoding or 'ascii')
175
 
                                for chunk, encoding in decode_header(s)])
176
 
            else:
177
 
                # Cope with python2 stripping whitespace.
178
 
                # https://bugs.python.org/issue1467619
179
 
                return ' '.join([chunk.decode(encoding or 'ascii')
180
 
                                 for chunk, encoding in decode_header(s)])
 
176
            return ' '.join([chunk.decode(encoding or 'ascii')
 
177
                             for chunk, encoding in decode_header(s)])
181
178
 
182
179
        address = 'jrandom@example.com'
183
180
        encoded = EmailMessage.address_to_encoded_header(address)
191
188
        encoded = EmailMessage.address_to_encoded_header(address)
192
189
        self.assertEqual(address, encoded)
193
190
 
194
 
        address = u'Pepe P\xe9rez <pperez@ejemplo.com>'  # unicode ok
195
 
        encoded = EmailMessage.address_to_encoded_header(address)
196
 
        # addr must be unencoded
 
191
        address = u'Pepe P\xe9rez <pperez@ejemplo.com>' # unicode ok
 
192
        encoded = EmailMessage.address_to_encoded_header(address)
 
193
        self.assertTrue('pperez@ejemplo.com' in encoded) # addr must be unencoded
 
194
        self.assertEqual(address, decode(encoded))
 
195
 
 
196
        address = 'Pepe P\xc3\xa9red <pperez@ejemplo.com>' # UTF-8 ok
 
197
        encoded = EmailMessage.address_to_encoded_header(address)
197
198
        self.assertTrue('pperez@ejemplo.com' in encoded)
198
 
        self.assertEqual(address, decode(encoded))
 
199
        self.assertEqual(address, decode(encoded).encode('utf-8'))
199
200
 
200
 
        address = b'Pepe P\xe9rez <pperez@ejemplo.com>'  # ISO-8859-1 not ok
 
201
        address = 'Pepe P\xe9rez <pperez@ejemplo.com>' # ISO-8859-1 not ok
201
202
        self.assertRaises(BzrBadParameterNotUnicode,
202
 
                          EmailMessage.address_to_encoded_header, address)
 
203
                EmailMessage.address_to_encoded_header, address)
203
204
 
204
205
    def test_string_with_encoding(self):
205
206
        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'),
 
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'),
210
212
        }
211
213
        for string_, pair in pairs.items():
212
214
            self.assertEqual(pair, EmailMessage.string_with_encoding(string_))
223
225
 
224
226
        self.overrideAttr(SMTPConnection, 'send_email', send_as_append)
225
227
 
 
228
 
 
229
 
226
230
    def send_email(self, attachment=None, attachment_filename=None,
227
231
                   attachment_mime_subtype='plain'):
228
232
        class FakeConfig:
248
252
        self.assertMessage(complex_multipart_message('x-patch'))
249
253
 
250
254
    def test_send_simple(self):
251
 
        self.send_email()
252
 
        self.assertMessage(SIMPLE_MESSAGE_ASCII)
 
255
          self.send_email()
 
256
          self.assertMessage(SIMPLE_MESSAGE_ASCII)
 
257