Merge lp:~thisfred/ubuntuone-couch/lp-781119 into lp:ubuntuone-couch

Proposed by Eric Casteleijn
Status: Merged
Approved by: Eric Casteleijn
Approved revision: 16
Merged at revision: 12
Proposed branch: lp:~thisfred/ubuntuone-couch/lp-781119
Merge into: lp:ubuntuone-couch
Diff against target: 115 lines (+41/-5)
3 files modified
bin/u1oauthrequest (+6/-1)
ubuntuone/couch/auth.py (+24/-3)
ubuntuone/tests/test_auth.py (+11/-1)
To merge this branch: bzr merge lp:~thisfred/ubuntuone-couch/lp-781119
Reviewer Review Type Date Requested Status
dobey (community) Approve
Natalia Bidart (community) Approve
Michael Terry Pending
Review via email: mp+62176@code.launchpad.net

Commit message

When no oauth credentials are passed in, *and* none are present in Ubuntu SSO, show a reasonable error message, rather than a python traceback. (LP: #781119)

Description of the change

When no oauth credentials are passed in, *and* none are present in Ubuntu SSO, show a reasonable error message, rather than a python traceback.

To post a comment you must log in.
Revision history for this message
Michael Terry (mterry) wrote :

Yeah, that looks like nicer behavior, thanks.

Revision history for this message
Natalia Bidart (nataliabidart) :
review: Approve
16. By Eric Casteleijn

error message does not need angle brackets

Revision history for this message
dobey (dobey) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/u1oauthrequest'
2--- bin/u1oauthrequest 2011-02-25 22:02:43 +0000
3+++ bin/u1oauthrequest 2011-05-25 14:06:03 +0000
4@@ -5,6 +5,7 @@
5 from ubuntuone.couch import auth
6
7 import socket
8+import sys
9 socket.setdefaulttimeout(5)
10
11 if __name__ == "__main__":
12@@ -35,9 +36,13 @@
13 (OPTIONS, ARGS) = PARSER.parse_args()
14 if len(ARGS) != 1:
15 PARSER.error("You must specify a url")
16- print auth.request(
17+ RESULT = auth.request(
18 url=ARGS[0], sigmeth=OPTIONS.sigmeth,
19 http_method=OPTIONS.http_method, request_body=OPTIONS.body,
20 access_token=OPTIONS.access_token, token_secret=OPTIONS.token_secret,
21 consumer_key=OPTIONS.consumer_key,
22 consumer_secret=OPTIONS.consumer_secret)
23+ print RESULT
24+ if RESULT == auth.NO_CREDENTIALS:
25+ sys.exit(1)
26+
27
28=== modified file 'ubuntuone/couch/auth.py'
29--- ubuntuone/couch/auth.py 2011-05-23 21:15:37 +0000
30+++ ubuntuone/couch/auth.py 2011-05-25 14:06:03 +0000
31@@ -20,6 +20,7 @@
32 import ubuntu_sso
33 import urlparse
34
35+from gettext import gettext as _
36 from oauth import oauth
37 from dbus.mainloop.glib import DBusGMainLoop
38
39@@ -31,6 +32,17 @@
40 APP_NAME = "Ubuntu One"
41 HMAC_SHA1 = oauth.OAuthSignatureMethod_HMAC_SHA1()
42 PLAINTEXT = oauth.OAuthSignatureMethod_PLAINTEXT()
43+NO_CREDENTIALS = _('No OAuth credentials passed in or found in Ubuntu SSO.')
44+
45+
46+class CredentialsNotFound(Exception):
47+ """Exception for missing data in SSO."""
48+
49+ def __init__(self, key): # pylint: disable=W0231
50+ self.key = key
51+
52+ def __str__(self):
53+ return "Credentials Not Found Error: missing key %s" % self.key
54
55
56 def _undbusify(value):
57@@ -64,8 +76,13 @@
58 if len(oauth_data) > 0:
59 logging.info(
60 'get_oauth_data: Got non emtpy credentials from Ubuntu SSO.')
61- return dict([(key, oauth_data[key]) for key in (
62- 'consumer_key', 'consumer_secret', 'token', 'token_secret')])
63+ credentials = {}
64+ for key in ('consumer_key', 'consumer_secret', 'token', 'token_secret'):
65+ try:
66+ credentials[key] = oauth_data[key]
67+ except KeyError:
68+ raise CredentialsNotFound(key)
69+ return credentials
70
71
72 def get_oauth_token(token, secret):
73@@ -100,7 +117,11 @@
74 else:
75 signature_method = HMAC_SHA1
76 if access_token is None or consumer_key is None:
77- credentials = get_oauth_credentials()
78+ try:
79+ credentials = get_oauth_credentials()
80+ except CredentialsNotFound, e:
81+ logging.error('No credentials found in SSO. %s', e)
82+ return NO_CREDENTIALS
83 access_token = credentials['token']
84 token_secret = credentials['token_secret']
85 consumer_key = credentials['consumer_key']
86
87=== modified file 'ubuntuone/tests/test_auth.py'
88--- ubuntuone/tests/test_auth.py 2011-05-24 17:24:43 +0000
89+++ ubuntuone/tests/test_auth.py 2011-05-25 14:06:03 +0000
90@@ -23,7 +23,7 @@
91
92 from ubuntuone.couch.auth import (
93 APP_NAME, HMAC_SHA1, get_oauth_request_header, get_oauth_credentials,
94- get_oauth_token, request)
95+ get_oauth_token, request, CredentialsNotFound, NO_CREDENTIALS)
96
97 CONSUMER_KEY = u'this_consumer_key'
98 CONSUMER_SECRET = u'sssssh!'
99@@ -128,6 +128,16 @@
100 result = request('https://something.one.ubuntu.com/')
101 self.assertEquals(fake_result, result)
102
103+ def test_request_no_oauth_credentials(self):
104+ """Test a request with no oauth credentials in SSO."""
105+ mock_get_oauth_credentials = self.mocker.replace(
106+ 'ubuntuone.couch.auth.get_oauth_credentials')
107+ mock_get_oauth_credentials()
108+ self.mocker.throw(CredentialsNotFound('consumer_key'))
109+ self.mocker.replay()
110+ result = request('https://something.one.ubuntu.com/')
111+ self.assertEquals(NO_CREDENTIALS, result)
112+
113 def test_request_plaintext(self):
114 """Test a full request."""
115 mock_get_oauth_credentials = self.mocker.replace(

Subscribers

People subscribed via source and target branches