summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNavaL <mnandri@thoughtworks.com>2015-12-22 18:16:17 +0100
committerNavaL <mnandri@thoughtworks.com>2015-12-22 19:05:10 +0100
commit4cb47c1848bb5d20b5ae167a7ab2879d00825f84 (patch)
treea0a68aeb4c1a7a404483215fbd720c16ead8933f
parent77b5ecdf0664b9b692df9bd0444062d47feddda1 (diff)
decode to base64 the raw attachment that is in bytes... and renaming id to attachment_id in the mail POST
Issue #548
-rw-r--r--service/pixelated/adapter/model/mail.py9
-rw-r--r--service/pixelated/resources/mails_resource.py15
-rw-r--r--service/test/unit/adapter/test_mail.py4
-rw-r--r--service/test/unit/resources/test_mails_resource.py8
4 files changed, 22 insertions, 14 deletions
diff --git a/service/pixelated/adapter/model/mail.py b/service/pixelated/adapter/model/mail.py
index 4f06588b..1a505481 100644
--- a/service/pixelated/adapter/model/mail.py
+++ b/service/pixelated/adapter/model/mail.py
@@ -19,6 +19,8 @@ import logging
from email import message_from_file
from email.mime.text import MIMEText
from email.header import Header
+
+import binascii
from email.MIMEMultipart import MIMEMultipart
from email.mime.nonmultipart import MIMENonMultipart
from pycryptopp.hash import sha256
@@ -111,8 +113,11 @@ class Mail(object):
def _add_attachments(self, mime):
for attachment in getattr(self, '_attachments', []):
major, sub = attachment['content-type'].split('/')
- attachment_mime = MIMENonMultipart(major, sub, Content_Disposition='attachment; filename=%s' % attachment['filename'])
- attachment_mime.set_payload(attachment['raw'])
+ attachment_mime = MIMENonMultipart(major, sub)
+ base64_attachment_file = binascii.b2a_base64(attachment['raw'])
+ attachment_mime.set_payload(base64_attachment_file)
+ attachment_mime['Content-Disposition'] = 'attachment; filename="%s"' % attachment['filename']
+ attachment_mime['Content-Transfer-Encoding'] = 'base64'
mime.attach(attachment_mime)
def _charset(self):
diff --git a/service/pixelated/resources/mails_resource.py b/service/pixelated/resources/mails_resource.py
index 38786e11..f9397263 100644
--- a/service/pixelated/resources/mails_resource.py
+++ b/service/pixelated/resources/mails_resource.py
@@ -177,25 +177,28 @@ class MailsResource(Resource):
return server.NOT_DONE_YET
@defer.inlineCallbacks
- def _fetch_attachment_contents(self, attachments):
+ def _fetch_attachment_contents(self, content_dict):
+ attachments = content_dict.get('attachments', []) if content_dict else []
for attachment in attachments:
- retrieved_attachment = yield self._mail_service.attachment(attachment['id'])
+ retrieved_attachment = yield self._mail_service.attachment(attachment['attachment_id'])
attachment['raw'] = retrieved_attachment['content']
+ content_dict['attachments'] = attachments
+ defer.returnValue(content_dict)
@defer.inlineCallbacks
def _handle_post(self, request):
content_dict = json.loads(request.content.read())
- self._fetch_attachment_contents(content_dict.get('attachments', []))
+ with_attachment_content = yield self._fetch_attachment_contents(content_dict)
- sent_mail = yield self._mail_service.send_mail(content_dict)
+ sent_mail = yield self._mail_service.send_mail(with_attachment_content)
respond_json_deferred(sent_mail.as_dict(), request)
@defer.inlineCallbacks
def _handle_put(self, request):
content_dict = json.loads(request.content.read())
- self._fetch_attachment_contents(content_dict.get('attachments', []))
+ with_attachment_content = yield self._fetch_attachment_contents(content_dict)
- _mail = InputMail.from_dict(content_dict)
+ _mail = InputMail.from_dict(with_attachment_content)
draft_id = content_dict.get('ident')
pixelated_mail = yield self._draft_service.process_draft(draft_id, _mail)
diff --git a/service/test/unit/adapter/test_mail.py b/service/test/unit/adapter/test_mail.py
index 006bde77..4a36bd42 100644
--- a/service/test/unit/adapter/test_mail.py
+++ b/service/test/unit/adapter/test_mail.py
@@ -149,8 +149,8 @@ class InputMailTest(unittest.TestCase):
mail = MIMEMultipart()
mail.attach(attachment)
- part_one = 'Content-Type: text/plain; Content-Disposition="attachment; filename=ayoyo.txt"\nMIME-Version: 1.0\n\nHamburg Ayoyoyooooo!!!'
- part_two = 'Content-Type: text/html; Content-Disposition="attachment; filename=hello.html"\nMIME-Version: 1.0\n\n<p>Hello html Hamburg!</p>'
+ part_one = 'Content-Type: text/plain\nMIME-Version: 1.0\nContent-Disposition: attachment; filename="ayoyo.txt"\nContent-Transfer-Encoding: base64\n\n'
+ part_two = 'Content-Type: text/html\nMIME-Version: 1.0\nContent-Disposition: attachment; filename="hello.html"\nContent-Transfer-Encoding: base64\n\n'
self.assertRegexpMatches(input_mail.raw, part_one)
self.assertRegexpMatches(input_mail.raw, part_two)
diff --git a/service/test/unit/resources/test_mails_resource.py b/service/test/unit/resources/test_mails_resource.py
index 76fe8bb3..352bd272 100644
--- a/service/test/unit/resources/test_mails_resource.py
+++ b/service/test/unit/resources/test_mails_resource.py
@@ -56,7 +56,7 @@ class TestMailsResource(unittest.TestCase):
request = DummyRequest(['/mails'])
request.method = 'PUT'
content = mock()
- when(content).read().thenReturn('{"attachments": [{"id": "some fake attachment id"}]}')
+ when(content).read().thenReturn('{"attachments": [{"attachment_id": "some fake attachment id"}]}')
when(self.mail_service).attachment('some fake attachment id').thenReturn(defer.Deferred())
request.content = content
@@ -76,11 +76,11 @@ class TestMailsResource(unittest.TestCase):
request = DummyRequest(['/mails'])
request.method = 'POST'
content = mock()
- when(content).read().thenReturn('{"attachments": [{"id": "some fake attachment id"}]}')
+ when(content).read().thenReturn('{"attachments": [{"attachment_id": "some fake attachment id"}]}')
when(self.mail_service).attachment('some fake attachment id').thenReturn(defer.succeed({"content": "some content"}))
as_dictable = mock()
when(as_dictable).as_dict().thenReturn({})
- when(self.mail_service).send_mail({"attachments": [{"id": "some fake attachment id", "raw": "some content"}]})\
+ when(self.mail_service).send_mail({"attachments": [{"attachment_id": "some fake attachment id", "raw": "some content"}]})\
.thenReturn(defer.succeed(as_dictable))
request.content = content
@@ -91,7 +91,7 @@ class TestMailsResource(unittest.TestCase):
def assert_response(_):
verify(self.mail_service).attachment('some fake attachment id')
- verify(self.mail_service).send_mail({"attachments": [{"id": "some fake attachment id", "raw": "some content"}]})
+ verify(self.mail_service).send_mail({"attachments": [{"attachment_id": "some fake attachment id", "raw": "some content"}]})
d.addCallback(assert_response)
return d