From 766c5617007650d90f1d249aaa253755dcd1906c Mon Sep 17 00:00:00 2001 From: NavaL Date: Wed, 27 Jan 2016 19:29:36 +0100 Subject: making async setup user services after auth Issue #583 --- .../test/support/integration/multi_user_client.py | 34 +++++++++++++++++----- service/test/unit/resources/test_login_resource.py | 9 ++++-- 2 files changed, 32 insertions(+), 11 deletions(-) (limited to 'service/test') diff --git a/service/test/support/integration/multi_user_client.py b/service/test/support/integration/multi_user_client.py index 13e1b64b..67b034cc 100644 --- a/service/test/support/integration/multi_user_client.py +++ b/service/test/support/integration/multi_user_client.py @@ -14,19 +14,36 @@ # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . -from leap.auth import SRPAuth +import json +import shutil + from leap.exceptions import SRPAuthenticationError -from mockito import mock, when +from leap.mail.imap.account import IMAPAccount +from mockito import mock, when, any as ANY +from twisted.cred import portal +from twisted.cred.checkers import AllowAnonymousAccess from twisted.internet import defer +from leap.auth import SRPAuth + +from pixelated.adapter.mailstore.leap_attachment_store import LeapAttachmentStore +from pixelated.adapter.services.feedback_service import FeedbackService from pixelated.application import UserAgentMode, ServicesFactory, set_up_protected_resources + +from pixelated.adapter.mailstore import LeapMailStore +from pixelated.adapter.mailstore.searchable_mailstore import SearchableMailStore + +from pixelated.adapter.search import SearchEngine +from pixelated.adapter.services.draft_service import DraftService from pixelated.bitmask_libraries.session import LeapSession, LeapSessionFactory import pixelated.config.services # from pixelated.config.services import Services +from pixelated.resources.auth import LeapPasswordChecker, SessionChecker, PixelatedRealm, PixelatedAuthSessionWrapper +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 AppTestAccount - +from test.support.integration.app_test_client import initialize_soledad, AppTestAccount +import test.support.mockito from test.support.test_helper import request_mock @@ -58,18 +75,19 @@ class MultiUserClient(AppTestClient): leap_session.config = config leap_session.fresh_account = False - self._set_leap_srp_auth(username, password) + mock_srp_auth = 'mocked so irrelevant but just need a return value' + self._set_leap_srp_auth(username, password, mock_srp_auth) + when(LeapSessionFactory).create(username, password, mock_srp_auth).thenReturn(leap_session) when(leap_session).initial_sync().thenAnswer(lambda: defer.succeed(None)) - when(LeapSessionFactory).create(username, password).thenReturn(leap_session) when(pixelated.config.services).Services(ANY()).thenReturn(self._test_account.services) request = request_mock(path='/login', method="POST", body={'username': username, 'password': password}) return self._render(request, as_json=False) - def _set_leap_srp_auth(self, username, password): + def _set_leap_srp_auth(self, username, password, mock_srp_auth): auth_dict = {'username': 'password'} if auth_dict[username] == password: - when(SRPAuth).authenticate(username, password).thenReturn(True) + when(SRPAuth).authenticate(username, password).thenReturn(mock_srp_auth) else: when(SRPAuth).authenticate(username, password).thenRaise(SRPAuthenticationError()) diff --git a/service/test/unit/resources/test_login_resource.py b/service/test/unit/resources/test_login_resource.py index 1ccc530d..0baf7ac0 100644 --- a/service/test/unit/resources/test_login_resource.py +++ b/service/test/unit/resources/test_login_resource.py @@ -1,3 +1,5 @@ +import os + from leap.exceptions import SRPAuthenticationError from mock import patch from mockito import mock, when, any as ANY, verify, verifyZeroInteractions @@ -93,7 +95,7 @@ class TestLoginPOST(unittest.TestCase): @patch('twisted.web.util.redirectTo') @patch('pixelated.config.services.Services.setup') - def test_login_setups_user_services_and_add_corresponding_session_to_services_factory(self, mock_service_setup, mock_redirect): + def test_login_responds_interstitial_and_add_corresponding_session_to_services_factory(self, mock_service_setup, mock_redirect): irrelevant = None when(self.portal).login(ANY(), None, IResource).thenReturn((irrelevant, self.leap_user, irrelevant)) d = self.web.get(self.request) @@ -101,7 +103,8 @@ class TestLoginPOST(unittest.TestCase): def assert_login_setup_service_for_user(_): verify(self.portal).login(ANY(), None, IResource) verify(self.services_factory).create_services_from(self.leap_session) - mock_redirect.assert_called_once_with('/', self.request) + interstitial_js_in_template = '' + self.assertIn(interstitial_js_in_template, self.request.written[0]) self.assertTrue(self.resource.is_logged_in(self.request)) d.addCallback(assert_login_setup_service_for_user) @@ -143,12 +146,12 @@ class TestLoginPOST(unittest.TestCase): @patch('pixelated.resources.session.PixelatedSession.is_logged_in') def test_should_not_process_login_if_already_logged_in(self, mock_logged_in, mock_redirect): mock_logged_in.return_value = True + mock_redirect.return_value = "mocked redirection" when(self.portal).login(ANY(), None, IResource).thenRaise(Exception()) d = self.web.get(self.request) def assert_login_setup_service_for_user(_): verifyZeroInteractions(self.portal) - self.assertEqual(200, self.request.responseCode) mock_redirect.assert_called_once_with('/', self.request) d.addCallback(assert_login_setup_service_for_user) -- cgit v1.2.3