summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNavaL <mnandri@thoughtworks.com>2016-02-03 13:44:13 +0100
committerNavaL <mnandri@thoughtworks.com>2016-02-03 13:49:54 +0100
commit4873b25259b7ec1c060e32993857a183314eaa66 (patch)
tree90c1d0c0e3ccf72074b75a21c307a581fcc381f7
parenta57953de3be89533be11c5d346fb98ab210954bc (diff)
checks to load leap sessions and services or not
Issue #590
-rw-r--r--service/pixelated/resources/login_resource.py12
-rw-r--r--service/test/support/integration/multi_user_client.py9
-rw-r--r--service/test/unit/resources/test_login_resource.py22
3 files changed, 29 insertions, 14 deletions
diff --git a/service/pixelated/resources/login_resource.py b/service/pixelated/resources/login_resource.py
index dd5563a5..9e47fd3c 100644
--- a/service/pixelated/resources/login_resource.py
+++ b/service/pixelated/resources/login_resource.py
@@ -131,9 +131,11 @@ class LoginResource(BaseResource):
@defer.inlineCallbacks
def _setup_user_services(self, srp_auth, request):
- leap_session = yield self._init_leap_session(srp_auth)
- yield self._initialize_services(leap_session)
- self._init_http_session(request, leap_session)
+ user_id = srp_auth.uuid
+ if not self._services_factory.is_logged_in(user_id):
+ leap_session = yield self._init_leap_session(srp_auth)
+ yield self._initialize_services(leap_session)
+ self._init_http_session(request, user_id)
@defer.inlineCallbacks
def _init_leap_session(self, srp_auth):
@@ -147,6 +149,6 @@ class LoginResource(BaseResource):
if leap_session.fresh_account:
yield add_welcome_mail(leap_session.mail_store)
- def _init_http_session(self, request, leap_session):
+ def _init_http_session(self, request, user_id):
session = IPixelatedSession(request.getSession())
- session.user_uuid = leap_session.user_auth.uuid
+ session.user_uuid = user_id
diff --git a/service/test/support/integration/multi_user_client.py b/service/test/support/integration/multi_user_client.py
index 692dc5e9..19833c9f 100644
--- a/service/test/support/integration/multi_user_client.py
+++ b/service/test/support/integration/multi_user_client.py
@@ -13,8 +13,6 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
-
-
from leap.exceptions import SRPAuthenticationError
from mockito import mock, when, any as ANY
from twisted.internet import defer
@@ -27,7 +25,7 @@ from pixelated.bitmask_libraries.session import LeapSession, LeapSessionFactory
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 initialize_soledad, AppTestAccount
+from test.support.integration.app_test_client import AppTestAccount
import test.support.mockito
from test.support.test_helper import request_mock
@@ -60,9 +58,8 @@ class MultiUserClient(AppTestClient):
leap_session.config = config
leap_session.fresh_account = False
- 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)
+ self._set_leap_srp_auth(username, password, user_auth)
+ when(LeapSessionFactory).create(username, password, user_auth).thenReturn(leap_session)
when(leap_session).initial_sync().thenAnswer(lambda: defer.succeed(None))
when(pixelated.config.services).Services(ANY()).thenReturn(self._test_account.services)
diff --git a/service/test/unit/resources/test_login_resource.py b/service/test/unit/resources/test_login_resource.py
index 50b96f15..8f65a030 100644
--- a/service/test/unit/resources/test_login_resource.py
+++ b/service/test/unit/resources/test_login_resource.py
@@ -1,8 +1,6 @@
-import os
-
from leap.exceptions import SRPAuthenticationError
from mock import patch
-from mockito import mock, when, any as ANY, verify, verifyZeroInteractions
+from mockito import mock, when, any as ANY, verify, verifyZeroInteractions, verifyNoMoreInteractions
from twisted.trial import unittest
from twisted.web.resource import IResource
from twisted.web.test.requesthelper import DummyRequest
@@ -113,6 +111,24 @@ class TestLoginPOST(unittest.TestCase):
d.addCallback(assert_login_setup_service_for_user)
return d
+ def test_login_does_not_reload_leap_sessions_and_services_if_already_loaded(self):
+ irrelevant = None
+ when(self.portal).login(ANY(), None, IResource).thenReturn((irrelevant, self.user_auth, irrelevant))
+ when(self.services_factory).is_logged_in('some_user_uuid').thenReturn(True)
+
+ d = self.web.get(self.request)
+
+ def assert_login_setup_service_for_user(_):
+ verify(self.portal).login(ANY(), None, IResource)
+ verify(self.services_factory).is_logged_in('some_user_uuid')
+ verifyNoMoreInteractions(self.services_factory)
+ interstitial_js_in_template = '<script src="startup-assets/Interstitial.js"></script>'
+ 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)
+ return d
+
def test_should_return_form_back_with_error_message_when_login_fails(self):
when(self.portal).login(ANY(), None, IResource).thenRaise(Exception())
d = self.web.get(self.request)