summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorFolker Bernitt <fbernitt@thoughtworks.com>2014-08-28 16:16:19 +0200
committerFolker Bernitt <fbernitt@thoughtworks.com>2014-08-28 17:45:51 +0200
commit8cfe696cd85ac0c5d6d863ebf71ebcf61d4ad540 (patch)
tree8c0c2652bbf8108261e0257827403e28537cea3d /service
parentdc3f7d1ae9a56192676ae4efb8286b0968ec9726 (diff)
Added mail filtering by tag.
- simple brute force implementation
Diffstat (limited to 'service')
-rw-r--r--service/pixelated/adapter/mail_service.py50
-rw-r--r--service/pixelated/user_agent.py5
-rw-r--r--service/test/adapter/mail_service_test.py47
-rw-r--r--service/test/search/test_search_query.py2
4 files changed, 79 insertions, 25 deletions
diff --git a/service/pixelated/adapter/mail_service.py b/service/pixelated/adapter/mail_service.py
index 3d87971e..f5bfd1c8 100644
--- a/service/pixelated/adapter/mail_service.py
+++ b/service/pixelated/adapter/mail_service.py
@@ -25,32 +25,46 @@ from pixelated.adapter.pixelated_mailbox import PixelatedMailbox
from pixelated.adapter.tag import Tag
+def open_leap_session(username, password, server_name):
+ try:
+ certs_home = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "certificates"))
+
+ config = LeapConfig(certs_home=certs_home)
+ provider = LeapProvider(server_name, config)
+ session = LeapSessionFactory(provider).create(LeapCredentials(username, password))
+ return session
+ except:
+ traceback.print_exc(file=sys.stdout)
+ raise
+
+
class MailService:
+ __slots__ = ['leap_session', 'account', 'mailbox_name']
- def __init__(self, username, password, server_name):
- try:
- self.username = username
- self.password = password
- self.server_name = server_name
- self.mailbox_name = 'INBOX'
- self.certs_home = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "certificates"))
- self._open_leap_session()
- except:
- traceback.print_exc(file=sys.stdout)
- raise
-
- def _open_leap_session(self):
- self.leap_config = LeapConfig(certs_home=self.certs_home)
- self.provider = LeapProvider(self.server_name, self.leap_config)
- self.leap_session = LeapSessionFactory(self.provider).create(LeapCredentials(self.username, self.password))
- self.account = self.leap_session.account
+ def __init__(self, leap_session):
+ self.leap_session = leap_session
+ self.account = leap_session.account
+ self.mailbox_name = 'INBOX'
@property
def mailbox(self):
return PixelatedMailbox(self.account.getMailbox(self.mailbox_name))
def mails(self, query):
- return self.mailbox.mails()
+ if not query:
+ return self.mailbox.mails()
+
+ mails = []
+ if query['tags']:
+ tags = [Tag(tag) for tag in query['tags']]
+ for leap_mailbox in self.account.mailboxes:
+ mailbox = PixelatedMailbox(leap_mailbox)
+ if len(mailbox.all_tags().intersection(tags)):
+ # mailbox has at least one mail with tag
+ for mail in mailbox.mails():
+ if len(mail.tags.intersection(tags)) > 0:
+ mails.append(mail)
+ return mails
def update_tags(self, mail_id, new_tags):
mail = self.mail(mail_id)
diff --git a/service/pixelated/user_agent.py b/service/pixelated/user_agent.py
index b9ac05bb..433d4770 100644
--- a/service/pixelated/user_agent.py
+++ b/service/pixelated/user_agent.py
@@ -24,7 +24,7 @@ from flask import Response
import pixelated.reactor_manager as reactor_manager
import pixelated.search_query as search_query
-from pixelated.adapter.mail_service import MailService
+from pixelated.adapter.mail_service import MailService, open_leap_session
from pixelated.adapter.pixelated_mail import PixelatedMail
static_folder = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "web-ui", "app"))
@@ -33,7 +33,8 @@ if not os.path.exists(static_folder):
static_folder = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "..", "web-ui", "app"))
app = Flask(__name__, static_url_path='', static_folder=static_folder)
app.config.from_pyfile(os.path.join(os.environ['HOME'], '.pixelated'))
-mail_service = MailService(app.config['LEAP_USERNAME'], app.config['LEAP_PASSWORD'], app.config['LEAP_SERVER_NAME'])
+leap_session = open_leap_session(app.config['LEAP_USERNAME'], app.config['LEAP_PASSWORD'], app.config['LEAP_SERVER_NAME'])
+mail_service = MailService(leap_session)
def respond_json(entity):
diff --git a/service/test/adapter/mail_service_test.py b/service/test/adapter/mail_service_test.py
index 7b3f4e96..6c5eca24 100644
--- a/service/test/adapter/mail_service_test.py
+++ b/service/test/adapter/mail_service_test.py
@@ -16,7 +16,7 @@
import unittest
from pixelated.adapter.mail_service import MailService
-from mock import Mock, MagicMock, patch
+from mock import Mock, MagicMock, patch, PropertyMock
import test_helper
from pixelated.adapter.tag import Tag
from pixelated.adapter.pixelated_mailbox import PixelatedMailbox
@@ -27,11 +27,13 @@ class TestMailService(unittest.TestCase):
@patch.object(MailService, '_append_mail_flags', return_value=None)
@patch.object(MailService, '_remove_mail_flags', return_value=None)
def test_custom_tags_get_created_if_not_exists(self, mockRemoveFlags, mockAppendFlags):
- MailService._open_leap_session = lambda self: None
- MailService.mailbox = PixelatedMailbox(test_helper.leap_mailbox(leap_flags=['\\Recent']))
- MailService.account = Mock(return_value=MagicMock())
+ mailbox = test_helper.leap_mailbox(leap_flags=['\\Recent'])
+ account = MagicMock()
+ leap_session = MagicMock()
+ leap_session.account = account
+ leap_session.account.getMailbox.return_value = mailbox
- mailservice = MailService('username', 'password', 'leap_server')
+ mailservice = MailService(leap_session)
new_tags = ['test', 'inbox']
updated_tags = mailservice.update_tags(6, new_tags)
@@ -40,3 +42,38 @@ class TestMailService(unittest.TestCase):
# make sure that special tags are skipped when setting leap flags (eg.: tag_inbox)
mockAppendFlags.assert_called_with(6, ['tag_test'])
mockRemoveFlags.assert_called_with(6, [])
+
+ def test_search_without_query_returns_unfiltered_mailbox(self):
+ # given
+ mailbox = test_helper.leap_mailbox(leap_flags=['\\Recent'], extra_flags=['tag_custom_tag'])
+ account = MagicMock()
+ account.getMailbox.return_value = mailbox
+ leap_session = MagicMock(account=account)
+
+ # when
+ mailservice = MailService(leap_session)
+ mails = mailservice.mails({})
+
+ # then
+ self.assertEqual(1, len(mails))
+
+ def test_search_for_tags(self):
+ # given
+ mailbox = test_helper.leap_mailbox(leap_flags=['\\Recent'], extra_flags=['tag_custom_tag'])
+ matching_mail = test_helper.leap_mail(uid=6, leap_flags=[], extra_flags=['tag_custom_tag'])
+ not_matching_mail = test_helper.leap_mail(uid=6, leap_flags=[], extra_flags=['tag_other'])
+ mailbox.messages = [matching_mail, not_matching_mail]
+ mailbox.all_tags.return_value = set()
+ account = MagicMock()
+ account.mailboxes = [mailbox]
+
+ leap_session = MagicMock(account=account)
+
+ # when
+ mailservice = MailService(leap_session)
+ mails = mailservice.mails({'tags': ['inbox', 'custom_tag']})
+
+ # then
+ self.assertEqual(1, len(mails))
+ self.assertEqual(set([Tag('custom_tag')]), mails[0].tags)
+
diff --git a/service/test/search/test_search_query.py b/service/test/search/test_search_query.py
index 3bcbd219..e9fcfa5a 100644
--- a/service/test/search/test_search_query.py
+++ b/service/test/search/test_search_query.py
@@ -37,3 +37,5 @@ class SearchTestCase(unittest.TestCase):
def test_tags_with_quotes(self):
self.assertEquals(search_query.compile(u"in:\"inbox\"")["tags"], ["inbox"])
self.assertEquals(search_query.compile(u"in:'inbox'")["tags"], ["inbox"])
+
+