summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
Diffstat (limited to 'service')
-rw-r--r--service/pixelated/config/leap.py11
-rw-r--r--service/pixelated/config/sessions.py10
-rw-r--r--service/test/unit/config/test_leap.py31
3 files changed, 23 insertions, 29 deletions
diff --git a/service/pixelated/config/leap.py b/service/pixelated/config/leap.py
index 54518bae..3d307fc7 100644
--- a/service/pixelated/config/leap.py
+++ b/service/pixelated/config/leap.py
@@ -1,6 +1,7 @@
from __future__ import absolute_import
import logging
from collections import namedtuple
+from twisted.cred.error import UnauthorizedLogin
from twisted.internet import defer, threads
from leap.common.events import (server as events_server)
from leap.soledad.common.errors import InvalidAuthTokenError
@@ -45,16 +46,6 @@ def initialize_leap_multi_user(provider_hostname,
@defer.inlineCallbacks
def create_leap_session(provider, username, password, auth=None):
leap_session = yield LeapSessionFactory(provider).create(username, password, auth)
- try:
- yield leap_session.first_required_sync()
- except InvalidAuthTokenError:
- try:
- leap_session.close()
- except Exception, e:
- log.error(e)
- leap_session = LeapSessionFactory(provider).create(username, password, auth)
- yield leap_session.first_required_sync()
-
defer.returnValue(leap_session)
diff --git a/service/pixelated/config/sessions.py b/service/pixelated/config/sessions.py
index 9ce0a212..3a8ffc56 100644
--- a/service/pixelated/config/sessions.py
+++ b/service/pixelated/config/sessions.py
@@ -2,8 +2,12 @@ from __future__ import absolute_import
import os
import errno
+import traceback
+
import requests
import logging
+
+import sys
from twisted.internet import defer, threads, reactor
from leap.soledad.common.crypto import WrongMacError, UnknownMacMethodError
from leap.soledad.client import Soledad
@@ -34,6 +38,7 @@ class LeapSessionFactory(object):
session = SessionCache.lookup_session(key)
if not session:
session = yield self._create_new_session(username, password, auth)
+ yield session.first_required_sync()
SessionCache.remember_session(key, session)
defer.returnValue(session)
@@ -178,8 +183,8 @@ class LeapSession(object):
self.stop_background_jobs()
unregister(events.KEYMANAGER_FINISHED_KEY_GENERATION, uid=self.account_email())
self.soledad.close()
- self.remove_from_cache()
self._close_account()
+ self.remove_from_cache()
@property
def is_closed(self):
@@ -224,8 +229,7 @@ class SessionCache(object):
if session is not None and session.is_closed:
SessionCache.remove_session(key)
return None
- else:
- return session
+ return session
@staticmethod
def remember_session(key, session):
diff --git a/service/test/unit/config/test_leap.py b/service/test/unit/config/test_leap.py
index b87065d2..ce533c7f 100644
--- a/service/test/unit/config/test_leap.py
+++ b/service/test/unit/config/test_leap.py
@@ -3,36 +3,35 @@ from mock import MagicMock, patch
from twisted.trial import unittest
from twisted.internet import defer
from pixelated.config.leap import create_leap_session
+from pixelated.config.sessions import LeapSessionFactory, SessionCache
class TestAuth(unittest.TestCase):
- @patch('pixelated.config.leap.LeapSessionFactory')
+ @patch('pixelated.config.sessions.SessionCache.session_key')
@defer.inlineCallbacks
- def test_create_leap_session_calls_initial_sync(self, session_factory__ctor_mock):
- session_factory_mock = session_factory__ctor_mock.return_value
+ def test_create_leap_session_calls_initial_sync_and_caches_sessions(self, mock_session_key):
+ mock_session_key.return_value = 'mocked key'
provider_mock = MagicMock()
auth_mock = MagicMock()
session = MagicMock()
-
- session_factory_mock.create.return_value = session
-
- yield create_leap_session(provider_mock, 'username', 'password', auth=auth_mock)
+ with patch.object(LeapSessionFactory, '_create_new_session', return_value=session):
+ yield create_leap_session(provider_mock, 'username', 'password', auth=auth_mock)
session.first_required_sync.assert_called_with()
+ self.assertEqual({'mocked key': session}, SessionCache.sessions)
- @patch('pixelated.config.leap.LeapSessionFactory')
+ @patch('pixelated.config.sessions.SessionCache.lookup_session')
@defer.inlineCallbacks
- def test_create_leap_session_calls_initial_sync_a_second_time_if_invalid_auth_exception_is_raised(self, session_factory__ctor_mock):
- session_factory_mock = session_factory__ctor_mock.return_value
+ def test_create_leap_session_uses_caches_when_available_and_not_sync(self, mock_cache_lookup_session):
+ mock_cache_lookup_session.return_value = 'mocked key'
provider_mock = MagicMock()
auth_mock = MagicMock()
session = MagicMock()
+ mock_cache_lookup_session.return_value = session
- session.first_required_sync.side_effect = [InvalidAuthTokenError, defer.succeed(None)]
- session_factory_mock.create.return_value = session
-
- yield create_leap_session(provider_mock, 'username', 'password', auth=auth_mock)
+ with patch.object(LeapSessionFactory, '_create_new_session', return_value=session):
+ returned_session = yield create_leap_session(provider_mock, 'username', 'password', auth=auth_mock)
- session.close.assert_called_with()
- self.assertEqual(2, session.first_required_sync.call_count)
+ self.assertFalse(session.first_required_sync.called)
+ self.assertEqual(session, returned_session)