1
# Copyright 2011 Canonical Ltd.
3
# Ubuntu One Couch is free software: you can redistribute it and/or
4
# modify it under the terms of the GNU Lesser General Public License
5
# version 3 as published by the Free Software Foundation.
7
# Ubuntu One Couch is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10
# Lesser General Public License for more details.
12
# You should have received a copy of the GNU Lesser General Public
13
# License along with desktopcouch. If not, see
14
# <http://www.gnu.org/licenses/>.
16
"""Tests for ubuntuone.couch.sign."""
20
from twisted.trial.unittest import TestCase
21
from mocker import Mocker, ANY
22
from ubuntuone.couch import auth
23
from oauth import oauth
25
from ubuntuone.couch.auth import (
26
APP_NAME, HMAC_SHA1, get_oauth_request_header, get_oauth_credentials,
27
get_oauth_token, request)
29
CONSUMER_KEY = u'this_consumer_key'
30
CONSUMER_SECRET = u'sssssh!'
31
TOKEN_KEY = u'tokentokentoken'
32
TOKEN_SECRET = u'ssssssshhhhhh!'
34
CONSUMER = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET)
35
TOKEN = oauth.OAuthToken(TOKEN_KEY, TOKEN_SECRET)
37
URL = 'https://example.com'
40
class QueryTestCase(TestCase):
41
"""Test case for u1couch.query."""
44
self.mocker = Mocker()
50
def test_get_oauth_request_header(self):
51
"""Test get_oauth_request_header returns correct headers."""
52
fake_headers = {'Authorization':
53
'OAuth realm="", oauth_nonce="39941541", '
54
'oauth_timestamp="1297958903", '
55
'oauth_consumer_key="this_consumer_key", '
56
'oauth_signature_method="HMAC-SHA1", oauth_version="1.0", '
57
'oauth_token="tokentokentoken", '
58
'oauth_signature="TNIfersCweWluuuJW%2FT%2FbW9IHD0%3D"'}
59
mock_oauth = self.mocker.replace("oauth.oauth")
60
mock_oauth.OAuthRequest # pylint: disable=W0104
61
MockOAuthRequest = self.mocker.mock() # pylint: disable=C0103
62
self.mocker.result(MockOAuthRequest)
63
MockOAuthRequest.from_consumer_and_token(
64
http_url=URL, http_method='GET', oauth_consumer=CONSUMER,
65
token=TOKEN, parameters=None)
66
oauth_request = self.mocker.mock()
67
self.mocker.result(oauth_request)
68
oauth_request.sign_request(HMAC_SHA1, CONSUMER, TOKEN)
69
oauth_request.to_header()
70
self.mocker.result(fake_headers)
72
headers = get_oauth_request_header(CONSUMER, TOKEN, URL)
73
self.assertEquals(fake_headers, headers)
75
def test_get_oauth_request_header_http(self):
76
"""Test get_oauth_request_header fails on http urls."""
78
AssertionError, get_oauth_request_header, CONSUMER,
79
TOKEN, 'http://example.com')
81
def test_get_oauth_credentials(self):
82
"""Test get_oauth_credentials returns proper oauth data."""
83
self.patch(auth, "_undbusify", lambda x: x)
84
dbus = self.mocker.replace("dbus")
85
self.mocker.replace("dbus.mainloop.glib.DBusGMainLoop")
86
bus = dbus.SessionBus()
88
ubuntu_sso.DBUS_BUS_NAME, ubuntu_sso.DBUS_CRED_PATH,
89
follow_name_owner_changes=True)
90
mock_proxy = self.mocker.mock()
91
self.mocker.result(mock_proxy)
92
mock_proxy.find_credentials(APP_NAME)
95
u'token_secret': TOKEN_SECRET,
96
u'consumer_secret': CONSUMER_SECRET,
97
u'consumer_key': CONSUMER_KEY})
99
oauth_data = get_oauth_credentials()
100
self.assertEquals(TOKEN_KEY, oauth_data['token'])
101
self.assertEquals(TOKEN_SECRET, oauth_data['token_secret'])
102
self.assertEquals(CONSUMER_KEY, oauth_data['consumer_key'])
103
self.assertEquals(CONSUMER_SECRET, oauth_data['consumer_secret'])
105
def test_get_oauth_token(self):
106
"""Test get_oauth_token turns strings into proper OAuth."""
107
token = get_oauth_token(TOKEN_KEY, TOKEN_SECRET)
108
self.assertEquals(TOKEN_KEY, token.key)
109
self.assertEquals(TOKEN_SECRET, token.secret)
111
def test_request(self):
112
"""Test a full request."""
113
mock_get_oauth_credentials = self.mocker.replace(
114
'ubuntuone.couch.auth.get_oauth_credentials')
115
mock_get_oauth_credentials()
118
u'token_secret': TOKEN_SECRET,
119
u'consumer_key': CONSUMER_KEY,
120
u'consumer_secret': CONSUMER_SECRET})
121
Http = self.mocker.replace("httplib2.Http") # pylint: disable=C0103
124
'https://something.one.ubuntu.com/', method='GET', headers=ANY,
126
fake_result = ({'status': '200'}, "Totally A-OK!")
127
self.mocker.result(fake_result)
129
result = request('https://something.one.ubuntu.com/')
130
self.assertEquals(fake_result, result)
132
def test_request_plaintext(self):
133
"""Test a full request."""
134
mock_get_oauth_credentials = self.mocker.replace(
135
'ubuntuone.couch.auth.get_oauth_credentials')
136
mock_get_oauth_credentials()
139
u'token_secret': TOKEN_SECRET,
140
u'consumer_key': CONSUMER_KEY,
141
u'consumer_secret': CONSUMER_SECRET})
142
Http = self.mocker.replace("httplib2.Http") # pylint: disable=C0103
145
'https://something.one.ubuntu.com/', method='GET', headers=ANY,
147
fake_result = ({'status': '200'}, "Totally A-OK!")
148
self.mocker.result(fake_result)
151
'https://something.one.ubuntu.com/', sigmeth='PLAINTEXT')
152
self.assertEquals(fake_result, result)