Token refresh to work with 'old' GData API

Contributed by crhyme.

Reviewed in http://codereview.appspot.com/6373049/.
This commit is contained in:
Joe Gregorio
2012-07-16 16:31:01 -04:00
parent c29aaa9f8e
commit 7c7c6b122f
2 changed files with 40 additions and 33 deletions

View File

@@ -416,8 +416,9 @@ class OAuth2Credentials(Credentials):
resp, content = request_orig(uri, method, body, headers, resp, content = request_orig(uri, method, body, headers,
redirections, connection_type) redirections, connection_type)
if resp.status == 401: # Older API (GData) respond with 403
logger.info('Refreshing due to a 401') if resp.status in [401, 403]:
logger.info('Refreshing due to a %s' % str(resp.status))
self._refresh(request_orig) self._refresh(request_orig)
self.apply(headers) self.apply(headers)
return request_orig(uri, method, body, headers, return request_orig(uri, method, body, headers,

View File

@@ -99,28 +99,32 @@ class OAuth2CredentialsTests(unittest.TestCase):
user_agent) user_agent)
def test_token_refresh_success(self): def test_token_refresh_success(self):
http = HttpMockSequence([ # Older API (GData) respond with 403
({'status': '401'}, ''), for status_code in ['401', '403']:
({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), http = HttpMockSequence([
({'status': '200'}, 'echo_request_headers'), ({'status': status_code}, ''),
]) ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'),
http = self.credentials.authorize(http) ({'status': '200'}, 'echo_request_headers'),
resp, content = http.request("http://example.com") ])
self.assertEqual('Bearer 1/3w', content['Authorization']) http = self.credentials.authorize(http)
self.assertFalse(self.credentials.access_token_expired) resp, content = http.request("http://example.com")
self.assertEqual('Bearer 1/3w', content['Authorization'])
self.assertFalse(self.credentials.access_token_expired)
def test_token_refresh_failure(self): def test_token_refresh_failure(self):
http = HttpMockSequence([ # Older API (GData) respond with 403
({'status': '401'}, ''), for status_code in ['401', '403']:
({'status': '400'}, '{"error":"access_denied"}'), http = HttpMockSequence([
]) ({'status': status_code}, ''),
http = self.credentials.authorize(http) ({'status': '400'}, '{"error":"access_denied"}'),
try: ])
http.request("http://example.com") http = self.credentials.authorize(http)
self.fail("should raise AccessTokenRefreshError exception") try:
except AccessTokenRefreshError: http.request("http://example.com")
pass self.fail("should raise AccessTokenRefreshError exception")
self.assertTrue(self.credentials.access_token_expired) except AccessTokenRefreshError:
pass
self.assertTrue(self.credentials.access_token_expired)
def test_non_401_error_response(self): def test_non_401_error_response(self):
http = HttpMockSequence([ http = HttpMockSequence([
@@ -148,17 +152,19 @@ class AccessTokenCredentialsTests(unittest.TestCase):
self.credentials = AccessTokenCredentials(access_token, user_agent) self.credentials = AccessTokenCredentials(access_token, user_agent)
def test_token_refresh_success(self): def test_token_refresh_success(self):
http = HttpMockSequence([ # Older API (GData) respond with 403
({'status': '401'}, ''), for status_code in ['401', '403']:
]) http = HttpMockSequence([
http = self.credentials.authorize(http) ({'status': status_code}, ''),
try: ])
resp, content = http.request("http://example.com") http = self.credentials.authorize(http)
self.fail("should throw exception if token expires") try:
except AccessTokenCredentialsError: resp, content = http.request("http://example.com")
pass self.fail("should throw exception if token expires")
except Exception: except AccessTokenCredentialsError:
self.fail("should only throw AccessTokenCredentialsError") pass
except Exception:
self.fail("should only throw AccessTokenCredentialsError")
def test_non_401_error_response(self): def test_non_401_error_response(self):
http = HttpMockSequence([ http = HttpMockSequence([