From f4d7541c9b6dcf67b57b13f7ca7434ec68eeb59c Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Thu, 6 Oct 2016 17:03:44 -0300 Subject: use test client in test case through composition instead of inheritance --- .../test/support/integration/app_test_client.py | 5 +++++ .../test/support/integration/multi_user_client.py | 6 ++++-- .../test/support/integration/soledad_test_base.py | 25 ++++++++++++++++------ service/test/support/mockito/__init__.py | 20 ++++++++--------- 4 files changed, 37 insertions(+), 19 deletions(-) (limited to 'service/test/support') diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index 02a44da1..13cd9f94 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -155,6 +155,7 @@ class StubServicesFactory(ServicesFactory): yield defer.succeed(None) +# TODO: some methods are used by 1 test class only, maybe push them down there class AppTestClient(object): INDEX_KEY = '\xde3?\x87\xff\xd9\xd3\x14\xf0\xa7>\x1f%C{\x16.\\\xae\x8c\x13\xa7\xfb\x04\xd4]+\x8d_\xed\xd1\x8d\x0bI' \ '\x8a\x0e\xa4tm\xab\xbf\xb4\xa5\x99\x00d\xd5w\x9f\x18\xbc\x1d\xd4_W\xd2\xb6\xe8H\x83\x1b\xd8\x9d\xad' @@ -273,6 +274,7 @@ class AppTestClient(object): def account_for(self, username): return self.accounts[username] + # TODO: remove def add_mail_to_user_inbox(self, input_mail, username): return self.account_for(username).mail_store.add_mail('INBOX', input_mail.raw) @@ -301,6 +303,7 @@ class AppTestClient(object): mail_sender.sendmail.side_effect = lambda mail: succeed(mail) return mail_sender + # TODO: remove def _generate_soledad_test_folder_name(self, soledad_test_folder='/tmp/soledad-test/test'): return os.path.join(soledad_test_folder, str(uuid.uuid4())) @@ -318,6 +321,7 @@ class AppTestClient(object): res = yield res defer.returnValue([ResponseMail(m) for m in res['mails']]) + # TODO: remove @defer.inlineCallbacks def get_mails_by_mailbox_name(self, mbox_name): mail_ids = yield self.mail_store.get_mailbox_mail_ids(mbox_name) @@ -357,6 +361,7 @@ class AppTestClient(object): res, req = self.get('/mail/%s' % mail_ident) return res + # TODO: remove def delete_mail(self, mail_ident): res, req = self.delete("/mail/%s" % mail_ident) return res diff --git a/service/test/support/integration/multi_user_client.py b/service/test/support/integration/multi_user_client.py index 2530db46..420ff54b 100644 --- a/service/test/support/integration/multi_user_client.py +++ b/service/test/support/integration/multi_user_client.py @@ -13,6 +13,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . +from mock import patch from mockito import mock, when, any as ANY from twisted.internet import defer @@ -25,8 +26,8 @@ import pixelated.config.services from pixelated.resources.root_resource import RootResource from test.support.integration import AppTestClient from test.support.integration.app_test_client import AppTestAccount, StubSRPChecker -import test.support.mockito from test.support.test_helper import request_mock +from test.support.mockito import AnswerSelector class MultiUserClient(AppTestClient): @@ -63,7 +64,8 @@ class MultiUserClient(AppTestClient): self.user_auth = session when(LeapSessionFactory).create(username, password, session).thenReturn(leap_session) - when(leap_session).initial_sync().thenAnswer(lambda: defer.succeed(None)) + with patch('mockito.invocation.AnswerSelector', AnswerSelector): + when(leap_session).initial_sync().thenAnswer(lambda: defer.succeed(None)) when(pixelated.config.services).Services(ANY()).thenReturn(self.services) request = request_mock(path='/login', method="POST", body={'username': username, 'password': password}) diff --git a/service/test/support/integration/soledad_test_base.py b/service/test/support/integration/soledad_test_base.py index e3e582d2..0ab07490 100644 --- a/service/test/support/integration/soledad_test_base.py +++ b/service/test/support/integration/soledad_test_base.py @@ -18,12 +18,13 @@ from leap.mail.adaptors.soledad import SoledadMailAdaptor from leap.mail.mail import Message from twisted.internet import defer from twisted.trial import unittest -from pixelated.adapter.mailstore import LeapMailStore from test.support.integration.app_test_client import AppTestClient +from test.support.integration.multi_user_client import MultiUserClient from leap.common.events.flags import set_events_enabled -class SoledadTestBase(unittest.TestCase, AppTestClient): +class SoledadTestBase(unittest.TestCase): + Client = AppTestClient # these are so long because our CI is so slow at the moment. DEFERRED_TIMEOUT = 120 DEFERRED_TIMEOUT_LONG = 300 @@ -34,18 +35,24 @@ class SoledadTestBase(unittest.TestCase, AppTestClient): super(SoledadTestBase, self).setUp() self.adaptor = SoledadMailAdaptor() self.mbox_uuid = str(uuid4()) - yield self.start_client() + yield self.app_test_client.start_client() def tearDown(self): set_events_enabled(True) - self.cleanup() + self.app_test_client.cleanup() + + @property + def app_test_client(self): + if not hasattr(self, '_app_test_client'): + self._app_test_client = self.Client() + return self._app_test_client @defer.inlineCallbacks def _create_mail_in_soledad(self, mail): - yield self.adaptor.initialize_store(self.soledad) - mbox = yield self.adaptor.get_or_create_mbox(self.soledad, 'INBOX') + yield self.adaptor.initialize_store(self.app_test_client.soledad) + mbox = yield self.adaptor.get_or_create_mbox(self.app_test_client.soledad, 'INBOX') message = self._convert_mail_to_leap_message(mail, mbox.uuid) - yield self.adaptor.create_msg(self.soledad, message) + yield self.adaptor.create_msg(self.app_test_client.soledad, message) defer.returnValue(message.get_wrapper().mdoc.doc_id) @@ -53,3 +60,7 @@ class SoledadTestBase(unittest.TestCase, AppTestClient): message = self.adaptor.get_msg_from_string(Message, mail.as_string()) message.get_wrapper().set_mbox_uuid(mbox_uuid) return message + + +class MultiUserSoledadTestBase(SoledadTestBase): + Client = MultiUserClient diff --git a/service/test/support/mockito/__init__.py b/service/test/support/mockito/__init__.py index c8ffc55e..8589fac0 100644 --- a/service/test/support/mockito/__init__.py +++ b/service/test/support/mockito/__init__.py @@ -13,7 +13,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . -from mockito.invocation import AnswerSelector, CompositeAnswer +from mockito.invocation import AnswerSelector as OriginalAnswerSelector, CompositeAnswer class FunctionReturn(object): @@ -27,14 +27,14 @@ class FunctionReturn(object): return self.function_answer() -def thenAnswer(self, answer_function): - """mockito does not support the thenAnswer style. This method monkey patches it into the library""" - if not self.answer: - self.answer = CompositeAnswer(FunctionReturn(answer_function)) - self.invocation.stub_with(self.answer) - else: - self.answer.add(FunctionReturn(answer_function)) +class AnswerSelector(OriginalAnswerSelector): - return self + def thenAnswer(self, answer_function): + """mockito does not support the thenAnswer style. This method monkey patches it into the library""" + if not self.answer: + self.answer = CompositeAnswer(FunctionReturn(answer_function)) + self.invocation.stub_with(self.answer) + else: + self.answer.add(FunctionReturn(answer_function)) -AnswerSelector.thenAnswer = thenAnswer + return self -- cgit v1.2.3