~dobey/ubuntu-sso-client/update-4-2

« back to all changes in this revision

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

  • Committer: Rodney Dawes
  • Date: 2013-01-09 22:23:57 UTC
  • mfrom: (1011.1.1 update-4-2)
  • Revision ID: rodney.dawes@canonical.com-20130109222357-3jqwkv44hzger1vl
[Mike McCracken]

    - Add util func returning translation function for platform/pyversion/frozen status. (LP: #1074116)

[Rodney Dawes]

    - Ignore some new pep8 errors, and update code to fix others.

[Brian Curtin]

    - Use states from networkstates instead of re-defining them locally.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
from ubuntu_sso.utils.webclient import qtnetwork
36
36
 
37
37
 
38
 
class FakeQNetworkProxy(object):
39
 
    """A fake network proxy class."""
40
 
 
41
 
    def __init__(self, called):
42
 
        """Create a new instance."""
43
 
        self.called = called
44
 
 
45
 
    def __call__(self, *args, **kwargs):
46
 
        """Fake construnctor."""
47
 
        return self
48
 
 
49
 
    # pylint: disable=C0103
50
 
    def setApplicationProxy(self, proxy):
51
 
        """Set the application proxy."""
52
 
        self.called.append(('setApplicationProxy', proxy))
53
 
    # pylint: enable=C0103
54
 
 
55
 
 
56
 
class FakeNetworkProxyFactory(object):
57
 
    """A fake network proxy factory."""
58
 
 
59
 
    def __init__(self, called, proxy):
60
 
        """Create a new instance."""
61
 
        self.called = called
62
 
        self.proxy = proxy
63
 
 
64
 
    def __call__(self, *args, **kwargs):
65
 
        """Fake construnctor."""
66
 
        return self
67
 
 
68
 
    # pylint: disable=C0103
69
 
    def systemProxyForQuery(self, query):
70
 
        """Get the system proxy."""
71
 
        self.called.append(('systemProxyForQuery', query))
72
 
        return [self.proxy]
73
 
    # pylint: enable=C0103
74
 
 
75
 
 
76
38
class SetupProxyTestCase(TestCase):
77
39
    """Test the proxy setup."""
78
40
 
81
43
        """Set the different tests."""
82
44
        yield super(SetupProxyTestCase, self).setUp()
83
45
        self.called = []
84
 
        self.proxy = 'fake_proxy'
85
 
 
86
 
        self.network_proxy = FakeQNetworkProxy(self.called)
87
 
        self.patch(qtnetwork, 'QNetworkProxy', self.network_proxy)
88
 
 
89
 
        self.network_proxy_factory = FakeNetworkProxyFactory(self.called,
90
 
                                                              self.proxy)
91
 
        self.patch(qtnetwork, 'QNetworkProxyFactory',
92
 
                                              self.network_proxy_factory)
93
46
 
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)
97
50
 
98
 
        def fake_build_proxy(settings):
99
 
            """Fake build proxy."""
 
51
        self.proxy = None
 
52
 
 
53
        self.real_build_proxy = qtnetwork.build_proxy
 
54
 
 
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)
101
59
            return self.proxy
102
60
 
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))
 
62
 
107
63
        self.addCleanup(self._clean_webclient_instance)
108
64
 
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)
136
90
 
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)
146
98
 
147
99
 
148
 
class SetupWindowsProxyTestCase(SetupProxyTestCase):
149
 
    """Test setting up the proxy."""
150
 
 
151
 
    @defer.inlineCallbacks
152
 
    def setUp(self):
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)
158
 
        self.query = 'query'
159
 
 
160
 
        self.patch(qtnetwork, 'QNetworkProxyQuery', lambda _: self.query)
161
 
 
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)
172
 
 
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)
180
 
 
181
 
 
182
100
class FakeQNetworkReply(object):
183
101
    """A fake QNetworkReply."""
184
102
 
202
120
        client = qtnetwork.WebClient()
203
121
        reply = FakeQNetworkReply()
204
122
        errors = [QSslError()]
205
 
        # pylint: disable=W0212
206
123
        client._handle_ssl_errors(reply, errors)
207
 
        # pylint: enable=W0212
208
124
        self.assertTrue(type(result[0]), unicode)