summaryrefslogtreecommitdiff
path: root/service/test/unit/test_application.py
blob: 67c044c2816a1bb89d7a59e0cd75c67f6bf3e8ea (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
from twisted.trial import unittest

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


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._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