summaryrefslogtreecommitdiff
path: root/service/test
diff options
context:
space:
mode:
authorFolker Bernitt <fbernitt@thoughtworks.com>2016-01-26 10:55:15 +0100
committerFolker Bernitt <fbernitt@thoughtworks.com>2016-01-26 10:58:38 +0100
commitc3bb7b3249a789763bd307b14f5b60b7ac1c14aa (patch)
tree0f6bc5535c28a845698e4343528bd81581f732b8 /service/test
parentbb823fda470a0c5e75e8d39da1cb269eaa8fc568 (diff)
Refactor test account account creation into own class
- Issue #576 - Step towards functional tests for multi user
Diffstat (limited to 'service/test')
-rw-r--r--service/test/integration/test_multi_user_login.py2
-rw-r--r--service/test/support/integration/app_test_client.py109
-rw-r--r--service/test/support/integration/multi_user_client.py43
3 files changed, 88 insertions, 66 deletions
diff --git a/service/test/integration/test_multi_user_login.py b/service/test/integration/test_multi_user_login.py
index 3b5b9d4b..c571cf29 100644
--- a/service/test/integration/test_multi_user_login.py
+++ b/service/test/integration/test_multi_user_login.py
@@ -45,7 +45,7 @@ class MultiUserLoginTest(MultiUserClient, SoledadTestBase):
@defer.inlineCallbacks
def test_wrong_credentials_cannot_access_resources(self):
- response, login_request = yield self.login('username', 'wrong_password')
+ response, login_request = self.login('username', 'wrong_password')
response_str = yield response
self.assertEqual(401, login_request.responseCode)
self.assertIn('Invalid credentials', login_request.written)
diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py
index dda13b4f..baf05e2a 100644
--- a/service/test/support/integration/app_test_client.py
+++ b/service/test/support/integration/app_test_client.py
@@ -46,52 +46,104 @@ from pixelated.resources.root_resource import RootResource
from test.support.integration.model import MailBuilder
from test.support.test_helper import request_mock
from test.support.integration.model import ResponseMail
+from tempdir import TempDir
-class AppTestClient(object):
+class AppTestAccount(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'
- ACCOUNT = 'test'
- MAIL_ADDRESS = 'test@pixelated.org'
- # def __init__(self):
- # self.start_client()
+ def __init__(self, user_id):
+ self._user_id = user_id
+ self._uuid = str(uuid.uuid4())
+ self._mail_address = '%s@pixelated.org' % user_id
+ self._tmp_dir = TempDir()
+ self._soledad = None
+ self._services = None
@defer.inlineCallbacks
- def start_client(self):
- soledad_test_folder = self._generate_soledad_test_folder_name()
- SearchEngine.DEFAULT_INDEX_HOME = soledad_test_folder
-
- self.cleanup = lambda: shutil.rmtree(soledad_test_folder)
-
- self.soledad = yield initialize_soledad(tempdir=soledad_test_folder)
-
- self.keymanager = mock()
-
+ def start(self):
+ soledad_test_folder = os.path.join(self._tmp_dir.name, self._uuid)
+ self.soledad = yield initialize_soledad(tempdir=soledad_test_folder, uuid=self._uuid)
self.search_engine = SearchEngine(self.INDEX_KEY, user_home=soledad_test_folder)
+ self.keymanager = mock()
self.mail_sender = self._create_mail_sender()
-
self.mail_store = SearchableMailStore(LeapMailStore(self.soledad), self.search_engine)
self.attachment_store = LeapAttachmentStore(self.soledad)
- account_ready_cb = defer.Deferred()
- self.account = IMAPAccount(self.ACCOUNT, self.soledad, account_ready_cb)
- yield account_ready_cb
+ yield self._initialize_imap_account()
+
self.draft_service = DraftService(self.mail_store)
self.leap_session = mock()
self.feedback_service = FeedbackService(self.leap_session)
self.mail_service = self._create_mail_service(self.mail_sender, self.mail_store, self.search_engine, self.attachment_store)
+
mails = yield self.mail_service.all_mails()
+ if len(mails) > 0:
+ raise Exception('What? Where did these come from?')
self.search_engine.index_mails(mails)
+ @property
+ def services(self):
+ if self._services is None:
+ services = mock()
+ services.keymanager = self.keymanager
+ services.mail_service = self.mail_service
+ services.draft_service = self.draft_service
+ services.search_engine = self.search_engine
+ services.feedback_service = self.feedback_service
+
+ self._services = services
+
+ return self._services
+
+ def cleanup(self):
+ self._tmp_dir.dissolve()
+
+ def _initialize_imap_account(self):
+ account_ready_cb = defer.Deferred()
+ self.account = IMAPAccount(self._user_id, self.soledad, account_ready_cb)
+ return account_ready_cb
+
+ def _create_mail_service(self, mail_sender, mail_store, search_engine, attachment_store):
+ return MailService(mail_sender, mail_store, search_engine, self._mail_address, attachment_store)
+
+ def _create_mail_sender(self):
+ mail_sender = Mock()
+ mail_sender.sendmail.side_effect = lambda mail: succeed(mail)
+ return mail_sender
+
+
+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'
+ ACCOUNT = 'test'
+ MAIL_ADDRESS = 'test@pixelated.org'
+
+ @defer.inlineCallbacks
+ def start_client(self):
+ self._test_account = AppTestAccount(self.ACCOUNT)
+
+ yield self._test_account.start()
+
+ self.cleanup = lambda: self._test_account.cleanup()
+
+ # copy fields for single user tests
+ self.soledad = self._test_account.soledad
+ self.search_engine = self._test_account.search_engine
+ self.keymanager = self._test_account.keymanager
+ self.mail_sender = self._test_account.mail_sender
+ self.mail_store = self._test_account.mail_store
+ self.attachment_store = self._test_account.attachment_store
+ self.draft_service = self._test_account.draft_service
+ self.leap_session = self._test_account.leap_session
+ self.feedback_service = self._test_account.feedback_service
+ self.mail_service = self._test_account.mail_service
+ self.account = self._test_account.account
+
self.service_factory = SingleUserServicesFactory(UserAgentMode(is_single_user=True))
- services = mock()
- services.keymanager = self.keymanager
- services.mail_service = self.mail_service
- services.draft_service = self.draft_service
- services.search_engine = self.search_engine
- services.feedback_service = self.feedback_service
+ services = self._test_account.services
self.service_factory.add_session('someuserid', services)
self.resource = RootResource(self.service_factory)
@@ -177,10 +229,6 @@ class AppTestClient(object):
mail_sender.sendmail.side_effect = lambda mail: succeed(mail)
return mail_sender
- def _create_mail_service(self, mail_sender, mail_store, search_engine, attachment_store):
- mail_service = MailService(mail_sender, mail_store, search_engine, self.MAIL_ADDRESS, attachment_store)
- return mail_service
-
def _generate_soledad_test_folder_name(self, soledad_test_folder='/tmp/soledad-test/test'):
return os.path.join(soledad_test_folder, str(uuid.uuid4()))
@@ -254,11 +302,10 @@ class AppTestClient(object):
@defer.inlineCallbacks
-def initialize_soledad(tempdir):
+def initialize_soledad(tempdir, uuid):
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")
diff --git a/service/test/support/integration/multi_user_client.py b/service/test/support/integration/multi_user_client.py
index 897799aa..af314fdf 100644
--- a/service/test/support/integration/multi_user_client.py
+++ b/service/test/support/integration/multi_user_client.py
@@ -41,7 +41,7 @@ from pixelated.resources.auth import LeapPasswordChecker, SessionChecker, Pixela
from pixelated.resources.login_resource import LoginResource
from pixelated.resources.root_resource import RootResource
from test.support.integration import AppTestClient
-from test.support.integration.app_test_client import initialize_soledad
+from test.support.integration.app_test_client import initialize_soledad, AppTestAccount
from test.support.test_helper import request_mock
@@ -50,10 +50,12 @@ class MultiUserClient(AppTestClient):
@defer.inlineCallbacks
def start_client(self):
- self.soledad_test_folder = self._generate_soledad_test_folder_name()
- SearchEngine.DEFAULT_INDEX_HOME = self.soledad_test_folder
- self.cleanup = lambda: shutil.rmtree(self.soledad_test_folder)
- self.soledad = yield initialize_soledad(tempdir=self.soledad_test_folder)
+ self._test_account = AppTestAccount('test')
+
+ yield self._test_account.start()
+
+ self.cleanup = lambda: self._test_account.cleanup()
+ self.soledad = self._test_account.soledad
self.service_factory = ServicesFactory(UserAgentMode(is_single_user=False))
@@ -61,7 +63,6 @@ class MultiUserClient(AppTestClient):
leap_provider = mock()
self.resource = set_up_protected_resources(root_resource, leap_provider, self.service_factory)
- @defer.inlineCallbacks
def login(self, username='username', password='password'):
leap_session = mock(LeapSession)
user_auth = mock()
@@ -74,12 +75,11 @@ class MultiUserClient(AppTestClient):
self._set_leap_srp_auth(username, password)
when(LeapSessionFactory).create(username, password).thenReturn(leap_session)
- _services = yield self.generate_services()
- when(config_services).Services(leap_session).thenReturn(_services)
+ when(config_services).Services(leap_session).thenReturn(self._test_account.services)
# when(Services).setup().thenReturn(defer.succeed('mocked so irrelevant'))
request = request_mock(path='/login', method="POST", body={'username': username, 'password': password})
- defer.returnValue(self._render(request, as_json=False))
+ return self._render(request, as_json=False)
def _set_leap_srp_auth(self, username, password):
auth_dict = {'username': 'password'}
@@ -95,28 +95,3 @@ class MultiUserClient(AppTestClient):
session = from_request.getSession()
request.session = session
return self._render(request, as_json)
-
- @defer.inlineCallbacks
- def generate_services(self):
- search_engine = SearchEngine(self.INDEX_KEY, user_home=self.soledad_test_folder)
- self.mail_sender = self._create_mail_sender()
-
- self.mail_store = SearchableMailStore(LeapMailStore(self.soledad), search_engine)
- self.attachment_store = LeapAttachmentStore(self.soledad)
-
- account_ready_cb = defer.Deferred()
- self.account = IMAPAccount(self.ACCOUNT, self.soledad, account_ready_cb)
- yield account_ready_cb
- self.leap_session = mock()
-
- mail_service = self._create_mail_service(self.mail_sender, self.mail_store, search_engine, self.attachment_store)
- mails = yield mail_service.all_mails()
- search_engine.index_mails(mails)
-
- services = mock()
- services.keymanager = mock()
- services.mail_service = mail_service
- services.draft_service = DraftService(self.mail_store)
- services.search_engine = search_engine
- services.feedback_service = FeedbackService(self.leap_session)
- defer.returnValue(services)