diff options
author | Duda Dornelles <ddornell@thoughtworks.com> | 2014-09-12 11:53:12 -0300 |
---|---|---|
committer | Patrick Maia <pmaia@thoughtworks.com> | 2014-09-15 11:54:32 -0300 |
commit | 6079196a9237ff1ae77ffb2bbc66f8bdc8698711 (patch) | |
tree | 26a105c1c220a290e3a0acb92277517d01c6d524 /service | |
parent | 03f7ece809492f8b3c295065a14833b78cbd58d8 (diff) |
When adding draft, return the mail with the new draft id so the UI can update it later
Diffstat (limited to 'service')
-rw-r--r-- | service/pixelated/adapter/mail_service.py | 6 | ||||
-rw-r--r-- | service/pixelated/adapter/pixelated_mail.py | 17 | ||||
-rw-r--r-- | service/pixelated/adapter/pixelated_mailbox.py | 8 | ||||
-rw-r--r-- | service/pixelated/adapter/pixelated_mailboxes.py | 6 | ||||
-rw-r--r-- | service/pixelated/user_agent.py | 18 | ||||
-rw-r--r-- | service/setup.py | 3 | ||||
-rw-r--r-- | service/test/adapter/mail_service_test.py | 7 | ||||
-rw-r--r-- | service/test/adapter/pixelated_mail_sender_test.py | 2 | ||||
-rw-r--r-- | service/test/adapter/pixelated_mail_test.py | 4 | ||||
-rw-r--r-- | service/test/adapter/pixelated_mailbox_test.py | 3 | ||||
-rw-r--r-- | service/test/adapter/pixelated_mailboxes_test.py | 14 | ||||
-rw-r--r-- | service/test/user_agent_test.py | 4 |
12 files changed, 61 insertions, 31 deletions
diff --git a/service/pixelated/adapter/mail_service.py b/service/pixelated/adapter/mail_service.py index 1183dae2..5f651187 100644 --- a/service/pixelated/adapter/mail_service.py +++ b/service/pixelated/adapter/mail_service.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see <http://www.gnu.org/licenses/>. from pixelated.adapter.tag_service import TagService +from pixelated.support.id_gen import gen_pixelated_uid class MailService: @@ -33,7 +34,7 @@ class MailService: if query['tags']: _mails = self.mailboxes.mails_by_tag(query['tags']) - return sorted(_mails or [], key=lambda mail: mail.date, reverse=True) + return sorted(_mails or [], key=lambda mail: mail.headers['date'], reverse=True) def update_tags(self, mail_id, new_tags): mail = self.mail(mail_id) @@ -48,8 +49,7 @@ class MailService: self.mail_sender.sendmail(mail) def create_draft(self, mail): - drafts = self.mailboxes.drafts() - drafts.add(mail) + return self.mailboxes.add_draft(mail) def send_draft(self, mail): pass diff --git a/service/pixelated/adapter/pixelated_mail.py b/service/pixelated/adapter/pixelated_mail.py index df9b4bbe..92b55ad4 100644 --- a/service/pixelated/adapter/pixelated_mail.py +++ b/service/pixelated/adapter/pixelated_mail.py @@ -25,7 +25,13 @@ from email.MIMEText import MIMEText class PixelatedMail: def __init__(self): - pass + self.body = '' + self.headers = {} + self.ident = None + self.status = [] + self.security_casing = {} + self.tags = [] + self.ident = None @staticmethod def from_leap_mail(leap_mail, leap_mailbox=None): @@ -34,7 +40,7 @@ class PixelatedMail: mail.leap_mailbox = leap_mailbox mail.body = leap_mail.bdoc.content['raw'] mail.headers = mail._extract_headers() - mail.date = PixelatedMail._get_date(mail.headers) + mail.headers['date'] = PixelatedMail._get_date(mail.headers) mail.status = set(mail._extract_status()) mail.security_casing = {} mail.tags = mail._extract_tags() @@ -121,10 +127,8 @@ class PixelatedMail: def as_dict(self): statuses = [status.name for status in self.status] - _headers = self.headers.copy() - _headers['date'] = self.date return { - 'header': _headers, + 'header': self.headers, 'ident': self.ident, 'tags': list(self.tags), 'status': statuses, @@ -147,6 +151,9 @@ class PixelatedMail: mime_multipart['From'] = PixelatedMail.from_email_address return mime_multipart.as_string() + def set_ident(self, mailbox_name, uid): + self.ident = gen_pixelated_uid(mailbox_name, uid) + @staticmethod def from_dict(mail_dict): return from_dict(mail_dict) diff --git a/service/pixelated/adapter/pixelated_mailbox.py b/service/pixelated/adapter/pixelated_mailbox.py index 191c171b..34c6c97b 100644 --- a/service/pixelated/adapter/pixelated_mailbox.py +++ b/service/pixelated/adapter/pixelated_mailbox.py @@ -16,6 +16,7 @@ from pixelated.adapter.pixelated_mail import PixelatedMail from pixelated.adapter.tag_service import TagService +from crochet import wait_for class PixelatedMailbox: @@ -29,6 +30,10 @@ class PixelatedMailbox: def messages(self): return self.leap_mailbox.messages + @property + def mailbox_name(self): + return self.leap_mailbox.mbox + def add_mailbox_tag_if_not_there(self, pixelated_mail): if not pixelated_mail.has_tag(self.mailbox_tag): pixelated_mail.update_tags({self.mailbox_tag}.union(pixelated_mail.tags)) @@ -58,8 +63,9 @@ class PixelatedMailbox: original_flags = mail.leap_mail.getFlags() self.leap_mailbox.addMessage(mail.raw_message(), original_flags) + @wait_for(timeout=3.0) def add(self, mail): - self.leap_mailbox.messages.add_msg(mail.to_smtp_format()) + return self.leap_mailbox.messages.add_msg(mail.to_smtp_format()) @classmethod def create(cls, account, mailbox_name='INBOX'): diff --git a/service/pixelated/adapter/pixelated_mailboxes.py b/service/pixelated/adapter/pixelated_mailboxes.py index 50d0ddc4..4866dbe4 100644 --- a/service/pixelated/adapter/pixelated_mailboxes.py +++ b/service/pixelated/adapter/pixelated_mailboxes.py @@ -26,6 +26,12 @@ class PixelatedMailBoxes(): return mails + def add_draft(self, mail): + drafts = self.drafts() + draft_id = drafts.add(mail) + mail.set_ident(drafts.mailbox_name, draft_id) + return mail + def mail(self, mail_id): for mailbox in self.mailboxes: mail = mailbox.mail(mail_id) diff --git a/service/pixelated/user_agent.py b/service/pixelated/user_agent.py index 643d0a30..952af297 100644 --- a/service/pixelated/user_agent.py +++ b/service/pixelated/user_agent.py @@ -15,14 +15,16 @@ # along with Pixelated. If not, see <http://www.gnu.org/licenses/>. import json import argparse +import getpass + import os import os.path +import crochet from flask import Flask from flask import request from flask import Response from pixelated.adapter.pixelated_mail_sender import PixelatedMailSender from pixelated.adapter.pixelated_mailboxes import PixelatedMailBoxes -from pixelated.adapter.tag_service import TagService import pixelated.reactor_manager as reactor_manager import pixelated.search_query as search_query import pixelated.bitmask_libraries.session as LeapSession @@ -31,7 +33,7 @@ from pixelated.bitmask_libraries.provider import LeapProvider from pixelated.bitmask_libraries.auth import LeapAuthenticator, LeapCredentials from pixelated.adapter.mail_service import MailService from pixelated.adapter.pixelated_mail import PixelatedMail -import getpass + static_folder = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "web-ui", "app")) @@ -60,12 +62,12 @@ def disabled_features(): @app.route('/mails', methods=['POST']) def send_mail(): - mail = PixelatedMail.from_dict(request.json) - if mail.ident: - mail_service.send_draft(mail) + _mail = PixelatedMail.from_dict(request.json) + if _mail.ident: + mail_service.send_draft(_mail) else: - mail_service.create_draft(mail) - return respond_json(None) + _mail = mail_service.create_draft(_mail) + return respond_json(_mail.as_dict()) @app.route('/mails', methods=['PUT']) @@ -186,6 +188,8 @@ def setup(): args = parser.parse_args() debug_enabled = args.debug or os.environ.get('DEBUG', False) reactor_manager.start_reactor(logging=debug_enabled) + + crochet.setup() app.config.from_pyfile(os.path.join(os.environ['HOME'], '.pixelated')) if args.register: diff --git a/service/setup.py b/service/setup.py index cb6ce68a..0821e28f 100644 --- a/service/setup.py +++ b/service/setup.py @@ -76,7 +76,8 @@ setup(name='pixelated-user-agent', 'leap.soledad.common==0.5.2', 'leap.soledad.client==0.5.2', 'leap.mail==0.3.9-1-gc1f9c92', - 'gunicorn' + 'gunicorn', + 'crochet' ], tests_require=[ 'nose', diff --git a/service/test/adapter/mail_service_test.py b/service/test/adapter/mail_service_test.py index 96359d30..782e891a 100644 --- a/service/test/adapter/mail_service_test.py +++ b/service/test/adapter/mail_service_test.py @@ -40,9 +40,8 @@ class TestMailService(unittest.TestCase): verify(mail).mark_as_read() def test_create_draft(self): - drafts_mailbox = mock() - when(self.mailboxes).drafts().thenReturn(drafts_mailbox) + mail = '' - self.mail_service.create_draft('a new draft') + self.mail_service.create_draft(mail) - verify(drafts_mailbox).add('a new draft') + verify(self.mailboxes).add_draft(mail) diff --git a/service/test/adapter/pixelated_mail_sender_test.py b/service/test/adapter/pixelated_mail_sender_test.py index 4bac327c..b41f1d0c 100644 --- a/service/test/adapter/pixelated_mail_sender_test.py +++ b/service/test/adapter/pixelated_mail_sender_test.py @@ -33,7 +33,7 @@ class PixelatedMailSenderTest(unittest.TestCase): mail_dict['header']['bcc'] = ['bcc@pixelated.org', 'anotherbcc@pixelated.org'] mail = PixelatedMail.from_dict(mail_dict) - mail.to_smtp_format = lambda : "mail as smtp string" + mail.to_smtp_format = lambda: "mail as smtp string" self.mail_sender.sendmail(mail) diff --git a/service/test/adapter/pixelated_mail_test.py b/service/test/adapter/pixelated_mail_test.py index 922c147d..9eab3d2c 100644 --- a/service/test/adapter/pixelated_mail_test.py +++ b/service/test/adapter/pixelated_mail_test.py @@ -41,7 +41,7 @@ class TestPixelatedMail(unittest.TestCase): mail = PixelatedMail.from_leap_mail(leap_mail) - self.assertEqual(str(mail.date), leap_mail_date_in_iso_format) + self.assertEqual(str(mail.headers['date']), leap_mail_date_in_iso_format) def test_parse_date_from_leap_mail_fallback_to_received_header_if_date_header_isnt_available(self): leap_mail_date = "Wed, 03 Sep 2014 13:11:15 -0300" @@ -52,7 +52,7 @@ class TestPixelatedMail(unittest.TestCase): mail = PixelatedMail.from_leap_mail(leap_mail) - self.assertEqual(str(mail.date), leap_mail_date_in_iso_format) + self.assertEqual(str(mail.headers['date']), leap_mail_date_in_iso_format) def test_from_dict(self): mail = PixelatedMail.from_dict(self.mail_dict) diff --git a/service/test/adapter/pixelated_mailbox_test.py b/service/test/adapter/pixelated_mailbox_test.py index bff6d6de..9ac4f53e 100644 --- a/service/test/adapter/pixelated_mailbox_test.py +++ b/service/test/adapter/pixelated_mailbox_test.py @@ -43,7 +43,6 @@ class TestPixelatedMailbox(unittest.TestCase): leap_mailbox_messages = mock() self.mailbox.leap_mailbox.messages = leap_mailbox_messages - self.mailbox.add(mail) + self.mailbox.add.wrapped_function(self.mailbox, mail) verify(leap_mailbox_messages).add_msg('the mail in smtp format') - diff --git a/service/test/adapter/pixelated_mailboxes_test.py b/service/test/adapter/pixelated_mailboxes_test.py index eebdc904..15606b51 100644 --- a/service/test/adapter/pixelated_mailboxes_test.py +++ b/service/test/adapter/pixelated_mailboxes_test.py @@ -23,10 +23,12 @@ from mockito import * class PixelatedMailboxesTest(unittest.TestCase): def setUp(self): self.account = mock() + self.drafts_mailbox = mock() + self.drafts_mailbox.mailbox_name = 'drafts' self.mailboxes = PixelatedMailBoxes(self.account) + self.mailboxes.drafts = lambda: self.drafts_mailbox def test_search_for_tags(self): - # given mailbox = mock() self.account.mailboxes = ['INBOX'] tags_to_search_for = {'tags': ['inbox', 'custom_tag']} @@ -34,10 +36,16 @@ class PixelatedMailboxesTest(unittest.TestCase): when(PixelatedMailbox).create(self.account, 'INBOX').thenReturn(mailbox) when(mailbox).mails_by_tags(any(list)).thenReturn(["mail"]) - # when mails = self.mailboxes.mails_by_tag(tags_to_search_for['tags']) - # then self.assertEqual(1, len(mails)) self.assertEqual("mail", mails[0]) + def test_add_draft(self): + mail = mock() + when(self.drafts_mailbox).add(mail).thenReturn(1) + + self.mailboxes.add_draft(mail) + + verify(self.drafts_mailbox).add(mail) + verify(mail).set_ident('drafts', 1) diff --git a/service/test/user_agent_test.py b/service/test/user_agent_test.py index 10d7fc6c..28fb4b69 100644 --- a/service/test/user_agent_test.py +++ b/service/test/user_agent_test.py @@ -30,7 +30,7 @@ class UserAgentTest(unittest.TestCase): def test_create_or_send_draft_should_create_draft_if_mail_has_no_ident(self): mail = self.mail_without_ident() - pixelated.adapter.pixelated_mail.from_dict = lambda self: mail #has no ident + pixelated.adapter.pixelated_mail.from_dict = lambda self: mail # has no ident self.app.post('/mails', data='{}', content_type="application/json") @@ -38,7 +38,7 @@ class UserAgentTest(unittest.TestCase): def test_create_or_send_draft_should_send_draft_if_mail_has_ident(self): mail = self.mail_with_ident() - pixelated.adapter.pixelated_mail.from_dict = lambda self: mail #does have ident + pixelated.adapter.pixelated_mail.from_dict = lambda self: mail # does have ident self.app.post('/mails', data='{}', content_type="application/json") |