1
# Copyright 2009-2010 Canonical Ltd.
3
# This program is free software: you can redistribute it and/or modify it
4
# under the terms of the GNU General Public License version 3, as published
5
# by the Free Software Foundation.
7
# This program is distributed in the hope that it will be useful, but
8
# WITHOUT ANY WARRANTY; without even the implied warranties of
9
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
10
# PURPOSE. See the GNU General Public License for more details.
12
# You should have received a copy of the GNU General Public License along
13
# with this program. If not, see <http://www.gnu.org/licenses/>.
16
from twisted.trial.unittest import TestCase
17
from ubuntuone.api.restclient import RestClient
18
from oauth import oauth
20
class FakedResponse(object):
21
"""A fake response."""
23
def __init__(self, *args, **kwargs):
26
self.read = lambda: '{"field": "value"}'
28
def return_a_fake_response(*args, **kwargs):
29
"""Create a fake urllib2 response."""
30
return FakedResponse(*args, **kwargs)
32
class RestClientTest(TestCase):
33
"""Test the RestClient object."""
36
"""Tests calling a REST API method."""
37
self.patch(urllib2, 'urlopen', return_a_fake_response)
39
url = 'https://one.ubuntu.com/api'
40
rest_client = RestClient(url)
41
result = rest_client.call(url, "https",
42
oauth.OAuthConsumer('ubuntuone', 'hammertime'),
43
oauth.OAuthToken.from_string('oauth_token_secret=12345678ABC&oauth_token=12345678ABC'))
44
self.assertTrue(result.has_key('field'))
45
self.assertTrue(result['field'] == 'value')