~mvo/ubuntu-sso-client/strawman-lp711413

« back to all changes in this revision

Viewing changes to ubuntu_sso/utils/webclient/tests/test_qtnetwork.py

  • Committer: Natalia B. Bidart
  • Date: 2011-12-20 16:29:34 UTC
  • Revision ID: natalia.bidart@canonical.com-20111220162934-2s5xou06v3usxyr6
Tags: ubuntu-sso-client-2_99_0
- Release v2.99.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
#
3
 
# Copyright 2011-2012 Canonical Ltd.
4
 
#
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.
8
 
#
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.
13
 
#
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/>.
16
 
#
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
21
 
# including the two.
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."""
30
 
 
31
 
from twisted.internet import defer
32
 
from ubuntuone.devtools.testcases import TestCase
33
 
 
34
 
from ubuntu_sso.utils.webclient import qtnetwork
35
 
 
36
 
 
37
 
class FakeQNetworkProxy(object):
38
 
    """A fake network proxy class."""
39
 
 
40
 
    def __init__(self, called):
41
 
        """Create a new instance."""
42
 
        self.called = called
43
 
 
44
 
    def __call__(self, *args, **kwargs):
45
 
        """Fake construnctor."""
46
 
        return self
47
 
 
48
 
    # pylint: disable=C0103
49
 
    def setApplicationProxy(self, proxy):
50
 
        """Set the application proxy."""
51
 
        self.called.append(('setApplicationProxy', proxy))
52
 
    # pylint: enable=C0103
53
 
 
54
 
 
55
 
class FakeNetworkProxyFactory(object):
56
 
    """A fake network proxy factory."""
57
 
 
58
 
    def __init__(self, called, proxy):
59
 
        """Create a new instance."""
60
 
        self.called = called
61
 
        self.proxy = proxy
62
 
 
63
 
    def __call__(self, *args, **kwargs):
64
 
        """Fake construnctor."""
65
 
        return self
66
 
 
67
 
    # pylint: disable=C0103
68
 
    def systemProxyForQuery(self, query):
69
 
        """Get the system proxy."""
70
 
        self.called.append(('systemProxyForQuery', query))
71
 
        return [self.proxy]
72
 
    # pylint: enable=C0103
73
 
 
74
 
 
75
 
class SetupProxyTestCase(TestCase):
76
 
    """Test the proxy setup."""
77
 
 
78
 
    @defer.inlineCallbacks
79
 
    def setUp(self):
80
 
        """Set the different tests."""
81
 
        yield super(SetupProxyTestCase, self).setUp()
82
 
        self.called = []
83
 
        self.proxy = 'fake_proxy'
84
 
 
85
 
        self.network_proxy = FakeQNetworkProxy(self.called)
86
 
        self.patch(qtnetwork, 'QNetworkProxy', self.network_proxy)
87
 
 
88
 
        self.network_proxy_factory = FakeNetworkProxyFactory(self.called,
89
 
                                                              self.proxy)
90
 
        self.patch(qtnetwork, 'QNetworkProxyFactory',
91
 
                                              self.network_proxy_factory)
92
 
 
93
 
        self.settings = dict(https=dict(username='user', password='pasword'))
94
 
        self.patch(qtnetwork.gsettings, 'get_proxy_settings',
95
 
                lambda: self.settings)
96
 
 
97
 
        def fake_build_proxy(settings):
98
 
            """Fake build proxy."""
99
 
            self.called.append(('build_proxy', settings))
100
 
            return self.proxy
101
 
 
102
 
        self.patch(qtnetwork, 'build_proxy', fake_build_proxy)
103
 
        qtnetwork.WebClient.proxy_instance = None
104
 
        self.addCleanup(self._clean_webclient_instance)
105
 
 
106
 
    def _set_old_platform(self, platform):
107
 
        """Set back the platform."""
108
 
        qtnetwork.sys.platform = platform
109
 
 
110
 
    def _clean_webclient_instance(self):
111
 
        """Set the webclient not to have a proxy."""
112
 
        qtnetwork.WebClient.proxy_instance = None
113
 
 
114
 
 
115
 
class SetupLinuxProxyTestCase(SetupProxyTestCase):
116
 
    """Test setting up the proxy."""
117
 
 
118
 
    @defer.inlineCallbacks
119
 
    def setUp(self):
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)
125
 
 
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)
132
 
 
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)
141
 
 
142
 
 
143
 
class SetupWindowsProxyTestCase(SetupProxyTestCase):
144
 
    """Test setting up the proxy."""
145
 
 
146
 
    @defer.inlineCallbacks
147
 
    def setUp(self):
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)
153
 
        self.query = 'query'
154
 
 
155
 
        self.patch(qtnetwork, 'QNetworkProxyQuery', lambda _: self.query)
156
 
 
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)
163
 
 
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)