From 4b4002fc28101c8c906bad8ddf88e1503c154b29 Mon Sep 17 00:00:00 2001 From: Joe Gregorio Date: Thu, 14 Jun 2012 15:41:01 -0400 Subject: [PATCH] [mq]: smallfixes --- apiclient/discovery.py | 2 ++ oauth2client/client.py | 18 ++++++++++++++++-- tests/test_discovery.py | 10 ++++++++++ tests/test_oauth2client.py | 13 +++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/apiclient/discovery.py b/apiclient/discovery.py index c54b57f..3e1964c 100644 --- a/apiclient/discovery.py +++ b/apiclient/discovery.py @@ -267,6 +267,8 @@ def _cast(value, schema_type): if type(value) == type('') or type(value) == type(u''): return value else: + if value is None: + raise ValueError('String parameters can not be None.') return str(value) elif schema_type == 'integer': return str(int(value)) diff --git a/oauth2client/client.py b/oauth2client/client.py index 5d92b86..d8eaf69 100644 --- a/oauth2client/client.py +++ b/oauth2client/client.py @@ -593,7 +593,7 @@ class OAuth2Credentials(Credentials): body = self._generate_refresh_request_body() headers = self._generate_refresh_request_headers() - logger.info('Refresing access_token') + logger.info('Refreshing access_token') resp, content = http_request( self.token_uri, method='POST', body=body, headers=headers) if resp.status == 200: @@ -1028,10 +1028,24 @@ class OAuth2WebServerFlow(Flow): of the query parameters to the redirect_uri, which contains the code. http: httplib2.Http, optional http instance to use to do the fetch + + Returns: + An OAuth2Credentials object that can be used to authorize requests. + + Raises: + FlowExchangeError if a problem occured exchanging the code for a + refresh_token. """ if not (isinstance(code, str) or isinstance(code, unicode)): - code = code['code'] + if 'code' not in code: + if 'error' in code: + error_msg = code['error'] + else: + error_msg = 'No code was supplied in the query parameters.' + raise FlowExchangeError(error_msg) + else: + code = code['code'] body = urllib.urlencode({ 'grant_type': 'authorization_code', diff --git a/tests/test_discovery.py b/tests/test_discovery.py index 5970970..91db712 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -193,6 +193,16 @@ class Discovery(unittest.TestCase): self.assertEqual(q['trace'], ['html']) self.assertEqual(q['fields'], ['description']) + def test_string_params_none_is_invalid(self): + http = HttpMock(datafile('zoo.json'), {'status': '200'}) + zoo = build('zoo', 'v1', http) + # String isn't None + try: + request = zoo.query(trace=None, fields='description') + self.fail() + except ValueError, e: + self.assertTrue('None' in str(e)) + def test_model_added_query_parameters(self): http = HttpMock(datafile('zoo.json'), {'status': '200'}) zoo = build('zoo', 'v1', http) diff --git a/tests/test_oauth2client.py b/tests/test_oauth2client.py index cb1b4b3..eb2d7db 100644 --- a/tests/test_oauth2client.py +++ b/tests/test_oauth2client.py @@ -279,6 +279,19 @@ class OAuth2WebServerFlowTest(unittest.TestCase): credentials = self.flow.step2_exchange('some random code', http) self.assertEqual(None, credentials.token_expiry) + def test_exchange_fails_if_no_code(self): + http = HttpMockSequence([ + ({'status': '200'}, """{ "access_token":"SlAV32hkKG", + "refresh_token":"8xLOxBtZp8" }"""), + ]) + + code = {'error': 'thou shall not pass'} + try: + credentials = self.flow.step2_exchange(code, http) + self.fail('should raise exception if no code in dictionary.') + except FlowExchangeError, e: + self.assertTrue('shall not pass' in str(e)) + def test_exchange_id_token_fail(self): http = HttpMockSequence([ ({'status': '200'}, """{ "access_token":"SlAV32hkKG",