summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorFolker Bernitt <fbernitt@thoughtworks.com>2015-03-20 13:45:29 +0100
committerFolker Bernitt <fbernitt@thoughtworks.com>2015-03-20 13:45:29 +0100
commitd43abfceecee6051f6c29c48e64a073455356cb5 (patch)
treec507ca77756772542dc5b8c1ea8b4ed142753c08 /service
parente500877b7de0fa1f249887e7afe089f560094893 (diff)
Added support for encoded To, Cc and Bcc headers.
- Issue #248
Diffstat (limited to 'service')
-rw-r--r--service/pixelated/adapter/model/mail.py10
-rw-r--r--service/pixelated/adapter/search/__init__.py7
-rw-r--r--service/test/unit/adapter/search/test_search.py31
-rw-r--r--service/test/unit/adapter/test_mail.py13
4 files changed, 53 insertions, 8 deletions
diff --git a/service/pixelated/adapter/model/mail.py b/service/pixelated/adapter/model/mail.py
index c0fdcab6..f23c2708 100644
--- a/service/pixelated/adapter/model/mail.py
+++ b/service/pixelated/adapter/model/mail.py
@@ -282,7 +282,7 @@ class PixelatedMail(Mail):
hdoc_headers = self.hdoc.content['headers']
for header in ['To', 'Cc', 'Bcc']:
- header_value = hdoc_headers.get(header)
+ header_value = self._decode_header(hdoc_headers.get(header))
if not header_value:
continue
_headers[header] = header_value if type(header_value) is list else header_value.split(',')
@@ -304,8 +304,12 @@ class PixelatedMail(Mail):
return _headers
def _decode_header(self, header):
- arr_header = decode_header(header)
- return arr_header[0][0]
+ if not header:
+ return None
+ if isinstance(header, list):
+ return [decode_header(entry)[0][0] for entry in header]
+ else:
+ return decode_header(header)[0][0]
def _get_date(self):
date = self.hdoc.content.get('date', None)
diff --git a/service/pixelated/adapter/search/__init__.py b/service/pixelated/adapter/search/__init__.py
index 331ea8fe..dcdade11 100644
--- a/service/pixelated/adapter/search/__init__.py
+++ b/service/pixelated/adapter/search/__init__.py
@@ -127,13 +127,14 @@ class SearchEngine(object):
tags = mdict.get('tags', [])
tags.append(mail.mailbox_name.lower())
bounced = mail.bounced if mail.bounced else ['']
+
index_data = {
'sender': unicode(header.get('from', '').decode('utf-8')),
'subject': unicode(header.get('subject', '').decode('utf-8')),
'date': milliseconds(header.get('date', '')),
- 'to': u','.join(header.get('to', [''])),
- 'cc': u','.join(header.get('cc', [''])),
- 'bcc': u','.join(header.get('bcc', [''])),
+ 'to': u','.join([h.decode('utf-8') for h in header.get('to', [''])]),
+ 'cc': u','.join([h.decode('utf-8') for h in header.get('cc', [''])]),
+ 'bcc': u','.join([h.decode('utf-8') for h in header.get('bcc', [''])]),
'tag': u','.join(unique(tags)),
'bounced': u','.join(bounced),
'body': unicode(mdict['textPlainBody']),
diff --git a/service/test/unit/adapter/search/test_search.py b/service/test/unit/adapter/search/test_search.py
index a3726b88..d57b8227 100644
--- a/service/test/unit/adapter/search/test_search.py
+++ b/service/test/unit/adapter/search/test_search.py
@@ -50,11 +50,40 @@ class SearchEngineTest(unittest.TestCase):
soledad_querier = mock()
lock_stub = LockStub()
when(soledad_querier).get_index_masterkey().thenReturn(INDEX_KEY)
+
+ self.assertEqual(INDEX_KEY, soledad_querier.get_index_masterkey())
se = SearchEngine(soledad_querier, self.agent_home)
se._write_lock = lock_stub
+ headers = {
+ 'From': 'from@bar.tld',
+ 'To': 'to@bar.tld',
+ 'Subject': 'Some test mail',
+ }
+
# when
- se.index_mail(test_helper.pixelated_mail())
+ se.index_mail(test_helper.pixelated_mail(extra_headers=headers))
# then
self.assertTrue(lock_stub.called)
+
+ def test_encoding(self):
+ # given
+ soledad_querier = mock()
+ when(soledad_querier).get_index_masterkey().thenReturn(INDEX_KEY)
+
+ se = SearchEngine(soledad_querier, self.agent_home)
+
+ headers = {
+ 'From': 'foo@bar.tld',
+ 'To': '=?utf-8?b?IsOEw7zDtiDDlsO8w6QiIDxmb2xrZXJAcGl4ZWxhdGVkLXByb2plY3Qub3Jn?=\n =?utf-8?b?PiwgRsO2bGtlciA8Zm9sa2VyQHBpeGVsYXRlZC1wcm9qZWN0Lm9yZz4=?=',
+ 'Cc': '=?utf-8?b?IsOEw7zDtiDDlsO8w6QiIDxmb2xrZXJAcGl4ZWxhdGVkLXByb2plY3Qub3Jn?=\n =?utf-8?b?PiwgRsO2bGtlciA8Zm9sa2VyQHBpeGVsYXRlZC1wcm9qZWN0Lm9yZz4=?=',
+ 'Subject': 'Some test mail',
+ }
+
+ # when
+ se.index_mail(test_helper.pixelated_mail(extra_headers=headers, chash='mailid'))
+
+ result = se.search('folker')
+
+ self.assertEqual((['mailid'], 1), result)
diff --git a/service/test/unit/adapter/test_mail.py b/service/test/unit/adapter/test_mail.py
index fc0861d6..1b83f79e 100644
--- a/service/test/unit/adapter/test_mail.py
+++ b/service/test/unit/adapter/test_mail.py
@@ -306,22 +306,33 @@ class TestPixelatedMail(unittest.TestCase):
def test_encoding_special_character_on_header(self):
subject = "=?UTF-8?Q?test_encoding_St=C3=A4ch?="
email_from = "=?UTF-8?Q?St=C3=A4ch_<stach@pixelated-project.org>?="
+ email_to = "=?utf-8?b?IsOEw7zDtiDDlsO8w6QiIDxmb2xrZXJAcGl4ZWxhdGVkLXByb2plY3Qub3Jn?=\n =?utf-8?b?PiwgRsO2bGtlciA8Zm9sa2VyQHBpeGVsYXRlZC1wcm9qZWN0Lm9yZz4=?="
pixel_mail = PixelatedMail()
self.assertEqual(pixel_mail._decode_header(subject), 'test encoding St\xc3\xa4ch')
self.assertEqual(pixel_mail._decode_header(email_from), 'St\xc3\xa4ch <stach@pixelated-project.org>')
+ self.assertEqual(pixel_mail._decode_header(email_to), '"\xc3\x84\xc3\xbc\xc3\xb6 \xc3\x96\xc3\xbc\xc3\xa4" <folker@pixelated-project.org>, F\xc3\xb6lker <folker@pixelated-project.org>')
+ self.assertEqual(pixel_mail._decode_header(None), None)
def test_headers_are_encoded_right(self):
subject = "=?UTF-8?Q?test_encoding_St=C3=A4ch?="
email_from = "=?UTF-8?Q?St=C3=A4ch_<stach@pixelated-project.org>?="
+ email_to = "=?utf-8?b?IsOEw7zDtiDDlsO8w6QiIDxmb2xrZXJAcGl4ZWxhdGVkLXByb2plY3Qub3Jn?=\n =?utf-8?b?PiwgRsO2bGtlciA8Zm9sa2VyQHBpeGVsYXRlZC1wcm9qZWN0Lm9yZz4=?="
+ email_cc = "=?UTF-8?Q?St=C3=A4ch_<stach@pixelated-project.org>?="
+ email_bcc = "=?UTF-8?Q?St=C3=A4ch_<stach@pixelated-project.org>?="
- leap_mail = test_helper.leap_mail(extra_headers={'Subject': subject, 'From': email_from})
+ leap_mail = test_helper.leap_mail(extra_headers={'Subject': subject, 'From': email_from, 'To': email_to, 'Cc': email_cc, 'Bcc': email_bcc})
mail = PixelatedMail.from_soledad(*leap_mail, soledad_querier=self.querier)
self.assertEqual(str(mail.headers['Subject']), 'test encoding St\xc3\xa4ch')
self.assertEqual(str(mail.headers['From']), 'St\xc3\xa4ch <stach@pixelated-project.org>')
+ self.assertEqual(mail.headers['To'], ['"\xc3\x84\xc3\xbc\xc3\xb6 \xc3\x96\xc3\xbc\xc3\xa4" <folker@pixelated-project.org>', 'F\xc3\xb6lker <folker@pixelated-project.org>'])
+ self.assertEqual(mail.headers['Cc'], ['St\xc3\xa4ch <stach@pixelated-project.org>'])
+ self.assertEqual(mail.headers['Bcc'], ['St\xc3\xa4ch <stach@pixelated-project.org>'])
+
+ mail.as_dict()
class InputMailTest(unittest.TestCase):