Merge lp:~mandel/ubuntu-sso-client/retrieve-proxy-creds into lp:ubuntu-sso-client

Proposed by Manuel de la Peña on 2012-03-22
Status: Merged
Approved by: Manuel de la Peña on 2012-03-29
Approved revision: 933
Merged at revision: 937
Proposed branch: lp:~mandel/ubuntu-sso-client/retrieve-proxy-creds
Merge into: lp:ubuntu-sso-client
Diff against target: 195 lines (+165/-1)
2 files modified
ubuntu_sso/utils/webclient/qtnetwork.py (+8/-1)
ubuntu_sso/utils/webclient/tests/test_qtnetwork.py (+157/-0)
To merge this branch: bzr merge lp:~mandel/ubuntu-sso-client/retrieve-proxy-creds
Reviewer Review Type Date Requested Status
Alejandro J. Cura (community) 2012-03-22 Approve on 2012-03-29
Roberto Alsina (community) 2012-03-22 Approve on 2012-03-28
Review via email: mp+98828@code.launchpad.net

Commit Message

- Changed the way in which the proxy settings are retrieved on windows to ensure that proxy() returns the correct one and not a null initialized one. This fixes the creds retrieval issue because the hostName is not longer '' (LP: #958938).

Description of the Change

- Changed the way in which the proxy settings are retrieved on windows to ensure that proxy() returns the correct one and not a null initialized one. This fixes the creds retrieval issue because the hostName is not longer '' (LP: #958938).

Please make sure that you do IRL on windows since the tests are patching the QtNetwork objects that do the actual system calls.

To post a comment you must log in.
Roberto Alsina (ralsina) wrote :

I see nothing wrong here, but couldn't do IRL testing.

review: Approve
Alejandro J. Cura (alecu) wrote :

Tested IRL, seems to be working.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntu_sso/utils/webclient/qtnetwork.py'
2--- ubuntu_sso/utils/webclient/qtnetwork.py 2012-03-19 23:53:06 +0000
3+++ ubuntu_sso/utils/webclient/qtnetwork.py 2012-03-22 12:13:26 +0000
4@@ -28,6 +28,7 @@
5 QNetworkAccessManager,
6 QNetworkProxy,
7 QNetworkProxyFactory,
8+ QNetworkProxyQuery,
9 QNetworkReply,
10 QNetworkRequest,
11 QSslCertificate,
12@@ -46,6 +47,7 @@
13 from ubuntu_sso.utils.webclient import gsettings
14
15 logger = setup_logging("ubuntu_sso.utils.webclient.qtnetwork")
16+PROXY_REQUEST = 'https://one.ubuntu.com'
17
18
19 def build_proxy(settings_groups):
20@@ -102,7 +104,12 @@
21 else:
22 logger.info("Proxy is disabled.")
23 else:
24- QNetworkProxyFactory.setUseSystemConfiguration(True)
25+ if WebClient.proxy_instance is None:
26+ logger.info("Querying OS for proxy.")
27+ query = QNetworkProxyQuery(QUrl(PROXY_REQUEST))
28+ proxies = QNetworkProxyFactory.systemProxyForQuery(query)
29+ QNetworkProxy.setApplicationProxy(proxies[0])
30+ WebClient.proxy_instance = proxies[0]
31
32 def handle_proxy_auth(self, proxy, authenticator):
33 """Proxy authentication is required."""
34
35=== added file 'ubuntu_sso/utils/webclient/tests/test_qtnetwork.py'
36--- ubuntu_sso/utils/webclient/tests/test_qtnetwork.py 1970-01-01 00:00:00 +0000
37+++ ubuntu_sso/utils/webclient/tests/test_qtnetwork.py 2012-03-22 12:13:26 +0000
38@@ -0,0 +1,157 @@
39+# -*- coding: utf-8 -*-
40+#
41+# Copyright 2011 Canonical Ltd.
42+#
43+# This program is free software: you can redistribute it and/or modify it
44+# under the terms of the GNU General Public License version 3, as published
45+# by the Free Software Foundation.
46+#
47+# This program is distributed in the hope that it will be useful, but
48+# WITHOUT AN WARRANTY; without even the implied warranties of
49+# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
50+# PURPOSE. See the GNU General Public License for more details.
51+#
52+# You should have received a copy of the GNU General Public License along
53+# with this program. If not, see <http://www.gnu.org/licenses/>.
54+"""Specific tests for the qt implementation."""
55+
56+from twisted.internet import defer
57+from ubuntuone.devtools.testcases import TestCase
58+
59+from ubuntu_sso.utils.webclient import qtnetwork
60+
61+
62+class FakeQNetworkProxy(object):
63+ """A fake network proxy class."""
64+
65+ def __init__(self, called):
66+ """Create a new instance."""
67+ self.called = called
68+
69+ def __call__(self, *args, **kwargs):
70+ """Fake construnctor."""
71+ return self
72+
73+ # pylint: disable=C0103
74+ def setApplicationProxy(self, proxy):
75+ """Set the application proxy."""
76+ self.called.append(('setApplicationProxy', proxy))
77+ # pylint: enable=C0103
78+
79+
80+class FakeNetworkProxyFactory(object):
81+ """A fake network proxy factory."""
82+
83+ def __init__(self, called, proxy):
84+ """Create a new instance."""
85+ self.called = called
86+ self.proxy = proxy
87+
88+ def __call__(self, *args, **kwargs):
89+ """Fake construnctor."""
90+ return self
91+
92+ # pylint: disable=C0103
93+ def systemProxyForQuery(self, query):
94+ """Get the system proxy."""
95+ self.called.append(('systemProxyForQuery', query))
96+ return [self.proxy]
97+ # pylint: enable=C0103
98+
99+
100+class SetupProxyTestCase(TestCase):
101+ """Test the proxy setup."""
102+
103+ @defer.inlineCallbacks
104+ def setUp(self):
105+ """Set the different tests."""
106+ yield super(SetupProxyTestCase, self).setUp()
107+ self.called = []
108+ self.proxy = 'fake_proxy'
109+
110+ self.network_proxy = FakeQNetworkProxy(self.called)
111+ self.patch(qtnetwork, 'QNetworkProxy', self.network_proxy)
112+
113+ self.network_proxy_factory = FakeNetworkProxyFactory(self.called,
114+ self.proxy)
115+ self.patch(qtnetwork, 'QNetworkProxyFactory',
116+ self.network_proxy_factory)
117+
118+ self.settings = dict(https=dict(username='user', password='pasword'))
119+ self.patch(qtnetwork.gsettings, 'get_proxy_settings',
120+ lambda: self.settings)
121+
122+ def fake_build_proxy(settings):
123+ """Fake build proxy."""
124+ self.called.append(('build_proxy', settings))
125+ return self.proxy
126+
127+ self.patch(qtnetwork, 'build_proxy', fake_build_proxy)
128+ qtnetwork.WebClient.proxy_instance = None
129+ self.addCleanup(self._clean_webclient_instance)
130+
131+ def _set_old_platform(self, platform):
132+ """Set back the platform."""
133+ qtnetwork.sys.platform = platform
134+
135+ def _clean_webclient_instance(self):
136+ """Set the webclient not to have a proxy."""
137+ qtnetwork.WebClient.proxy_instance = None
138+
139+
140+class SetupLinuxProxyTestCase(SetupProxyTestCase):
141+ """Test setting up the proxy."""
142+
143+ @defer.inlineCallbacks
144+ def setUp(self):
145+ """Set the different tests."""
146+ yield super(SetupLinuxProxyTestCase, self).setUp()
147+ old_platform = qtnetwork.sys.platform
148+ qtnetwork.sys.platform = 'linux'
149+ self.addCleanup(self._set_old_platform, old_platform)
150+
151+ def test_setup_proxy(self):
152+ """Test setting the proxy."""
153+ qtnetwork.WebClient()
154+ self.assertEqual(self.proxy, qtnetwork.WebClient.proxy_instance)
155+ self.assertIn(('setApplicationProxy', self.proxy), self.called)
156+ self.assertIn(('build_proxy', self.settings), self.called)
157+
158+ def test_setup_instance_present(self):
159+ """Test when the instanc is present."""
160+ # set the instance and assert we did not reset it
161+ qtnetwork.WebClient.proxy_instance = 'other_proxy'
162+ qtnetwork.WebClient()
163+ self.assertNotEqual(self.proxy, qtnetwork.WebClient.proxy_instance)
164+ self.assertNotIn(('setApplicationProxy', self.proxy), self.called)
165+ self.assertNotIn(('build_proxy', self.settings), self.called)
166+
167+
168+class SetupWindowsProxyTestCase(SetupProxyTestCase):
169+ """Test setting up the proxy."""
170+
171+ @defer.inlineCallbacks
172+ def setUp(self):
173+ """Set the different tests."""
174+ yield super(SetupWindowsProxyTestCase, self).setUp()
175+ old_platform = qtnetwork.sys.platform
176+ qtnetwork.sys.platform = 'win32'
177+ self.addCleanup(self._set_old_platform, old_platform)
178+ self.query = 'query'
179+
180+ self.patch(qtnetwork, 'QNetworkProxyQuery', lambda _: self.query)
181+
182+ def test_setup_proxy(self):
183+ """Test setting the proxy."""
184+ qtnetwork.WebClient()
185+ self.assertEqual(self.proxy, qtnetwork.WebClient.proxy_instance)
186+ self.assertIn(('systemProxyForQuery', self.query), self.called)
187+ self.assertIn(('setApplicationProxy', self.proxy), self.called)
188+
189+ def test_setup_instance_present(self):
190+ """Test when the instanc is present."""
191+ qtnetwork.WebClient.proxy_instance = 'other_proxy'
192+ qtnetwork.WebClient()
193+ self.assertNotEqual(self.proxy, qtnetwork.WebClient.proxy_instance)
194+ self.assertNotIn(('systemProxyForQuery', self.query), self.called)
195+ self.assertNotIn(('setApplicationProxy', self.proxy), self.called)

Subscribers

People subscribed via source and target branches