Merge ~cjwatson/launchpad:py3-IncomingTestCase into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: f1cee414bd9bccf1ce8d5bf7f0efd06292587af6
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:py3-IncomingTestCase
Merge into: launchpad:master
Diff against target: 102 lines (+25/-12)
1 file modified
lib/lp/services/mail/tests/test_incoming.py (+25/-12)
Reviewer Review Type Date Requested Status
Cristian Gonzalez (community) Approve
Review via email: mp+398924@code.launchpad.net

Commit message

Fix IncomingTestCase for Python 3

Description of the change

This needed several adjustments, mainly to process email messages as bytes, but also because Python 3's email library handles non-ASCII characters in message headers somewhat differently.

To post a comment you must log in.
Revision history for this message
Cristian Gonzalez (cristiangsp) wrote :

Looks good!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/services/mail/tests/test_incoming.py b/lib/lp/services/mail/tests/test_incoming.py
2index a9d615a..57735c3 100644
3--- a/lib/lp/services/mail/tests/test_incoming.py
4+++ b/lib/lp/services/mail/tests/test_incoming.py
5@@ -2,6 +2,11 @@
6 # GNU Affero General Public License version 3 (see the file LICENSE).
7
8 from doctest import DocTestSuite
9+from email.header import (
10+ decode_header,
11+ Header,
12+ make_header,
13+ )
14 from email.mime.multipart import MIMEMultipart
15 import logging
16 import os
17@@ -16,6 +21,7 @@ import transaction
18 from zope.interface import implementer
19 from zope.security.management import setSecurityPolicy
20
21+from lp.services.compat import message_as_bytes
22 from lp.services.config import config
23 from lp.services.gpg.interfaces import (
24 GPGKeyExpired,
25@@ -117,7 +123,7 @@ class IncomingTestCase(TestCaseWithFactory):
26 "An error occurred while processing a mail you sent to "
27 "Launchpad's email\ninterface.\n\n\n"
28 "Error message:\n\nSignature couldn't be verified: "
29- "(7, 58, u'No data')",
30+ "(7, 58, %r)" % u"No data",
31 body)
32
33 def test_expired_key(self):
34@@ -213,8 +219,9 @@ class IncomingTestCase(TestCaseWithFactory):
35
36 def test_invalid_to_addresses(self):
37 # Invalid To: header should not be handled as an OOPS.
38- raw_mail = open(os.path.join(
39- testmails_path, 'invalid-to-header.txt')).read()
40+ with open(os.path.join(testmails_path, 'invalid-to-header.txt'),
41+ 'rb') as f:
42+ raw_mail = f.read()
43 # Due to the way handleMail works, even if we pass a valid To header
44 # to the TestMailer, as we're doing here, it falls back to parse all
45 # To and CC headers from the raw_mail. Also, TestMailer is used here
46@@ -224,20 +231,20 @@ class IncomingTestCase(TestCaseWithFactory):
47 self.assertEqual([], self.oopses)
48
49 def makeSentMessage(self, sender, to, subject='subject', body='body',
50- cc=None, handler_domain=None):
51+ cc=None, handler_domain=None):
52 if handler_domain is None:
53 extra, handler_domain = to.split('@')
54 test_handler = FakeHandler()
55 mail_handlers.add(handler_domain, test_handler)
56 message = MIMEMultipart()
57 message['Message-Id'] = '<message-id>'
58- message['To'] = to
59- message['From'] = sender
60- message['Subject'] = subject
61+ message['To'] = Header(to)
62+ message['From'] = Header(sender)
63+ message['Subject'] = Header(subject)
64 if cc is not None:
65 message['Cc'] = cc
66 message.set_payload(body)
67- TestMailer().send(sender, to, message.as_string())
68+ TestMailer().send(sender, to, message_as_bytes(message))
69 return message, test_handler
70
71 def test_invalid_from_address_no_at(self):
72@@ -261,20 +268,26 @@ class IncomingTestCase(TestCaseWithFactory):
73 def test_invalid_from_address_unicode(self):
74 # Invalid From: header such as no "@" is handled.
75 message, test_handler = self.makeSentMessage(
76- 'm\xeda@eg.dom', 'test@lp.dev')
77+ u'm\xeda@eg.dom', 'test@lp.dev')
78 handleMail()
79 self.assertEqual([], self.oopses)
80 self.assertEqual(1, len(test_handler.handledMails))
81- self.assertEqual('m\xeda@eg.dom', test_handler.handledMails[0]['From'])
82+ self.assertEqual(
83+ u'm\xeda@eg.dom',
84+ six.text_type(make_header(decode_header(
85+ test_handler.handledMails[0]['From']))))
86
87 def test_invalid_cc_address_unicode(self):
88 # Invalid Cc: header such as no "@" is handled.
89 message, test_handler = self.makeSentMessage(
90- 'me@eg.dom', 'test@lp.dev', cc='m\xeda@eg.dom')
91+ 'me@eg.dom', 'test@lp.dev', cc=u'm\xeda@eg.dom')
92 handleMail()
93 self.assertEqual([], self.oopses)
94 self.assertEqual(1, len(test_handler.handledMails))
95- self.assertEqual('m\xeda@eg.dom', test_handler.handledMails[0]['Cc'])
96+ self.assertEqual(
97+ u'm\xeda@eg.dom',
98+ six.text_type(make_header(decode_header(
99+ test_handler.handledMails[0]['Cc']))))
100
101
102 class AuthenticateEmailTestCase(TestCaseWithFactory):

Subscribers

People subscribed via source and target branches

to status/vote changes: