1
# -*- coding: utf-8 -*-
3
# Copyright 2011-2012 Canonical Ltd.
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License version 3, as published
7
# by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful, but
10
# WITHOUT AN WARRANTY; without even the implied warranties of
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12
# PURPOSE. See the GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License along
15
# with this program. If not, see <http://www.gnu.org/licenses/>.
17
# In addition, as a special exception, the copyright holders give
18
# permission to link the code of portions of this program with the
19
# OpenSSL library under certain conditions as described in each
20
# individual source file, and distribute linked combinations
22
# You must obey the GNU General Public License in all respects
23
# for all of the code used other than OpenSSL. If you modify
24
# file(s) with this exception, you may extend this exception to your
25
# version of the file(s), but you are not obligated to do so. If you
26
# do not wish to do so, delete this exception statement from your
27
# version. If you delete this exception statement from all source
28
# files in the program, then also delete it here.
29
"""Specific tests for the qt implementation."""
31
from twisted.internet import defer
32
from ubuntuone.devtools.testcases import TestCase
34
from ubuntu_sso.utils.webclient import qtnetwork
37
class FakeQNetworkProxy(object):
38
"""A fake network proxy class."""
40
def __init__(self, called):
41
"""Create a new instance."""
44
def __call__(self, *args, **kwargs):
45
"""Fake construnctor."""
48
# pylint: disable=C0103
49
def setApplicationProxy(self, proxy):
50
"""Set the application proxy."""
51
self.called.append(('setApplicationProxy', proxy))
52
# pylint: enable=C0103
55
class FakeNetworkProxyFactory(object):
56
"""A fake network proxy factory."""
58
def __init__(self, called, proxy):
59
"""Create a new instance."""
63
def __call__(self, *args, **kwargs):
64
"""Fake construnctor."""
67
# pylint: disable=C0103
68
def systemProxyForQuery(self, query):
69
"""Get the system proxy."""
70
self.called.append(('systemProxyForQuery', query))
72
# pylint: enable=C0103
75
class SetupProxyTestCase(TestCase):
76
"""Test the proxy setup."""
78
@defer.inlineCallbacks
80
"""Set the different tests."""
81
yield super(SetupProxyTestCase, self).setUp()
83
self.proxy = 'fake_proxy'
85
self.network_proxy = FakeQNetworkProxy(self.called)
86
self.patch(qtnetwork, 'QNetworkProxy', self.network_proxy)
88
self.network_proxy_factory = FakeNetworkProxyFactory(self.called,
90
self.patch(qtnetwork, 'QNetworkProxyFactory',
91
self.network_proxy_factory)
93
self.settings = dict(https=dict(username='user', password='pasword'))
94
self.patch(qtnetwork.gsettings, 'get_proxy_settings',
95
lambda: self.settings)
97
def fake_build_proxy(settings):
98
"""Fake build proxy."""
99
self.called.append(('build_proxy', settings))
102
self.patch(qtnetwork, 'build_proxy', fake_build_proxy)
103
qtnetwork.WebClient.proxy_instance = None
104
self.addCleanup(self._clean_webclient_instance)
106
def _set_old_platform(self, platform):
107
"""Set back the platform."""
108
qtnetwork.sys.platform = platform
110
def _clean_webclient_instance(self):
111
"""Set the webclient not to have a proxy."""
112
qtnetwork.WebClient.proxy_instance = None
115
class SetupLinuxProxyTestCase(SetupProxyTestCase):
116
"""Test setting up the proxy."""
118
@defer.inlineCallbacks
120
"""Set the different tests."""
121
yield super(SetupLinuxProxyTestCase, self).setUp()
122
old_platform = qtnetwork.sys.platform
123
qtnetwork.sys.platform = 'linux'
124
self.addCleanup(self._set_old_platform, old_platform)
126
def test_setup_proxy(self):
127
"""Test setting the proxy."""
128
qtnetwork.WebClient()
129
self.assertEqual(self.proxy, qtnetwork.WebClient.proxy_instance)
130
self.assertIn(('setApplicationProxy', self.proxy), self.called)
131
self.assertIn(('build_proxy', self.settings), self.called)
133
def test_setup_instance_present(self):
134
"""Test when the instanc is present."""
135
# set the instance and assert we did not reset it
136
qtnetwork.WebClient.proxy_instance = 'other_proxy'
137
qtnetwork.WebClient()
138
self.assertNotEqual(self.proxy, qtnetwork.WebClient.proxy_instance)
139
self.assertNotIn(('setApplicationProxy', self.proxy), self.called)
140
self.assertNotIn(('build_proxy', self.settings), self.called)
143
class SetupWindowsProxyTestCase(SetupProxyTestCase):
144
"""Test setting up the proxy."""
146
@defer.inlineCallbacks
148
"""Set the different tests."""
149
yield super(SetupWindowsProxyTestCase, self).setUp()
150
old_platform = qtnetwork.sys.platform
151
qtnetwork.sys.platform = 'win32'
152
self.addCleanup(self._set_old_platform, old_platform)
155
self.patch(qtnetwork, 'QNetworkProxyQuery', lambda _: self.query)
157
def test_setup_proxy(self):
158
"""Test setting the proxy."""
159
qtnetwork.WebClient()
160
self.assertEqual(self.proxy, qtnetwork.WebClient.proxy_instance)
161
self.assertIn(('systemProxyForQuery', self.query), self.called)
162
self.assertIn(('setApplicationProxy', self.proxy), self.called)
164
def test_setup_instance_present(self):
165
"""Test when the instanc is present."""
166
qtnetwork.WebClient.proxy_instance = 'other_proxy'
167
qtnetwork.WebClient()
168
self.assertNotEqual(self.proxy, qtnetwork.WebClient.proxy_instance)
169
self.assertNotIn(('systemProxyForQuery', self.query), self.called)
170
self.assertNotIn(('setApplicationProxy', self.proxy), self.called)