/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: 2020-05-06 02:13:25 UTC
  • mfrom: (7490.7.21 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200506021325-awbmmqu1zyorz7sj
Merge 3.1 branch.

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
 
try:
19
 
    from email.header import decode_header
20
 
except ImportError:  # python < 3
21
 
    from email.Header import decode_header
 
18
from email.header import decode_header
22
19
 
23
20
from .. import __version__ as _breezy_version
24
21
from ..email_message import EmailMessage
67
64
Content-Disposition: inline
68
65
 
69
66
body
70
 
''' %  { 'version': _breezy_version, 'boundary': BOUNDARY }
71
 
 
72
 
 
73
 
def final_newline_or_not(msg):
74
 
    if sys.version_info >= (2, 7, 6):
75
 
        # Some internals of python's email module changed in an (minor)
76
 
        # incompatible way: a final newline is appended in 2.7.6...
77
 
       msg += '\n'
 
67
''' % {'version': _breezy_version, 'boundary': BOUNDARY}
 
68
 
 
69
 
 
70
def simple_multipart_message():
 
71
    msg = _MULTIPART_HEAD + '--%s--\n' % BOUNDARY
78
72
    return msg
79
73
 
80
74
 
81
 
def simple_multipart_message():
82
 
    msg = _MULTIPART_HEAD + '--%s--' % BOUNDARY
83
 
    return final_newline_or_not(msg)
84
 
 
85
 
 
86
75
def complex_multipart_message(typ):
87
76
    msg = _MULTIPART_HEAD + '''\
88
77
--%(boundary)s
97
86
d
98
87
e
99
88
 
100
 
--%(boundary)s--''' %  { 'boundary': BOUNDARY }
101
 
    msg = final_newline_or_not(msg)
 
89
--%(boundary)s--
 
90
''' % {'boundary': BOUNDARY}
102
91
    return msg % (typ,)
103
92
 
104
93
 
110
99
 
111
100
    def test_simple_message(self):
112
101
        pairs = {
113
 
            'body': SIMPLE_MESSAGE_ASCII,
 
102
            b'body': SIMPLE_MESSAGE_ASCII,
114
103
            u'b\xf3dy': SIMPLE_MESSAGE_UTF8,
115
 
            'b\xc3\xb3dy': SIMPLE_MESSAGE_UTF8,
116
 
            'b\xf4dy': SIMPLE_MESSAGE_8BIT,
 
104
            b'b\xc3\xb3dy': SIMPLE_MESSAGE_UTF8,
 
105
            b'b\xf4dy': SIMPLE_MESSAGE_8BIT,
117
106
        }
118
107
        for body, expected in pairs.items():
119
108
            msg = EmailMessage('from@from.com', 'to@to.com', 'subject', body)
125
114
        self.assertEqualDiff(simple_multipart_message(),
126
115
                             msg.as_string(BOUNDARY))
127
116
 
128
 
 
129
117
    def test_multipart_message_complex(self):
130
118
        msg = EmailMessage('from@from.com', 'to@to.com', 'subject', 'body')
131
119
        msg.add_inline_attachment(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-subtype')
133
121
                             msg.as_string(BOUNDARY))
134
122
 
135
123
    def test_headers_accept_unicode_and_utf8(self):
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
 
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
139
127
 
140
128
            for header in ['From', 'To', 'Subject']:
141
129
                value = msg[header]
142
 
                str(value).decode('ascii') # no UnicodeDecodeError
 
130
                value.encode('ascii')  # no UnicodeDecodeError
143
131
 
144
132
    def test_headers_reject_8bit(self):
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>'
 
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>'
148
136
            self.assertRaises(BzrBadParameterNotUnicode, EmailMessage, *x)
149
137
 
150
138
    def test_multiple_destinations(self):
151
 
        to_addresses = [ 'to1@to.com', 'to2@to.com', 'to3@to.com' ]
 
139
        to_addresses = ['to1@to.com', 'to2@to.com', 'to3@to.com']
152
140
        msg = EmailMessage('from@from.com', to_addresses, 'subject')
153
 
        self.assertContainsRe(msg.as_string(), 'To: ' +
154
 
                ', '.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 ^$
155
143
 
156
144
    def test_retrieving_headers(self):
157
145
        msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
158
146
        for header, value in [('From', 'from@from.com'), ('To', 'to@to.com'),
159
 
                ('Subject', 'subject')]:
 
147
                              ('Subject', 'subject')]:
160
148
            self.assertEqual(value, msg.get(header))
161
149
            self.assertEqual(value, msg[header])
162
150
        self.assertEqual(None, msg.get('Does-Not-Exist'))
173
161
    def test_address_to_encoded_header(self):
174
162
        def decode(s):
175
163
            """Convert a RFC2047-encoded string to a unicode string."""
176
 
            return ' '.join([chunk.decode(encoding or 'ascii')
177
 
                             for chunk, encoding in decode_header(s)])
 
164
            return ''.join([chunk.decode(encoding or 'ascii')
 
165
                            for chunk, encoding in decode_header(s)])
178
166
 
179
167
        address = 'jrandom@example.com'
180
168
        encoded = EmailMessage.address_to_encoded_header(address)
188
176
        encoded = EmailMessage.address_to_encoded_header(address)
189
177
        self.assertEqual(address, encoded)
190
178
 
191
 
        address = u'Pepe P\xe9rez <pperez@ejemplo.com>' # unicode ok
 
179
        address = u'Pepe P\xe9rez <pperez@ejemplo.com>'  # unicode ok
192
180
        encoded = EmailMessage.address_to_encoded_header(address)
193
 
        self.assertTrue('pperez@ejemplo.com' in encoded) # addr must be unencoded
 
181
        # addr must be unencoded
 
182
        self.assertTrue('pperez@ejemplo.com' in encoded)
194
183
        self.assertEqual(address, decode(encoded))
195
184
 
196
 
        address = 'Pepe P\xc3\xa9red <pperez@ejemplo.com>' # UTF-8 ok
197
 
        encoded = EmailMessage.address_to_encoded_header(address)
198
 
        self.assertTrue('pperez@ejemplo.com' in encoded)
199
 
        self.assertEqual(address, decode(encoded).encode('utf-8'))
200
 
 
201
 
        address = 'Pepe P\xe9rez <pperez@ejemplo.com>' # ISO-8859-1 not ok
 
185
        address = b'Pepe P\xe9rez <pperez@ejemplo.com>'  # ISO-8859-1 not ok
202
186
        self.assertRaises(BzrBadParameterNotUnicode,
203
 
                EmailMessage.address_to_encoded_header, address)
 
187
                          EmailMessage.address_to_encoded_header, address)
204
188
 
205
189
    def test_string_with_encoding(self):
206
190
        pairs = {
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'),
 
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'),
212
195
        }
213
196
        for string_, pair in pairs.items():
214
197
            self.assertEqual(pair, EmailMessage.string_with_encoding(string_))
225
208
 
226
209
        self.overrideAttr(SMTPConnection, 'send_email', send_as_append)
227
210
 
228
 
 
229
 
 
230
211
    def send_email(self, attachment=None, attachment_filename=None,
231
212
                   attachment_mime_subtype='plain'):
232
213
        class FakeConfig:
252
233
        self.assertMessage(complex_multipart_message('x-patch'))
253
234
 
254
235
    def test_send_simple(self):
255
 
          self.send_email()
256
 
          self.assertMessage(SIMPLE_MESSAGE_ASCII)
257
 
 
 
236
        self.send_email()
 
237
        self.assertMessage(SIMPLE_MESSAGE_ASCII)