summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--service/pixelated/adapter/services/mail_service.py12
-rw-r--r--service/pixelated/adapter/services/mailboxes.py2
-rw-r--r--service/pixelated/resources/mail_resource.py11
-rw-r--r--service/pixelated/resources/mails_resource.py14
-rw-r--r--service/test/integration/test_delete_mail.py30
-rw-r--r--service/test/support/integration/app_test_client.py10
6 files changed, 53 insertions, 26 deletions
diff --git a/service/pixelated/adapter/services/mail_service.py b/service/pixelated/adapter/services/mail_service.py
index b66d7345..da1b1bea 100644
--- a/service/pixelated/adapter/services/mail_service.py
+++ b/service/pixelated/adapter/services/mail_service.py
@@ -100,19 +100,21 @@ class MailService(object):
yield mail.mark_as_unread()
self.search_engine.index_mail(mail)
+ @defer.inlineCallbacks
def delete_mail(self, mail_id):
- mail = self.mail(mail_id)
+ mail = yield self.mail(mail_id)
if mail.mailbox_name == 'TRASH':
- self.delete_permanent(mail_id)
+ yield self.delete_permanent(mail_id)
else:
- trashed_mail = self.mailboxes.move_to_trash(mail_id)
+ trashed_mail = yield self.mailboxes.move_to_trash(mail_id)
self.search_engine.index_mail(trashed_mail)
def recover_mail(self, mail_id):
recovered_mail = self.mailboxes.move_to_inbox(mail_id)
self.search_engine.index_mail(recovered_mail)
+ @defer.inlineCallbacks
def delete_permanent(self, mail_id):
- mail = self.mail(mail_id)
+ mail = yield self.mail(mail_id)
self.search_engine.remove_from_index(mail_id)
- self.querier.remove_mail(mail)
+ yield self.querier.remove_mail(mail)
diff --git a/service/pixelated/adapter/services/mailboxes.py b/service/pixelated/adapter/services/mailboxes.py
index 45dd9192..19176a32 100644
--- a/service/pixelated/adapter/services/mailboxes.py
+++ b/service/pixelated/adapter/services/mailboxes.py
@@ -77,7 +77,7 @@ class Mailboxes(object):
@defer.inlineCallbacks
def _move_to(self, mail_id, mailbox):
mailbox = yield mailbox
- mail = self.querier.mail(mail_id)
+ mail = yield self.querier.mail(mail_id)
mail.set_mailbox(mailbox.mailbox_name)
mail.save()
defer.returnValue(mail)
diff --git a/service/pixelated/resources/mail_resource.py b/service/pixelated/resources/mail_resource.py
index f11862af..436842fb 100644
--- a/service/pixelated/resources/mail_resource.py
+++ b/service/pixelated/resources/mail_resource.py
@@ -2,6 +2,7 @@ import json
from pixelated.resources import respond_json, respond_json_deferred
from twisted.web.resource import Resource
from twisted.web.server import NOT_DONE_YET
+from twisted.python.log import err
class MailTags(Resource):
@@ -42,8 +43,14 @@ class Mail(Resource):
return NOT_DONE_YET
def render_DELETE(self, request):
- self._mail_service.delete_mail(self._mail_id)
- return respond_json(None, request)
+ def response_failed(failure):
+ err(failure, 'something failed')
+ request.finish()
+
+ d = self._mail_service.delete_mail(self._mail_id)
+ d.addCallback(lambda _: respond_json_deferred(None, request))
+ d.addErrback(response_failed)
+ return NOT_DONE_YET
class MailResource(Resource):
diff --git a/service/pixelated/resources/mails_resource.py b/service/pixelated/resources/mails_resource.py
index 27232ffc..1056dcdb 100644
--- a/service/pixelated/resources/mails_resource.py
+++ b/service/pixelated/resources/mails_resource.py
@@ -6,6 +6,7 @@ from pixelated.resources import respond_json, respond_json_deferred
from twisted.web.resource import Resource
from twisted.web import server
from twisted.internet import defer
+from twisted.python.log import err
from leap.common.events import (
register,
catalog as events
@@ -60,10 +61,19 @@ class MailsDeleteResource(Resource):
self._mail_service = mail_service
def render_POST(self, request):
+ def response_failed(failure):
+ err(failure, 'something failed')
+ request.finish()
+
idents = json.loads(request.content.read())['idents']
+ deferreds = []
for ident in idents:
- self._mail_service.delete_mail(ident)
- return respond_json(None, request)
+ deferreds.append(self._mail_service.delete_mail(ident))
+
+ d = defer.gatherResults(deferreds, consumeErrors=True)
+ d.addCallback(lambda _: respond_json_deferred(None, request))
+ d.addErrback(response_failed)
+ return NOT_DONE_YET
class MailsRecoverResource(Resource):
diff --git a/service/test/integration/test_delete_mail.py b/service/test/integration/test_delete_mail.py
index 18bae44a..58c9e33b 100644
--- a/service/test/integration/test_delete_mail.py
+++ b/service/test/integration/test_delete_mail.py
@@ -19,42 +19,46 @@ from test.support.integration import SoledadTestBase, MailBuilder
class DeleteMailTest(SoledadTestBase):
+ @defer.inlineCallbacks
def test_move_mail_to_trash_when_deleting(self):
input_mail = MailBuilder().with_subject('Mail with tags').build_input_mail()
- self.add_mail_to_inbox(input_mail)
+ yield self.add_mail_to_inbox(input_mail)
- inbox_mails = self.get_mails_by_tag('inbox')
+ inbox_mails = yield self.get_mails_by_tag('inbox')
self.assertEquals(1, len(inbox_mails))
- self.delete_mail(input_mail.ident)
+ yield self.delete_mail(input_mail.ident)
- inbox_mails = self.get_mails_by_tag('inbox')
+ inbox_mails = yield self.get_mails_by_tag('inbox')
self.assertEquals(0, len(inbox_mails))
- trash_mails = self.get_mails_by_tag('trash')
+ trash_mails = yield self.get_mails_by_tag('trash')
self.assertEquals(1, len(trash_mails))
@defer.inlineCallbacks
def test_delete_mail_when_trashing_mail_from_trash_mailbox(self):
mails = yield self.add_multiple_to_mailbox(1, 'trash')
- self.delete_mails([mails[0].ident])
+ yield self.delete_mails([mails[0].ident])
- trash_mails = self.get_mails_by_tag('trash')
+ trash_mails = yield self.get_mails_by_tag('trash')
self.assertEqual(0, len(trash_mails))
+ @defer.inlineCallbacks
def test_move_mail_to_trash_when_delete_multiple(self):
- mails = self.add_multiple_to_mailbox(5, 'inbox')
+ mails = yield self.add_multiple_to_mailbox(5, 'inbox')
mail_idents = [m.ident for m in mails]
- self.delete_mails(mail_idents)
+ yield self.delete_mails(mail_idents)
- inbox = self.get_mails_by_tag('inbox')
+ inbox = yield self.get_mails_by_tag('inbox')
self.assertEquals(0, len(inbox))
+ @defer.inlineCallbacks
def test_delete_permanently_when_mails_are_in_trash(self):
- mails = self.add_multiple_to_mailbox(5, 'trash')
- self.delete_mails([m.ident for m in mails])
+ mails = yield self.add_multiple_to_mailbox(5, 'trash')
+ mail_idents = [m.ident for m in mails]
- trash = self.get_mails_by_tag('trash')
+ yield self.delete_mails(mail_idents)
+ trash = yield self.get_mails_by_tag('trash')
self.assertEquals(0, len(trash))
diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py
index 9acef1bd..da237284 100644
--- a/service/test/support/integration/app_test_client.py
+++ b/service/test/support/integration/app_test_client.py
@@ -20,6 +20,8 @@ import os
import shutil
import time
import uuid
+import random
+
from leap.mail.imap.account import IMAPAccount
from leap.soledad.client import Soledad
@@ -140,7 +142,9 @@ class AppTestClient(object):
def add_multiple_to_mailbox(self, num, mailbox='', flags=[], tags=[], to='recipient@to.com', cc='recipient@cc.com', bcc='recipient@bcc.com'):
mails = []
for _ in range(num):
- input_mail = MailBuilder().with_status(flags).with_tags(tags).with_to(to).with_cc(cc).with_bcc(bcc).build_input_mail()
+ builder = MailBuilder().with_status(flags).with_tags(tags).with_to(to).with_cc(cc).with_bcc(bcc)
+ builder.with_body(str(random.random()))
+ input_mail = builder.build_input_mail()
mbx = yield self.mailboxes._create_or_get(mailbox)
mail = yield mbx.add(input_mail)
mails.append(mail)
@@ -202,11 +206,11 @@ class AppTestClient(object):
def delete_mail(self, mail_ident):
res, req = self.delete("/mail/%s" % mail_ident)
- return req
+ return res
def delete_mails(self, idents):
res, req = self.post("/mails/delete", json.dumps({'idents': idents}))
- return req
+ return res
def mark_many_as_unread(self, idents):
res, req = self.post('/mails/unread', json.dumps({'idents': idents}))