diff options
author | Duda Dornelles <ddornell@thoughtworks.com> | 2015-02-03 18:58:13 -0200 |
---|---|---|
committer | Duda Dornelles <ddornell@thoughtworks.com> | 2015-02-03 18:58:13 -0200 |
commit | 9e7643588e124290879a9ae82f20fa628a9c090a (patch) | |
tree | bd7979919eb1bd8be573afd2fdc8fbbf7115ad17 | |
parent | 543e43ce85bb5d8863dbbf2a458af92d21b018cf (diff) |
Movinge MailsResource#render_POST logic to MailService and some search_engine calls to Mailbox
-rw-r--r-- | service/pixelated/adapter/services/mail_service.py | 11 | ||||
-rw-r--r-- | service/pixelated/adapter/services/mailbox.py | 15 | ||||
-rw-r--r-- | service/pixelated/adapter/services/mailboxes.py | 5 | ||||
-rw-r--r-- | service/pixelated/config/app_factory.py | 2 | ||||
-rw-r--r-- | service/pixelated/resources/mails_resource.py | 10 | ||||
-rw-r--r-- | service/test/functional/features/environment.py | 4 | ||||
-rw-r--r-- | service/test/support/integration/app_test_client.py | 4 | ||||
-rw-r--r-- | service/test/unit/adapter/test_mail_service.py | 11 | ||||
-rw-r--r-- | service/test/unit/adapter/test_mailbox.py | 3 |
9 files changed, 35 insertions, 30 deletions
diff --git a/service/pixelated/adapter/services/mail_service.py b/service/pixelated/adapter/services/mail_service.py index ab0b16bd..da74c0ef 100644 --- a/service/pixelated/adapter/services/mail_service.py +++ b/service/pixelated/adapter/services/mail_service.py @@ -13,6 +13,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.model.mail import InputMail class MailService: @@ -47,10 +48,16 @@ class MailService: def mail_exists(self, mail_id): return not(not(self.querier.get_header_by_chash(mail_id))) - def send(self, mail): + def send_mail(self, content_dict): + mail = InputMail.from_dict(content_dict) + draft_id = content_dict.get('ident') + self.mail_sender.sendmail(mail) + sent_mail = self.move_to_sent(draft_id, mail) + + return sent_mail - def move_to_send(self, last_draft_ident, mail): + def move_to_sent(self, last_draft_ident, mail): if last_draft_ident: self.mailboxes.drafts().remove(last_draft_ident) return self.mailboxes.sent().add(mail) diff --git a/service/pixelated/adapter/services/mailbox.py b/service/pixelated/adapter/services/mailbox.py index 40d9cb8e..f934abcc 100644 --- a/service/pixelated/adapter/services/mailbox.py +++ b/service/pixelated/adapter/services/mailbox.py @@ -17,25 +17,26 @@ class Mailbox: - def __init__(self, mailbox_name, querier): + def __init__(self, mailbox_name, querier, search_engine): self.mailbox_name = mailbox_name self.mailbox_tag = mailbox_name.lower() + self.search_engine = search_engine self.querier = querier def mail(self, mail_id): return self.querier.mail(mail_id) def add(self, mail): - return self.querier.create_mail(mail, self.mailbox_name) + added_mail = self.querier.create_mail(mail, self.mailbox_name) + self.search_engine.index_mail(added_mail) + return added_mail def remove(self, ident): mail = self.querier.mail(ident) - self.remove_mail(mail) - - def remove_mail(self, mail): + self.search_engine.remove_from_index(mail.ident) mail.remove_all_tags() self.querier.remove_mail(mail) @classmethod - def create(cls, mailbox_name, soledad_querier): - return Mailbox(mailbox_name, soledad_querier) + def create(cls, mailbox_name, soledad_querier, search_engine): + return Mailbox(mailbox_name, soledad_querier, search_engine) diff --git a/service/pixelated/adapter/services/mailboxes.py b/service/pixelated/adapter/services/mailboxes.py index 51d4add9..0a1c9a4b 100644 --- a/service/pixelated/adapter/services/mailboxes.py +++ b/service/pixelated/adapter/services/mailboxes.py @@ -19,9 +19,10 @@ from pixelated.adapter.listeners.mailbox_indexer_listener import MailboxIndexerL class Mailboxes(): - def __init__(self, account, soledad_querier): + def __init__(self, account, soledad_querier, search_engine): self.account = account self.querier = soledad_querier + self.search_engine = search_engine for mailbox_name in account.mailboxes: MailboxIndexerListener.listen(self.account, mailbox_name, soledad_querier) @@ -30,7 +31,7 @@ class Mailboxes(): if mailbox_name not in self.account.mailboxes: self.account.addMailbox(mailbox_name) MailboxIndexerListener.listen(self.account, mailbox_name, self.querier) - return Mailbox.create(mailbox_name, self.querier) + return Mailbox.create(mailbox_name, self.querier, self.search_engine) def inbox(self): return self._create_or_get('INBOX') diff --git a/service/pixelated/config/app_factory.py b/service/pixelated/config/app_factory.py index 9937c65b..a1fd92dd 100644 --- a/service/pixelated/config/app_factory.py +++ b/service/pixelated/config/app_factory.py @@ -103,7 +103,7 @@ def init_app(app, leap_home, leap_session): search_engine = SearchEngine(soledad_querier, agent_home=leap_home) pixelated_mail_sender = MailSender(leap_session.account_email()) - pixelated_mailboxes = Mailboxes(leap_session.account, soledad_querier) + pixelated_mailboxes = Mailboxes(leap_session.account, soledad_querier, search_engine) draft_service = DraftService(pixelated_mailboxes) mail_service = MailService(pixelated_mailboxes, pixelated_mail_sender, tag_service, soledad_querier, search_engine) diff --git a/service/pixelated/resources/mails_resource.py b/service/pixelated/resources/mails_resource.py index 41d1d702..714a68c2 100644 --- a/service/pixelated/resources/mails_resource.py +++ b/service/pixelated/resources/mails_resource.py @@ -105,15 +105,7 @@ class MailsResource(Resource): def render_POST(self, request): content_dict = json.loads(request.content.read()) - _mail = InputMail.from_dict(content_dict) - draft_id = content_dict.get('ident') - - self._mail_service.send(_mail) - sent_mail = self._mail_service.move_to_send(draft_id, _mail) - self._search_engine.index_mail(sent_mail) - - if draft_id: - self._search_engine.remove_from_index(draft_id) + sent_mail = self._mail_service.send_mail(content_dict) return respond_json(sent_mail.as_dict(), request) diff --git a/service/test/functional/features/environment.py b/service/test/functional/features/environment.py index 5e93c840..d78083d0 100644 --- a/service/test/functional/features/environment.py +++ b/service/test/functional/features/environment.py @@ -38,8 +38,8 @@ def after_all(context): def before_feature(context, feature): - # context.browser = webdriver.Firefox() - context.browser = webdriver.PhantomJS() + context.browser = webdriver.Firefox() + # context.browser = webdriver.PhantomJS() context.browser.set_window_size(1280, 1024) context.browser.implicitly_wait(5) context.browser.set_page_load_timeout(60) # wait for data diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index 45f81cc1..dd2d0200 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -63,11 +63,11 @@ class AppTestClient: self.soledad_querier.get_index_masterkey = lambda: self.INDEX_KEY self.account = SoledadBackedAccount('test', self.soledad, MagicMock()) - self.mailboxes = Mailboxes(self.account, self.soledad_querier) + self.search_engine = SearchEngine(self.soledad_querier, agent_home=soledad_test_folder) + self.mailboxes = Mailboxes(self.account, self.soledad_querier, self.search_engine) self.mail_sender = Mock() self.tag_service = TagService() self.draft_service = DraftService(self.mailboxes) - self.search_engine = SearchEngine(self.soledad_querier, agent_home=soledad_test_folder) self.mail_service = MailService(self.mailboxes, self.mail_sender, self.tag_service, self.soledad_querier, self.search_engine) self.search_engine.index_mails(self.mail_service.all_mails()) diff --git a/service/test/unit/adapter/test_mail_service.py b/service/test/unit/adapter/test_mail_service.py index 32b45dc4..4b1c60d6 100644 --- a/service/test/unit/adapter/test_mail_service.py +++ b/service/test/unit/adapter/test_mail_service.py @@ -14,8 +14,10 @@ # 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 unittest +from pixelated.adapter.model.mail import InputMail from pixelated.adapter.services.mail_service import MailService +from test.support.test_helper import mail_dict from mockito import * @@ -29,14 +31,15 @@ class TestMailService(unittest.TestCase): self.mailboxes.sent = lambda: mock() self.mail_sender = mock() - self.mail_service = MailService(self.mailboxes, self.mail_sender, self.tag_service, self.querier) + self.search_engine = mock() + self.mail_service = MailService(self.mailboxes, self.mail_sender, self.tag_service, self.querier, self.search_engine) def test_send_mail(self): - mail = "mail" + when(InputMail).from_dict(any()).thenReturn('inputmail') - self.mail_service.send(mail) + self.mail_service.send_mail(mail_dict()) - verify(self.mail_sender).sendmail(mail) + verify(self.mail_sender).sendmail("inputmail") def test_mark_as_read(self): mail = mock() diff --git a/service/test/unit/adapter/test_mailbox.py b/service/test/unit/adapter/test_mailbox.py index 9725f418..b44f507b 100644 --- a/service/test/unit/adapter/test_mailbox.py +++ b/service/test/unit/adapter/test_mailbox.py @@ -25,7 +25,8 @@ class PixelatedMailboxTest(unittest.TestCase): def setUp(self): self.tag_service = mock() self.querier = mock() - self.mailbox = Mailbox('INBOX', self.querier) + self.search_engine = mock() + self.mailbox = Mailbox('INBOX', self.querier, self.search_engine) def test_remove_message_from_mailbox(self): mail = PixelatedMail.from_soledad(*test_helper.leap_mail(), soledad_querier=self.querier) |