~facundo/magicicada-client/changes-for-xenial

« back to all changes in this revision

Viewing changes to ubuntuone/utils/webclient/tests/test_gsettings.py

  • Committer: Magicicada Bot
  • Author(s): Natalia
  • Date: 2016-05-30 15:43:30 UTC
  • mfrom: (1418.1.21 no-sso-client)
  • Revision ID: magicicada_bot-20160530154330-b4his4s3wlucu7zv
[r=facundo] - Decouple client code from ubuntu-sso-client code. Copied and made an initial cleanup on the networkstate, utils and keyring modules.
- Removed completely dependencies with oauthlibs.
- Moved tests/ folder to inside ubuntuone/ proper folders.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright 2011-2012 Canonical Ltd.
 
4
# Copyright 2015-2016 Chicharreros (https://launchpad.net/~chicharreros)
 
5
#
 
6
# This program is free software: you can redistribute it and/or modify it
 
7
# under the terms of the GNU General Public License version 3, as published
 
8
# by the Free Software Foundation.
 
9
#
 
10
# This program is distributed in the hope that it will be useful, but
 
11
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
12
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
13
# PURPOSE.  See the GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License along
 
16
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
#
 
18
# In addition, as a special exception, the copyright holders give
 
19
# permission to link the code of portions of this program with the
 
20
# OpenSSL library under certain conditions as described in each
 
21
# individual source file, and distribute linked combinations
 
22
# including the two.
 
23
# You must obey the GNU General Public License in all respects
 
24
# for all of the code used other than OpenSSL.  If you modify
 
25
# file(s) with this exception, you may extend this exception to your
 
26
# version of the file(s), but you are not obligated to do so.  If you
 
27
# do not wish to do so, delete this exception statement from your
 
28
# version.  If you delete this exception statement from all source
 
29
# files in the program, then also delete it here.
 
30
 
 
31
"""Test the gsettings parser."""
 
32
 
 
33
import logging
 
34
 
 
35
from twisted.trial.unittest import TestCase
 
36
from ubuntuone.devtools.handlers import MementoHandler
 
37
 
 
38
from ubuntuone.utils.webclient import gsettings
 
39
from ubuntuone.utils.webclient.tests import (
 
40
    BASE_GSETTINGS_VALUES,
 
41
    TEMPLATE_GSETTINGS_OUTPUT,
 
42
)
 
43
 
 
44
 
 
45
class ProxySettingsTestCase(TestCase):
 
46
    """Test the getting of the proxy settings."""
 
47
 
 
48
    def test_gsettings_cmdline_correct(self):
 
49
        """The command line used to get the proxy settings is the right one."""
 
50
        expected = "gsettings list-recursively org.gnome.system.proxy".split()
 
51
        called = []
 
52
 
 
53
        def append_output(args):
 
54
            """Append the output and return some settings."""
 
55
            called.append(args)
 
56
            return TEMPLATE_GSETTINGS_OUTPUT.format(**BASE_GSETTINGS_VALUES)
 
57
 
 
58
        self.patch(gsettings.subprocess, "check_output", append_output)
 
59
        gsettings.get_proxy_settings()
 
60
        self.assertEqual(called[0], expected)
 
61
 
 
62
    def test_gsettings_parser_none(self):
 
63
        """Test a parser of gsettings."""
 
64
        expected = {}
 
65
        fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**BASE_GSETTINGS_VALUES)
 
66
        self.patch(gsettings.subprocess, "check_output",
 
67
                   lambda _: fake_output)
 
68
        ps = gsettings.get_proxy_settings()
 
69
        self.assertEqual(ps, expected)
 
70
 
 
71
    def _assert_parser_anonymous(self, scheme):
 
72
        """Assert the parsing of anonymous settings."""
 
73
        template_values = dict(BASE_GSETTINGS_VALUES)
 
74
        expected_host = "expected_host"
 
75
        expected_port = 54321
 
76
        expected = {
 
77
            "host": expected_host,
 
78
            "port": expected_port,
 
79
        }
 
80
        template_values.update({
 
81
            "mode": "manual",
 
82
            scheme + "_host": expected_host,
 
83
            scheme + "_port": expected_port,
 
84
        })
 
85
        fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values)
 
86
        self.patch(gsettings.subprocess, "check_output",
 
87
                   lambda _: fake_output)
 
88
        ps = gsettings.get_proxy_settings()
 
89
        self.assertEqual(ps[scheme], expected)
 
90
 
 
91
    def test_gsettings_parser_http_anonymous(self):
 
92
        """Test a parser of gsettings."""
 
93
        self._assert_parser_anonymous('http')
 
94
 
 
95
    def test_gsettings_parser_https_anonymus(self):
 
96
        """Test a parser of gsettings."""
 
97
        self._assert_parser_anonymous('https')
 
98
 
 
99
    def test_gsettings_empty_ignore_hosts(self):
 
100
        """Missing values in the ignore hosts."""
 
101
        troublesome_value = "@as []"
 
102
        template_values = dict(BASE_GSETTINGS_VALUES)
 
103
        template_values["ignore_hosts"] = troublesome_value
 
104
        fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values)
 
105
        self.patch(gsettings.subprocess, "check_output",
 
106
                   lambda _: fake_output)
 
107
        ps = gsettings.get_proxy_settings()
 
108
        self.assertEqual(ps, {})
 
109
 
 
110
    def test_gsettings_cannot_parse(self):
 
111
        """Some weird setting that cannot be parsed is logged with warning."""
 
112
        memento = MementoHandler()
 
113
        memento.setLevel(logging.DEBUG)
 
114
        gsettings.logger.addHandler(memento)
 
115
        self.addCleanup(gsettings.logger.removeHandler, memento)
 
116
 
 
117
        troublesome_value = "#bang"
 
118
        template_values = dict(BASE_GSETTINGS_VALUES)
 
119
        template_values["ignore_hosts"] = troublesome_value
 
120
        fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values)
 
121
        self.patch(gsettings.subprocess, "check_output",
 
122
                   lambda _: fake_output)
 
123
        ps = gsettings.get_proxy_settings()
 
124
        self.assertTrue(memento.check_warning(gsettings.CANNOT_PARSE_WARNING %
 
125
                                              troublesome_value))
 
126
        self.assertEqual(ps, {})
 
127
 
 
128
    def test_gsettings_parser_http_authenticated(self):
 
129
        """Test a parser of gsettings."""
 
130
        template_values = dict(BASE_GSETTINGS_VALUES)
 
131
        expected_host = "expected_host"
 
132
        expected_port = 54321
 
133
        expected_user = "carlitos"
 
134
        expected_password = "very secret password"
 
135
        expected = {
 
136
            "host": expected_host,
 
137
            "port": expected_port,
 
138
            "username": expected_user,
 
139
            "password": expected_password,
 
140
        }
 
141
        template_values.update({
 
142
            "mode": "manual",
 
143
            "http_host": expected_host,
 
144
            "http_port": expected_port,
 
145
            "auth_user": expected_user,
 
146
            "auth_password": expected_password,
 
147
            "http_use_auth": "true",
 
148
        })
 
149
        fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values)
 
150
        self.patch(gsettings.subprocess, "check_output",
 
151
                   lambda _: fake_output)
 
152
        ps = gsettings.get_proxy_settings()
 
153
        self.assertEqual(ps["http"], expected)
 
154
 
 
155
    def _assert_parser_authenticated_url(self, scheme):
 
156
        """Test a parser of gsettings with creds in the url."""
 
157
        template_values = dict(BASE_GSETTINGS_VALUES)
 
158
        expected_host = "expected_host"
 
159
        expected_port = 54321
 
160
        expected_user = "carlitos"
 
161
        expected_password = "very secret password"
 
162
        composed_url = '%s:%s@%s' % (expected_user, expected_password,
 
163
                                     expected_host)
 
164
        expected = {
 
165
            "host": expected_host,
 
166
            "port": expected_port,
 
167
            "username": expected_user,
 
168
            "password": expected_password,
 
169
        }
 
170
        template_values.update({
 
171
            "mode": "manual",
 
172
            scheme + "_host": composed_url,
 
173
            scheme + "_port": expected_port,
 
174
            "http_use_auth": "false",
 
175
        })
 
176
        fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values)
 
177
        self.patch(gsettings.subprocess, "check_output",
 
178
                   lambda _: fake_output)
 
179
        ps = gsettings.get_proxy_settings()
 
180
        self.assertEqual(ps[scheme], expected)
 
181
 
 
182
    def test_gsettings_parser_http_authenticated_url(self):
 
183
        """Test a parser of gsettings with creds in the url."""
 
184
        self._assert_parser_authenticated_url('http')
 
185
 
 
186
    def test_gsettings_parser_https_authenticated_url(self):
 
187
        """Test a parser of gsettings with creds in the url."""
 
188
        self._assert_parser_authenticated_url('https')
 
189
 
 
190
    def test_gsettings_auth_over_url(self):
 
191
        """Test that the settings are more important that the url."""
 
192
        template_values = dict(BASE_GSETTINGS_VALUES)
 
193
        expected_host = "expected_host"
 
194
        expected_port = 54321
 
195
        expected_user = "carlitos"
 
196
        expected_password = "very secret password"
 
197
        composed_url = '%s:%s@%s' % ('user', 'random',
 
198
                                     expected_host)
 
199
        http_expected = {
 
200
            "host": expected_host,
 
201
            "port": expected_port,
 
202
            "username": expected_user,
 
203
            "password": expected_password,
 
204
        }
 
205
        template_values.update({
 
206
            "mode": "manual",
 
207
            "http_host": composed_url,
 
208
            "http_port": expected_port,
 
209
            "auth_user": expected_user,
 
210
            "auth_password": expected_password,
 
211
            "http_use_auth": "true",
 
212
        })
 
213
        fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values)
 
214
        self.patch(gsettings.subprocess, "check_output",
 
215
                   lambda _: fake_output)
 
216
        ps = gsettings.get_proxy_settings()
 
217
        self.assertEqual(ps["http"], http_expected)
 
218
 
 
219
    def _assert_parser_empty_url(self, scheme):
 
220
        """Assert the parsing of an empty url."""
 
221
        template_values = dict(BASE_GSETTINGS_VALUES)
 
222
        template_values.update({
 
223
            "mode": "manual",
 
224
            scheme + "_host": '',
 
225
            scheme + "_port": 0,
 
226
            "http_use_auth": "false",
 
227
        })
 
228
        fake_output = TEMPLATE_GSETTINGS_OUTPUT.format(**template_values)
 
229
        self.patch(gsettings.subprocess, "check_output",
 
230
                   lambda _: fake_output)
 
231
        ps = gsettings.get_proxy_settings()
 
232
        self.assertNotIn(scheme, ps)
 
233
 
 
234
    def test_gsettings_parser_empty_http_url(self):
 
235
        """Test when there is no http proxy set."""
 
236
        self._assert_parser_empty_url('http')
 
237
 
 
238
    def test_gsettings_parser_empty_https_url(self):
 
239
        """Test when there is no https proxy set."""
 
240
        self._assert_parser_empty_url('https')
 
241
 
 
242
 
 
243
class ParseProxyHostTestCase(TestCase):
 
244
    """Test the parsing of the domain."""
 
245
 
 
246
    def test_onlyhost(self):
 
247
        """Parse a host with no username or password."""
 
248
        sample = "hostname"
 
249
        hostname, username, password = gsettings.parse_proxy_host(sample)
 
250
        self.assertEqual(username, None)
 
251
        self.assertEqual(password, None)
 
252
        self.assertEqual(hostname, "hostname")
 
253
 
 
254
    def test_user_and_host(self):
 
255
        """Parse host just with the username."""
 
256
        sample = "username@hostname"
 
257
        hostname, username, password = gsettings.parse_proxy_host(sample)
 
258
        self.assertEqual(username, "username")
 
259
        self.assertEqual(password, None)
 
260
        self.assertEqual(hostname, "hostname")
 
261
 
 
262
    def test_user_pass_and_host(self):
 
263
        """Test parsing a host with a username and password."""
 
264
        sample = "username:password@hostname"
 
265
        hostname, username, password = gsettings.parse_proxy_host(sample)
 
266
        self.assertEqual(username, "username")
 
267
        self.assertEqual(password, "password")
 
268
        self.assertEqual(hostname, "hostname")
 
269
 
 
270
    def test_username_with_at(self):
 
271
        """Test parsing the host with a username with @."""
 
272
        sample = "username@company.com:password@hostname"
 
273
        hostname, username, password = gsettings.parse_proxy_host(sample)
 
274
        self.assertEqual(username, "username@company.com")
 
275
        self.assertEqual(password, "password")
 
276
        self.assertEqual(hostname, "hostname")
 
277
 
 
278
    def test_username_with_at_nopass(self):
 
279
        """Test parsing the host without a password."""
 
280
        sample = "username@company.com@hostname"
 
281
        hostname, username, password = gsettings.parse_proxy_host(sample)
 
282
        self.assertEqual(username, "username@company.com")
 
283
        self.assertEqual(password, None)
 
284
        self.assertEqual(hostname, "hostname")
 
285
 
 
286
    def test_user_pass_with_colon_and_host(self):
 
287
        """Test parsing the host with a password that contains :."""
 
288
        sample = "username:pass:word@hostname"
 
289
        hostname, username, password = gsettings.parse_proxy_host(sample)
 
290
        self.assertEqual(username, "username")
 
291
        self.assertEqual(password, "pass:word")
 
292
        self.assertEqual(hostname, "hostname")