~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_email_message.py

  • Committer: Vincent Ladeuil
  • Date: 2016-01-21 17:48:07 UTC
  • mto: This revision was merged to the branch mainline in revision 6613.
  • Revision ID: v.ladeuil+lp@free.fr-20160121174807-g4ybpaij9ln5wj6a
Make all transport put_bytes() raises TypeError when given unicode strings rather than bytes.

There was a mix of AssertionError or UnicodeEncodeError.

Also deleted test_put_file_unicode() which was bogus, files contain bytes not unicode strings.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
import sys
17
18
from email.Header import decode_header
18
19
 
19
20
from bzrlib import __version__ as _bzrlib_version
20
21
from bzrlib.email_message import EmailMessage
21
22
from bzrlib.errors import BzrBadParameterNotUnicode
22
23
from bzrlib.smtp_connection import SMTPConnection
23
 
from bzrlib.tests import TestCase
 
24
from bzrlib import tests
24
25
 
25
26
EMPTY_MESSAGE = '''\
26
27
From: from@from.com
65
66
body
66
67
''' %  { 'version': _bzrlib_version, 'boundary': BOUNDARY }
67
68
 
68
 
SIMPLE_MULTIPART_MESSAGE = _MULTIPART_HEAD + '--%s--' % BOUNDARY
69
 
 
70
 
COMPLEX_MULTIPART_MESSAGE = _MULTIPART_HEAD + '''\
 
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
 
76
 
 
77
 
 
78
def simple_multipart_message():
 
79
    msg = _MULTIPART_HEAD + '--%s--' % BOUNDARY
 
80
    return final_newline_or_not(msg)
 
81
 
 
82
 
 
83
def complex_multipart_message(typ):
 
84
    msg = _MULTIPART_HEAD + '''\
71
85
--%(boundary)s
72
86
MIME-Version: 1.0
73
87
Content-Type: text/%%s; charset="us-ascii"; name="lines.txt"
81
95
e
82
96
 
83
97
--%(boundary)s--''' %  { 'boundary': BOUNDARY }
84
 
 
85
 
 
86
 
class TestEmailMessage(TestCase):
 
98
    msg = final_newline_or_not(msg)
 
99
    return msg % (typ,)
 
100
 
 
101
 
 
102
class TestEmailMessage(tests.TestCase):
87
103
 
88
104
    def test_empty_message(self):
89
105
        msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
100
116
            msg = EmailMessage('from@from.com', 'to@to.com', 'subject', body)
101
117
            self.assertEqualDiff(expected, msg.as_string())
102
118
 
103
 
    def test_multipart_message(self):
 
119
    def test_multipart_message_simple(self):
104
120
        msg = EmailMessage('from@from.com', 'to@to.com', 'subject')
105
121
        msg.add_inline_attachment('body')
106
 
        self.assertEqualDiff(SIMPLE_MULTIPART_MESSAGE, msg.as_string(BOUNDARY))
107
 
 
 
122
        self.assertEqualDiff(simple_multipart_message(),
 
123
                             msg.as_string(BOUNDARY))
 
124
 
 
125
 
 
126
    def test_multipart_message_complex(self):
108
127
        msg = EmailMessage('from@from.com', 'to@to.com', 'subject', 'body')
109
128
        msg.add_inline_attachment(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-subtype')
110
 
        self.assertEqualDiff(COMPLEX_MULTIPART_MESSAGE % 'x-subtype',
111
 
                msg.as_string(BOUNDARY))
 
129
        self.assertEqualDiff(complex_multipart_message('x-subtype'),
 
130
                             msg.as_string(BOUNDARY))
112
131
 
113
132
    def test_headers_accept_unicode_and_utf8(self):
114
133
        for user in [ u'Pepe P\xe9rez <pperez@ejemplo.com>',
148
167
        self.assertEqual('to2@to.com', msg['To'])
149
168
        self.assertEqual('cc@cc.com', msg['Cc'])
150
169
 
151
 
    def test_send(self):
152
 
        class FakeConfig:
153
 
            def get_user_option(self, option):
154
 
                return None
155
 
 
156
 
        messages = []
157
 
 
158
 
        def send_as_append(_self, msg):
159
 
            messages.append(msg.as_string(BOUNDARY))
160
 
 
161
 
        old_send_email = SMTPConnection.send_email
162
 
        try:
163
 
            SMTPConnection.send_email = send_as_append
164
 
 
165
 
            EmailMessage.send(FakeConfig(), 'from@from.com', 'to@to.com',
166
 
                    'subject', 'body', u'a\nb\nc\nd\ne\n', 'lines.txt')
167
 
            self.assertEqualDiff(COMPLEX_MULTIPART_MESSAGE % 'plain',
168
 
                    messages[0])
169
 
            messages[:] = []
170
 
 
171
 
            EmailMessage.send(FakeConfig(), 'from@from.com', 'to@to.com',
172
 
                    'subject', 'body', u'a\nb\nc\nd\ne\n', 'lines.txt',
173
 
                    'x-patch')
174
 
            self.assertEqualDiff(COMPLEX_MULTIPART_MESSAGE % 'x-patch',
175
 
                    messages[0])
176
 
            messages[:] = []
177
 
 
178
 
            EmailMessage.send(FakeConfig(), 'from@from.com', 'to@to.com',
179
 
                    'subject', 'body')
180
 
            self.assertEqualDiff(SIMPLE_MESSAGE_ASCII , messages[0])
181
 
            messages[:] = []
182
 
        finally:
183
 
            SMTPConnection.send_email = old_send_email
184
 
 
185
170
    def test_address_to_encoded_header(self):
186
171
        def decode(s):
187
172
            """Convert a RFC2047-encoded string to a unicode string."""
224
209
        }
225
210
        for string_, pair in pairs.items():
226
211
            self.assertEqual(pair, EmailMessage.string_with_encoding(string_))
 
212
 
 
213
 
 
214
class TestSend(tests.TestCase):
 
215
 
 
216
    def setUp(self):
 
217
        super(TestSend, self).setUp()
 
218
        self.messages = []
 
219
 
 
220
        def send_as_append(_self, msg):
 
221
            self.messages.append(msg.as_string(BOUNDARY))
 
222
 
 
223
        self.overrideAttr(SMTPConnection, 'send_email', send_as_append)
 
224
 
 
225
 
 
226
 
 
227
    def send_email(self, attachment=None, attachment_filename=None,
 
228
                   attachment_mime_subtype='plain'):
 
229
        class FakeConfig:
 
230
            def get(self, option):
 
231
                return None
 
232
 
 
233
        EmailMessage.send(FakeConfig(), 'from@from.com', 'to@to.com',
 
234
                          'subject', 'body',
 
235
                          attachment=attachment,
 
236
                          attachment_filename=attachment_filename,
 
237
                          attachment_mime_subtype=attachment_mime_subtype)
 
238
 
 
239
    def assertMessage(self, expected):
 
240
        self.assertLength(1, self.messages)
 
241
        self.assertEqualDiff(expected, self.messages[0])
 
242
 
 
243
    def test_send_plain(self):
 
244
        self.send_email(u'a\nb\nc\nd\ne\n', 'lines.txt')
 
245
        self.assertMessage(complex_multipart_message('plain'))
 
246
 
 
247
    def test_send_patch(self):
 
248
        self.send_email(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-patch')
 
249
        self.assertMessage(complex_multipart_message('x-patch'))
 
250
 
 
251
    def test_send_simple(self):
 
252
          self.send_email()
 
253
          self.assertMessage(SIMPLE_MESSAGE_ASCII)
 
254