~thisfred/+junk/ubuntuone-couch-0.1.0

« back to all changes in this revision

Viewing changes to ubuntuone/tests/test_auth.py

  • Committer: Tarmac
  • Author(s): eric.casteleijn at canonical
  • Date: 2011-02-28 15:46:15 UTC
  • mfrom: (2.1.15 testing-infrastructure)
  • Revision ID: tarmac-20110228154615-ah05l2nji40t5km9
Brought test coverage up to 97%, added setup.py, fixed so both scripts work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2011 Canonical Ltd.
 
2
#
 
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.
 
6
#
 
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.
 
11
#
 
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/>.
 
15
 
 
16
"""Tests for ubuntuone.couch.sign."""
 
17
 
 
18
import ubuntu_sso
 
19
 
 
20
from twisted.trial.unittest import TestCase
 
21
from mocker import Mocker, ANY
 
22
from ubuntuone.couch import auth
 
23
from oauth import oauth
 
24
 
 
25
from ubuntuone.couch.auth import (
 
26
    APP_NAME, HMAC_SHA1, get_oauth_request_header, get_oauth_credentials,
 
27
    get_oauth_token, request)
 
28
 
 
29
CONSUMER_KEY = u'this_consumer_key'
 
30
CONSUMER_SECRET = u'sssssh!'
 
31
TOKEN_KEY = u'tokentokentoken'
 
32
TOKEN_SECRET = u'ssssssshhhhhh!'
 
33
 
 
34
CONSUMER = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET)
 
35
TOKEN = oauth.OAuthToken(TOKEN_KEY, TOKEN_SECRET)
 
36
 
 
37
URL = 'https://example.com'
 
38
 
 
39
 
 
40
class QueryTestCase(TestCase):
 
41
    """Test case for u1couch.query."""
 
42
 
 
43
    def setUp(self):
 
44
        self.mocker = Mocker()
 
45
 
 
46
    def tearDown(self):
 
47
        self.mocker.restore()
 
48
        self.mocker.verify()
 
49
 
 
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)
 
71
        self.mocker.replay()
 
72
        headers = get_oauth_request_header(CONSUMER, TOKEN, URL)
 
73
        self.assertEquals(fake_headers, headers)
 
74
 
 
75
    def test_get_oauth_request_header_http(self):
 
76
        """Test get_oauth_request_header fails on http urls."""
 
77
        self.assertRaises(
 
78
            AssertionError, get_oauth_request_header, CONSUMER,
 
79
            TOKEN, 'http://example.com')
 
80
 
 
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()
 
87
        bus.get_object(
 
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)
 
93
        self.mocker.result({
 
94
            u'token': TOKEN_KEY,
 
95
            u'token_secret': TOKEN_SECRET,
 
96
            u'consumer_secret': CONSUMER_SECRET,
 
97
            u'consumer_key': CONSUMER_KEY})
 
98
        self.mocker.replay()
 
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'])
 
104
 
 
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)
 
110
 
 
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()
 
116
        self.mocker.result({
 
117
            u'token': TOKEN_KEY,
 
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
 
122
        http = Http()
 
123
        http.request(
 
124
            'https://something.one.ubuntu.com/', method='GET', headers=ANY,
 
125
            body=None)
 
126
        fake_result = ({'status': '200'}, "Totally A-OK!")
 
127
        self.mocker.result(fake_result)
 
128
        self.mocker.replay()
 
129
        result = request('https://something.one.ubuntu.com/')
 
130
        self.assertEquals(fake_result, result)
 
131
 
 
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()
 
137
        self.mocker.result({
 
138
            u'token': TOKEN_KEY,
 
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
 
143
        http = Http()
 
144
        http.request(
 
145
            'https://something.one.ubuntu.com/', method='GET', headers=ANY,
 
146
            body=None)
 
147
        fake_result = ({'status': '200'}, "Totally A-OK!")
 
148
        self.mocker.result(fake_result)
 
149
        self.mocker.replay()
 
150
        result = request(
 
151
            'https://something.one.ubuntu.com/', sigmeth='PLAINTEXT')
 
152
        self.assertEquals(fake_result, result)