1
# -*- coding: utf-8 -*-
3
# Authors: Alejandro J. Cura <alecu@canonical.com>
4
# Authors: Natalia B. Bidart <nataliabidart@canonical.com>
6
# Copyright 2010 Canonical Ltd.
8
# This program is free software: you can redistribute it and/or modify it
9
# under the terms of the GNU General Public License version 3, as published
10
# by the Free Software Foundation.
12
# This program is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranties of
14
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15
# PURPOSE. See the GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License along
18
# with this program. If not, see <http://www.gnu.org/licenses/>.
20
"""Integration tests for the control panel backend webservice client."""
22
from twisted.application import internet, service
23
from twisted.internet import defer
24
from twisted.internet.defer import inlineCallbacks
25
from twisted.web import server, resource
27
from ubuntuone.controlpanel import web_client
28
from ubuntuone.controlpanel.tests import TestCase
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",
42
def sample_get_credentials():
43
"""Will return the sample credentials right now."""
44
return defer.succeed(SAMPLE_CREDENTIALS)
47
class MockResource(resource.Resource):
48
"""A simple web resource."""
52
# pylint: disable=C0103
53
# t.w.resource methods have freeform cased names
55
def getChild(self, name, request):
56
"""Get a given child resource."""
59
return resource.Resource.getChild(self, name, request)
61
def render_GET(self, request):
62
"""Make a bit of html out of these resource's content."""
66
class MockWebService(object):
67
"""A mock webservice for testing"""
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)
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()
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
96
#pylint: disable=E1101
97
self.service_collection.stopService()
100
class WebClientTestCase(TestCase):
101
"""Test for the webservice client."""
106
super(WebClientTestCase, self).setUp()
107
self.ws = MockWebService()
108
test_base_url = self.ws.get_url()
109
self.wc = web_client.web_client_factory(sample_get_credentials,
114
super(WebClientTestCase, self).tearDown()
119
def test_get_url(self):
120
"""A method is successfully called in the mock webservice."""
121
result = yield self.wc.call_api("devices")
122
self.assertIn(SAMPLE_KEY, result)
123
self.assertEqual(SAMPLE_VALUE, result[SAMPLE_KEY])
126
def test_get_url_error(self):
127
"""The errback is called when there's some error."""
128
yield self.assertFailure(self.wc.call_api("throwerror"),
129
web_client.WebClientError)
132
def test_unauthorized(self):
133
"""Detect when a request failed with UNAUTHORIZED."""
134
yield self.assertFailure(self.wc.call_api("unauthorized"),
135
web_client.UnauthorizedError)
138
class OAuthTestCase(TestCase):
139
"""Test for the oauth signing code."""
141
def test_build_oauth_headers(self):
142
"""Build the oauth headers for a sample request."""
144
sample_method = "GET"
145
sample_url = "http://one.ubuntu.com/"
146
headers = web_client.build_oauth_headers(sample_method, sample_url,
148
self.assertIn("Authorization", headers)
150
def test_add_oauth_headers(self):
151
"""Add the OAuth headers to a request."""
153
def sample_build_headers(*a):
154
"""Build some sample headers."""
155
return {"header1": "h1", "header2": "h2"}
157
self.patch(web_client, "build_oauth_headers", sample_build_headers)
158
test_request_headers = {}
159
append_method = test_request_headers.__setitem__
160
web_client.add_oauth_headers(append_method, "GET", "http://this", {})
161
self.assertIn("header1", test_request_headers)
162
self.assertIn("header2", test_request_headers)