From 7fa4661018272edaf11109efdb9ff834d365f3ab Mon Sep 17 00:00:00 2001 From: Duda Dornelles Date: Mon, 29 Sep 2014 18:00:52 -0300 Subject: Testing drafts in integration, fixing a bug where the sent wasnt leaving the drafts mailbox --- service/integration/__init__.py | 50 +++++++++++++++++++++++++------- service/integration/drafts_test.py | 53 ++++++++++++++++++++++++++++++++++ service/integration/mail_fetch_test.py | 25 ---------------- 3 files changed, 92 insertions(+), 36 deletions(-) create mode 100644 service/integration/drafts_test.py delete mode 100644 service/integration/mail_fetch_test.py (limited to 'service/integration') diff --git a/service/integration/__init__.py b/service/integration/__init__.py index 717fa3e9..aff5cec0 100644 --- a/service/integration/__init__.py +++ b/service/integration/__init__.py @@ -21,6 +21,8 @@ import os from mock import Mock import shutil from pixelated.adapter.mail_service import MailService +from pixelated.adapter.tag_index import TagIndex +from pixelated.adapter.tag_service import TagService import pixelated.user_agent from pixelated.adapter.pixelated_mail import PixelatedMail from pixelated.adapter.pixelated_mailboxes import PixelatedMailBoxes @@ -89,34 +91,60 @@ class JSONMailBuilder: self.mail['header']['subject'] = subject return self + def with_ident(self, ident): + self.mail['ident'] = ident + return self + def build(self): return json.dumps(self.mail) class SoledadTestBase: - def __init__(self): - pass - def teardown_soledad(self): self.soledad.close() shutil.rmtree(soledad_test_folder) def setup_soledad(self): - self.app = pixelated.user_agent.app.test_client() - self.account = FakeAccount() - self.mail_sender = mock() - self.mail_address = "test@pixelated.org" self.soledad = initialize_soledad(tempdir=soledad_test_folder) + self.mail_address = "test@pixelated.org" SoledadQuerier.instance = None SoledadQuerier.get_instance(soledad=self.soledad) PixelatedMail.from_email_address = self.mail_address - pixelated_mailboxes = PixelatedMailBoxes(self.account) - pixelated.user_agent.mail_service = MailService(pixelated_mailboxes, self.mail_sender) + + self.app = pixelated.user_agent.app.test_client() + self.account = FakeAccount() + self.mail_sender = mock() + self.tag_index = TagIndex(os.path.join(soledad_test_folder, 'tag_index')) + self.tag_service = TagService(self.tag_index) + self.pixelated_mailboxes = PixelatedMailBoxes(self.account) + self.mail_service = MailService(self.pixelated_mailboxes, self.mail_sender, self.tag_service) + + pixelated.user_agent.mail_service = self.mail_service def get_mails_by_tag(self, tag): - return json.loads(self.app.get("/mails?q=tag" + tag).data) + response = json.loads(self.app.get("/mails?q=tag:" + tag).data) + return [ResponseMail(m) for m in response['mails']] def post_mail(self, data): - self.app.post('/mails', data=data, content_type="application/json") \ No newline at end of file + response = json.loads(self.app.post('/mails', data=data, content_type="application/json").data) + return ResponseMail(response) + + +class ResponseMail: + + def __init__(self, mail_dict): + self.mail_dict = mail_dict + + @property + def subject(self): + return self.headers['subject'] + + @property + def headers(self): + return self.mail_dict['header'] + + @property + def ident(self): + return self.mail_dict['ident'] \ No newline at end of file diff --git a/service/integration/drafts_test.py b/service/integration/drafts_test.py new file mode 100644 index 00000000..1f1712a8 --- /dev/null +++ b/service/integration/drafts_test.py @@ -0,0 +1,53 @@ +# +# 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 integration import JSONMailBuilder, SoledadTestBase + + +class DraftsTest(unittest.TestCase, SoledadTestBase): + + def setUp(self): + self.setup_soledad() + + def tearDown(self): + self.teardown_soledad() + + def test_post_creates_a_draft_when_data_has_no_ident(self): + mail = JSONMailBuilder().with_subject('A new draft').build() + + self.post_mail(mail) + mails = self.get_mails_by_tag('drafts') + + self.assertEquals('A new draft', mails[0].subject) + + def test_post_sends_mail_and_deletes_previous_draft_when_data_has_ident(self): + first_draft = JSONMailBuilder().with_subject('First draft').build() + first_draft_response = self.post_mail(first_draft) + first_draft_ident = first_draft_response.ident + + second_draft = JSONMailBuilder().with_subject('Second draft').with_ident(first_draft_ident).build() + self.post_mail(second_draft) + + sent_mails = self.get_mails_by_tag('sent') + drafts = self.get_mails_by_tag('drafts') + + import pdb;pdb.set_trace() + + self.assertEquals(1, len(sent_mails)) + self.assertEquals('Second draft', sent_mails[0].subject) + self.assertEquals(0, len(drafts)) + + diff --git a/service/integration/mail_fetch_test.py b/service/integration/mail_fetch_test.py deleted file mode 100644 index 882e9d6f..00000000 --- a/service/integration/mail_fetch_test.py +++ /dev/null @@ -1,25 +0,0 @@ -import unittest - -from integration import JSONMailBuilder, SoledadTestBase - - -class MailFetchTest(unittest.TestCase, SoledadTestBase): - - def setUp(self): - self.setup_soledad() - - def tearDown(self): - self.teardown_soledad() - - def test_get_mails(self): - mail_one = JSONMailBuilder().with_subject("Mail One").build() - mail_two = JSONMailBuilder().with_subject("Mail Two").build() - - self.post_mail(mail_one) - self.post_mail(mail_two) - - response = self.get_mails_by_tag("drafts") - - # ordered by creation date - self.assertEquals(u'Mail Two', response['mails'][0]['header']['subject']) - self.assertEquals(u'Mail One', response['mails'][1]['header']['subject']) -- cgit v1.2.3