diff options
author | Patrick Maia and Victor Shyba <pixelated-team+pmaia+vshyba@thoughtworks.com> | 2014-08-27 20:28:31 +0000 |
---|---|---|
committer | Patrick Maia <pmaia@thoughtworks.com> | 2014-08-27 20:28:47 +0000 |
commit | ee501988463fa09884987487828cda07fbe2b264 (patch) | |
tree | 6884e0f3f89ec707b54c3fd52dd39340863e4c9b | |
parent | 2823137cf813d227ff7110f92e0885027e3e959e (diff) |
#51 - sets leap mailbox flags on mail tags update
-rw-r--r-- | service/pixelated/adapter/mail_service.py | 19 | ||||
-rw-r--r-- | service/pixelated/adapter/pixelated_mailbox.py | 7 | ||||
-rw-r--r-- | service/pixelated/adapter/tag.py | 7 | ||||
-rw-r--r-- | service/test/adapter/mail_service_test.py | 4 | ||||
-rw-r--r-- | service/test/adapter/pixelated_mailbox_test.py | 23 | ||||
-rw-r--r-- | service/test/adapter/test_helper.py | 3 | ||||
-rw-r--r-- | service/test/adapter/test_tag.py | 17 |
7 files changed, 56 insertions, 24 deletions
diff --git a/service/pixelated/adapter/mail_service.py b/service/pixelated/adapter/mail_service.py index 1f4279e7..d9c30e51 100644 --- a/service/pixelated/adapter/mail_service.py +++ b/service/pixelated/adapter/mail_service.py @@ -21,8 +21,8 @@ from pixelated.bitmask_libraries.config import LeapConfig from pixelated.bitmask_libraries.provider import LeapProvider from pixelated.bitmask_libraries.session import LeapSessionFactory from pixelated.bitmask_libraries.auth import LeapCredentials -from pixelated.adapter.pixelated_mail import PixelatedMail from pixelated.adapter.pixelated_mailbox import PixelatedMailbox +from pixelated.adapter.tag import Tag class MailService: @@ -55,20 +55,19 @@ class MailService: def update_tags(self, mail_id, new_tags): mail = self.mail(mail_id) new_tags = mail.update_tags(new_tags) - self._update_flags(new_tags, mail_id) - self._update_tag_list(new_tags) + self._update_mail_flags(new_tags, mail_id) + self._update_mailbox_tags(new_tags) return new_tags - def _update_tag_list(self, tags): - for tag in tags: - pass - # self.tags.add(tag) + def _update_mailbox_tags(self, str_tags): + tags = [Tag(str_tag) for str_tag in str_tags] + self.mailbox.update_tags(tags) - def _update_flags(self, new_tags, mail_id): + def _update_mail_flags(self, new_tags, mail_id): new_tags_flag_name = ['tag_' + tag.name for tag in new_tags if tag.name not in PixelatedMailbox.SPECIAL_TAGS] - self.set_flags(mail_id, new_tags_flag_name) + self._set_mail_flags(mail_id, new_tags_flag_name) - def set_flags(self, mail_id, new_tags_flag_name): + def _set_mail_flags(self, mail_id, new_tags_flag_name): observer = defer.Deferred() leap_mailbox = self.account.getMailbox(self.mailbox_name) self.mailbox.messages.set_flags(leap_mailbox, [mail_id], tuple(new_tags_flag_name), 1, observer) diff --git a/service/pixelated/adapter/pixelated_mailbox.py b/service/pixelated/adapter/pixelated_mailbox.py index 501d179d..7521de6e 100644 --- a/service/pixelated/adapter/pixelated_mailbox.py +++ b/service/pixelated/adapter/pixelated_mailbox.py @@ -42,3 +42,10 @@ class PixelatedMailbox: def all_tags(self): return Tag.from_flags(self.leap_mailbox.getFlags()) + + def update_tags(self, tags): + new_flags = set(tag.to_flag() for tag in tags) + current_flags = set(self.leap_mailbox.getFlags()) + + flags = tuple(current_flags.union(new_flags)) + self.leap_mailbox.setFlags(flags) diff --git a/service/pixelated/adapter/tag.py b/service/pixelated/adapter/tag.py index 8d1d209d..386ccafc 100644 --- a/service/pixelated/adapter/tag.py +++ b/service/pixelated/adapter/tag.py @@ -13,7 +13,6 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see <http://www.gnu.org/licenses/>. -import json class Tag: @@ -36,6 +35,12 @@ class Tag: return Tag(cls._remove_prefix(flag)) return None + def to_flag(self): + for flag, tag in self.LEAP_FLAGS_TAGS.items(): + if tag == str(self.name): + return flag + return 'tag_' + str(self.name) + @classmethod def _remove_prefix(cls, flag_name): return flag_name.replace('tag_', '', 1) diff --git a/service/test/adapter/mail_service_test.py b/service/test/adapter/mail_service_test.py index 7c928ec4..6972f6f6 100644 --- a/service/test/adapter/mail_service_test.py +++ b/service/test/adapter/mail_service_test.py @@ -24,10 +24,10 @@ from pixelated.adapter.pixelated_mailbox import PixelatedMailbox class TestMailService(unittest.TestCase): - @patch.object(MailService, 'set_flags', return_value=None) + @patch.object(MailService, '_set_mail_flags', return_value=None) def test_custom_tags_get_created_if_not_exists(self, mockSetFlags): MailService._open_leap_session = lambda self: None - MailService.mailbox = PixelatedMailbox(Mock(messages=[test_helper.leap_mail(uid=6, leap_flags=['\\Recent'])])) + MailService.mailbox = PixelatedMailbox(test_helper.leap_mailbox(leap_flags=['\\Recent'])) MailService.account = Mock(return_value=MagicMock()) mailservice = MailService('username', 'password', 'leap_server') diff --git a/service/test/adapter/pixelated_mailbox_test.py b/service/test/adapter/pixelated_mailbox_test.py index 33cac5b9..1047eed4 100644 --- a/service/test/adapter/pixelated_mailbox_test.py +++ b/service/test/adapter/pixelated_mailbox_test.py @@ -15,8 +15,6 @@ # along with Pixelated. If not, see <http://www.gnu.org/licenses/>. import unittest -from pixelated.adapter.mail_service import MailService -from mock import Mock, MagicMock, patch import test_helper from pixelated.adapter.tag import Tag from pixelated.adapter.pixelated_mailbox import PixelatedMailbox @@ -24,13 +22,20 @@ from pixelated.adapter.pixelated_mailbox import PixelatedMailbox class TestPixelatedMailbox(unittest.TestCase): - @patch.object(MailService, 'set_flags', return_value=None) - def test_retrieve_all_tags_from_mailbox(self, mockSetFlags): - MailService._open_leap_session = lambda self: None + def test_retrieve_all_tags_from_mailbox(self): leap_flags = ['\\Deleted', '\\Draft', '\\Recent', 'tag_custom', 'should_ignore_all_from_here', 'List'] - MailService.mailbox = PixelatedMailbox(test_helper.leap_mailbox(leap_flags=leap_flags)) - MailService.account = Mock(return_value=MagicMock()) + mailbox = PixelatedMailbox(test_helper.leap_mailbox(leap_flags=leap_flags)) - mailservice = MailService('username', 'password', 'leap_server') + self.assertEquals(set([Tag('trash'), Tag('inbox'), Tag('drafts'), Tag('custom')]), mailbox.all_tags()) - self.assertEquals(set([Tag('trash'), Tag('inbox'), Tag('drafts'), Tag('custom')]), mailservice.all_tags()) + def test_new_tags_are_added_to_mailbox(self): + leap_flags = ['\\Deleted', 'tag_custom_one', 'tag_custom_two'] + leap_mailbox_mock = test_helper.leap_mailbox(leap_flags=leap_flags) + mailbox = PixelatedMailbox(leap_mailbox_mock) + tags = [Tag('custom_one'), Tag('custom_three')] + mailbox.update_tags(tags) + + expected = set(('\\Deleted', 'tag_custom_one', 'tag_custom_two', 'tag_custom_three')) + actual_args = set(leap_mailbox_mock.setFlags.call_args[0][0]) + + self.assertEquals(expected, actual_args) diff --git a/service/test/adapter/test_helper.py b/service/test/adapter/test_helper.py index ae4bf887..5362b029 100644 --- a/service/test/adapter/test_helper.py +++ b/service/test/adapter/test_helper.py @@ -35,4 +35,5 @@ def leap_mail(uid=0, leap_flags=LEAP_FLAGS, extra_flags=[], headers={'date': str def leap_mailbox(leap_flags=LEAP_FLAGS, extra_flags=[]): flags = leap_flags + extra_flags - return Mock(getFlags=Mock(return_value=flags)) + return Mock(getFlags=Mock(return_value=flags), + messages=[leap_mail(uid=6, leap_flags=[], extra_flags=[])]) diff --git a/service/test/adapter/test_tag.py b/service/test/adapter/test_tag.py index 30bc1550..b9b502d4 100644 --- a/service/test/adapter/test_tag.py +++ b/service/test/adapter/test_tag.py @@ -16,7 +16,6 @@ import unittest from pixelated.adapter.tag import Tag -import test_helper class TestTag(unittest.TestCase): @@ -44,3 +43,19 @@ class TestTag(unittest.TestCase): def test_bulk_conversion(self): tags = Tag.from_flags(['\\Answered', '\\Seen', '\\Recent', 'tag_a_custom', 'List']) self.assertEquals(set([Tag('inbox'), Tag('a_custom')]), tags) + + def test_inbox_tag_is_translated_to_leap_recent_flag(self): + flag = Tag('inbox').to_flag() + self.assertEquals('\\Recent', flag) + + def test_trash_tag_is_translated_to_leap_deleted_flag(self): + flag = Tag('trash').to_flag() + self.assertEquals('\\Deleted', flag) + + def test_drafts_tag_is_translated_to_leap_draft_flag(self): + flag = Tag('drafts').to_flag() + self.assertEquals('\\Draft', flag) + + def test_custom_tag_has_prefix_when_translated_to_flag(self): + flag = Tag('work').to_flag() + self.assertEquals('tag_work', flag) |