From 4462f7f8ca4b0e22396ef3a82cee884f8d348276 Mon Sep 17 00:00:00 2001 From: NavaL Date: Mon, 26 Sep 2016 12:54:45 +0200 Subject: rearranged leap session creation to not sync nor setup account/fetcher, when using the cache Issue #773 --- service/test/unit/config/test_leap.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) (limited to 'service/test/unit/config') 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) -- cgit v1.2.3