diff options
-rw-r--r-- | service/pixelated/user_agent.py | 9 | ||||
-rw-r--r-- | service/test/user_agent_test.py | 20 |
2 files changed, 28 insertions, 1 deletions
diff --git a/service/pixelated/user_agent.py b/service/pixelated/user_agent.py index c61618b9..93a3caf3 100644 --- a/service/pixelated/user_agent.py +++ b/service/pixelated/user_agent.py @@ -19,6 +19,7 @@ import getpass import os import os.path +import re import crochet from flask import Flask from flask import request @@ -120,7 +121,13 @@ def delete_mails(): @app.route('/tags') def tags(): - tags = mail_service.all_tags() + query = request.args.get('q') + + if query: + tags = [tag for tag in mail_service.all_tags() if bool(re.match(query, tag.name, re.IGNORECASE))] + else: + tags = mail_service.all_tags() + return respond_json([tag.as_dict() for tag in tags]) diff --git a/service/test/user_agent_test.py b/service/test/user_agent_test.py index fea8393a..2f1ed01d 100644 --- a/service/test/user_agent_test.py +++ b/service/test/user_agent_test.py @@ -18,6 +18,8 @@ import unittest import pixelated.user_agent from pixelated.adapter.pixelated_mail import PixelatedMail from pixelated.adapter.pixelated_mail import InputMail +from pixelated.adapter.tag import Tag +from pixelated.adapter.tag_service import TagService from mockito import * import crochet import pixelated.reactor_manager as reactor_manager @@ -97,3 +99,21 @@ class UserAgentTest(unittest.TestCase): verify(pixelated.user_agent.app.config).from_pyfile('/tmp/some/config/file') finally: pixelated.user_agent.app.config = orig_config + + def test_that_tags_returns_all_tags(self): + when(self.mail_service).all_tags().thenReturn(TagService.SPECIAL_TAGS) + + response = self.app.get('/tags') + + self.assertEqual(200, response.status_code) + expected = json.dumps([tag.as_dict() for tag in TagService.SPECIAL_TAGS]) + self.assertEqual(expected, response.data) + + def test_that_tags_are_filtered_by_query(self): + when(self.mail_service).all_tags().thenReturn(TagService.SPECIAL_TAGS) + + response = self.app.get('/tags?q=dr') + + self.assertEqual(200, response.status_code) + expected = json.dumps([Tag('drafts', True).as_dict()]) + self.assertEqual(expected, response.data) |