Commit 9c8624b1 authored by Carlos Ramos Carreño's avatar Carlos Ramos Carreño

Fix erp5 interface post in Python 2

See merge request nexedi/erp5!1925
parents a7991cfa 2348985c
......@@ -34,12 +34,49 @@ if six.PY3:
else:
from email import message_from_string as message_from_bytes
# pylint:enable=no-name-in-module
from email.generator import Generator
from Products.ERP5Type.tests.ERP5TypeLiveTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.Sequence import SequenceList
from Products.ERP5Type.Utils import bytes2str, str2bytes
from Products.ZSQLCatalog.SQLCatalog import SimpleQuery
from DateTime import DateTime
from six import StringIO
import re
def normalize_email_bytes(email_bytes):
# type: (bytes) -> str
"""
Normalizes the representation of email text, so that it can be compared.
The fields of the message are written in a predefined order, with
the `unixfrom` field removed and no line wrapping.
The code is intended to be compatible with both Python 2 and Python 3.
Args:
email_bytes: Content of the email, including headers.
Returns:
Normalized string representation of the e-mail contents.
"""
# Unfolding removes newlines followed by whitespace, as per RFC5322.
# This SHOULD be done by Python itself, but seemingly no-one cared
# enough.
email_bytes_unfolded = re.sub(
br"\r?\n(?P<space>\s)",
br"\g<space>",
email_bytes,
)
msg = message_from_bytes(email_bytes_unfolded)
fp = StringIO()
g = Generator(fp, mangle_from_=False, maxheaderlen=0)
g.flatten(msg)
return fp.getvalue()
class TestInterfacePost(ERP5TypeTestCase):
"""
......@@ -238,7 +275,7 @@ class TestInterfacePost(ERP5TypeTestCase):
for internet_message_post in internet_message_post_list:
self.assertEqual(internet_message_post.getSimulationState(), 'exported')
mail_object = email.message_from_string(internet_message_post.getData().decode())
mail_object = email.message_from_string(bytes2str(internet_message_post.getData()))
self.assertEqual(
internet_message_post.getReference(), mail_object['message-id'].strip('<>')
)
......@@ -255,8 +292,8 @@ class TestInterfacePost(ERP5TypeTestCase):
self.assertNotEqual((), last_message)
_, _, message_text = last_message
self.assertEqual(
bytes(message_from_bytes(message_text)),
bytes(message_from_bytes(sequence['internet_message_post'].getData())),
normalize_email_bytes(message_text),
normalize_email_bytes(sequence['internet_message_post'].getData()),
)
def _getMailHostMessageForRecipient(self, recipient_email_address):
......@@ -273,14 +310,14 @@ class TestInterfacePost(ERP5TypeTestCase):
message_list = self.portal.MailHost._message_list
self.assertEqual(len(message_list), len(self.recipient_list))
for post in sequence['internet_message_post_list']:
post_recipient = email.message_from_string(post.getData().decode())['to']
post_recipient = email.message_from_string(bytes2str(post.getData()))['to']
message_list = self._getMailHostMessageForRecipient(post_recipient)
self.assertEqual(len(message_list), 1)
message = message_list[0]
_, _, message_text = message
self.assertEqual(
bytes(message_from_bytes(message_text)),
bytes(message_from_bytes(post.getData())),
normalize_email_bytes(message_text),
normalize_email_bytes(post.getData()),
)
def stepCheckMailMessagePreviewDisplaysLatestInternetMessagePostData(self, sequence=None, sequence_list=None):
......@@ -295,7 +332,7 @@ class TestInterfacePost(ERP5TypeTestCase):
post = sequence['internet_message_post']
# Create a response mail object
mail_object = email.message_from_string(post.getData().decode())
mail_object = email.message_from_string(bytes2str(post.getData()))
sender = mail_object['from']
recipient = mail_object['to']
......@@ -310,7 +347,7 @@ class TestInterfacePost(ERP5TypeTestCase):
# Ingest it
response_post = self.portal.internet_message_post_module.newContent(
portal_type='Internet Message Post',
data=mail_object.as_string().encode(),
data=str2bytes(mail_object.as_string()),
)
response_post.prepareImport()
sequence['internet_message_post_response'] = response_post
......
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