Commit 4eea613c authored by Jérome Perrin's avatar Jérome Perrin

testNotificationTool: py3

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