From 3170c112c72db67419b020c8b3bd4491f1af8817 Mon Sep 17 00:00:00 2001 From: hparekh Date: Thu, 5 Nov 2015 10:38:58 +0530 Subject: [PATCH] Resolved encoding/decoding problem. In MIMEtext by default encodes in 'us-ascii' for py27 and 'utf-8' in py34. So 'utf-8' encoding is specified in argument. Also in py27 constant string is already in unicode type but in py34 it is byte type. So string is decoded for py27 in unit test. Partially-Implements: blueprint mistral-py3 Change-Id: I7004bae1d46d20630a30cac1abb65efe68765401 --- mistral/actions/std_actions.py | 10 ++++---- .../unit/actions/test_std_email_action.py | 25 +++++++++++++++++-- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/mistral/actions/std_actions.py b/mistral/actions/std_actions.py index 55af66665..ba60655d3 100644 --- a/mistral/actions/std_actions.py +++ b/mistral/actions/std_actions.py @@ -15,18 +15,19 @@ # See the License for the specific language governing permissions and # limitations under the License. +from email.header import Header from email.mime import text + import json import requests import smtplib -from oslo_log import log as logging - from mistral.actions import base from mistral import exceptions as exc from mistral.utils import javascript from mistral.utils import ssh_utils from mistral.workflow import utils as wf_utils +from oslo_log import log as logging LOG = logging.getLogger(__name__) @@ -286,9 +287,8 @@ class SendEmailAction(base.Action): (self.sender, self.to, self.subject, self.smtp_server, self.body[:128])) - # TODO(dzimine): handle utf-8, http://stackoverflow.com/a/14506784 - message = text.MIMEText(self.body) - message['Subject'] = self.subject + message = text.MIMEText(self.body, _charset='utf-8') + message['Subject'] = Header(self.subject, 'utf-8') message['From'] = self.sender message['To'] = self.to diff --git a/mistral/tests/unit/actions/test_std_email_action.py b/mistral/tests/unit/actions/test_std_email_action.py index 9f1554a61..ca1a18547 100644 --- a/mistral/tests/unit/actions/test_std_email_action.py +++ b/mistral/tests/unit/actions/test_std_email_action.py @@ -14,8 +14,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +import base64 +from email.header import decode_header from email import parser import mock +import six import testtools from mistral.actions import std_actions as std @@ -103,8 +106,26 @@ class SendEmailActionTest(base.BaseTest): self.assertEqual(self.from_addr, message['from']) self.assertEqual(self.to_addrs_str, message['to']) - self.assertEqual(self.subject, message['subject']) - self.assertEqual(self.body, message.get_payload()) + if six.PY3: + self.assertEqual( + self.subject, + decode_header(message['subject'])[0][0].decode('utf-8') + ) + else: + self.assertEqual( + self.subject.decode('utf-8'), + decode_header(message['subject'])[0][0].decode('utf-8') + ) + if six.PY3: + self.assertEqual( + self.body, + base64.b64decode(message.get_payload()).decode('utf-8') + ) + else: + self.assertEqual( + self.body.decode('utf-8'), + base64.b64decode(message.get_payload()).decode('utf-8') + ) @mock.patch('smtplib.SMTP') def test_with_password(self, smtp):