Tweak the computation of seconds from a datetime.timedelta object, so

that we consider only the whole seconds.
This commit is contained in:
Orest Bolohan
2014-06-03 10:39:19 -07:00
parent e0c51b98dd
commit 4bcd15ed0e
3 changed files with 17 additions and 15 deletions

View File

@@ -634,9 +634,9 @@ class OAuth2Credentials(Credentials):
now = datetime.datetime.utcnow()
if self.token_expiry > now:
time_delta = self.token_expiry - now
return int(round(time_delta.days * 86400.0 +
time_delta.seconds +
time_delta.microseconds * 0.000001))
# TODO(orestica): return time_delta.total_seconds()
# once dropping support for Python 2.6
return time_delta.days * 86400 + time_delta.seconds
else:
return 0

View File

@@ -583,8 +583,9 @@ class BasicCredentialsTests(unittest.TestCase):
self.assertEqual('foobar', instance.token_response)
def test_get_access_token(self):
token_response_first = {'access_token': 'first_token', 'expires_in': 1}
token_response_second = {'access_token': 'second_token', 'expires_in': 1}
S = 2 # number of seconds in which the token expires
token_response_first = {'access_token': 'first_token', 'expires_in': S}
token_response_second = {'access_token': 'second_token', 'expires_in': S}
http = HttpMockSequence([
({'status': '200'}, simplejson.dumps(token_response_first)),
({'status': '200'}, simplejson.dumps(token_response_second)),
@@ -592,22 +593,22 @@ class BasicCredentialsTests(unittest.TestCase):
token = self.credentials.get_access_token(http=http)
self.assertEqual('first_token', token.access_token)
self.assertEqual(1, token.expires_in)
self.assertEqual(S - 1, token.expires_in)
self.assertFalse(self.credentials.access_token_expired)
self.assertEqual(token_response_first, self.credentials.token_response)
token = self.credentials.get_access_token(http=http)
self.assertEqual('first_token', token.access_token)
self.assertEqual(1, token.expires_in)
self.assertEqual(S - 1, token.expires_in)
self.assertFalse(self.credentials.access_token_expired)
self.assertEqual(token_response_first, self.credentials.token_response)
time.sleep(1)
time.sleep(S)
self.assertTrue(self.credentials.access_token_expired)
token = self.credentials.get_access_token(http=http)
self.assertEqual('second_token', token.access_token)
self.assertEqual(1, token.expires_in)
self.assertEqual(S - 1, token.expires_in)
self.assertFalse(self.credentials.access_token_expired)
self.assertEqual(token_response_second, self.credentials.token_response)

View File

@@ -94,8 +94,9 @@ class ServiceAccountCredentialsTests(unittest.TestCase):
self.assertEqual('dummy_scope', new_credentials._scopes)
def test_access_token(self):
token_response_first = {'access_token': 'first_token', 'expires_in': 1}
token_response_second = {'access_token': 'second_token', 'expires_in': 1}
S = 2 # number of seconds in which the token expires
token_response_first = {'access_token': 'first_token', 'expires_in': S}
token_response_second = {'access_token': 'second_token', 'expires_in': S}
http = HttpMockSequence([
({'status': '200'}, simplejson.dumps(token_response_first)),
({'status': '200'}, simplejson.dumps(token_response_second)),
@@ -103,21 +104,21 @@ class ServiceAccountCredentialsTests(unittest.TestCase):
token = self.credentials.get_access_token(http=http)
self.assertEqual('first_token', token.access_token)
self.assertEqual(1, token.expires_in)
self.assertEqual(S - 1, token.expires_in)
self.assertFalse(self.credentials.access_token_expired)
self.assertEqual(token_response_first, self.credentials.token_response)
token = self.credentials.get_access_token(http=http)
self.assertEqual('first_token', token.access_token)
self.assertEqual(1, token.expires_in)
self.assertEqual(S - 1, token.expires_in)
self.assertFalse(self.credentials.access_token_expired)
self.assertEqual(token_response_first, self.credentials.token_response)
time.sleep(1)
time.sleep(S)
self.assertTrue(self.credentials.access_token_expired)
token = self.credentials.get_access_token(http=http)
self.assertEqual('second_token', token.access_token)
self.assertEqual(1, token.expires_in)
self.assertEqual(S - 1, token.expires_in)
self.assertFalse(self.credentials.access_token_expired)
self.assertEqual(token_response_second, self.credentials.token_response)