summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNavaL <mnandri@thoughtworks.com>2016-01-07 18:12:50 +0100
committerNavaL <mnandri@thoughtworks.com>2016-01-11 16:56:29 +0100
commitf07aa46453a311010dad3218689891f91f76e3fc (patch)
treef17a21831c3d54ce629adfaf5ac7b55e5c1d7725
parente10e43968e2a4a8ef3ef84fd7ff7e741af5e40b0 (diff)
matching POST response and GET of an attachment -- API specification
Issue #548
-rw-r--r--service/pixelated/adapter/mailstore/leap_mailstore.py20
-rw-r--r--service/pixelated/resources/attachments_resource.py5
-rw-r--r--service/test/integration/test_retrieve_attachment.py5
-rw-r--r--service/test/unit/adapter/mailstore/test_leap_mail.py6
-rw-r--r--service/test/unit/adapter/mailstore/test_leap_mailstore.py6
-rw-r--r--service/test/unit/resources/test_attachments_resource.py2
6 files changed, 25 insertions, 19 deletions
diff --git a/service/pixelated/adapter/mailstore/leap_mailstore.py b/service/pixelated/adapter/mailstore/leap_mailstore.py
index 10aad0b3..3539b498 100644
--- a/service/pixelated/adapter/mailstore/leap_mailstore.py
+++ b/service/pixelated/adapter/mailstore/leap_mailstore.py
@@ -29,10 +29,12 @@ from pixelated.support.functional import to_unicode
class AttachmentInfo(object):
- def __init__(self, ident, name, encoding):
- self.ident = ident
- self.name = name
- self.encoding = encoding
+ def __init__(self, part_map, headers):
+ self.ident = part_map['phash']
+ self.name = _extract_filename(headers)
+ self.encoding = headers.get('Content-Transfer-Encoding', None)
+ self.ctype = part_map.get('ctype') or headers.get('Content-Type')
+ self.size = headers.get('size', 0)
def __repr__(self):
return 'AttachmentInfo[%s, %s, %s]' % (self.ident, self.name, self.encoding)
@@ -40,6 +42,9 @@ class AttachmentInfo(object):
def __str__(self):
return 'AttachmentInfo[%s, %s, %s]' % (self.ident, self.name, self.encoding)
+ def as_dict(self):
+ return {'ident': self.ident, 'name': self.name, 'encoding': self.encoding, 'size': self.size, 'content-type': self.ctype}
+
class LeapMail(Mail):
@@ -140,7 +145,7 @@ class LeapMail(Mail):
'textPlainBody': self._body,
'replying': self._replying_dict(),
'mailbox': self._mailbox_name.lower(),
- 'attachments': [{'ident': attachment.ident, 'name': attachment.name, 'encoding': attachment.encoding} for attachment in self._attachments]
+ 'attachments': [attachment.as_dict() for attachment in self._attachments]
}
def _replying_dict(self):
@@ -403,11 +408,8 @@ class LeapMailStore(MailStore):
for nr, part_map in part_maps.items():
if 'headers' in part_map and 'phash' in part_map:
headers = {header[0]: header[1] for header in part_map['headers']}
- phash = part_map['phash']
if self._is_attachment(part_map, headers):
- filename = _extract_filename(headers)
- encoding = headers.get('Content-Transfer-Encoding', None)
- result.append(AttachmentInfo(phash, filename, encoding))
+ result.append(AttachmentInfo(part_map, headers))
if 'part_map' in part_map:
result += self._extract_part_map(part_map['part_map'])
diff --git a/service/pixelated/resources/attachments_resource.py b/service/pixelated/resources/attachments_resource.py
index 51fcdd3e..fc3efee7 100644
--- a/service/pixelated/resources/attachments_resource.py
+++ b/service/pixelated/resources/attachments_resource.py
@@ -89,8 +89,9 @@ class AttachmentsResource(Resource):
request.headers['Location'] = '/%s/%s' % (self.BASE_URL, attachment_id)
response_json = {"ident": attachment_id,
"content-type": _file.type,
- "filename": _file.filename,
- "filesize": len(_file.value)}
+ "encoding": "base64", # hard coded for now -- not really used
+ "name": _file.filename,
+ "size": len(_file.value)}
respond_json_deferred(response_json, request, status_code=201)
def error_handler(error):
diff --git a/service/test/integration/test_retrieve_attachment.py b/service/test/integration/test_retrieve_attachment.py
index acb23409..7de03c59 100644
--- a/service/test/integration/test_retrieve_attachment.py
+++ b/service/test/integration/test_retrieve_attachment.py
@@ -69,6 +69,7 @@ class RetrieveAttachmentTest(SoledadTestBase):
self.assertEqual('/attachment/B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A', req.headers['Location'])
response_json = {'ident': 'B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A',
'content-type': content_type,
- 'filename': filename,
- 'filesize': len(data)}
+ 'name': filename,
+ 'size': len(data),
+ 'encoding': 'base64'}
self.assertEqual(response_json, json.loads(req.written[0]))
diff --git a/service/test/unit/adapter/mailstore/test_leap_mail.py b/service/test/unit/adapter/mailstore/test_leap_mail.py
index 3f117dbe..f883dad1 100644
--- a/service/test/unit/adapter/mailstore/test_leap_mail.py
+++ b/service/test/unit/adapter/mailstore/test_leap_mail.py
@@ -85,9 +85,11 @@ class TestLeapMail(TestCase):
self.assertEqual(body, mail.as_dict()['body'])
def test_as_dict_with_attachments(self):
- mail = LeapMail('doc id', 'INBOX', attachments=[AttachmentInfo('id', 'name', 'encoding')])
+ mail = LeapMail('doc id', 'INBOX', attachments=[AttachmentInfo({'phash': 'id', 'ctype': 'text/plain'},
+ {'Content-Description': 'name',
+ 'Content-Transfer-Encoding': 'encoding', 'size': 2})])
- self.assertEqual([{'ident': 'id', 'name': 'name', 'encoding': 'encoding'}],
+ self.assertEqual([{'ident': 'id', 'name': 'name', 'encoding': 'encoding', 'content-type': 'text/plain', 'size': 2}],
mail.as_dict()['attachments'])
def test_as_dict_headers_with_special_chars(self):
diff --git a/service/test/unit/adapter/mailstore/test_leap_mailstore.py b/service/test/unit/adapter/mailstore/test_leap_mailstore.py
index f4fa367f..090c6b06 100644
--- a/service/test/unit/adapter/mailstore/test_leap_mailstore.py
+++ b/service/test/unit/adapter/mailstore/test_leap_mailstore.py
@@ -238,7 +238,7 @@ class TestLeapMailStore(TestCase):
message = yield store.add_mail('INBOX', input_mail.as_string())
- expected = [{'ident': self._cdoc_phash_from_message(mocked_message, 2), 'name': 'filename.txt', 'encoding': 'base64'}]
+ expected = [{'ident': self._cdoc_phash_from_message(mocked_message, 2), 'name': 'filename.txt', 'encoding': 'base64', 'size': 0, 'content-type': 'application/octet-stream'}]
self.assertEqual(expected, message.as_dict()['attachments'])
@defer.inlineCallbacks
@@ -253,7 +253,7 @@ class TestLeapMailStore(TestCase):
message = yield store.add_mail('INBOX', input_mail.as_string())
- expected = [{'ident': self._cdoc_phash_from_message(mocked_message, 2), 'name': 'super_nice_photo.jpg', 'encoding': 'base64'}]
+ expected = [{'ident': self._cdoc_phash_from_message(mocked_message, 2), 'name': 'super_nice_photo.jpg', 'encoding': 'base64', 'size': 0, 'content-type': 'application/octet-stream'}]
self.assertEqual(expected, message.as_dict()['attachments'])
@defer.inlineCallbacks
@@ -270,7 +270,7 @@ class TestLeapMailStore(TestCase):
message = yield store.add_mail('INBOX', input_mail.as_string())
- expected = [{'ident': self._cdoc_phash_from_message(mocked_message, 2), 'name': 'filename.txt', 'encoding': 'base64'}]
+ expected = [{'ident': self._cdoc_phash_from_message(mocked_message, 2), 'name': 'filename.txt', 'encoding': 'base64', 'size': 0, 'content-type': 'application/octet-stream'}]
self.assertEqual(expected, message.as_dict()['attachments'])
def test_extract_attachment_filename_with_or_without_quotes(self):
diff --git a/service/test/unit/resources/test_attachments_resource.py b/service/test/unit/resources/test_attachments_resource.py
index b12d9e4b..d7a35351 100644
--- a/service/test/unit/resources/test_attachments_resource.py
+++ b/service/test/unit/resources/test_attachments_resource.py
@@ -37,7 +37,7 @@ class AttachmentsResourceTest(unittest.TestCase):
self.assertEqual(201, request.code)
self.assertEqual('/attachment/%s' % attachment_id, request.headers['Location'])
response_json = {'ident': attachment_id, 'content-type': 'some mocked type',
- 'filename': 'filename.txt', 'filesize': 17}
+ 'name': 'filename.txt', 'size': 17, 'encoding': 'base64'}
self.assertEqual(response_json, json.loads(request.written[0]))
verify(self.mail_service).save_attachment('some mocked value', 'some mocked type')