diff options
author | NavaL <ayoyo@thoughtworks.com> | 2016-09-26 12:54:45 +0200 |
---|---|---|
committer | NavaL <ayoyo@thoughtworks.com> | 2016-09-26 12:54:45 +0200 |
commit | 4462f7f8ca4b0e22396ef3a82cee884f8d348276 (patch) | |
tree | caf5db25a885207c4aa823d04c2e9d0f9cbcce72 /service/test/unit | |
parent | 833ed49c24a2b1ba87d5bdf7c1d660e68602defc (diff) |
rearranged leap session creation to not sync nor setup account/fetcher, when using the cache
Issue #773
Diffstat (limited to 'service/test/unit')
-rw-r--r-- | service/test/unit/config/test_leap.py | 31 |
1 files changed, 15 insertions, 16 deletions
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) |