summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Shyba <victor.shyba@gmail.com>2015-07-18 00:19:25 +0200
committerFolker Bernitt <fbernitt@thoughtworks.com>2015-08-11 17:00:24 +0200
commitd415a3a89535278ee455c79f6ddf912cd383e4f0 (patch)
treecf06351c9c17e903ac8244b6e05b144ad2329744
parentad65388274feaea5f4e0c3df0aafeb800825491e (diff)
fix integration.test_drafts
-rw-r--r--service/pixelated/adapter/model/mail.py2
-rw-r--r--service/pixelated/adapter/services/draft_service.py13
-rw-r--r--service/pixelated/adapter/services/mail_service.py17
-rw-r--r--service/pixelated/adapter/services/mailbox.py7
-rw-r--r--service/pixelated/adapter/soledad/soledad_writer_mixin.py7
-rw-r--r--service/pixelated/resources/mails_resource.py18
-rw-r--r--service/test/integration/test_drafts.py68
7 files changed, 72 insertions, 60 deletions
diff --git a/service/pixelated/adapter/model/mail.py b/service/pixelated/adapter/model/mail.py
index 552c2011..3cf97111 100644
--- a/service/pixelated/adapter/model/mail.py
+++ b/service/pixelated/adapter/model/mail.py
@@ -419,7 +419,7 @@ class PixelatedMail(Mail):
self.fdoc.content['mbox'] = mailbox_name
def remove_all_tags(self):
- self.update_tags(set([]))
+ return self.update_tags(set([]))
@defer.inlineCallbacks
def update_tags(self, tags):
diff --git a/service/pixelated/adapter/services/draft_service.py b/service/pixelated/adapter/services/draft_service.py
index c8df0a05..5a0ee5f3 100644
--- a/service/pixelated/adapter/services/draft_service.py
+++ b/service/pixelated/adapter/services/draft_service.py
@@ -13,6 +13,7 @@
#
# 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 twisted.internet import defer
class DraftService(object):
@@ -21,11 +22,13 @@ class DraftService(object):
def __init__(self, mailboxes):
self._mailboxes = mailboxes
+ @defer.inlineCallbacks
def create_draft(self, input_mail):
- pixelated_mail = self._mailboxes.drafts.add(input_mail)
- return pixelated_mail
+ pixelated_mail = yield (yield self._mailboxes.drafts).add(input_mail)
+ defer.returnValue(pixelated_mail)
+ @defer.inlineCallbacks
def update_draft(self, ident, input_mail):
- pixelated_mail = self.create_draft(input_mail)
- self._mailboxes.drafts.remove(ident)
- return pixelated_mail
+ pixelated_mail = yield self.create_draft(input_mail)
+ yield (yield self._mailboxes.drafts).remove(ident)
+ defer.returnValue(pixelated_mail)
diff --git a/service/pixelated/adapter/services/mail_service.py b/service/pixelated/adapter/services/mail_service.py
index f98020a2..3dfbc9a7 100644
--- a/service/pixelated/adapter/services/mail_service.py
+++ b/service/pixelated/adapter/services/mail_service.py
@@ -69,24 +69,23 @@ class MailService(object):
def attachment(self, attachment_id, encoding):
return self.querier.attachment(attachment_id, encoding)
+ @defer.inlineCallbacks
def mail_exists(self, mail_id):
- return not(not(self.querier.get_header_by_chash(mail_id)))
+ defer.returnValue(not(not((yield self.querier.get_header_by_chash(mail_id)))))
+ @defer.inlineCallbacks
def send_mail(self, content_dict):
mail = InputMail.from_dict(content_dict)
draft_id = content_dict.get('ident')
- def move_to_sent(_):
- return self.move_to_sent(draft_id, mail)
-
- deferred = self.mail_sender.sendmail(mail)
- deferred.addCallback(move_to_sent)
- return deferred
+ yield self.mail_sender.sendmail(mail)
+ yield self.move_to_sent(draft_id, mail)
+ @defer.inlineCallbacks
def move_to_sent(self, last_draft_ident, mail):
if last_draft_ident:
- self.mailboxes.drafts.remove(last_draft_ident)
- return self.mailboxes.sent.add(mail)
+ yield (yield self.mailboxes.drafts).remove(last_draft_ident)
+ defer.returnValue((yield (yield self.mailboxes.sent).add(mail)))
def mark_as_read(self, mail_id):
mail = self.mail(mail_id)
diff --git a/service/pixelated/adapter/services/mailbox.py b/service/pixelated/adapter/services/mailbox.py
index 479d0ee7..3ed311b2 100644
--- a/service/pixelated/adapter/services/mailbox.py
+++ b/service/pixelated/adapter/services/mailbox.py
@@ -39,11 +39,12 @@ class Mailbox(object):
self.search_engine.index_mail(added_mail)
defer.returnValue(added_mail)
+ @defer.inlineCallbacks
def remove(self, ident):
- mail = self.querier.mail(ident)
+ mail = yield self.querier.mail(ident)
self.search_engine.remove_from_index(mail.ident)
- mail.remove_all_tags()
- self.querier.remove_mail(mail)
+ yield mail.remove_all_tags()
+ yield self.querier.remove_mail(mail)
@classmethod
def create(cls, mailbox_name, soledad_querier, search_engine):
diff --git a/service/pixelated/adapter/soledad/soledad_writer_mixin.py b/service/pixelated/adapter/soledad/soledad_writer_mixin.py
index bce7d083..2e45b7d1 100644
--- a/service/pixelated/adapter/soledad/soledad_writer_mixin.py
+++ b/service/pixelated/adapter/soledad/soledad_writer_mixin.py
@@ -46,8 +46,9 @@ class SoledadWriterMixin(SoledadDbFacadeMixin, object):
defer.returnValue((yield self.mail(mail.ident)))
+ @defer.inlineCallbacks
def remove_mail(self, mail):
# FIX-ME: Must go through all the part_map phash to delete all the cdocs
- self.delete_doc(mail.fdoc)
- self.delete_doc(mail.hdoc)
- self.delete_doc(mail.bdoc)
+ yield self.delete_doc(mail.fdoc)
+ yield self.delete_doc(mail.hdoc)
+ yield self.delete_doc(mail.bdoc)
diff --git a/service/pixelated/resources/mails_resource.py b/service/pixelated/resources/mails_resource.py
index 7c9f8cc5..ef112900 100644
--- a/service/pixelated/resources/mails_resource.py
+++ b/service/pixelated/resources/mails_resource.py
@@ -128,11 +128,19 @@ class MailsResource(Resource):
_mail = InputMail.from_dict(content_dict)
draft_id = content_dict.get('ident')
+ def defer_response(deferred):
+ deferred.addCallback(lambda pixelated_mail: respond_json_deferred({'ident': pixelated_mail.ident}, request))
+
if draft_id:
- if not self._mail_service.mail_exists(draft_id):
- return respond_json("", request, status_code=422)
- pixelated_mail = self._draft_service.update_draft(draft_id, _mail)
+ deferred_check = self._mail_service.mail_exists(draft_id)
+
+ def return422otherwise(mail_exists):
+ if not mail_exists:
+ respond_json_deferred("", request, status_code=422)
+ else:
+ defer_response(self._draft_service.update_draft(draft_id, _mail))
+ deferred_check.addCallback(return422otherwise)
else:
- pixelated_mail = self._draft_service.create_draft(_mail)
+ defer_response(self._draft_service.create_draft(_mail))
- return respond_json({'ident': pixelated_mail.ident}, request)
+ return server.NOT_DONE_YET
diff --git a/service/test/integration/test_drafts.py b/service/test/integration/test_drafts.py
index 3a0f120b..d0505d75 100644
--- a/service/test/integration/test_drafts.py
+++ b/service/test/integration/test_drafts.py
@@ -16,7 +16,7 @@
from test.support.integration import SoledadTestBase, MailBuilder
from mockito import unstub, when, any
-from twisted.internet.defer import Deferred
+from twisted.internet import defer
class DraftsTest(SoledadTestBase):
@@ -24,14 +24,15 @@ class DraftsTest(SoledadTestBase):
def tearDown(self):
unstub()
+ @defer.inlineCallbacks
def test_post_sends_mail_and_deletes_previous_draft_if_it_exists(self):
- # act is if sending the mail by SMTP succeeded
- sendmail_deferred = Deferred()
+ # act as if sending the mail by SMTP succeeded
+ sendmail_deferred = defer.Deferred()
when(self.mail_sender).sendmail(any()).thenReturn(sendmail_deferred)
# creates one draft
first_draft = MailBuilder().with_subject('First draft').build_json()
- first_draft_ident = self.put_mail(first_draft)[0]['ident']
+ first_draft_ident = (yield self.put_mail(first_draft)[0])['ident']
# sends an updated version of the draft
second_draft = MailBuilder().with_subject('Second draft').with_ident(first_draft_ident).build_json()
@@ -39,69 +40,68 @@ class DraftsTest(SoledadTestBase):
sendmail_deferred.callback(None) # SMTP succeeded
- def onSuccess(mail):
- sent_mails = self.get_mails_by_tag('sent')
- drafts = self.get_mails_by_tag('drafts')
+ yield deferred_res
- # make sure there is one email in the sent mailbox and it is the second draft
- self.assertEquals(1, len(sent_mails))
- self.assertEquals('Second draft', sent_mails[0].subject)
+ sent_mails = yield self.get_mails_by_tag('sent')
+ drafts = yield self.get_mails_by_tag('drafts')
- # make sure that there are no drafts in the draft mailbox
- self.assertEquals(0, len(drafts))
+ # make sure there is one email in the sent mailbox and it is the second draft
+ self.assertEquals(1, len(sent_mails))
+ self.assertEquals('Second draft', sent_mails[0].subject)
- deferred_res.addCallback(onSuccess)
- return deferred_res
+ # make sure that there are no drafts in the draft mailbox
+ self.assertEquals(0, len(drafts))
+ @defer.inlineCallbacks
def test_post_sends_mail_even_when_draft_does_not_exist(self):
- # act is if sending the mail by SMTP succeeded
- sendmail_deferred = Deferred()
+ # act as if sending the mail by SMTP succeeded
+ sendmail_deferred = defer.Deferred()
when(self.mail_sender).sendmail(any()).thenReturn(sendmail_deferred)
first_draft = MailBuilder().with_subject('First draft').build_json()
- deferred_res = self.post_mail(first_draft)
+ res = self.post_mail(first_draft)
sendmail_deferred.callback(True)
+ yield res
- def onSuccess(result):
- sent_mails = self.get_mails_by_tag('sent')
- drafts = self.get_mails_by_tag('drafts')
-
- self.assertEquals(1, len(sent_mails))
- self.assertEquals('First draft', sent_mails[0].subject)
- self.assertEquals(0, len(drafts))
+ sent_mails = yield self.get_mails_by_tag('sent')
+ drafts = yield self.get_mails_by_tag('drafts')
- deferred_res.addCallback(onSuccess)
- return deferred_res
+ self.assertEquals(1, len(sent_mails))
+ self.assertEquals('First draft', sent_mails[0].subject)
+ self.assertEquals(0, len(drafts))
def post_mail(self, data):
deferred_res, req = self.post('/mails', data)
- deferred_res.callback(None)
return deferred_res
+ @defer.inlineCallbacks
def test_put_creates_a_draft_if_it_does_not_exist(self):
mail = MailBuilder().with_subject('A new draft').build_json()
- self.put_mail(mail)
- mails = self.get_mails_by_tag('drafts')
+ yield self.put_mail(mail)[0]
+ mails = yield self.get_mails_by_tag('drafts')
self.assertEquals('A new draft', mails[0].subject)
+ @defer.inlineCallbacks
def test_put_updates_draft_if_it_already_exists(self):
draft = MailBuilder().with_subject('First draft').build_json()
- draft_ident = self.put_mail(draft)[0]['ident']
+ draft_ident = (yield self.put_mail(draft)[0])['ident']
updated_draft = MailBuilder().with_subject('First draft edited').with_ident(draft_ident).build_json()
- self.put_mail(updated_draft)
+ yield self.put_mail(updated_draft)[0]
- drafts = self.get_mails_by_tag('drafts')
+ drafts = yield self.get_mails_by_tag('drafts')
self.assertEquals(1, len(drafts))
self.assertEquals('First draft edited', drafts[0].subject)
+ @defer.inlineCallbacks
def test_respond_unprocessable_entity_if_draft_to_remove_doesnt_exist(self):
draft = MailBuilder().with_subject('First draft').build_json()
- self.put_mail(draft)
+ yield self.put_mail(draft)[0]
updated_draft = MailBuilder().with_subject('First draft edited').with_ident('NOTFOUND').build_json()
- _, request = self.put_mail(updated_draft)
+ response, request = self.put_mail(updated_draft)
+ yield response
self.assertEquals(422, request.code)