summaryrefslogtreecommitdiff
path: root/service/test/unit/test_application.py
blob: 4861099c1fd3ef9cf5c1162fe8b5ba9cd94b9bcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#
# Copyright (c) 2015 ThoughtWorks, Inc.
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.

from twisted.trial import unittest

from leap.common.events import catalog as events
from mock import patch, MagicMock, ANY
import pixelated
from pixelated.authentication import Authenticator


class ApplicationTest(unittest.TestCase):

    class MockConfig:
        def __init__(self, port, host, sslkey=None, sslcert=None, manhole=False):
            self.port = port
            self.host = host
            self.sslkey = sslkey
            self.sslcert = sslcert
            self.home = 'leap_home'
            self.manhole = manhole

    @patch('leap.common.events.client')
    @patch('pixelated.application.reactor')
    def test_that_start_site_binds_to_tcp_port_if_no_ssl_options(self, reactor_mock, _):
        app_mock = MagicMock()
        config = ApplicationTest.MockConfig(12345, '127.0.0.1')

        pixelated.application.start_site(config, app_mock)

        reactor_mock.listenTCP.assert_called_once_with(12345, ANY, interface='127.0.0.1')

    @patch('leap.common.events.client')
    @patch('pixelated.application.reactor')
    def test_that_start_site_binds_to_ssl_if_ssl_options(self, reactor_mock, _):
        app_mock = MagicMock()
        pixelated.application._ssl_options = lambda x, y: 'options'

        config = ApplicationTest.MockConfig(12345, '127.0.0.1', sslkey="sslkey", sslcert="sslcert")

        pixelated.application.start_site(config, app_mock)

        reactor_mock.listenSSL.assert_called_once_with(12345, ANY, 'options', interface='127.0.0.1')

    @patch('leap.common.events.client')
    @patch('pixelated.application.reactor')
    @patch('pixelated.application.services.Services')
    def test_that_start_user_agent_binds_to_tcp_port_if_no_ssl_options(self, services_mock, reactor_mock, _):
        # FIXME patch something closer, instead of leap.common
        app_mock = MagicMock()
        services_factory_mock = MagicMock()
        leap_session = MagicMock()
        leap_session.fresh_account = False
        config = ApplicationTest.MockConfig(12345, '127.0.0.1', leap_session)

        d = pixelated.application.start_user_agent_in_single_user_mode(app_mock, services_factory_mock, config.home, leap_session)

        def _assert(_):
            services_mock.assert_called_once_with(leap_session)

        d.addCallback(_assert)
        return d

    @patch('leap.common.events.client')
    @patch('pixelated.application.reactor')
    @patch('pixelated.application.services.Services')
    def test_that_start_user_agent_binds_to_ssl_if_ssl_options(self, services_mock, reactor_mock, _):
        # FIXME patch something closer, instead of leap.common
        app_mock = MagicMock()
        services_factory_mock = MagicMock()
        leap_session = MagicMock()
        leap_session.fresh_account = False
        pixelated.application._ssl_options = lambda x, y: 'options'

        config = ApplicationTest.MockConfig(12345, '127.0.0.1', sslkey="sslkey", sslcert="sslcert")

        d = pixelated.application.start_user_agent_in_single_user_mode(app_mock, services_factory_mock, config.home, leap_session)

        def _assert(_):
            services_mock.assert_called_once_with(leap_session)

        d.addCallback(_assert)
        return d

    @patch('leap.common.events.client')
    @patch('pixelated.application.reactor')
    @patch('pixelated.application.services.Services')
    def test_should_log_user_out_if_invalid_soledad_token(self, services_mock, reactor_mock, events_mock):
        app_mock = MagicMock()
        services_factory_mock = MagicMock()

        mock_service_log_user_out = MagicMock(return_value=None)
        services_factory_mock.destroy_session = mock_service_log_user_out

        leap_session = MagicMock()
        leap_session.fresh_account = False
        register_mock = events_mock.register
        register_mock.register.return_value = None

        config = ApplicationTest.MockConfig(12345, '127.0.0.1')
        d = pixelated.application.start_user_agent_in_single_user_mode(app_mock, services_factory_mock, config.home, leap_session)

        pixelated.application.add_top_level_system_callbacks(d, services_factory_mock)

        def _assert_user_logged_out_using_uuid(_):
            used_arguments = register_mock.call_args[0]
            self.assertIsNotNone(used_arguments)
            soledad_invalid_auth_event = used_arguments[0]
            self.assertEqual(soledad_invalid_auth_event, events.SOLEDAD_INVALID_AUTH_TOKEN)
            used_log_out_method = used_arguments[1]
            used_log_out_method(events.SOLEDAD_INVALID_AUTH_TOKEN, {'uuid': 'some_uuid'})
            mock_service_log_user_out.assert_called_once_with(user_id='some_uuid')

        def _assert_user_logged_out_using_email_id(_):
            mock_service_log_user_out.reset_mock()
            used_arguments = register_mock.call_args[0]
            used_log_out_method = used_arguments[1]
            used_log_out_method(events.SOLEDAD_INVALID_AUTH_TOKEN, 'haha@ayo.yo')
            mock_service_log_user_out.assert_called_once_with(user_id='haha@ayo.yo', using_email=True)

        d.addCallback(_assert_user_logged_out_using_uuid)
        d.addCallback(_assert_user_logged_out_using_email_id)
        return d

    @patch('pixelated.application.reactor')
    @patch('pixelated.application.services.Services')
    def test_initialize_authenticator_in_single_user_mode(self, mock_services, _):
        root_resources_mock = MagicMock()
        services_factory_mock = MagicMock()
        leap_session = MagicMock()
        leap_session.fresh_account = False

        d = pixelated.application.start_user_agent_in_single_user_mode(
            root_resources_mock,
            services_factory_mock,
            "",
            leap_session)

        def assert_root_resource_initialize_called_with_authenticator(_):
            authenticator = root_resources_mock.initialize.call_args[1]['authenticator']
            self.assertIsInstance(authenticator, Authenticator)

        d.addCallback(assert_root_resource_initialize_called_with_authenticator)
        return d

    @patch('pixelated.application.reactor')
    @patch('pixelated.application._setup_multi_user')
    def test_should_defer_fail_errors_during_multi_user_start_site(self, mock_multi_user_bootstrap, reactor_mock):
        args_mock = MagicMock()
        root_resources_mock = MagicMock()
        services_factory_mock = MagicMock()

        mock_multi_user_bootstrap.side_effect = Exception('multi-user failed bootstrap for whatever reason')

        d = pixelated.application._start_in_multi_user_mode(args_mock, root_resources_mock, services_factory_mock)

        def _assert_the_same_error_is_relayed_in_the_deferred(e):
            self.assertIsInstance(e.value, Exception)
            self.assertEqual(e.value.message, 'multi-user failed bootstrap for whatever reason')

        d.addErrback(_assert_the_same_error_is_relayed_in_the_deferred)
        return d

    @patch('pixelated.application.reactor')
    @patch('pixelated.application.start_site')
    @patch('pixelated.application._setup_multi_user')
    def test_should_defer_fail_errors_during_multi_user_bootstrap(self, ignore_setup_multi_user, mock_start_site, reactor_mock):
        args_mock = MagicMock()
        root_resources_mock = MagicMock()
        services_factory_mock = MagicMock()

        mock_start_site.side_effect = Exception('multi-user failed start site for whatever reason')

        d = pixelated.application._start_in_multi_user_mode(args_mock, root_resources_mock, services_factory_mock)

        def _assert_the_same_error_is_relayed_in_the_deferred(e):
            self.assertIsInstance(e.value, Exception)
            self.assertEqual(e.value.message, 'multi-user failed start site for whatever reason')

        d.addErrback(_assert_the_same_error_is_relayed_in_the_deferred)
        return d

    def test_set_up_protected_resources_initializes_authenticator(self):
        mock_root_resource = MagicMock()
        mock_provider = MagicMock()
        pixelated.application.set_up_protected_resources(mock_root_resource, mock_provider, MagicMock())

        authenticator = mock_root_resource.initialize.call_args[1]['authenticator']
        self.assertIsInstance(authenticator, Authenticator)