summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorVictor Shyba <victor.shyba@gmail.com>2015-07-17 22:50:28 +0200
committerFolker Bernitt <fbernitt@thoughtworks.com>2015-08-11 17:00:24 +0200
commitad65388274feaea5f4e0c3df0aafeb800825491e (patch)
tree694400cc3ebd1885694cc22cf3810669dc490982 /service
parent422c434224c965385a21d0d2948b005b6d44cccb (diff)
fixing integration.test_tags
Diffstat (limited to 'service')
-rw-r--r--service/pixelated/adapter/model/mail.py9
-rw-r--r--service/pixelated/adapter/services/mail_service.py7
-rw-r--r--service/pixelated/adapter/soledad/soledad_writer_mixin.py2
-rw-r--r--service/pixelated/resources/mail_resource.py13
-rw-r--r--service/test/integration/test_tags.py51
5 files changed, 48 insertions, 34 deletions
diff --git a/service/pixelated/adapter/model/mail.py b/service/pixelated/adapter/model/mail.py
index ed82b55e..552c2011 100644
--- a/service/pixelated/adapter/model/mail.py
+++ b/service/pixelated/adapter/model/mail.py
@@ -30,6 +30,8 @@ from pixelated.adapter.model.status import Status
from pixelated.support import date
from pixelated.support.functional import compact
+from twisted.internet import defer
+
logger = logging.getLogger(__name__)
@@ -419,9 +421,10 @@ class PixelatedMail(Mail):
def remove_all_tags(self):
self.update_tags(set([]))
+ @defer.inlineCallbacks
def update_tags(self, tags):
- self._persist_mail_tags(tags)
- return self.tags
+ yield self._persist_mail_tags(tags)
+ defer.returnValue(self.tags)
def mark_as_read(self):
if Status.SEEN in self.flags:
@@ -444,7 +447,7 @@ class PixelatedMail(Mail):
def _persist_mail_tags(self, current_tags):
self.fdoc.content['tags'] = json.dumps(list(current_tags))
- self.save()
+ return self.save()
def has_tag(self, tag):
return tag in self.tags
diff --git a/service/pixelated/adapter/services/mail_service.py b/service/pixelated/adapter/services/mail_service.py
index 01f22570..f98020a2 100644
--- a/service/pixelated/adapter/services/mail_service.py
+++ b/service/pixelated/adapter/services/mail_service.py
@@ -38,17 +38,18 @@ class MailService(object):
defer.returnValue((mails, total))
+ @defer.inlineCallbacks
def update_tags(self, mail_id, new_tags):
new_tags = self._filter_white_space_tags(new_tags)
reserved_words = extract_reserved_tags(new_tags)
if len(reserved_words):
raise ValueError('None of the following words can be used as tags: ' + ' '.join(reserved_words))
new_tags = self._favor_existing_tags_casing(new_tags)
- mail = self.mail(mail_id)
- mail.update_tags(set(new_tags))
+ mail = yield self.mail(mail_id)
+ yield mail.update_tags(set(new_tags))
self.search_engine.index_mail(mail)
- return mail
+ defer.returnValue(mail)
def _filter_white_space_tags(self, tags):
return [tag.strip() for tag in tags if not tag.isspace()]
diff --git a/service/pixelated/adapter/soledad/soledad_writer_mixin.py b/service/pixelated/adapter/soledad/soledad_writer_mixin.py
index 43e5d323..bce7d083 100644
--- a/service/pixelated/adapter/soledad/soledad_writer_mixin.py
+++ b/service/pixelated/adapter/soledad/soledad_writer_mixin.py
@@ -31,7 +31,7 @@ class SoledadWriterMixin(SoledadDbFacadeMixin, object):
self.put_doc(rct)
def save_mail(self, mail):
- self.put_doc(mail.fdoc)
+ return self.put_doc(mail.fdoc)
@defer.inlineCallbacks
def create_mail(self, mail, mailbox_name):
diff --git a/service/pixelated/resources/mail_resource.py b/service/pixelated/resources/mail_resource.py
index 15ca1939..f11862af 100644
--- a/service/pixelated/resources/mail_resource.py
+++ b/service/pixelated/resources/mail_resource.py
@@ -16,11 +16,14 @@ class MailTags(Resource):
def render_POST(self, request):
new_tags = json.loads(request.content.read()).get('newtags')
- try:
- mail = self._mail_service.update_tags(self._mail_id, new_tags)
- except ValueError as ve:
- return respond_json(ve.message, request, 403)
- return respond_json(mail.as_dict(), request)
+ d = self._mail_service.update_tags(self._mail_id, new_tags)
+ d.addCallback(lambda mail: respond_json_deferred(mail.as_dict(), request))
+
+ def handle403(failure):
+ failure.trap(ValueError)
+ return respond_json_deferred(failure.getErrorMessage(), request, 403)
+ d.addErrback(handle403)
+ return NOT_DONE_YET
class Mail(Resource):
diff --git a/service/test/integration/test_tags.py b/service/test/integration/test_tags.py
index 168e035f..75cb81f6 100644
--- a/service/test/integration/test_tags.py
+++ b/service/test/integration/test_tags.py
@@ -15,6 +15,8 @@
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
import json
+from twisted.internet import defer
+
from test.support.integration import SoledadTestBase, MailBuilder
from pixelated.adapter.services.tag_service import SPECIAL_TAGS
@@ -24,67 +26,72 @@ class TagsTest(SoledadTestBase):
def _tags_json(self, tags):
return json.dumps({'newtags': tags})
+ @defer.inlineCallbacks
def test_add_tag_to_an_inbox_mail_and_query(self):
mail = MailBuilder().with_subject('Mail with tags').build_input_mail()
- self.add_mail_to_inbox(mail)
+ yield self.add_mail_to_inbox(mail)
- self.post_tags(mail.ident, self._tags_json(['IMPORTANT']))
+ yield self.post_tags(mail.ident, self._tags_json(['IMPORTANT']))
- mails = self.get_mails_by_tag('inbox')
+ mails = yield self.get_mails_by_tag('inbox')
self.assertEquals({'IMPORTANT'}, set(mails[0].tags))
- mails = self.get_mails_by_tag('IMPORTANT')
+ mails = yield self.get_mails_by_tag('IMPORTANT')
self.assertEquals('Mail with tags', mails[0].subject)
+ @defer.inlineCallbacks
def test_use_old_casing_when_same_tag_with_different_casing_is_posted(self):
mail = MailBuilder().with_subject('Mail with tags').build_input_mail()
- self.add_mail_to_inbox(mail)
- self.post_tags(mail.ident, self._tags_json(['ImPoRtAnT']))
- mails = self.get_mails_by_tag('ImPoRtAnT')
+ yield self.add_mail_to_inbox(mail)
+ yield self.post_tags(mail.ident, self._tags_json(['ImPoRtAnT']))
+ mails = yield self.get_mails_by_tag('ImPoRtAnT')
self.assertEquals({'ImPoRtAnT'}, set(mails[0].tags))
another_mail = MailBuilder().with_subject('Mail with tags').build_input_mail()
- self.add_mail_to_inbox(another_mail)
- self.post_tags(another_mail.ident, self._tags_json(['IMPORTANT']))
- mails = self.get_mails_by_tag('IMPORTANT')
+ yield self.add_mail_to_inbox(another_mail)
+ yield self.post_tags(another_mail.ident, self._tags_json(['IMPORTANT']))
+ mails = yield self.get_mails_by_tag('IMPORTANT')
self.assertEquals(0, len(mails))
- mails = self.get_mails_by_tag('ImPoRtAnT')
+ mails = yield self.get_mails_by_tag('ImPoRtAnT')
self.assertEquals(2, len(mails))
self.assertEquals({'ImPoRtAnT'}, set(mails[0].tags))
self.assertEquals({'ImPoRtAnT'}, set(mails[1].tags))
+ @defer.inlineCallbacks
def test_tags_are_case_sensitive(self):
mail = MailBuilder().with_subject('Mail with tags').build_input_mail()
- self.add_mail_to_inbox(mail)
+ yield self.add_mail_to_inbox(mail)
- self.post_tags(mail.ident, self._tags_json(['ImPoRtAnT']))
+ yield self.post_tags(mail.ident, self._tags_json(['ImPoRtAnT']))
- mails = self.get_mails_by_tag('important')
+ mails = yield self.get_mails_by_tag('important')
self.assertEquals(0, len(mails))
- mails = self.get_mails_by_tag('IMPORTANT')
+ mails = yield self.get_mails_by_tag('IMPORTANT')
self.assertEquals(0, len(mails))
- mails = self.get_mails_by_tag('ImPoRtAnT')
+ mails = yield self.get_mails_by_tag('ImPoRtAnT')
self.assertEquals({'ImPoRtAnT'}, set(mails[0].tags))
+ @defer.inlineCallbacks
def test_empty_tags_are_not_allowed(self):
mail = MailBuilder().with_subject('Mail with tags').build_input_mail()
- self.add_mail_to_inbox(mail)
+ yield self.add_mail_to_inbox(mail)
- self.post_tags(mail.ident, self._tags_json(['tag1', ' ']))
+ yield self.post_tags(mail.ident, self._tags_json(['tag1', ' ']))
- mail = self.get_mail(mail.ident)
+ mail = yield self.get_mail(mail.ident)
self.assertEquals(mail['tags'], ['tag1'])
+ @defer.inlineCallbacks
def test_addition_of_reserved_tags_is_not_allowed(self):
mail = MailBuilder().with_subject('Mail with tags').build_input_mail()
- self.add_mail_to_inbox(mail)
+ yield self.add_mail_to_inbox(mail)
for tag in SPECIAL_TAGS:
- response = self.post_tags(mail.ident, self._tags_json([tag.name.upper()]))
+ response = yield self.post_tags(mail.ident, self._tags_json([tag.name.upper()]))
self.assertEquals("None of the following words can be used as tags: %s" % tag.name, response)
- mail = self.mailboxes.inbox.mail(mail.ident)
+ mail = yield (yield self.mailboxes.inbox).mail(mail.ident)
self.assertNotIn('drafts', mail.tags)