Commit 5e80d414 authored by Jérome Perrin's avatar Jérome Perrin Committed by Arnaud Fontaine

py3: DummyMailHostMixin message is bytes().

Co-authored-by: Arnaud Fontaine's avatarArnaud Fontaine <arnaud.fontaine@nexedi.com>
parent 531b8eca
...@@ -30,9 +30,13 @@ ...@@ -30,9 +30,13 @@
"""Test suite for erp5_accounting_l10n_fr """Test suite for erp5_accounting_l10n_fr
""" """
import six
import unittest import unittest
import zipfile import zipfile
import email if six.PY2:
from email import message_from_string as message_from_bytes
else:
from email import message_from_bytes
import os.path import os.path
import io import io
from DateTime import DateTime from DateTime import DateTime
...@@ -118,7 +122,7 @@ class TestAccounting_l10n_fr(AccountingTestCase): ...@@ -118,7 +122,7 @@ class TestAccounting_l10n_fr(AccountingTestCase):
self.assertNotEqual((), last_message) self.assertNotEqual((), last_message)
_, mto, message_text = last_message _, mto, message_text = last_message
self.assertEqual('"%s" <%s>' % (self.first_name, self.recipient_email_address), mto[0]) self.assertEqual('"%s" <%s>' % (self.first_name, self.recipient_email_address), mto[0])
mail_message = email.message_from_string(message_text) mail_message = message_from_bytes(message_text)
for part in mail_message.walk(): for part in mail_message.walk():
content_type = part.get_content_type() content_type = part.get_content_type()
file_name = part.get_filename() file_name = part.get_filename()
......
...@@ -644,7 +644,7 @@ class TestAuthenticationPolicy(ERP5TypeTestCase): ...@@ -644,7 +644,7 @@ class TestAuthenticationPolicy(ERP5TypeTestCase):
self.tic() self.tic()
_, (to,), message = self.portal.MailHost._last_message _, (to,), message = self.portal.MailHost._last_message
self.assertEqual(to, 'user@example.com') self.assertEqual(to, 'user@example.com')
self.assertIn('Password Recovery', message) self.assertIn(b'Password Recovery', message)
def test_HttpRequest(self): def test_HttpRequest(self):
""" """
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
# #
############################################################################## ##############################################################################
import six
import unittest import unittest
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
...@@ -33,7 +34,10 @@ from AccessControl.SecurityManagement import newSecurityManager ...@@ -33,7 +34,10 @@ from AccessControl.SecurityManagement import newSecurityManager
from AccessControl.SecurityManagement import getSecurityManager from AccessControl.SecurityManagement import getSecurityManager
from Products.ERP5Type.tests.Sequence import SequenceList from Products.ERP5Type.tests.Sequence import SequenceList
from Products.ERP5Type.tests.utils import DummyMailHost from Products.ERP5Type.tests.utils import DummyMailHost
import email if six.PY2:
from email import message_from_string as message_from_bytes
else:
from email import message_from_bytes
from email.header import decode_header, make_header from email.header import decode_header, make_header
from email.utils import parseaddr from email.utils import parseaddr
...@@ -47,7 +51,7 @@ def decode_email(file_): ...@@ -47,7 +51,7 @@ def decode_email(file_):
'headers': {} 'headers': {}
} }
# Get Message # Get Message
msg = email.message_from_string(file_) msg = message_from_bytes(file_)
# Back up original file # Back up original file
theMail['__original__'] = file_ theMail['__original__'] = file_
for key, value in msg.items(): for key, value in msg.items():
......
...@@ -125,7 +125,7 @@ class TestPasswordTool(ERP5TypeTestCase): ...@@ -125,7 +125,7 @@ class TestPasswordTool(ERP5TypeTestCase):
self.assertEqual(['userA@example.invalid'], mto) self.assertEqual(['userA@example.invalid'], mto)
reset_key, = list(six.iterkeys(self.portal.portal_password._password_request_dict)) reset_key, = list(six.iterkeys(self.portal.portal_password._password_request_dict))
self.assertIn( self.assertIn(
('PasswordTool_viewResetPassword?reset_key=' + reset_key).encode(), ('PasswordTool_viewResetPassword?reset_key=' + reset_key),
mbody) mbody)
ret = self.portal.portal_password.changeUserPassword( ret = self.portal.portal_password.changeUserPassword(
......
...@@ -26,13 +26,18 @@ ...@@ -26,13 +26,18 @@
# #
############################################################################## ##############################################################################
import six
import unittest import unittest
from Products.ERP5Type.tests.utils import reindex from Products.ERP5Type.tests.utils import reindex
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.utils import DummyMailHost from Products.ERP5Type.tests.utils import DummyMailHost
from Products.ERP5Type.tests.Sequence import SequenceList from Products.ERP5Type.tests.Sequence import SequenceList
from DateTime import DateTime from DateTime import DateTime
import email, re import re
if six.PY2:
from email import message_from_string as message_from_bytes
else:
from email import message_from_bytes
from email.header import decode_header, make_header from email.header import decode_header, make_header
from email.utils import parseaddr from email.utils import parseaddr
import six.moves.urllib.parse import six.moves.urllib.parse
...@@ -172,7 +177,7 @@ class TestERP5Credential(ERP5TypeTestCase): ...@@ -172,7 +177,7 @@ class TestERP5Credential(ERP5TypeTestCase):
'headers': {} 'headers': {}
} }
# Get Message # Get Message
msg = email.message_from_string(file_) msg = message_from_bytes(file_)
# Back up original file # Back up original file
theMail['__original__'] = file_ theMail['__original__'] = file_
for key, value in msg.items(): for key, value in msg.items():
......
...@@ -2043,7 +2043,7 @@ class TestCRMMailSend(BaseTestCRM): ...@@ -2043,7 +2043,7 @@ class TestCRMMailSend(BaseTestCRM):
self.assertEqual(5, len(self.portal.MailHost._message_list)) self.assertEqual(5, len(self.portal.MailHost._message_list))
for message_info in self.portal.MailHost._message_list: for message_info in self.portal.MailHost._message_list:
self.assertIn(mail_text_content, message_info[-1]) self.assertIn(mail_text_content, message_info[-1].decode())
message = message_from_bytes(message_info[-1]) message = message_from_bytes(message_info[-1])
self.assertTrue(DateTime(message.get("Date")).isCurrentDay()) self.assertTrue(DateTime(message.get("Date")).isCurrentDay())
...@@ -2061,7 +2061,7 @@ class TestCRMMailSend(BaseTestCRM): ...@@ -2061,7 +2061,7 @@ class TestCRMMailSend(BaseTestCRM):
attachment_list=[]) attachment_list=[])
self.tic() self.tic()
(from_url, to_url, last_message,), = self.portal.MailHost._message_list (from_url, to_url, last_message,), = self.portal.MailHost._message_list
self.assertIn("Body Simple Case", last_message) self.assertIn(b"Body Simple Case", last_message)
self.assertEqual('FG ER <eee@eee.com>', from_url) self.assertEqual('FG ER <eee@eee.com>', from_url)
self.assertEqual(['Expert User <expert@in24.test>'], to_url) self.assertEqual(['Expert User <expert@in24.test>'], to_url)
......
...@@ -29,12 +29,16 @@ ...@@ -29,12 +29,16 @@
############################################################################## ##############################################################################
import six
import unittest import unittest
from DateTime import DateTime from DateTime import DateTime
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.Sequence import SequenceList from Products.ERP5Type.tests.Sequence import SequenceList
from Products.ERP5Type.tests.utils import DummyMailHost from Products.ERP5Type.tests.utils import DummyMailHost
from email import message_from_string if six.PY2:
from email import message_from_string as message_from_bytes
else:
from email import message_from_bytes
from email.header import decode_header from email.header import decode_header
...@@ -218,7 +222,7 @@ class TestBug(ERP5TypeTestCase): ...@@ -218,7 +222,7 @@ class TestBug(ERP5TypeTestCase):
mfrom, mto, messageText = last_message mfrom, mto, messageText = last_message
self.assertEqual('dummy <loggedperson@localhost>', mfrom) self.assertEqual('dummy <loggedperson@localhost>', mfrom)
self.assertEqual(['person1@localhost'], mto) self.assertEqual(['person1@localhost'], mto)
message = message_from_string(messageText) message = message_from_bytes(messageText)
self.assertTrue(decode_header(message['Subject'])[0][0].endswith(bug.getTitle())) self.assertTrue(decode_header(message['Subject'])[0][0].endswith(bug.getTitle()))
def stepCheckBugMessageNotification(self, sequence=None, def stepCheckBugMessageNotification(self, sequence=None,
...@@ -232,7 +236,7 @@ class TestBug(ERP5TypeTestCase): ...@@ -232,7 +236,7 @@ class TestBug(ERP5TypeTestCase):
mfrom, mto, messageText = last_message mfrom, mto, messageText = last_message
self.assertEqual('person2@localhost', mfrom) self.assertEqual('person2@localhost', mfrom)
self.assertEqual(['person1@localhost'], mto) self.assertEqual(['person1@localhost'], mto)
message = message_from_string(messageText) message = message_from_bytes(messageText)
self.assertTrue(decode_header(message['Subject'])[0][0].endswith(bug.getTitle())) self.assertTrue(decode_header(message['Subject'])[0][0].endswith(bug.getTitle()))
def stepSetSourceProject(self, sequence=None, sequence_list=None, **kw): def stepSetSourceProject(self, sequence=None, sequence_list=None, **kw):
......
...@@ -1141,6 +1141,7 @@ class TestCMFActivity(ERP5TypeTestCase, LogInterceptor): ...@@ -1141,6 +1141,7 @@ class TestCMFActivity(ERP5TypeTestCase, LogInterceptor):
self.flushAllActivities(silent=1, loop_size=100) self.flushAllActivities(silent=1, loop_size=100)
# Check there is a traceback in the email notification # Check there is a traceback in the email notification
sender, recipients, mail = message_list.pop() sender, recipients, mail = message_list.pop()
mail = mail.decode()
self.assertIn("Module %s, line %s, in failingMethod" % ( self.assertIn("Module %s, line %s, in failingMethod" % (
__name__, inspect.getsourcelines(failingMethod)[1]), mail) __name__, inspect.getsourcelines(failingMethod)[1]), mail)
self.assertIn("ValueError:", mail) self.assertIn("ValueError:", mail)
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
# #
############################################################################## ##############################################################################
import six
import textwrap import textwrap
import unittest import unittest
import textwrap import textwrap
...@@ -36,7 +37,11 @@ from AccessControl.SecurityManagement import newSecurityManager ...@@ -36,7 +37,11 @@ from AccessControl.SecurityManagement import newSecurityManager
from Acquisition import aq_base from Acquisition import aq_base
from Products.ERP5OOo.tests.utils import Validator from Products.ERP5OOo.tests.utils import Validator
from lxml import html from lxml import html
import email, six.moves.urllib.parse, six.moves.http_client if six.PY2:
from email import message_from_string as message_from_bytes
else:
from email import message_from_bytes
import six.moves.urllib.parse, six.moves.http_client
from Products.Formulator.MethodField import Method from Products.Formulator.MethodField import Method
...@@ -133,7 +138,7 @@ class TestDeferredStyleBase(DeferredStyleTestCase): ...@@ -133,7 +138,7 @@ class TestDeferredStyleBase(DeferredStyleTestCase):
self.assertNotEqual(last_message, ()) self.assertNotEqual(last_message, ())
mfrom, mto, message_text = last_message mfrom, mto, message_text = last_message
self.assertEqual('"%s" <%s>' % (self.first_name, self.recipient_email_address), mto[0]) self.assertEqual('"%s" <%s>' % (self.first_name, self.recipient_email_address), mto[0])
mail_message = email.message_from_string(message_text) mail_message = message_from_bytes(message_text)
for part in mail_message.walk(): for part in mail_message.walk():
content_type = part.get_content_type() content_type = part.get_content_type()
file_name = part.get_filename() file_name = part.get_filename()
...@@ -198,7 +203,7 @@ class TestDeferredStyleBase(DeferredStyleTestCase): ...@@ -198,7 +203,7 @@ class TestDeferredStyleBase(DeferredStyleTestCase):
self.assertNotEqual(last_message, ()) self.assertNotEqual(last_message, ())
mfrom, mto, message_text = last_message mfrom, mto, message_text = last_message
self.assertEqual('"%s" <%s>' % (self.first_name, self.recipient_email_address), mto[0]) self.assertEqual('"%s" <%s>' % (self.first_name, self.recipient_email_address), mto[0])
mail_message = email.message_from_string(message_text) mail_message = message_from_bytes(message_text)
for part in mail_message.walk(): for part in mail_message.walk():
content_type = part.get_content_type() content_type = part.get_content_type()
if content_type == "text/html": if content_type == "text/html":
...@@ -236,7 +241,7 @@ class TestDeferredStyleBase(DeferredStyleTestCase): ...@@ -236,7 +241,7 @@ class TestDeferredStyleBase(DeferredStyleTestCase):
self.assertNotEqual(last_message, ()) self.assertNotEqual(last_message, ())
mfrom, mto, message_text = last_message mfrom, mto, message_text = last_message
self.assertEqual('"%s" <%s>' % (self.first_name, self.recipient_email_address), mto[0]) self.assertEqual('"%s" <%s>' % (self.first_name, self.recipient_email_address), mto[0])
mail_message = email.message_from_string(message_text) mail_message = message_from_bytes(message_text)
for part in mail_message.walk(): for part in mail_message.walk():
content_type = part.get_content_type() content_type = part.get_content_type()
if content_type == "text/html": if content_type == "text/html":
...@@ -260,7 +265,7 @@ class TestDeferredStyleBase(DeferredStyleTestCase): ...@@ -260,7 +265,7 @@ class TestDeferredStyleBase(DeferredStyleTestCase):
self.assertNotEqual(last_message, ()) self.assertNotEqual(last_message, ())
mfrom, mto, message_text = last_message mfrom, mto, message_text = last_message
self.assertEqual('"%s" <%s>' % (self.first_name, self.recipient_email_address), mto[0]) self.assertEqual('"%s" <%s>' % (self.first_name, self.recipient_email_address), mto[0])
mail_message = email.message_from_string(message_text) mail_message = message_from_bytes(message_text)
for part in mail_message.walk(): for part in mail_message.walk():
content_type = part.get_content_type() content_type = part.get_content_type()
if content_type == "text/html": if content_type == "text/html":
...@@ -285,7 +290,7 @@ class TestDeferredStyleBase(DeferredStyleTestCase): ...@@ -285,7 +290,7 @@ class TestDeferredStyleBase(DeferredStyleTestCase):
self.assertNotEqual(last_message, ()) self.assertNotEqual(last_message, ())
mfrom, mto, message_text = last_message mfrom, mto, message_text = last_message
self.assertEqual('"%s" <%s>' % (self.first_name, self.recipient_email_address), mto[0]) self.assertEqual('"%s" <%s>' % (self.first_name, self.recipient_email_address), mto[0])
mail_message = email.message_from_string(message_text) mail_message = message_from_bytes(message_text)
for part in mail_message.walk(): for part in mail_message.walk():
content_type = part.get_content_type() content_type = part.get_content_type()
if content_type == self.content_type: if content_type == self.content_type:
...@@ -315,7 +320,7 @@ class TestDeferredStyleBase(DeferredStyleTestCase): ...@@ -315,7 +320,7 @@ class TestDeferredStyleBase(DeferredStyleTestCase):
'HTTP_ACCEPT_LANGUAGE': 'fr;q=0.9,en;q=0.8', 'HTTP_ACCEPT_LANGUAGE': 'fr;q=0.9,en;q=0.8',
}) })
self.tic() self.tic()
mail_message = email.message_from_string(self.portal.MailHost._last_message[2]) mail_message = message_from_bytes(self.portal.MailHost._last_message[2])
# mail subject is translated # mail subject is translated
self.assertEqual('Historique', mail_message['subject']) self.assertEqual('Historique', mail_message['subject'])
# content is translated # content is translated
...@@ -345,11 +350,11 @@ class TestDeferredStyleBase(DeferredStyleTestCase): ...@@ -345,11 +350,11 @@ class TestDeferredStyleBase(DeferredStyleTestCase):
'HTTP_COOKIE': 'LOCALIZER_LANGUAGE="fr"', 'HTTP_COOKIE': 'LOCALIZER_LANGUAGE="fr"',
}) })
self.tic() self.tic()
mail_message = email.message_from_string(self.portal.MailHost._last_message[2]) mail_message = message_from_bytes(self.portal.MailHost._last_message[2])
# mail subject is translated # mail subject is translated
self.assertEqual('Historique', mail_message['subject']) self.assertEqual('Historique', mail_message['subject'])
# content is translated # content is translated
mail_message = email.message_from_string(self.portal.MailHost._last_message[2]) mail_message = message_from_bytes(self.portal.MailHost._last_message[2])
part, = [x for x in mail_message.walk() if x.get_content_type() == self.content_type] part, = [x for x in mail_message.walk() if x.get_content_type() == self.content_type]
self.assertIn( self.assertIn(
b'Historique', b'Historique',
...@@ -450,7 +455,7 @@ class TestDeferredStyleBase(DeferredStyleTestCase): ...@@ -450,7 +455,7 @@ class TestDeferredStyleBase(DeferredStyleTestCase):
# inspect the report as text and check the selection was initialized from # inspect the report as text and check the selection was initialized from
# request parameter. # request parameter.
mail_message = email.message_from_string(self.portal.MailHost._last_message[2]) mail_message = message_from_bytes(self.portal.MailHost._last_message[2])
part, = [x for x in mail_message.walk() if x.get_content_type() == self.content_type] part, = [x for x in mail_message.walk() if x.get_content_type() == self.content_type]
doc = self.portal.document_module.newContent( doc = self.portal.document_module.newContent(
...@@ -487,7 +492,7 @@ class TestODSDeferredStyle(TestDeferredStyleBase): ...@@ -487,7 +492,7 @@ class TestODSDeferredStyle(TestDeferredStyleBase):
self.assertNotEqual(last_message, ()) self.assertNotEqual(last_message, ())
mfrom, mto, message_text = last_message mfrom, mto, message_text = last_message
self.assertEqual('"%s" <%s>' % (self.first_name, self.recipient_email_address), mto[0]) self.assertEqual('"%s" <%s>' % (self.first_name, self.recipient_email_address), mto[0])
mail_message = email.message_from_string(message_text) mail_message = message_from_bytes(message_text)
for part in mail_message.walk(): for part in mail_message.walk():
content_type = part.get_content_type() content_type = part.get_content_type()
file_name = part.get_filename() file_name = part.get_filename()
......
...@@ -49,7 +49,6 @@ from Zope2.Startup.datatypes import ZopeDatabase ...@@ -49,7 +49,6 @@ from Zope2.Startup.datatypes import ZopeDatabase
from Testing import ZopeTestCase from Testing import ZopeTestCase
import Products.ERP5Type import Products.ERP5Type
from Products.MailHost.MailHost import MailHost from Products.MailHost.MailHost import MailHost
from email import message_from_string
from Products.ERP5Type.Globals import PersistentMapping from Products.ERP5Type.Globals import PersistentMapping
from Products.ERP5Type.Utils import simple_decorator from Products.ERP5Type.Utils import simple_decorator
from Products.ZSQLCatalog.SQLCatalog import Catalog from Products.ZSQLCatalog.SQLCatalog import Catalog
...@@ -59,9 +58,11 @@ import lxml.html ...@@ -59,9 +58,11 @@ import lxml.html
if six.PY2: if six.PY2:
FileIO = file FileIO = file
from email import message_from_string as message_from_bytes
else: else:
from io import FileIO from io import FileIO
from importlib import reload from importlib import reload
from email import message_from_bytes
def canonical_html(html): def canonical_html(html):
...@@ -106,12 +107,15 @@ class DummyMailHostMixin(object): ...@@ -106,12 +107,15 @@ class DummyMailHostMixin(object):
@staticmethod @staticmethod
def _decodeMessage(messageText): def _decodeMessage(messageText):
# type: (bytes) -> str
""" Decode message""" """ Decode message"""
message_text = messageText message_text = messageText
for part in message_from_string(messageText).walk(): for part in message_from_bytes(messageText).walk():
if part.get_content_type() in ['text/plain', 'text/html' ] \ if part.get_content_type() in ['text/plain', 'text/html' ] \
and not part.is_multipart(): and not part.is_multipart():
message_text = part.get_payload(decode=1) message_text = part.get_payload(decode=True)
if six.PY3:
message_text = message_text.decode(part.get_content_charset('ascii'))
return message_text return message_text
security.declarePrivate('getMessageList') security.declarePrivate('getMessageList')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment