summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDuda Dornelles <ddornell@thoughtworks.com>2014-09-19 11:19:45 -0300
committerDuda Dornelles <ddornell@thoughtworks.com>2014-09-19 11:19:45 -0300
commit610d356c0ad2e9ea47946f0212a945ed663d0106 (patch)
tree936d0826481e376037067d95f03f5faddf925f28
parent90aff82d713e4c288612ad8c339e7613be706781 (diff)
PixelatedMail#to_smtp_format shouldnt add empty fields
-rw-r--r--service/pixelated/adapter/pixelated_mail.py12
-rw-r--r--service/test/adapter/pixelated_mail_test.py26
2 files changed, 29 insertions, 9 deletions
diff --git a/service/pixelated/adapter/pixelated_mail.py b/service/pixelated/adapter/pixelated_mail.py
index 5c3025b6..6d5a9335 100644
--- a/service/pixelated/adapter/pixelated_mail.py
+++ b/service/pixelated/adapter/pixelated_mail.py
@@ -138,10 +138,14 @@ class PixelatedMail:
def to_mime_multipart(self):
mime_multipart = MIMEMultipart()
- mime_multipart['To'] = ", ".join(self.headers['to'])
- mime_multipart['Cc'] = ", ".join(self.headers['cc'])
- mime_multipart['Bcc'] = ", ".join(self.headers['bcc'])
- mime_multipart['Subject'] = self.headers['subject']
+
+ for header in ['To', 'Cc', 'Bcc']:
+ if self.headers[header.lower()]:
+ mime_multipart[header] = ", ".join(self.headers[header.lower()])
+
+ if self.headers['subject']:
+ mime_multipart['Subject'] = self.headers['subject']
+
mime_multipart['Date'] = self.headers['date']
mime_multipart.attach(MIMEText(self.body, 'plain'))
return mime_multipart
diff --git a/service/test/adapter/pixelated_mail_test.py b/service/test/adapter/pixelated_mail_test.py
index 3ba5e659..7199fc55 100644
--- a/service/test/adapter/pixelated_mail_test.py
+++ b/service/test/adapter/pixelated_mail_test.py
@@ -26,7 +26,7 @@ from mock import Mock
class TestPixelatedMail(unittest.TestCase):
- mail_dict = {
+ mail_dict = lambda x: {
'body': 'Este \xe9 o corpo',
'header': {
'cc': ['cc@pixelated.org', 'anothercc@pixelated.org'],
@@ -60,7 +60,7 @@ class TestPixelatedMail(unittest.TestCase):
self.assertEqual(str(mail.headers['date']), leap_mail_date_in_iso_format)
def test_from_dict(self):
- mail = PixelatedMail.from_dict(self.mail_dict)
+ mail = PixelatedMail.from_dict(self.mail_dict())
self.assertEqual(mail.headers['cc'], ['cc@pixelated.org', 'anothercc@pixelated.org'])
self.assertEqual(mail.headers['to'], ['to@pixelated.org', 'anotherto@pixelated.org'])
@@ -73,7 +73,7 @@ class TestPixelatedMail(unittest.TestCase):
def test_from_dict_adds_current_date(self):
pixelated.support.date.iso_now = lambda: 'date now'
- mail = PixelatedMail.from_dict(self.mail_dict)
+ mail = PixelatedMail.from_dict(self.mail_dict())
self.assertEqual('date now', mail.headers['date'])
@@ -85,7 +85,7 @@ class TestPixelatedMail(unittest.TestCase):
def test_to_mime_multipart(self):
pixelated.support.date.iso_now = lambda: 'date now'
- mime_multipart = PixelatedMail.from_dict(self.mail_dict).to_mime_multipart()
+ mime_multipart = PixelatedMail.from_dict(self.mail_dict()).to_mime_multipart()
self.assertRegexpMatches(mime_multipart.as_string(), "\nTo: to@pixelated.org, anotherto@pixelated.org\n")
self.assertRegexpMatches(mime_multipart.as_string(), "\nCc: cc@pixelated.org, anothercc@pixelated.org\n")
@@ -94,9 +94,25 @@ class TestPixelatedMail(unittest.TestCase):
self.assertRegexpMatches(mime_multipart.as_string(), "\nSubject: Oi\n")
self.assertRegexpMatches(mime_multipart.as_string(), "\nEste \xe9 o corpo")
+ def test_to_mime_multipart_should_add_blank_fields(self):
+ pixelated.support.date.iso_now = lambda: 'date now'
+
+ mail_dict = self.mail_dict()
+ mail_dict['header']['to'] = ''
+ mail_dict['header']['bcc'] = ''
+ mail_dict['header']['cc'] = ''
+ mail_dict['header']['subject'] = ''
+
+ mime_multipart = PixelatedMail.from_dict(mail_dict).to_mime_multipart()
+
+ self.assertNotRegexpMatches(mime_multipart.as_string(), "\nTo: \n")
+ self.assertNotRegexpMatches(mime_multipart.as_string(), "\nBcc: \n")
+ self.assertNotRegexpMatches(mime_multipart.as_string(), "\nCc: \n")
+ self.assertNotRegexpMatches(mime_multipart.as_string(), "\nSubject: \n")
+
def test_smtp_format(self):
PixelatedMail.from_email_address = 'pixelated@org'
- mail = PixelatedMail.from_dict(self.mail_dict)
+ mail = PixelatedMail.from_dict(self.mail_dict())
smtp_format = mail.to_smtp_format()