summaryrefslogtreecommitdiff
path: root/service/test
diff options
context:
space:
mode:
authorFolker Bernitt <fbernitt@thoughtworks.com>2015-09-02 12:35:33 +0200
committerFolker Bernitt <fbernitt@thoughtworks.com>2015-09-02 12:35:33 +0200
commite5d718f982e0cd3fc85da00d3abdccce1907e488 (patch)
treeebf566a96a2b70c7f234c81065ee48054bf555a3 /service/test
parent503c917ede122fc97046e35af8ec30a25adbad32 (diff)
Download attachments from mail store instead of querier
- Issue #435 - Improved error handling of attachment resource
Diffstat (limited to 'service/test')
-rw-r--r--service/test/integration/test_retrieve_attachment.py33
-rw-r--r--service/test/support/integration/app_test_client.py6
-rw-r--r--service/test/unit/adapter/mailstore/test_leap_mailstore.py6
-rw-r--r--service/test/unit/adapter/test_mail_service.py9
4 files changed, 39 insertions, 15 deletions
diff --git a/service/test/integration/test_retrieve_attachment.py b/service/test/integration/test_retrieve_attachment.py
index 1cf8006f..e7e8670d 100644
--- a/service/test/integration/test_retrieve_attachment.py
+++ b/service/test/integration/test_retrieve_attachment.py
@@ -13,6 +13,9 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
+from email.mime.application import MIMEApplication
+from email.mime.multipart import MIMEMultipart
+from email.mime.text import MIMEText
from test.support.integration.soledad_test_base import SoledadTestBase
from twisted.internet import defer
@@ -22,16 +25,26 @@ class RetrieveAttachmentTest(SoledadTestBase):
@defer.inlineCallbacks
def test_attachment_content_is_retrieved(self):
- ident = 'F4E99C1CEC4D300A4223A96CCABBE0304BDBC31C550A5A03E207A5E4C3C71A22'
- attachment_dict = {'content-disposition': 'attachment',
- 'content-transfer-encoding': '',
- 'type': 'cnt',
- 'raw': 'cGVxdWVubyBhbmV4byA6RAo=',
- 'phash': ident,
- 'content_type': 'text/plain; charset=US-ASCII; name="attachment_pequeno.txt"'}
+ attachment_id, input_mail = self._create_mail_with_attachment()
+ yield self.mail_store.add_mail('INBOX', input_mail.as_string())
- yield self.add_document_to_soledad(attachment_dict)
+ attachment, req = yield self.get_attachment(attachment_id, 'base64')
- attachment = yield self.get_attachment(ident, 'base64')
+ self.assertEqual(200, req.code)
+ self.assertEquals('pretend to be binary attachment data', attachment)
- self.assertEquals('pequeno anexo :D\n', attachment)
+ def _create_mail_with_attachment(self):
+ input_mail = MIMEMultipart()
+ input_mail.attach(MIMEText(u'a utf8 message', _charset='utf-8'))
+ attachment = MIMEApplication('pretend to be binary attachment data')
+ attachment.add_header('Content-Disposition', 'attachment', filename='filename.txt')
+ input_mail.attach(attachment)
+ attachment_id = 'B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A'
+ return attachment_id, input_mail
+
+ @defer.inlineCallbacks
+ def test_attachment_error_returned_if_id_not_found(self):
+ attachment, req = yield self.get_attachment('invalid attachment id', 'base64')
+
+ self.assertEqual(404, req.code)
+ self.assertIsNone(attachment)
diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py
index fe9eaffd..51fbf483 100644
--- a/service/test/support/integration/app_test_client.py
+++ b/service/test/support/integration/app_test_client.py
@@ -193,9 +193,11 @@ class AppTestClient(object):
res = yield res
defer.returnValue([ResponseMail(m) for m in res['mails']])
+ @defer.inlineCallbacks
def get_attachment(self, ident, encoding):
- res, req = self.get("/attachment/%s" % ident, {'encoding': [encoding]}, as_json=False)
- return res
+ deferred_result, req = self.get("/attachment/%s" % ident, {'encoding': [encoding]}, as_json=False)
+ res = yield deferred_result
+ defer.returnValue((res, req))
def put_mail(self, data):
res, req = self.put('/mails', data)
diff --git a/service/test/unit/adapter/mailstore/test_leap_mailstore.py b/service/test/unit/adapter/mailstore/test_leap_mailstore.py
index 7cbd1405..96a862be 100644
--- a/service/test/unit/adapter/mailstore/test_leap_mailstore.py
+++ b/service/test/unit/adapter/mailstore/test_leap_mailstore.py
@@ -137,7 +137,7 @@ class TestLeapMailStore(TestCase):
@defer.inlineCallbacks
def test_get_mail_attachment(self):
attachment_id = 'AAAA9AAD9E153D24265395203C53884506ABA276394B9FEC02B214BF9E77E48E'
- doc = ContentDocWrapper(content_type='foo/bar', raw='asdf')
+ doc = SoledadDocument(json=json.dumps({'content_type': 'foo/bar', 'raw': 'asdf'}))
when(self.soledad).get_from_index('by-type-and-payloadhash', 'cnt', attachment_id).thenReturn(defer.succeed([doc]))
store = LeapMailStore(self.soledad)
@@ -153,8 +153,8 @@ class TestLeapMailStore(TestCase):
('quoted-printable', 'äsdf', '=C3=A4sdf')]
for transfer_encoding, data, encoded_data in encoding_examples:
- doc = ContentDocWrapper(content_type='foo/bar', raw=encoded_data,
- content_transfer_encoding=transfer_encoding)
+ doc = SoledadDocument(json=json.dumps({'content_type': 'foo/bar', 'raw': encoded_data,
+ 'content_transfer_encoding': transfer_encoding}))
when(self.soledad).get_from_index('by-type-and-payloadhash', 'cnt', attachment_id).thenReturn(defer.succeed([doc]))
store = LeapMailStore(self.soledad)
diff --git a/service/test/unit/adapter/test_mail_service.py b/service/test/unit/adapter/test_mail_service.py
index 79161a04..30784769 100644
--- a/service/test/unit/adapter/test_mail_service.py
+++ b/service/test/unit/adapter/test_mail_service.py
@@ -130,3 +130,12 @@ class TestMailService(unittest.TestCase):
yield self.mail_service.recover_mail(1)
verify(self.mail_store).move_mail_to_mailbox(1, 'INBOX')
+
+ @defer.inlineCallbacks
+ def test_get_attachment(self):
+ attachment_dict = {'content': bytearray('data'), 'content-type': 'text/plain'}
+ when(self.mail_store).get_mail_attachment('some attachment id').thenReturn(defer.succeed(attachment_dict))
+
+ attachment = yield self.mail_service.attachment('some attachment id')
+
+ self.assertEqual(attachment_dict, attachment)