From a7e4c6238e29962653d2c53ad2887eab6e98b420 Mon Sep 17 00:00:00 2001 From: Duda Dornelles Date: Wed, 19 Nov 2014 12:06:05 -0200 Subject: better organization for integration test support classes --- service/test/functional/features/environment.py | 15 +- .../test/functional/features/steps/data_setup.py | 4 +- service/test/integration/delete_mail_test.py | 7 +- service/test/integration/drafts_test.py | 3 +- .../test/integration/mark_as_read_unread_test.py | 19 +- .../test/integration/retrieve_attachment_test.py | 5 +- service/test/integration/search_test.py | 30 +-- service/test/integration/soledad_querier_test.py | 10 +- service/test/integration/tags_test.py | 9 +- service/test/support/integration/__init__.py | 19 ++ .../test/support/integration/app_test_client.py | 156 +++++++++++ service/test/support/integration/model.py | 88 +++++++ .../test/support/integration/soledad_test_base.py | 83 ++++++ service/test/support/integration_helper.py | 290 --------------------- 14 files changed, 395 insertions(+), 343 deletions(-) create mode 100644 service/test/support/integration/__init__.py create mode 100644 service/test/support/integration/app_test_client.py create mode 100644 service/test/support/integration/model.py create mode 100644 service/test/support/integration/soledad_test_base.py delete mode 100644 service/test/support/integration_helper.py (limited to 'service') diff --git a/service/test/functional/features/environment.py b/service/test/functional/features/environment.py index 5d481e25..537cd969 100644 --- a/service/test/functional/features/environment.py +++ b/service/test/functional/features/environment.py @@ -15,20 +15,19 @@ # along with Pixelated. If not, see . import time import multiprocessing - -from selenium import webdriver import logging -from test.support.integration_helper import setup_test_app - -logging.disable('INFO') -import pixelated.controllers.features_controller +from test.support.integration import AppTestClient +from selenium import webdriver +import pixelated def before_all(context): pixelated.controllers.features_controller.FeaturesController.DISABLED_FEATURES.append('autoRefresh') - setup_test_app(context) + client = AppTestClient() + context.client = client + logging.disable('INFO') - worker = lambda: context.app.run(host='localhost', port=4567, logFile=open('/tmp/behave-tests.log', 'w')) + worker = lambda: client.app.run(host='localhost', port=4567, logFile=open('/tmp/behave-tests.log', 'w')) context._process = multiprocessing.Process(target=worker) context._process.start() diff --git a/service/test/functional/features/steps/data_setup.py b/service/test/functional/features/steps/data_setup.py index e3e9a389..4e349f05 100644 --- a/service/test/functional/features/steps/data_setup.py +++ b/service/test/functional/features/steps/data_setup.py @@ -13,10 +13,10 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . -from test.support.integration_helper import MailBuilder +from test.support.integration import MailBuilder @given('I have a mail in my inbox') def add_mail_impl(context): input_mail = MailBuilder().build_input_mail() - context.add_mail_to_inbox(input_mail) + context.client.add_mail_to_inbox(input_mail) diff --git a/service/test/integration/delete_mail_test.py b/service/test/integration/delete_mail_test.py index 69ab1f03..15232b0d 100644 --- a/service/test/integration/delete_mail_test.py +++ b/service/test/integration/delete_mail_test.py @@ -13,9 +13,8 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . -import unittest -from test.support.integration_helper import MailBuilder, SoledadTestBase +from test.support.integration import * class DeleteMailTest(SoledadTestBase): @@ -28,7 +27,7 @@ class DeleteMailTest(SoledadTestBase): def test_move_mail_to_trash_when_deleting(self): input_mail = MailBuilder().with_subject('Mail with tags').build_input_mail() - self.add_mail_to_inbox(input_mail) + self.client.add_mail_to_inbox(input_mail) inbox_mails = self.get_mails_by_tag('inbox') self.assertEquals(1, len(inbox_mails)) @@ -41,7 +40,7 @@ class DeleteMailTest(SoledadTestBase): self.assertEquals(1, len(trash_mails)) def test_delete_mail_when_trashing_mail_from_trash_mailbox(self): - mails = self.add_multiple_to_mailbox(1, 'trash') + mails = self.client.add_multiple_to_mailbox(1, 'trash') self.delete_mail(mails[0].ident) trash_mails = self.get_mails_by_tag('trash') diff --git a/service/test/integration/drafts_test.py b/service/test/integration/drafts_test.py index 41fda0df..2ba14dfd 100644 --- a/service/test/integration/drafts_test.py +++ b/service/test/integration/drafts_test.py @@ -13,9 +13,8 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . -import unittest -from test.support.integration_helper import MailBuilder, SoledadTestBase +from test.support.integration import * class DraftsTest(SoledadTestBase): diff --git a/service/test/integration/mark_as_read_unread_test.py b/service/test/integration/mark_as_read_unread_test.py index c04cdbc6..55467e9e 100644 --- a/service/test/integration/mark_as_read_unread_test.py +++ b/service/test/integration/mark_as_read_unread_test.py @@ -13,9 +13,8 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . -import unittest -from test.support.integration_helper import MailBuilder, SoledadTestBase +from test.support.integration import * from pixelated.adapter.status import Status @@ -29,7 +28,7 @@ class MarkAsReadUnreadTest(SoledadTestBase): def test_mark_single_as_read(self): input_mail = MailBuilder().build_input_mail() - self.add_mail_to_inbox(input_mail) + self.client.add_mail_to_inbox(input_mail) mails = self.get_mails_by_tag('inbox') self.assertNotIn('read', mails[0].status) @@ -41,7 +40,7 @@ class MarkAsReadUnreadTest(SoledadTestBase): def test_mark_single_as_unread(self): input_mail = MailBuilder().with_status([Status.SEEN]).build_input_mail() - self.add_mail_to_inbox(input_mail) + self.client.add_mail_to_inbox(input_mail) self.mark_as_unread(input_mail.ident) mail = self.get_mails_by_tag('inbox')[0] @@ -52,8 +51,8 @@ class MarkAsReadUnreadTest(SoledadTestBase): input_mail = MailBuilder().with_status([Status.SEEN]).build_input_mail() input_mail2 = MailBuilder().with_status([Status.SEEN]).build_input_mail() - self.add_mail_to_inbox(input_mail) - self.add_mail_to_inbox(input_mail2) + self.client.add_mail_to_inbox(input_mail) + self.client.add_mail_to_inbox(input_mail2) self.mark_many_as_unread([input_mail.ident, input_mail2.ident]) @@ -66,8 +65,8 @@ class MarkAsReadUnreadTest(SoledadTestBase): input_mail = MailBuilder().build_input_mail() input_mail2 = MailBuilder().build_input_mail() - self.add_mail_to_inbox(input_mail) - self.add_mail_to_inbox(input_mail2) + self.client.add_mail_to_inbox(input_mail) + self.client.add_mail_to_inbox(input_mail2) mails = self.get_mails_by_tag('inbox') @@ -86,8 +85,8 @@ class MarkAsReadUnreadTest(SoledadTestBase): input_mail = MailBuilder().build_input_mail() input_mail2 = MailBuilder().with_status([Status.SEEN]).build_input_mail() - self.add_mail_to_inbox(input_mail) - self.add_mail_to_inbox(input_mail2) + self.client.add_mail_to_inbox(input_mail) + self.client.add_mail_to_inbox(input_mail2) mails = self.get_mails_by_tag('inbox') diff --git a/service/test/integration/retrieve_attachment_test.py b/service/test/integration/retrieve_attachment_test.py index 4e0e76e0..5f754a27 100644 --- a/service/test/integration/retrieve_attachment_test.py +++ b/service/test/integration/retrieve_attachment_test.py @@ -13,9 +13,8 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . -import unittest -from test.support.integration_helper import SoledadTestBase +from test.support.integration.soledad_test_base import SoledadTestBase class RetrieveAttachmentTest(SoledadTestBase): @@ -35,7 +34,7 @@ class RetrieveAttachmentTest(SoledadTestBase): 'phash': ident, 'content-type': 'text/plain; charset=US-ASCII; name="attachment_pequeno.txt"'} - self.add_document_to_soledad(attachment_dict) + self.client.add_document_to_soledad(attachment_dict) attachment = self.get_attachment(ident, 'base64') diff --git a/service/test/integration/search_test.py b/service/test/integration/search_test.py index 7415b9a1..21326ec7 100644 --- a/service/test/integration/search_test.py +++ b/service/test/integration/search_test.py @@ -15,7 +15,7 @@ # along with Pixelated. If not, see . from nose.twistedtools import deferred -from test.support.integration_helper import MailBuilder, SoledadTestBase +from test.support.integration import * class SearchTest(SoledadTestBase): @@ -26,7 +26,7 @@ class SearchTest(SoledadTestBase): @deferred(timeout=SoledadTestBase.DEFERRED_TIMEOUT) def test_that_tags_returns_all_tags(self): input_mail = MailBuilder().with_tags(['important']).build_input_mail() - self.add_mail_to_inbox(input_mail) + self.client.add_mail_to_inbox(input_mail) d = self.get_tags() @@ -43,7 +43,7 @@ class SearchTest(SoledadTestBase): @deferred(timeout=SoledadTestBase.DEFERRED_TIMEOUT) def test_that_tags_are_filtered_by_query(self): input_mail = MailBuilder().with_tags(['ateu', 'catoa', 'luat', 'zuado']).build_input_mail() - self.add_mail_to_inbox(input_mail) + self.client.add_mail_to_inbox(input_mail) d = self.get_tags(q=["at"], skipDefaultTags=["true"]) @@ -60,7 +60,7 @@ class SearchTest(SoledadTestBase): @deferred(timeout=SoledadTestBase.DEFERRED_TIMEOUT) def test_that_default_tags_are_ignorable(self): input_mail = MailBuilder().with_tags(['sometag']).build_input_mail() - self.add_mail_to_inbox(input_mail) + self.client.add_mail_to_inbox(input_mail) d = self.get_tags(skipDefaultTags=["true"]) @@ -73,10 +73,10 @@ class SearchTest(SoledadTestBase): @deferred(timeout=SoledadTestBase.DEFERRED_TIMEOUT_LONG) def test_tags_count(self): - self.add_multiple_to_mailbox(num=10, mailbox='inbox', flags=['\\Recent']) - self.add_multiple_to_mailbox(num=5, mailbox='inbox', flags=['\\Seen']) - self.add_multiple_to_mailbox(num=3, mailbox='inbox', flags=['\\Recent'], tags=['important', 'later']) - self.add_multiple_to_mailbox(num=1, mailbox='inbox', flags=['\\Seen'], tags=['important']) + self.client.add_multiple_to_mailbox(num=10, mailbox='inbox', flags=['\\Recent']) + self.client.add_multiple_to_mailbox(num=5, mailbox='inbox', flags=['\\Seen']) + self.client.add_multiple_to_mailbox(num=3, mailbox='inbox', flags=['\\Recent'], tags=['important', 'later']) + self.client.add_multiple_to_mailbox(num=1, mailbox='inbox', flags=['\\Seen'], tags=['important']) d = self.get_tags() @@ -91,8 +91,8 @@ class SearchTest(SoledadTestBase): def test_search_mails_different_window(self): input_mail = MailBuilder().build_input_mail() input_mail2 = MailBuilder().build_input_mail() - self.add_mail_to_inbox(input_mail) - self.add_mail_to_inbox(input_mail2) + self.client.add_mail_to_inbox(input_mail) + self.client.add_mail_to_inbox(input_mail2) first_page = self.get_mails_by_tag('inbox', page=1, window=1) @@ -101,8 +101,8 @@ class SearchTest(SoledadTestBase): def test_search_mails_with_multiple_pages(self): input_mail = MailBuilder().build_input_mail() input_mail2 = MailBuilder().build_input_mail() - self.add_mail_to_inbox(input_mail) - self.add_mail_to_inbox(input_mail2) + self.client.add_mail_to_inbox(input_mail) + self.client.add_mail_to_inbox(input_mail2) first_page = self.get_mails_by_tag('inbox', page=1, window=1) second_page = self.get_mails_by_tag('inbox', page=2, window=1) @@ -114,7 +114,7 @@ class SearchTest(SoledadTestBase): def test_page_zero_fetches_first_page(self): input_mail = MailBuilder().build_input_mail() - self.add_mail_to_inbox(input_mail) + self.client.add_mail_to_inbox(input_mail) page = self.get_mails_by_tag('inbox', page=0, window=1) self.assertEqual(page[0].ident, input_mail.ident) @@ -127,8 +127,8 @@ class SearchTest(SoledadTestBase): input_mail = MailBuilder().with_date('2014-10-15T15:15').build_input_mail() input_mail2 = MailBuilder().with_date('2014-10-15T15:16').build_input_mail() - self.add_mail_to_inbox(input_mail) - self.add_mail_to_inbox(input_mail2) + self.client.add_mail_to_inbox(input_mail) + self.client.add_mail_to_inbox(input_mail2) results = self.get_mails_by_tag('inbox') self.assertEqual(results[0].ident, input_mail2.ident) diff --git a/service/test/integration/soledad_querier_test.py b/service/test/integration/soledad_querier_test.py index e8be431b..4a99a620 100644 --- a/service/test/integration/soledad_querier_test.py +++ b/service/test/integration/soledad_querier_test.py @@ -15,9 +15,9 @@ # along with Pixelated. If not, see . import copy -import unittest import time -from test.support.integration_helper import SoledadTestBase, MailBuilder + +from test.support.integration import * from leap.mail.imap.fields import WithMsgFields @@ -25,6 +25,8 @@ class SoledadQuerierTest(SoledadTestBase, WithMsgFields): def setUp(self): SoledadTestBase.setUp(self) + self.soledad = self.client.soledad + self.soledad_querier = self.client.soledad_querier def tearDown(self): SoledadTestBase.tearDown(self) @@ -42,7 +44,7 @@ class SoledadQuerierTest(SoledadTestBase, WithMsgFields): return [m for m in self.soledad.get_from_index('by-type', 'mbox') if m.content['mbox'] == mailbox_name] def test_remove_dup_mailboxes_keeps_the_one_with_the_highest_last_uid(self): - self.add_multiple_to_mailbox(3, 'INBOX') # by now we already have one inbox with 3 mails + self.client.add_multiple_to_mailbox(3, 'INBOX') # by now we already have one inbox with 3 mails self._create_mailbox('INBOX') # now we have a duplicate # make sure we have two @@ -77,7 +79,7 @@ class SoledadQuerierTest(SoledadTestBase, WithMsgFields): self.assertEqual(1, len(mails)) def test_get_mails_by_chash(self): - mails = self.add_multiple_to_mailbox(3, 'INBOX') + mails = self.client.add_multiple_to_mailbox(3, 'INBOX') chashes = [mail.ident for mail in mails] fetched_mails = self.soledad_querier.mails(chashes) diff --git a/service/test/integration/tags_test.py b/service/test/integration/tags_test.py index bb3bdbdb..03d6f4b3 100644 --- a/service/test/integration/tags_test.py +++ b/service/test/integration/tags_test.py @@ -14,9 +14,8 @@ # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . import json -import unittest -from test.support.integration_helper import MailBuilder, SoledadTestBase +from test.support.integration import * class TagsTest(SoledadTestBase): @@ -32,7 +31,7 @@ class TagsTest(SoledadTestBase): def test_add_tag_to_an_inbox_mail_and_query(self): mail = MailBuilder().with_subject('Mail with tags').build_input_mail() - self.add_mail_to_inbox(mail) + self.client.add_mail_to_inbox(mail) self.post_tags(mail.ident, self._tags_json(['IMPORTANT'])) @@ -44,10 +43,10 @@ class TagsTest(SoledadTestBase): def test_addition_of_reserved_tags_is_not_allowed(self): mail = MailBuilder().with_subject('Mail with tags').build_input_mail() - self.add_mail_to_inbox(mail) + self.client.add_mail_to_inbox(mail) response = self.post_tags(mail.ident, self._tags_json(['DRAFTS'])) self.assertEquals("None of the following words can be used as tags: drafts", response) - mail = self.mailboxes.inbox().mail(mail.ident) + mail = self.client.mailboxes.inbox().mail(mail.ident) self.assertNotIn('drafts', mail.tags) diff --git a/service/test/support/integration/__init__.py b/service/test/support/integration/__init__.py new file mode 100644 index 00000000..bb2f0263 --- /dev/null +++ b/service/test/support/integration/__init__.py @@ -0,0 +1,19 @@ +# +# 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 . +from .app_test_client import AppTestClient +from .model import MailBuilder, ResponseMail +from .soledad_test_base import SoledadTestBase + diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py new file mode 100644 index 00000000..f6a95422 --- /dev/null +++ b/service/test/support/integration/app_test_client.py @@ -0,0 +1,156 @@ +# +# 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 json +import shutil + +from klein.test_resource import requestMock, _render +from leap.mail.imap.account import SoledadBackedAccount +from leap.soledad.client import Soledad +from mock import MagicMock, Mock +import os +from pixelated.adapter.draft_service import DraftService +from pixelated.adapter.mail_service import MailService +from pixelated.adapter.mailboxes import Mailboxes +from pixelated.adapter.soledad_querier import SoledadQuerier +from pixelated.adapter.tag_service import TagService +from pixelated.config import app_factory +from pixelated.controllers import FeaturesController, HomeController, MailsController, TagsController, \ + SyncInfoController, AttachmentsController +import pixelated.runserver +from pixelated.adapter.mail import PixelatedMail +from pixelated.adapter.search import SearchEngine +from test.support.integration.model import MailBuilder + + +class AppTestClient: + def __init__(self, soledad_test_folder='soledad-test'): + + self.soledad = initialize_soledad(tempdir=soledad_test_folder) + self.mail_address = "test@pixelated.org" + + # setup app + PixelatedMail.from_email_address = self.mail_address + + SearchEngine.INDEX_FOLDER = soledad_test_folder + '/search_index' + + self.app = pixelated.runserver.app + + self.soledad_querier = SoledadQuerier(self.soledad) + self.soledad_querier.get_index_masterkey = lambda: '_yg2oG_5ELM8_-sQYcsxI37WesI0dOtZQXpwAqjvhR4=' + + self.account = SoledadBackedAccount('test', self.soledad, MagicMock()) + self.mailboxes = Mailboxes(self.account, self.soledad_querier) + self.mail_sender = Mock() + self.tag_service = TagService() + self.draft_service = DraftService(self.mailboxes) + self.mail_service = MailService(self.mailboxes, self.mail_sender, self.tag_service, + self.soledad_querier) + self.search_engine = SearchEngine(self.soledad_querier) + self.search_engine.index_mails(self.mail_service.all_mails()) + + features_controller = FeaturesController() + features_controller.DISABLED_FEATURES.append('autoReload') + home_controller = HomeController() + mails_controller = MailsController(mail_service=self.mail_service, + draft_service=self.draft_service, + search_engine=self.search_engine) + tags_controller = TagsController(search_engine=self.search_engine) + sync_info_controller = SyncInfoController() + attachments_controller = AttachmentsController(self.soledad_querier) + + app_factory._setup_routes(self.app, home_controller, mails_controller, tags_controller, + features_controller, sync_info_controller, attachments_controller) + + def _render(self, request, as_json=True): + def get_request_written_data(_=None): + written_data = request.getWrittenData() + if written_data: + return json.loads(written_data) if as_json else written_data + + d = _render(self.app.resource(), request) + if request.finished: + return get_request_written_data(), request + else: + d.addCallback(get_request_written_data) + return d, request + + def get(self, path, get_args, as_json=True): + request = requestMock(path) + request.args = get_args + return self._render(request, as_json) + + def post(self, path, body=''): + request = requestMock(path=path, method="POST", body=body, headers={'Content-Type': ['application/json']}) + return self._render(request) + + def put(self, path, body): + request = requestMock(path=path, method="PUT", body=body, headers={'Content-Type': ['application/json']}) + return self._render(request) + + def delete(self, path): + request = requestMock(path=path, method="DELETE") + return self._render(request) + + def add_document_to_soledad(self, _dict): + self.soledad_querier.soledad.create_doc(_dict) + + def add_mail_to_inbox(self, input_mail): + mail = self.mailboxes.inbox().add(input_mail) + mail.update_tags(input_mail.tags) + self.search_engine.index_mail(mail) + + def add_multiple_to_mailbox(self, num, mailbox='', flags=[], tags=[]): + mails = [] + for _ in range(num): + input_mail = MailBuilder().with_status(flags).with_tags(tags).build_input_mail() + mail = self.mailboxes._create_or_get(mailbox).add(input_mail) + mails.append(mail) + mail.update_tags(input_mail.tags) + self.search_engine.index_mail(mail) + return mails + + +def initialize_soledad(tempdir): + if os.path.isdir(tempdir): + shutil.rmtree(tempdir) + + uuid = "foobar-uuid" + passphrase = u"verysecretpassphrase" + secret_path = os.path.join(tempdir, "secret.gpg") + local_db_path = os.path.join(tempdir, "soledad.u1db") + server_url = "http://provider" + cert_file = "" + + class MockSharedDB(object): + get_doc = Mock(return_value=None) + put_doc = Mock() + lock = Mock(return_value=('atoken', 300)) + unlock = Mock(return_value=True) + close = Mock() + + def __call__(self): + return self + + Soledad._shared_db = MockSharedDB() + + _soledad = Soledad( + uuid, + passphrase, + secret_path, + local_db_path, + server_url, + cert_file) + return _soledad diff --git a/service/test/support/integration/model.py b/service/test/support/integration/model.py new file mode 100644 index 00000000..46da77fd --- /dev/null +++ b/service/test/support/integration/model.py @@ -0,0 +1,88 @@ +# +# 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 json +from pixelated.adapter.mail import InputMail +from pixelated.adapter.status import Status + + +class MailBuilder: + def __init__(self): + self.mail = { + 'header': { + 'to': ['recipient@to.com'], + 'cc': ['recipient@cc.com'], + 'bcc': ['recipient@bcc.com'], + 'subject': 'Hi! This the subject' + }, + 'body': "Hello,\nThis is the body of this message\n\nRegards,\n\n--\nPixelated.\n", + 'status': [] + } + + def with_body(self, body): + self.mail['body'] = body + return self + + def with_tags(self, tags): + self.mail['tags'] = tags + return self + + def with_subject(self, subject): + self.mail['header']['subject'] = subject + return self + + def with_status(self, flags): + for status in Status.from_flags(flags): + self.mail['status'].append(status) + return self + + def with_date(self, date_string): + self.mail['header']['date'] = date_string + return self + + def with_ident(self, ident): + self.mail['ident'] = ident + return self + + def build_json(self): + return json.dumps(self.mail) + + def build_input_mail(self): + return InputMail.from_dict(self.mail) + + +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'] + + @property + def tags(self): + return self.mail_dict['tags'] + + @property + def status(self): + return self.mail_dict['status'] \ No newline at end of file diff --git a/service/test/support/integration/soledad_test_base.py b/service/test/support/integration/soledad_test_base.py new file mode 100644 index 00000000..2221679f --- /dev/null +++ b/service/test/support/integration/soledad_test_base.py @@ -0,0 +1,83 @@ +# +# 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 pixelated.controllers import * +from test.support.integration.app_test_client import AppTestClient +from test.support.integration.model import ResponseMail + + +class SoledadTestBase(unittest.TestCase): + # these are so long because our CI is so slow at the moment. + DEFERRED_TIMEOUT = 120 + DEFERRED_TIMEOUT_LONG = 300 + + @classmethod + def setUpClass(cls): + from nose.twistedtools import threaded_reactor + + threaded_reactor() + + def setUp(self): + self.client = AppTestClient() + + def get_mails_by_tag(self, tag, page=1, window=100): + res, req = self.client.get("/mails", { + 'q': ['tag:%s' % tag], + 'w': [str(window)], + 'p': [str(page)] + }) + return [ResponseMail(m) for m in res['mails']] + + def post_mail(self, data): + res, req = self.client.post('/mails', data) + return ResponseMail(res) + + def get_attachment(self, ident, encoding): + res, req = self.client.get("/attachment/%s" % ident, {'encoding': [encoding]}, as_json=False) + return res + + def put_mail(self, data): + res, req = self.client.put('/mails', data) + return res['ident'] + + def post_tags(self, mail_ident, tags_json): + res, req = self.client.post("/mail/%s/tags" % mail_ident, tags_json) + return res + + def get_tags(self, **kwargs): + res, req = self.client.get('/tags', kwargs) + return res + + def delete_mail(self, mail_ident): + res, req = self.client.delete("/mail/%s" % mail_ident) + return req + + def mark_as_read(self, mail_ident): + res, req = self.client.post("/mail/%s/read" % mail_ident) + return req + + def mark_as_unread(self, mail_ident): + res, req = self.client.post("/mail/%s/unread" % mail_ident) + return req + + def mark_many_as_unread(self, idents): + res, req = self.client.post('/mails/unread', json.dumps({'idents': idents})) + return req + + def mark_many_as_read(self, idents): + res, req = self.client.post('/mails/read', json.dumps({'idents': idents})) + return req diff --git a/service/test/support/integration_helper.py b/service/test/support/integration_helper.py deleted file mode 100644 index d4f2a308..00000000 --- a/service/test/support/integration_helper.py +++ /dev/null @@ -1,290 +0,0 @@ -# -# 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 shutil - - -from klein.resource import KleinResource -from leap.soledad.client import Soledad -from mockito import mock -import os -from mock import Mock, MagicMock -from pixelated.adapter.mail_service import MailService -from pixelated.adapter.search import SearchEngine -from pixelated.adapter.status import Status -from pixelated.adapter.tag_service import TagService -from pixelated.adapter.draft_service import DraftService -from pixelated.adapter.mail import PixelatedMail, InputMail -import pixelated.runserver -from pixelated.adapter.mailboxes import Mailboxes -from pixelated.adapter.soledad_querier import SoledadQuerier -from pixelated.controllers import * -import pixelated.config.app_factory as app_factory -from leap.mail.imap.account import SoledadBackedAccount -from klein.test_resource import requestMock, _render -import unittest - -soledad_test_folder = "soledad-test" - - -def initialize_soledad(tempdir): - if os.path.isdir(soledad_test_folder): - shutil.rmtree(soledad_test_folder) - - uuid = "foobar-uuid" - passphrase = u"verysecretpassphrase" - secret_path = os.path.join(tempdir, "secret.gpg") - local_db_path = os.path.join(tempdir, "soledad.u1db") - server_url = "http://provider" - cert_file = "" - - class MockSharedDB(object): - get_doc = Mock(return_value=None) - put_doc = Mock() - lock = Mock(return_value=('atoken', 300)) - unlock = Mock(return_value=True) - close = Mock() - - def __call__(self): - return self - - Soledad._shared_db = MockSharedDB() - - _soledad = Soledad( - uuid, - passphrase, - secret_path, - local_db_path, - server_url, - cert_file) - return _soledad - - -class MailBuilder: - def __init__(self): - self.mail = { - 'header': { - 'to': ['recipient@to.com'], - 'cc': ['recipient@cc.com'], - 'bcc': ['recipient@bcc.com'], - 'subject': 'Hi! This the subject' - }, - 'body': "Hello,\nThis is the body of this message\n\nRegards,\n\n--\nPixelated.\n", - 'status': [] - } - - def with_body(self, body): - self.mail['body'] = body - return self - - def with_tags(self, tags): - self.mail['tags'] = tags - return self - - def with_subject(self, subject): - self.mail['header']['subject'] = subject - return self - - def with_status(self, flags): - for status in Status.from_flags(flags): - self.mail['status'].append(status) - return self - - def with_date(self, date_string): - self.mail['header']['date'] = date_string - return self - - def with_ident(self, ident): - self.mail['ident'] = ident - return self - - def build_json(self): - return json.dumps(self.mail) - - def build_input_mail(self): - return InputMail.from_dict(self.mail) - - -def add_document_to_soledad(self, _dict): - self.soledad_querier.soledad.create_doc(_dict) - - -def add_mail_to_inbox(self, input_mail): - mail = self.mailboxes.inbox().add(input_mail) - mail.update_tags(input_mail.tags) - self.search_engine.index_mail(mail) - - -def add_multiple_to_mailbox(self, num, mailbox='', flags=[], tags=[]): - mails = [] - for _ in range(num): - input_mail = MailBuilder().with_status(flags).with_tags(tags).build_input_mail() - mail = self.mailboxes._create_or_get(mailbox).add(input_mail) - mails.append(mail) - mail.update_tags(input_mail.tags) - self.search_engine.index_mail(mail) - return mails - - -def setup_test_app(self): - from functools import partial - self.add_document_to_soledad = partial(add_document_to_soledad, self) - self.add_mail_to_inbox = partial(add_mail_to_inbox, self) - self.add_multiple_to_mailbox = partial(add_multiple_to_mailbox, self) - - self.soledad = initialize_soledad(tempdir=soledad_test_folder) - self.mail_address = "test@pixelated.org" - - # setup app - PixelatedMail.from_email_address = self.mail_address - - SearchEngine.INDEX_FOLDER = soledad_test_folder + '/search_index' - - self.app = pixelated.runserver.app - - self.soledad_querier = SoledadQuerier(self.soledad) - self.soledad_querier.get_index_masterkey = lambda: '_yg2oG_5ELM8_-sQYcsxI37WesI0dOtZQXpwAqjvhR4=' - - self.account = SoledadBackedAccount('test', self.soledad, MagicMock()) - self.mailboxes = Mailboxes(self.account, self.soledad_querier) - self.mail_sender = mock() - self.tag_service = TagService() - self.draft_service = DraftService(self.mailboxes) - self.mail_service = MailService(self.mailboxes, self.mail_sender, self.tag_service, - self.soledad_querier) - self.search_engine = SearchEngine(self.soledad_querier) - self.search_engine.index_mails(self.mail_service.all_mails()) - - features_controller = FeaturesController() - features_controller.DISABLED_FEATURES.append('autoReload') - home_controller = HomeController() - mails_controller = MailsController(mail_service=self.mail_service, - draft_service=self.draft_service, - search_engine=self.search_engine) - tags_controller = TagsController(search_engine=self.search_engine) - sync_info_controller = SyncInfoController() - attachments_controller = AttachmentsController(self.soledad_querier) - - app_factory._setup_routes(self.app, home_controller, mails_controller, tags_controller, - features_controller, sync_info_controller, attachments_controller) - self.resource = KleinResource(self.app) - - -class SoledadTestBase(unittest.TestCase): - # these are so long because our CI is so slow at the moment. - DEFERRED_TIMEOUT = 120 - DEFERRED_TIMEOUT_LONG = 300 - - @classmethod - def setUpClass(cls): - from nose.twistedtools import threaded_reactor - threaded_reactor() - - def setUp(self): - setup_test_app(self) - - def get_mails_by_tag(self, tag, page=1, window=100): - request = requestMock(path="/mails") - request.args = { - 'q': ['tag:%s' % tag], - 'w': [str(window)], - 'p': [str(page)] - } - _render(self.resource, request) - response = json.loads(request.getWrittenData()) - return [ResponseMail(m) for m in response['mails']] - - def post_mail(self, data): - request = requestMock(path='/mails', method="POST", body=data, headers={'Content-Type': ['application/json']}) - _render(self.resource, request) - response = json.loads(request.getWrittenData()) - return ResponseMail(response) - - def get_attachment(self, ident, encoding): - request = requestMock(path='/attachment/' + ident) - request.args = { - 'encoding': [encoding] - } - _render(self.resource, request) - return request.getWrittenData() - - def put_mail(self, data): - request = requestMock('/mails', method="PUT", body=data, headers={'Content-Type': ['application/json']}) - _render(self.resource, request) - response = json.loads(request.getWrittenData()) - return response['ident'] - - def post_tags(self, mail_ident, tags_json): - request = requestMock('/mail/' + mail_ident + '/tags', method="POST", body=tags_json, - headers={'Content-Type': ['application/json']}) - _render(self.resource, request) - return json.loads(request.getWrittenData()) - - def get_tags(self, **kwargs): - request = requestMock('/tags') - request.args = kwargs - d = _render(self.resource, request) - d.addCallback(lambda _: json.loads(request.getWrittenData())) - return d - - def delete_mail(self, mail_ident): - request = requestMock(path='/mail/' + mail_ident, method="DELETE") - _render(self.resource, request) - return request - - def mark_as_read(self, mail_ident): - request = requestMock('/mail/' + mail_ident + '/read', method="POST", headers={'Content-Type': ['application/json']}) - _render(self.resource, request) - return request - - def mark_as_unread(self, mail_ident): - request = requestMock('/mail/' + mail_ident + '/unread', method="POST", headers={'Content-Type': ['application/json']}) - _render(self.resource, request) - return request - - def mark_many_as_unread(self, idents): - request = requestMock('/mails/unread', method="POST", body=json.dumps({'idents': idents}), headers={'Content-Type': ['application/json']}) - _render(self.resource, request) - return request - - def mark_many_as_read(self, idents): - request = requestMock('/mails/read', method="POST", body=json.dumps({'idents': idents}), headers={'Content-Type': ['application/json']}) - _render(self.resource, request) - return request - - -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'] - - @property - def tags(self): - return self.mail_dict['tags'] - - @property - def status(self): - return self.mail_dict['status'] -- cgit v1.2.3