From 68a893237c9cdf67f91909981e024d7bf306b73c Mon Sep 17 00:00:00 2001 From: Duda Dornelles Date: Fri, 9 Jan 2015 13:11:33 -0200 Subject: #164 sometimes a mail with more than one alternative will not have an html part --- service/go | 2 +- service/pixelated/adapter/model/mail.py | 5 +++-- service/test/unit/adapter/mail_test.py | 10 ++++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/service/go b/service/go index 241cae46..111f7515 100755 --- a/service/go +++ b/service/go @@ -24,7 +24,7 @@ function runPep8 { pep8 pixelated test --ignore=E501 } -if [ "$2" == 'test' ]; then +if [ "$1" == 'test' ]; then runPep8 runUnitTests runIntegrationTests diff --git a/service/pixelated/adapter/model/mail.py b/service/pixelated/adapter/model/mail.py index 98533a74..7984cb05 100644 --- a/service/pixelated/adapter/model/mail.py +++ b/service/pixelated/adapter/model/mail.py @@ -236,8 +236,9 @@ class PixelatedMail(Mail): @property def html_body(self): if self.parts and len(self.alternatives) > 1: - html_part = [e for e in self.alternatives if re.match('text/html', e['headers']['Content-Type'])][0] - return self._decode_part(html_part) + html_parts = [e for e in self.alternatives if re.match('text/html', e['headers'].get('Content-Type', ''))] + if len(html_parts): + return self._decode_part(html_parts[0]) @property def headers(self): diff --git a/service/test/unit/adapter/mail_test.py b/service/test/unit/adapter/mail_test.py index a845d665..9dc54e66 100644 --- a/service/test/unit/adapter/mail_test.py +++ b/service/test/unit/adapter/mail_test.py @@ -151,6 +151,16 @@ class TestPixelatedMail(unittest.TestCase): self.assertRegexpMatches(mail.html_body, '^

blablabla

$') self.assertRegexpMatches(mail.text_plain_body, '^blablabla$') + def test_html_is_none_if_multiple_alternatives_have_no_html_part(self): + parts = { + 'attachments': [], + 'alternatives': [ + {'content': u'content', 'headers': {u'Content-Type': u'text/plain; charset=us-ascii'}}, + {'content': u'', 'headers': {u'Some info': u'info'}}]} + + mail = PixelatedMail.from_soledad(None, None, None, parts=parts, soledad_querier=None) + self.assertIsNone(mail.html_body) + def test_percent_character_is_allowed_on_body(self): parts = {'alternatives': [], 'attachments': []} parts['alternatives'].append({'content': '100% happy with percentage symbol', 'headers': {'Content-Type': 'text/plain'}}) -- cgit v1.2.3