示例#1
0
    def test_address_to_encoded_header(self):
        def decode(s):
            """Convert a RFC2047-encoded string to a unicode string."""
            return ' '.join([chunk.decode(encoding or 'ascii')
                             for chunk, encoding in decode_header(s)])

        address = '*****@*****.**'
        encoded = EmailMessage.address_to_encoded_header(address)
        self.assertEqual(address, encoded)

        address = 'J Random Developer <*****@*****.**>'
        encoded = EmailMessage.address_to_encoded_header(address)
        self.assertEqual(address, encoded)

        address = '"J. Random Developer" <*****@*****.**>'
        encoded = EmailMessage.address_to_encoded_header(address)
        self.assertEqual(address, encoded)

        address = u'Pepe P\xe9rez <*****@*****.**>' # unicode ok
        encoded = EmailMessage.address_to_encoded_header(address)
        self.assert_('*****@*****.**' in encoded) # addr must be unencoded
        self.assertEquals(address, decode(encoded))

        address = 'Pepe P\xc3\xa9red <*****@*****.**>' # UTF-8 ok
        encoded = EmailMessage.address_to_encoded_header(address)
        self.assert_('*****@*****.**' in encoded)
        self.assertEquals(address, decode(encoded).encode('utf-8'))

        address = 'Pepe P\xe9rez <*****@*****.**>' # ISO-8859-1 not ok
        self.assertRaises(BzrBadParameterNotUnicode,
                EmailMessage.address_to_encoded_header, address)
示例#2
0
    def test_address_to_encoded_header(self):
        def decode(s):
            """Convert a RFC2047-encoded string to a unicode string."""
            return ' '.join([
                chunk.decode(encoding or 'ascii')
                for chunk, encoding in decode_header(s)
            ])

        address = '*****@*****.**'
        encoded = EmailMessage.address_to_encoded_header(address)
        self.assertEqual(address, encoded)

        address = 'J Random Developer <*****@*****.**>'
        encoded = EmailMessage.address_to_encoded_header(address)
        self.assertEqual(address, encoded)

        address = '"J. Random Developer" <*****@*****.**>'
        encoded = EmailMessage.address_to_encoded_header(address)
        self.assertEqual(address, encoded)

        address = u'Pepe P\xe9rez <*****@*****.**>'  # unicode ok
        encoded = EmailMessage.address_to_encoded_header(address)
        self.assert_('*****@*****.**' in encoded)  # addr must be unencoded
        self.assertEquals(address, decode(encoded))

        address = 'Pepe P\xc3\xa9red <*****@*****.**>'  # UTF-8 ok
        encoded = EmailMessage.address_to_encoded_header(address)
        self.assert_('*****@*****.**' in encoded)
        self.assertEquals(address, decode(encoded).encode('utf-8'))

        address = 'Pepe P\xe9rez <*****@*****.**>'  # ISO-8859-1 not ok
        self.assertRaises(BzrBadParameterNotUnicode,
                          EmailMessage.address_to_encoded_header, address)
示例#3
0
 def test_retrieving_headers(self):
     msg = EmailMessage('*****@*****.**', '*****@*****.**', 'subject')
     for header, value in [('From', '*****@*****.**'), ('To', '*****@*****.**'),
             ('Subject', 'subject')]:
         self.assertEqual(value, msg.get(header))
         self.assertEqual(value, msg[header])
     self.assertEqual(None, msg.get('Does-Not-Exist'))
     self.assertEqual(None, msg['Does-Not-Exist'])
     self.assertEqual('None', msg.get('Does-Not-Exist', 'None'))
示例#4
0
    def test_multipart_message(self):
        msg = EmailMessage('*****@*****.**', '*****@*****.**', 'subject')
        msg.add_inline_attachment('body')
        self.assertEqualDiff(SIMPLE_MULTIPART_MESSAGE, msg.as_string(BOUNDARY))

        msg = EmailMessage('*****@*****.**', '*****@*****.**', 'subject', 'body')
        msg.add_inline_attachment(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-subtype')
        self.assertEqualDiff(COMPLEX_MULTIPART_MESSAGE % 'x-subtype',
                             msg.as_string(BOUNDARY))
示例#5
0
 def test_retrieving_headers(self):
     msg = EmailMessage('*****@*****.**', '*****@*****.**', 'subject')
     for header, value in [('From', '*****@*****.**'), ('To', '*****@*****.**'),
                           ('Subject', 'subject')]:
         self.assertEqual(value, msg.get(header))
         self.assertEqual(value, msg[header])
     self.assertEqual(None, msg.get('Does-Not-Exist'))
     self.assertEqual(None, msg['Does-Not-Exist'])
     self.assertEqual('None', msg.get('Does-Not-Exist', 'None'))
示例#6
0
 def test_simple_message(self):
     pairs = {
         'body': SIMPLE_MESSAGE_ASCII,
         u'b\xf3dy': SIMPLE_MESSAGE_UTF8,
         'b\xc3\xb3dy': SIMPLE_MESSAGE_UTF8,
         'b\xf4dy': SIMPLE_MESSAGE_8BIT,
     }
     for body, expected in pairs.items():
         msg = EmailMessage('*****@*****.**', '*****@*****.**', 'subject', body)
         self.assertEqualDiff(expected, msg.as_string())
示例#7
0
 def test_simple_message(self):
     pairs = {
         'body': SIMPLE_MESSAGE_ASCII,
         u'b\xf3dy': SIMPLE_MESSAGE_UTF8,
         'b\xc3\xb3dy': SIMPLE_MESSAGE_UTF8,
         'b\xf4dy': SIMPLE_MESSAGE_8BIT,
     }
     for body, expected in pairs.items():
         msg = EmailMessage('*****@*****.**', '*****@*****.**', 'subject', body)
         self.assertEqualDiff(expected, msg.as_string())
示例#8
0
    def send_email(self, attachment=None, attachment_filename=None,
                   attachment_mime_subtype='plain'):
        class FakeConfig:
            def get(self, option):
                return None

        EmailMessage.send(FakeConfig(), '*****@*****.**', '*****@*****.**',
                          'subject', 'body',
                          attachment=attachment,
                          attachment_filename=attachment_filename,
                          attachment_mime_subtype=attachment_mime_subtype)
示例#9
0
    def send_email(self,
                   attachment=None,
                   attachment_filename=None,
                   attachment_mime_subtype='plain'):
        class FakeConfig:
            def get(self, option):
                return None

        EmailMessage.send(FakeConfig(),
                          '*****@*****.**',
                          '*****@*****.**',
                          'subject',
                          'body',
                          attachment=attachment,
                          attachment_filename=attachment_filename,
                          attachment_mime_subtype=attachment_mime_subtype)
示例#10
0
    def test_multipart_message(self):
        msg = EmailMessage('*****@*****.**', '*****@*****.**', 'subject')
        msg.add_inline_attachment('body')
        self.assertEqualDiff(SIMPLE_MULTIPART_MESSAGE, msg.as_string(BOUNDARY))

        msg = EmailMessage('*****@*****.**', '*****@*****.**', 'subject', 'body')
        msg.add_inline_attachment(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-subtype')
        self.assertEqualDiff(COMPLEX_MULTIPART_MESSAGE % 'x-subtype',
                msg.as_string(BOUNDARY))
示例#11
0
 def test_string_with_encoding(self):
     pairs = {
         u'Pepe': ('Pepe', 'ascii'),
         u'P\xe9rez': ('P\xc3\xa9rez', 'utf-8'),
         'Perez': ('Perez', 'ascii'),  # u'Pepe' == 'Pepe'
         'P\xc3\xa9rez': ('P\xc3\xa9rez', 'utf-8'),
         'P\xe8rez': ('P\xe8rez', '8-bit'),
     }
     for string_, pair in pairs.items():
         self.assertEqual(pair, EmailMessage.string_with_encoding(string_))
示例#12
0
 def test_string_with_encoding(self):
     pairs = {
             u'Pepe':        ('Pepe', 'ascii'),
             u'P\xe9rez':    ('P\xc3\xa9rez', 'utf-8'),
             'Perez':         ('Perez', 'ascii'), # u'Pepe' == 'Pepe'
             'P\xc3\xa9rez': ('P\xc3\xa9rez', 'utf-8'),
             'P\xe8rez':     ('P\xe8rez', '8-bit'),
     }
     for string_, pair in pairs.items():
         self.assertEqual(pair, EmailMessage.string_with_encoding(string_))
示例#13
0
    def test_headers_accept_unicode_and_utf8(self):
        for user in [
                u'Pepe P\xe9rez <*****@*****.**>',
                'Pepe P\xc3\xa9red <*****@*****.**>'
        ]:
            msg = EmailMessage(user, user, user)  # no exception raised

            for header in ['From', 'To', 'Subject']:
                value = msg[header]
                str(value).decode('ascii')  # no UnicodeDecodeError
示例#14
0
    def test_send(self):
        class FakeConfig:
            def get_user_option(self, option):
                return None

        messages = []

        def send_as_append(_self, msg):
            messages.append(msg.as_string(BOUNDARY))

        old_send_email = SMTPConnection.send_email
        try:
            SMTPConnection.send_email = send_as_append

            EmailMessage.send(FakeConfig(), '*****@*****.**', '*****@*****.**',
                    'subject', 'body', u'a\nb\nc\nd\ne\n', 'lines.txt')
            self.assertEqualDiff(COMPLEX_MULTIPART_MESSAGE % 'plain',
                    messages[0])
            messages[:] = []

            EmailMessage.send(FakeConfig(), '*****@*****.**', '*****@*****.**',
                    'subject', 'body', u'a\nb\nc\nd\ne\n', 'lines.txt',
                    'x-patch')
            self.assertEqualDiff(COMPLEX_MULTIPART_MESSAGE % 'x-patch',
                    messages[0])
            messages[:] = []

            EmailMessage.send(FakeConfig(), '*****@*****.**', '*****@*****.**',
                    'subject', 'body')
            self.assertEqualDiff(SIMPLE_MESSAGE_ASCII , messages[0])
            messages[:] = []
        finally:
            SMTPConnection.send_email = old_send_email
示例#15
0
    def test_send(self):
        class FakeConfig:
            def get_user_option(self, option):
                return None

        messages = []

        def send_as_append(_self, msg):
            messages.append(msg.as_string(BOUNDARY))

        old_send_email = SMTPConnection.send_email
        try:
            SMTPConnection.send_email = send_as_append

            EmailMessage.send(FakeConfig(), '*****@*****.**', '*****@*****.**',
                              'subject', 'body', u'a\nb\nc\nd\ne\n',
                              'lines.txt')
            self.assertEqualDiff(COMPLEX_MULTIPART_MESSAGE % 'plain',
                                 messages[0])
            messages[:] = []

            EmailMessage.send(FakeConfig(), '*****@*****.**', '*****@*****.**',
                              'subject', 'body', u'a\nb\nc\nd\ne\n',
                              'lines.txt', 'x-patch')
            self.assertEqualDiff(COMPLEX_MULTIPART_MESSAGE % 'x-patch',
                                 messages[0])
            messages[:] = []

            EmailMessage.send(FakeConfig(), '*****@*****.**', '*****@*****.**',
                              'subject', 'body')
            self.assertEqualDiff(SIMPLE_MESSAGE_ASCII, messages[0])
            messages[:] = []
        finally:
            SMTPConnection.send_email = old_send_email
示例#16
0
    def to_email(self, mail_to, branch, sign=False):
        """Serialize as an email message.

        :param mail_to: The address to mail the message to
        :param branch: The source branch, to get the signing strategy and
            source email address
        :param sign: If True, gpg-sign the email
        :return: an email message
        """
        mail_from = branch.get_config().username()
        if self.message is not None:
            subject = self.message
        else:
            revision = branch.repository.get_revision(self.revision_id)
            subject = revision.message
        if sign:
            body = self.to_signed(branch)
        else:
            body = ''.join(self.to_lines())
        message = EmailMessage(mail_from, mail_to, subject, body)
        return message
示例#17
0
 def test_multiple_destinations(self):
     to_addresses = ['*****@*****.**', '*****@*****.**', '*****@*****.**']
     msg = EmailMessage('*****@*****.**', to_addresses, 'subject')
     self.assertContainsRe(
         msg.as_string(),
         'To: ' + ', '.join(to_addresses))  # re.M can't be passed, so no ^$
示例#18
0
 def test_multipart_message_simple(self):
     msg = EmailMessage('*****@*****.**', '*****@*****.**', 'subject')
     msg.add_inline_attachment('body')
     self.assertEqualDiff(simple_multipart_message(),
                          msg.as_string(BOUNDARY))
示例#19
0
 def test_setting_headers(self):
     msg = EmailMessage('*****@*****.**', '*****@*****.**', 'subject')
     msg['To'] = '*****@*****.**'
     msg['Cc'] = '*****@*****.**'
     self.assertEqual('*****@*****.**', msg['To'])
     self.assertEqual('*****@*****.**', msg['Cc'])
示例#20
0
 def test_empty_message(self):
     msg = EmailMessage('*****@*****.**', '*****@*****.**', 'subject')
     self.assertEqualDiff(EMPTY_MESSAGE , msg.as_string())
示例#21
0
 def test_empty_message(self):
     msg = EmailMessage('*****@*****.**', '*****@*****.**', 'subject')
     self.assertEqualDiff(EMPTY_MESSAGE, msg.as_string())
示例#22
0
 def test_multipart_message_complex(self):
     msg = EmailMessage('*****@*****.**', '*****@*****.**', 'subject', 'body')
     msg.add_inline_attachment(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-subtype')
     self.assertEqualDiff(complex_multipart_message('x-subtype'),
                          msg.as_string(BOUNDARY))
示例#23
0
 def test_multipart_message_simple(self):
     msg = EmailMessage('*****@*****.**', '*****@*****.**', 'subject')
     msg.add_inline_attachment('body')
     self.assertEqualDiff(simple_multipart_message(),
                          msg.as_string(BOUNDARY))
示例#24
0
 def test_multiple_destinations(self):
     to_addresses = [ '*****@*****.**', '*****@*****.**', '*****@*****.**' ]
     msg = EmailMessage('*****@*****.**', to_addresses, 'subject')
     self.assertContainsRe(msg.as_string(), 'To: ' +
             ', '.join(to_addresses)) # re.M can't be passed, so no ^$
示例#25
0
 def test_multipart_message_complex(self):
     msg = EmailMessage('*****@*****.**', '*****@*****.**', 'subject', 'body')
     msg.add_inline_attachment(u'a\nb\nc\nd\ne\n', 'lines.txt', 'x-subtype')
     self.assertEqualDiff(complex_multipart_message('x-subtype'),
                          msg.as_string(BOUNDARY))