35
35
from ubuntu_sso.utils.webclient import qtnetwork
38
class FakeQNetworkProxy(object):
39
"""A fake network proxy class."""
41
def __init__(self, called):
42
"""Create a new instance."""
45
def __call__(self, *args, **kwargs):
46
"""Fake construnctor."""
49
# pylint: disable=C0103
50
def setApplicationProxy(self, proxy):
51
"""Set the application proxy."""
52
self.called.append(('setApplicationProxy', proxy))
53
# pylint: enable=C0103
56
class FakeNetworkProxyFactory(object):
57
"""A fake network proxy factory."""
59
def __init__(self, called, proxy):
60
"""Create a new instance."""
64
def __call__(self, *args, **kwargs):
65
"""Fake construnctor."""
68
# pylint: disable=C0103
69
def systemProxyForQuery(self, query):
70
"""Get the system proxy."""
71
self.called.append(('systemProxyForQuery', query))
73
# pylint: enable=C0103
76
38
class SetupProxyTestCase(TestCase):
77
39
"""Test the proxy setup."""
81
43
"""Set the different tests."""
82
44
yield super(SetupProxyTestCase, self).setUp()
84
self.proxy = 'fake_proxy'
86
self.network_proxy = FakeQNetworkProxy(self.called)
87
self.patch(qtnetwork, 'QNetworkProxy', self.network_proxy)
89
self.network_proxy_factory = FakeNetworkProxyFactory(self.called,
91
self.patch(qtnetwork, 'QNetworkProxyFactory',
92
self.network_proxy_factory)
94
47
self.settings = dict(https=dict(username='user', password='pasword'))
95
48
self.patch(qtnetwork.gsettings, 'get_proxy_settings',
96
49
lambda: self.settings)
98
def fake_build_proxy(settings):
99
"""Fake build proxy."""
53
self.real_build_proxy = qtnetwork.build_proxy
55
def build_proxy(settings):
56
"""Log build_proxy calls."""
100
57
self.called.append(('build_proxy', settings))
58
self.proxy = self.real_build_proxy(settings)
103
self.patch(qtnetwork, 'build_proxy', fake_build_proxy)
104
qtnetwork.WebClient.proxy_instance = None
105
self.patch(qtnetwork.QNetworkAccessManager, 'setProxy',
106
lambda _, p: self.called.append(('setProxy', p)))
61
self.patch(qtnetwork, 'build_proxy', lambda s: build_proxy(s))
107
63
self.addCleanup(self._clean_webclient_instance)
109
65
def _set_old_platform(self, platform):
130
86
"""Test setting the proxy."""
131
87
qtnetwork.WebClient()
132
88
self.assertEqual(self.proxy, qtnetwork.WebClient.proxy_instance)
133
self.assertIn(('setApplicationProxy', self.proxy), self.called)
134
self.assertIn(('setProxy', self.proxy), self.called)
135
89
self.assertIn(('build_proxy', self.settings), self.called)
137
91
def test_setup_instance_present(self):
138
"""Test when the instanc is present."""
92
"""Test when the instance is present."""
139
93
# set the instance and assert we did not reset it
140
94
qtnetwork.WebClient.proxy_instance = 'other_proxy'
141
95
qtnetwork.WebClient()
142
96
self.assertNotEqual(self.proxy, qtnetwork.WebClient.proxy_instance)
143
self.assertNotIn(('setApplicationProxy', self.proxy), self.called)
144
self.assertNotIn(('setProxy', self.proxy), self.called)
145
97
self.assertNotIn(('build_proxy', self.settings), self.called)
148
class SetupWindowsProxyTestCase(SetupProxyTestCase):
149
"""Test setting up the proxy."""
151
@defer.inlineCallbacks
153
"""Set the different tests."""
154
yield super(SetupWindowsProxyTestCase, self).setUp()
155
old_platform = qtnetwork.sys.platform
156
qtnetwork.sys.platform = 'win32'
157
self.addCleanup(self._set_old_platform, old_platform)
160
self.patch(qtnetwork, 'QNetworkProxyQuery', lambda _: self.query)
162
def test_setup_proxy(self):
163
"""Test setting the proxy."""
164
qtnetwork.WebClient()
165
self.assertEqual(self.proxy, qtnetwork.WebClient.proxy_instance)
166
# nam.setProxy does not work on Windows (PyQt 4.9.4)
167
# so WebClient._set_proxy was changed to not call setProxy
168
# like we do on Quantal.
169
self.assertNotIn(('setProxy', self.proxy), self.called)
170
self.assertIn(('systemProxyForQuery', self.query), self.called)
171
self.assertIn(('setApplicationProxy', self.proxy), self.called)
173
def test_setup_instance_present(self):
174
"""Test when the instanc is present."""
175
qtnetwork.WebClient.proxy_instance = 'other_proxy'
176
qtnetwork.WebClient()
177
self.assertNotEqual(self.proxy, qtnetwork.WebClient.proxy_instance)
178
self.assertNotIn(('systemProxyForQuery', self.query), self.called)
179
self.assertNotIn(('setApplicationProxy', self.proxy), self.called)
182
100
class FakeQNetworkReply(object):
183
101
"""A fake QNetworkReply."""