5a68c53a3740921b7c457d7bb2ea298b046613d3
[pixelated-user-agent.git] / service / test / unit / config / test_leap.py
1 #
2 # Copyright (c) 2015 ThoughtWorks, Inc.
3 #
4 # Pixelated is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # Pixelated is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
16
17 from leap.soledad.common.errors import InvalidAuthTokenError
18 from mock import MagicMock, patch, Mock
19 from twisted.trial import unittest
20 from twisted.internet import defer
21 from pixelated.config.leap import create_leap_session, BootstrapUserServices, initialize_leap_single_user
22 from pixelated.config.sessions import LeapSessionFactory, SessionCache
23
24
25 class TestLeapInit(unittest.TestCase):
26
27     @patch('pixelated.config.sessions.SessionCache.session_key')
28     @defer.inlineCallbacks
29     def test_create_leap_session_calls_initial_sync_and_caches_sessions(self, mock_session_key):
30         mock_session_key.return_value = 'mocked key'
31         provider_mock = MagicMock()
32         auth_mock = MagicMock()
33         session = MagicMock()
34         with patch.object(LeapSessionFactory, '_create_new_session', return_value=session):
35             yield create_leap_session(provider_mock, 'username', 'password', auth=auth_mock)
36
37         session.first_required_sync.assert_called_once()
38         self.assertEqual({'mocked key': session}, SessionCache.sessions)
39
40     @patch('pixelated.config.sessions.SessionCache.lookup_session')
41     @defer.inlineCallbacks
42     def test_create_leap_session_uses_caches_when_available_and_not_sync(self, mock_cache_lookup_session):
43         mock_cache_lookup_session.return_value = 'mocked key'
44         provider_mock = MagicMock()
45         auth_mock = MagicMock()
46         session = MagicMock()
47         mock_cache_lookup_session.return_value = session
48
49         with patch.object(LeapSessionFactory, '_create_new_session', return_value=session):
50             returned_session = yield create_leap_session(provider_mock, 'username', 'password', auth=auth_mock)
51
52         self.assertFalse(session.first_required_sync.called)
53         self.assertEqual(session, returned_session)
54
55     @patch('pixelated.config.leap.initialize_leap_provider')
56     @patch('pixelated.config.leap.credentials')
57     @patch('pixelated.config.leap.events_server')
58     @defer.inlineCallbacks
59     def test_init_single_user_does_bonafide_auth_and_gives_a_leap_session(self, mock_event_server, mock_credentials, mock_init_leap_provider):
60         provider_mock = MagicMock()
61         mock_init_leap_provider.return_value = provider_mock
62         mock_credentials.read.return_value = ('provider_url', 'username', 'password')
63         mock_authenticator = MagicMock()
64
65         with patch('pixelated.config.leap.Authenticator', return_value=mock_authenticator) as mock_instantiate_authenticator:
66             auth_mock = MagicMock()
67             mock_authenticator.authenticate.return_value = defer.succeed(auth_mock)
68             leap_session = MagicMock()
69             deferred_leap_session = defer.succeed(leap_session)
70             with patch.object(LeapSessionFactory, 'create', return_value=deferred_leap_session) as mock_create_leap_session:
71                 returned_session = yield initialize_leap_single_user('leap_provider_cert', 'leap_provider_cert_fingerprint', 'credentials_file', 'leap_home')
72
73         mock_event_server.ensure_server.assert_called_once()
74         mock_credentials.read.assert_called_once_with('credentials_file')
75         mock_init_leap_provider.asser_called_once_with('provider_url', 'leap_provider_cert', 'leap_provider_cert_fingerprint', 'leap_home')
76         mock_instantiate_authenticator.assert_called_once_with(provider_mock)
77         mock_authenticator.authenticate.assert_called_once_with('username', 'password')
78         mock_create_leap_session.assert_called_once_with('username', 'password', auth_mock)
79         self.assertEqual(leap_session, returned_session)
80
81
82 class TestUserBootstrap(unittest.TestCase):
83
84     def setUp(self):
85         self._service_factory = Mock()
86         self._provider = Mock()
87         self._user_bootstrap = BootstrapUserServices(self._service_factory, self._provider)
88
89         username = 'ayoyo'
90         password = 'ayoyo_password'
91         self.username = username
92         self.password = password
93
94         user_auth = Mock()
95         user_auth.username = username
96         self.uuid = 'some_user_uuid'
97         user_auth.uuid = self.uuid
98         self.user_auth = user_auth
99
100         leap_session = Mock()
101         leap_session.user_auth = user_auth
102         leap_session.fresh_account = False
103         self.leap_session = leap_session
104
105     @patch('pixelated.config.leap.create_leap_session')
106     def test_should_create_leap_session(self, mock_create_leap_session):
107         mock_create_leap_session.return_value = self.leap_session
108         self._service_factory.has_session.return_value = False
109
110         self._user_bootstrap.setup(self.user_auth, self.password)
111
112         mock_create_leap_session.called_once_with(self._provider, self.username, self.password, self.user_auth)
113
114     @patch('pixelated.config.leap.create_leap_session')
115     def test_should_setup_user_services_and_map_email(self, mock_create_leap_session):
116         mock_create_leap_session.return_value = self.leap_session
117         self._service_factory.has_session.return_value = False
118
119         self._user_bootstrap.setup(self.user_auth, self.password)
120
121         self._service_factory.create_services_from.assert_called_once_with(self.leap_session)
122         self._service_factory.map_email.assert_called_once_with(self.username, self.uuid)
123
124     @patch('pixelated.config.leap.create_leap_session')
125     def test_should_not_user_services_if_there_is_already_a_session(self, mock_create_leap_session):
126         mock_create_leap_session.return_value = self.leap_session
127         self._service_factory.has_session.return_value = True
128
129         self._user_bootstrap.setup(self.user_auth, self.password)
130
131         self.assertFalse(self._service_factory.create_services_from.called)
132
133     @patch('pixelated.config.leap.add_welcome_mail')
134     @patch('pixelated.config.leap.create_leap_session')
135     def test_should_add_welcome_email_on_a_fresh_account(self, mock_create_leap_session, mock_add_welcome_email):
136         self.leap_session.fresh_account = True
137         mail_store = Mock()
138         self.leap_session.mail_store = mail_store
139         mock_create_leap_session.return_value = self.leap_session
140         self._service_factory.has_session.return_value = False
141         default_language = 'pt-BR'
142
143         self._user_bootstrap.setup(self.user_auth, self.password)
144
145         mock_add_welcome_email.assert_called_once_with(mail_store, default_language)