Commit b65e3c26 authored by Jérome Perrin's avatar Jérome Perrin

testNotificationTool: py3

parent d8430b71
......@@ -34,10 +34,11 @@ from AccessControl.SecurityManagement import getSecurityManager
from Products.ERP5Type.tests.Sequence import SequenceList
from Products.ERP5Type.tests.utils import DummyMailHost
import email
import six
from email.header import decode_header, make_header
from email.utils import parseaddr
# Copied from ERP5Type/patches/CMFMailIn.py
# Copied from bt5/erp5_egov/TestTemplateItem/testEGovMixin.py
def decode_email(file_):
# Prepare result
theMail = {
......@@ -47,22 +48,22 @@ def decode_email(file_):
'headers': {}
}
# Get Message
msg = email.message_from_string(file_)
msg = email.message_from_string(file_.decode())
# Back up original file
theMail['__original__'] = file_
# Recode headers to UTF-8 if needed
for key, value in msg.items():
for key, value in six.iteritems(msg):
decoded_value_list = decode_header(value)
unicode_value = make_header(decoded_value_list)
# TODO PY3
new_value = unicode_value.__unicode__().encode('utf-8')
new_value = make_header(decoded_value_list)
if six.PY2:
# Recode headers to UTF-8 if needed
new_value = new_value.__unicode__().encode('utf-8')
theMail['headers'][key.lower()] = new_value
# Filter mail addresses
for header in ('resent-to', 'resent-from', 'resent-cc', 'resent-sender',
'to', 'from', 'cc', 'sender', 'reply-to'):
'to', 'from', 'cc', 'sender', 'reply-to'):
header_field = theMail['headers'].get(header)
if header_field:
theMail['headers'][header] = parseaddr(header_field)[1]
theMail['headers'][header] = parseaddr(header_field.encode())[1]
# Get attachments
body_found = 0
for part in msg.walk():
......@@ -78,15 +79,17 @@ def decode_email(file_):
elif content_type == 'message/rfc822':
continue
elif content_type in ("text/plain", "text/html"):
charset = part.get_content_charset()
charset = part.get_content_charset() or 'utf-8'
payload = part.get_payload(decode=True)
#LOG('CMFMailIn -> ',0,'charset: %s, payload: %s' % (charset,payload))
if charset:
payload = unicode(payload, charset).encode('utf-8')
payload = payload.decode(charset)
if six.PY2:
payload = payload.encode('utf-8')
if body_found:
# Keep the content type
theMail['attachment_list'].append((file_name,
content_type, payload))
content_type, payload))
else:
theMail['body'] = payload
body_found = 1
......@@ -94,7 +97,7 @@ def decode_email(file_):
payload = part.get_payload(decode=True)
# Keep the content type
theMail['attachment_list'].append((file_name, content_type,
payload))
payload))
return theMail
class TestNotificationTool(ERP5TypeTestCase):
......
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