~ubuntu-branches/ubuntu/oneiric/ubuntuone-control-panel/oneiric

« back to all changes in this revision

Viewing changes to ubuntuone/controlpanel/web_client/tests/test_txwebclient.py

* New upstream release:
  [ Alejandro J. Cura <alecu@canonical.com>]
    - Do not throw a webclient error when closing
      (LP: #845105).
  [ Natalia B. Bidart <natalia.bidart@canonical.com> ]
    - Removed all code related to Bookmarks (LP: #850142).
    - Replaces references to "Evolution" by "Thunderbird" (LP: #849494).
  [ Rodney Dawes <rodney.dawes@canonical.com> ]
    - Don't install a .desktop file for control panel
      (part of LP: #838778).
    - Point the indicator/Unity API at the installer .desktop file
      (part of LP: #838778).
    - Set the WMCLASS so Unity will fall back properly
      (part of LP: #838778).
    - Fix a few grammar mistakes (LP: #835093).
    - Don't show the "Get NGB free!" label on "Join now" button at all
      (LP: #819955).
* debian/control:
  - ubuntuone-control-panel-gtk depends now on ubuntuone-installer >= 2.0.0.
  - require ubuntuone-client >= 2.0.0.
  - require ubuntu-sso-client >= 1.4.0.
  - no longer install a .desktop file (will be installed by ubuntuone-installer).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
# Authors: Alejandro J. Cura <alecu@canonical.com>
 
4
#
 
5
# Copyright 2011 Canonical Ltd.
 
6
#
 
7
# This program is free software: you can redistribute it and/or modify it
 
8
# under the terms of the GNU General Public License version 3, as published
 
9
# by the Free Software Foundation.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but
 
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
14
# PURPOSE.  See the GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along
 
17
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
"""Unit tests for the control panel backend twisted webservice client."""
 
20
 
 
21
from twisted.application import internet, service
 
22
from twisted.internet import defer, reactor
 
23
from twisted.internet.defer import inlineCallbacks
 
24
from twisted.web import server, resource
 
25
from ubuntuone.devtools.testcase import skipIfOS
 
26
 
 
27
from ubuntuone.controlpanel.web_client import txwebclient
 
28
from ubuntuone.controlpanel.tests import TestCase
 
29
 
 
30
 
 
31
SAMPLE_KEY = "result"
 
32
SAMPLE_VALUE = "sample result"
 
33
SAMPLE_RESOURCE = '{"%s": "%s"}' % (SAMPLE_KEY, SAMPLE_VALUE)
 
34
SAMPLE_CREDENTIALS = dict(
 
35
    consumer_key="consumer key",
 
36
    consumer_secret="consumer secret",
 
37
    token="the real token",
 
38
    token_secret="the token secret",
 
39
)
 
40
 
 
41
 
 
42
def sample_get_credentials():
 
43
    """Will return the sample credentials right now."""
 
44
    return defer.succeed(SAMPLE_CREDENTIALS)
 
45
 
 
46
 
 
47
class MockResource(resource.Resource):
 
48
    """A simple web resource."""
 
49
    isLeaf = True
 
50
    contents = ""
 
51
 
 
52
    # pylint: disable=C0103
 
53
    # t.w.resource methods have freeform cased names
 
54
 
 
55
    def getChild(self, name, request):
 
56
        """Get a given child resource."""
 
57
        if name == '':
 
58
            return self
 
59
        return resource.Resource.getChild(self, name, request)
 
60
 
 
61
    def render_GET(self, request):
 
62
        """Make a bit of html out of these resource's content."""
 
63
        return self.contents
 
64
 
 
65
 
 
66
class MockWebService(object):
 
67
    """A mock webservice for testing"""
 
68
 
 
69
    def __init__(self):
 
70
        """Start up this instance."""
 
71
        root = resource.Resource()
 
72
        devices_resource = MockResource()
 
73
        devices_resource.contents = SAMPLE_RESOURCE
 
74
        root.putChild("devices", devices_resource)
 
75
        root.putChild("throwerror", resource.NoResource())
 
76
        unauthorized = resource.ErrorPage(resource.http.UNAUTHORIZED,
 
77
                                          "Unauthrorized", "Unauthrorized")
 
78
        root.putChild("unauthorized", unauthorized)
 
79
 
 
80
        site = server.Site(root)
 
81
        application = service.Application('web')
 
82
        self.service_collection = service.IServiceCollection(application)
 
83
        #pylint: disable=E1101
 
84
        self.tcpserver = internet.TCPServer(0, site)
 
85
        self.tcpserver.setServiceParent(self.service_collection)
 
86
        self.service_collection.startService()
 
87
 
 
88
    def get_url(self):
 
89
        """Build the url for this mock server."""
 
90
        #pylint: disable=W0212
 
91
        port_num = self.tcpserver._port.getHost().port
 
92
        return "http://localhost:%d/" % port_num
 
93
 
 
94
    def stop(self):
 
95
        """Shut it down."""
 
96
        #pylint: disable=E1101
 
97
        self.service_collection.stopService()
 
98
 
 
99
 
 
100
class WebClientTestCase(TestCase):
 
101
    """Test for the webservice client."""
 
102
 
 
103
    timeout = 8
 
104
 
 
105
    def setUp(self):
 
106
        super(WebClientTestCase, self).setUp()
 
107
        self.ws = MockWebService()
 
108
        test_base_url = self.ws.get_url()
 
109
        self.wc = txwebclient.WebClient(sample_get_credentials, test_base_url)
 
110
 
 
111
    @inlineCallbacks
 
112
    def tearDown(self):
 
113
        super(WebClientTestCase, self).tearDown()
 
114
        yield self.ws.stop()
 
115
        self.wc.shutdown()
 
116
 
 
117
    @inlineCallbacks
 
118
    def test_get_url(self):
 
119
        """A method is successfully called in the mock webservice."""
 
120
        result = yield self.wc.call_api("devices")
 
121
        self.assertIn(SAMPLE_KEY, result)
 
122
        self.assertEqual(SAMPLE_VALUE, result[SAMPLE_KEY])
 
123
 
 
124
    @inlineCallbacks
 
125
    def test_get_url_error(self):
 
126
        """The errback is called when there's some error."""
 
127
        yield self.assertFailure(self.wc.call_api("throwerror"),
 
128
                                 txwebclient.WebClientError)
 
129
 
 
130
    @inlineCallbacks
 
131
    def test_unauthorized(self):
 
132
        """Detect when a request failed with UNAUTHORIZED."""
 
133
        yield self.assertFailure(self.wc.call_api("unauthorized"),
 
134
                                 txwebclient.UnauthorizedError)
 
135
 
 
136
 
 
137
class WebClientShutdownTestCase(TestCase):
 
138
    """The webclient behaviour during shutdown."""
 
139
 
 
140
    @skipIfOS('win32', 'Failing on windows, see LP: #851158.')
 
141
    @inlineCallbacks
 
142
    def test_shutdown(self):
 
143
        """The webclient behaves well during shutdown."""
 
144
        d3 = defer.Deferred()
 
145
        # pylint: disable=E1101
 
146
        reactor.callLater(1, d3.callback, None)
 
147
        ws = MockWebService()
 
148
        test_base_url = ws.get_url()
 
149
        wc = txwebclient.WebClient(sample_get_credentials, test_base_url)
 
150
        d1 = wc.call_api("throwerror")
 
151
        d2 = ws.stop()
 
152
        wc.shutdown()
 
153
        yield d2
 
154
        yield defer.DeferredList([d1, d3], fireOnOneCallback=True,
 
155
                                 fireOnOneErrback=True)