summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorPatrick Maia <patrickjourdanmaia@gmail.com>2014-10-01 11:59:29 -0300
committerPatrick Maia <patrickjourdanmaia@gmail.com>2014-10-01 16:43:43 -0300
commit6244ef377aace0659c088f6c95878ec8aeb0cd02 (patch)
tree5546d537341322bbdeea3809f5aebf2513b0600b /service
parentd58d64c764a1a83411cff6a28a150cc8138a0228 (diff)
#53 - does not allow reserved names in tags
Diffstat (limited to 'service')
-rw-r--r--service/integration/tags_test.py10
-rw-r--r--service/pixelated/adapter/mail_service.py3
-rw-r--r--service/pixelated/adapter/pixelated_mail.py6
-rw-r--r--service/pixelated/adapter/tag_service.py6
-rw-r--r--service/pixelated/user_agent.py13
5 files changed, 29 insertions, 9 deletions
diff --git a/service/integration/tags_test.py b/service/integration/tags_test.py
index a0743b6a..eb278a71 100644
--- a/service/integration/tags_test.py
+++ b/service/integration/tags_test.py
@@ -33,7 +33,7 @@ class TagsTest(unittest.TestCase, SoledadTestBase):
mail = MailBuilder().with_subject('Mail with tags').build_input_mail()
self.pixelated_mailboxes.inbox().add(mail)
- self.post_tags(mail.ident, self._tags_json(['INBOX', 'IMPORTANT']))
+ self.post_tags(mail.ident, self._tags_json(['IMPORTANT']))
mails = self.get_mails_by_tag('inbox')
self.assertEquals({'inbox', 'important'}, set(mails[0].tags))
@@ -41,4 +41,12 @@ class TagsTest(unittest.TestCase, SoledadTestBase):
mails = self.get_mails_by_tag('important')
self.assertEquals('Mail with tags', mails[0].subject)
+ def test_addition_of_reserved_tags_is_not_allowed(self):
+ mail = MailBuilder().with_subject('Mail with tags').build_input_mail()
+ self.pixelated_mailboxes.inbox().add(mail)
+
+ response = self.post_tags(mail.ident, self._tags_json(['DRAFTS']))
+ self.assertEquals("None of the following words can be used as tags: ['drafts']", response)
+ mail = self.pixelated_mailboxes.inbox().mail(mail.ident)
+ self.assertNotIn('drafts', mail.tags)
diff --git a/service/pixelated/adapter/mail_service.py b/service/pixelated/adapter/mail_service.py
index c216e7ae..8be04984 100644
--- a/service/pixelated/adapter/mail_service.py
+++ b/service/pixelated/adapter/mail_service.py
@@ -35,6 +35,9 @@ class MailService:
return sorted(_mails or [], key=lambda mail: mail.headers['Date'], reverse=True)
def update_tags(self, mail_id, new_tags):
+ reserved_words = self.tag_service.extract_reserved(new_tags)
+ if len(reserved_words):
+ raise ValueError('None of the following words can be used as tags: %s' % list(reserved_words))
mail = self.mail(mail_id)
return mail.update_tags(set(new_tags))
diff --git a/service/pixelated/adapter/pixelated_mail.py b/service/pixelated/adapter/pixelated_mail.py
index 3e0f1ee0..2f13cb01 100644
--- a/service/pixelated/adapter/pixelated_mail.py
+++ b/service/pixelated/adapter/pixelated_mail.py
@@ -24,7 +24,6 @@ import pixelated.support.date
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from pycryptopp.hash import sha256
-from pixelated.support.functional import flatten
class InputMail:
@@ -250,8 +249,9 @@ class PixelatedMail:
return self
def mark_as_not_recent(self):
- self.fdoc.content['flags'].remove(Status.PixelatedStatus.RECENT)
- self.save()
+ if Status.PixelatedStatus.RECENT in self.fdoc.content['flags']:
+ self.fdoc.content['flags'].remove(Status.PixelatedStatus.RECENT)
+ self.save()
return self
def _persist_mail_tags(self, current_tags):
diff --git a/service/pixelated/adapter/tag_service.py b/service/pixelated/adapter/tag_service.py
index 00f28b40..ecc64fad 100644
--- a/service/pixelated/adapter/tag_service.py
+++ b/service/pixelated/adapter/tag_service.py
@@ -20,7 +20,11 @@ from pixelated.adapter.tag_index import TagIndex
class TagService:
instance = None
- SPECIAL_TAGS = {Tag('inbox', True), Tag('sent', True), Tag('drafts', True), Tag('trash', True)}
+ SPECIAL_TAGS = [Tag('inbox', True), Tag('sent', True), Tag('drafts', True), Tag('trash', True)]
+
+ @classmethod
+ def extract_reserved(cls, tags):
+ return set(tag.name for tag in cls.SPECIAL_TAGS if tag.name in tags)
@classmethod
def get_instance(cls):
diff --git a/service/pixelated/user_agent.py b/service/pixelated/user_agent.py
index 2dc71bf0..8c7440b0 100644
--- a/service/pixelated/user_agent.py
+++ b/service/pixelated/user_agent.py
@@ -47,9 +47,11 @@ app = Flask(__name__, static_url_path='', static_folder=static_folder)
DISABLED_FEATURES = ['draftReply', 'signatureStatus', 'encryptionStatus', 'contacts']
-def respond_json(entity):
- response = json.dumps(entity)
- return Response(response=response, mimetype="application/json")
+def respond_json(entity, status_code=200):
+ json_response = json.dumps(entity)
+ response = Response(response=json_response, mimetype="application/json")
+ response.status_code = status_code
+ return response
@app.route('/disabled_features')
@@ -127,7 +129,10 @@ def mail(mail_id):
@app.route('/mail/<mail_id>/tags', methods=['POST'])
def mail_tags(mail_id):
new_tags = map(lambda tag: tag.lower(), request.get_json()['newtags'])
- tags = mail_service.update_tags(mail_id, new_tags)
+ try:
+ tags = mail_service.update_tags(mail_id, new_tags)
+ except ValueError as ve:
+ return respond_json(ve.message, 403)
return respond_json(list(tags))