From 3a22e05d083986b8111d7e98831f79960b83b993 Mon Sep 17 00:00:00 2001 From: Patrick Maia and Victor Shyba Date: Tue, 26 Aug 2014 22:19:48 +0000 Subject: - #51 - retrieves tag from leap and introduces PixelatedMailbox abstraction --- service/pixelated/adapter/mail_service.py | 18 +++++------ service/pixelated/adapter/pixelated_mailbox.py | 43 ++++++++++++++++++++++++++ service/test/adapter/mail_service_test.py | 3 +- service/test/adapter/pixelated_mailbox_test.py | 35 +++++++++++++++++++++ service/test/adapter/test_helper.py | 4 +++ 5 files changed, 92 insertions(+), 11 deletions(-) create mode 100644 service/pixelated/adapter/pixelated_mailbox.py create mode 100644 service/test/adapter/pixelated_mailbox_test.py (limited to 'service') diff --git a/service/pixelated/adapter/mail_service.py b/service/pixelated/adapter/mail_service.py index f2db8d16..edb6936b 100644 --- a/service/pixelated/adapter/mail_service.py +++ b/service/pixelated/adapter/mail_service.py @@ -22,6 +22,7 @@ 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 class MailService: @@ -46,12 +47,10 @@ class MailService: @property def mailbox(self): - return self.account.getMailbox(self.mailbox_name) + return PixelatedMailbox(self.account.getMailbox(self.mailbox_name)) def mails(self, query): - mails = self.mailbox.messages or [] - mails = [PixelatedMail.from_leap_mail(mail) for mail in mails] - return mails + return self.mailbox.mails() def update_tags(self, mail_id, new_tags): mail = self.mail(mail_id) @@ -66,20 +65,19 @@ class MailService: # self.tags.add(tag) def _update_flags(self, new_tags, mail_id): - new_tags_flag_name = ['tag_' + tag.name for tag in new_tags if tag.name not in ['inbox', 'drafts', 'sent', 'trash']] + 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) def set_flags(self, mail_id, new_tags_flag_name): observer = defer.Deferred() - self.mailbox.messages.set_flags(self.mailbox, [mail_id], tuple(new_tags_flag_name), 1, observer) + leap_mailbox = self.account.getMailbox(self.mailbox_name) + self.mailbox.messages.set_flags(leap_mailbox, [mail_id], tuple(new_tags_flag_name), 1, observer) def mail(self, mail_id): - for message in self.mailbox.messages: - if message.getUID() == int(mail_id): - return PixelatedMail.from_leap_mail(message) + return self.mailbox.mail(mail_id) def all_tags(self): - return [] + return self.mailbox.all_tags(); def thread(self, thread_id): raise NotImplementedError() diff --git a/service/pixelated/adapter/pixelated_mailbox.py b/service/pixelated/adapter/pixelated_mailbox.py new file mode 100644 index 00000000..52ca4655 --- /dev/null +++ b/service/pixelated/adapter/pixelated_mailbox.py @@ -0,0 +1,43 @@ + +# +# Copyright (c) 2014 ThoughtWorks, Inc. +# +# Pixelated is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pixelated is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Pixelated. If not, see . + +from pixelated.adapter.pixelated_mail import PixelatedMail +from pixelated.adapter.tag import Tag + +class PixelatedMailbox: + + SPECIAL_TAGS = ['inbox', 'sent', 'drafts', 'trash'] + + def __init__(self, leap_mailbox): + self.leap_mailbox = leap_mailbox + + @property + def messages(self): + return self.leap_mailbox.messages + + def mails(self): + mails = self.leap_mailbox.messages or [] + mails = [PixelatedMail.from_leap_mail(mail) for mail in mails] + return mails + + def mail(self, mail_id): + for message in self.leap_mailbox.messages: + if message.getUID() == int(mail_id): + return PixelatedMail.from_leap_mail(message) + + def all_tags(self): + return Tag.from_flags(self.leap_mailbox.getFlags()) diff --git a/service/test/adapter/mail_service_test.py b/service/test/adapter/mail_service_test.py index 86194036..b6ee3381 100644 --- a/service/test/adapter/mail_service_test.py +++ b/service/test/adapter/mail_service_test.py @@ -19,13 +19,14 @@ 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 class TestMailService(unittest.TestCase): @patch.object(MailService, 'set_flags', return_value=None) def test_custom_tags_get_created_if_not_exists(self, mockSetFlags): MailService._open_leap_session = lambda self: None - MailService.mailbox = Mock(messages=[test_helper.leap_mail(uid=6, leap_flags=['\\Recent'])]) + MailService.mailbox = PixelatedMailbox(Mock(messages=[test_helper.leap_mail(uid=6, 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 new file mode 100644 index 00000000..6604e737 --- /dev/null +++ b/service/test/adapter/pixelated_mailbox_test.py @@ -0,0 +1,35 @@ +# +# Copyright (c) 2014 ThoughtWorks, Inc. +# +# Pixelated is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pixelated is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Pixelated. If not, see . +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 + +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 + 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()) + + mailservice = MailService('username', 'password', 'leap_server') + + self.assertEquals(set([Tag('trash'), Tag('inbox'), Tag('drafts'), Tag('custom')]), mailservice.all_tags()) diff --git a/service/test/adapter/test_helper.py b/service/test/adapter/test_helper.py index 70f7585f..2c44b8f8 100644 --- a/service/test/adapter/test_helper.py +++ b/service/test/adapter/test_helper.py @@ -31,3 +31,7 @@ def leap_mail(uid=0, leap_flags=LEAP_FLAGS, extra_flags=[], headers={'date': str getFlags=Mock(return_value=flags), bdoc=Mock(content={'raw': 'test'}), hdoc=Mock(content={'headers': headers})) + +def leap_mailbox(leap_flags=LEAP_FLAGS, extra_flags=[]): + flags = leap_flags + extra_flags + return Mock(getFlags=Mock(return_value=flags)) -- cgit v1.2.3