Fix broken test for default language for user
[pixelated-user-agent.git] / service / test / unit / config / test_leap.py
1 from leap.soledad.common.errors import InvalidAuthTokenError
2 from mock import MagicMock, patch, Mock
3 from twisted.trial import unittest
4 from twisted.internet import defer
5 from pixelated.config.leap import create_leap_session, BootstrapUserServices, initialize_leap_single_user
6 from pixelated.config.sessions import LeapSessionFactory, SessionCache
7
8
9 class TestLeapInit(unittest.TestCase):
10
11     @patch('pixelated.config.sessions.SessionCache.session_key')
12     @defer.inlineCallbacks
13     def test_create_leap_session_calls_initial_sync_and_caches_sessions(self, mock_session_key):
14         mock_session_key.return_value = 'mocked key'
15         provider_mock = MagicMock()
16         auth_mock = MagicMock()
17         session = MagicMock()
18         with patch.object(LeapSessionFactory, '_create_new_session', return_value=session):
19             yield create_leap_session(provider_mock, 'username', 'password', auth=auth_mock)
20
21         session.first_required_sync.assert_called_once()
22         self.assertEqual({'mocked key': session}, SessionCache.sessions)
23
24     @patch('pixelated.config.sessions.SessionCache.lookup_session')
25     @defer.inlineCallbacks
26     def test_create_leap_session_uses_caches_when_available_and_not_sync(self, mock_cache_lookup_session):
27         mock_cache_lookup_session.return_value = 'mocked key'
28         provider_mock = MagicMock()
29         auth_mock = MagicMock()
30         session = MagicMock()
31         mock_cache_lookup_session.return_value = session
32
33         with patch.object(LeapSessionFactory, '_create_new_session', return_value=session):
34             returned_session = yield create_leap_session(provider_mock, 'username', 'password', auth=auth_mock)
35
36         self.assertFalse(session.first_required_sync.called)
37         self.assertEqual(session, returned_session)
38
39     @patch('pixelated.config.leap.initialize_leap_provider')
40     @patch('pixelated.config.leap.credentials')
41     @patch('pixelated.config.leap.events_server')
42     @defer.inlineCallbacks
43     def test_init_single_user_does_bonafide_auth_and_gives_a_leap_session(self, mock_event_server, mock_credentials, mock_init_leap_provider):
44         provider_mock = MagicMock()
45         mock_init_leap_provider.return_value = provider_mock
46         mock_credentials.read.return_value = ('provider_url', 'username', 'password')
47         mock_authenticator = MagicMock()
48
49         with patch('pixelated.config.leap.Authenticator', return_value=mock_authenticator) as mock_instantiate_authenticator:
50             auth_mock = MagicMock()
51             mock_authenticator.authenticate.return_value = defer.succeed(auth_mock)
52             leap_session = MagicMock()
53             deferred_leap_session = defer.succeed(leap_session)
54             with patch.object(LeapSessionFactory, 'create', return_value=deferred_leap_session) as mock_create_leap_session:
55                 returned_session = yield initialize_leap_single_user('leap_provider_cert', 'leap_provider_cert_fingerprint', 'credentials_file', 'leap_home')
56
57         mock_event_server.ensure_server.assert_called_once()
58         mock_credentials.read.assert_called_once_with('credentials_file')
59         mock_init_leap_provider.asser_called_once_with('provider_url', 'leap_provider_cert', 'leap_provider_cert_fingerprint', 'leap_home')
60         mock_instantiate_authenticator.assert_called_once_with(provider_mock)
61         mock_authenticator.authenticate.assert_called_once_with('username', 'password')
62         mock_create_leap_session.assert_called_once_with('username', 'password', auth_mock)
63         self.assertEqual(leap_session, returned_session)
64
65
66 class TestUserBootstrap(unittest.TestCase):
67
68     def setUp(self):
69         self._service_factory = Mock()
70         self._provider = Mock()
71         self._user_bootstrap = BootstrapUserServices(self._service_factory, self._provider)
72
73         username = 'ayoyo'
74         password = 'ayoyo_password'
75         self.username = username
76         self.password = password
77
78         user_auth = Mock()
79         user_auth.username = username
80         self.uuid = 'some_user_uuid'
81         user_auth.uuid = self.uuid
82         self.user_auth = user_auth
83
84         leap_session = Mock()
85         leap_session.user_auth = user_auth
86         leap_session.fresh_account = False
87         self.leap_session = leap_session
88
89     @patch('pixelated.config.leap.create_leap_session')
90     def test_should_create_leap_session(self, mock_create_leap_session):
91         mock_create_leap_session.return_value = self.leap_session
92         self._service_factory.has_session.return_value = False
93
94         self._user_bootstrap.setup(self.user_auth, self.password)
95
96         mock_create_leap_session.called_once_with(self._provider, self.username, self.password, self.user_auth)
97
98     @patch('pixelated.config.leap.create_leap_session')
99     def test_should_setup_user_services_and_map_email(self, mock_create_leap_session):
100         mock_create_leap_session.return_value = self.leap_session
101         self._service_factory.has_session.return_value = False
102
103         self._user_bootstrap.setup(self.user_auth, self.password)
104
105         self._service_factory.create_services_from.assert_called_once_with(self.leap_session)
106         self._service_factory.map_email.assert_called_once_with(self.username, self.uuid)
107
108     @patch('pixelated.config.leap.create_leap_session')
109     def test_should_not_user_services_if_there_is_already_a_session(self, mock_create_leap_session):
110         mock_create_leap_session.return_value = self.leap_session
111         self._service_factory.has_session.return_value = True
112
113         self._user_bootstrap.setup(self.user_auth, self.password)
114
115         self.assertFalse(self._service_factory.create_services_from.called)
116
117     @patch('pixelated.config.leap.add_welcome_mail')
118     @patch('pixelated.config.leap.create_leap_session')
119     def test_should_add_welcome_email_on_a_fresh_account(self, mock_create_leap_session, mock_add_welcome_email):
120         self.leap_session.fresh_account = True
121         mail_store = Mock()
122         self.leap_session.mail_store = mail_store
123         mock_create_leap_session.return_value = self.leap_session
124         self._service_factory.has_session.return_value = False
125         default_language = 'pt-BR'
126
127         self._user_bootstrap.setup(self.user_auth, self.password)
128
129         mock_add_welcome_email.assert_called_once_with(mail_store, default_language)