def setUp(self): self.cache = oauth2_client.FileSystemTokenCache() self.start_time = datetime.datetime(2011, 3, 1, 10, 25, 13, 300826) self.token_1 = oauth2_client.AccessToken('token1', self.start_time) self.token_2 = oauth2_client.AccessToken( 'token2', self.start_time + datetime.timedelta(seconds=492)) self.key = 'token1key'
def testShouldRefresh(self): mock_datetime = MockDateTime() start = datetime.datetime(2011, 3, 1, 11, 25, 13, 300826) expiry = start + datetime.timedelta(minutes=60) token = oauth2_client.AccessToken('foo', expiry, datetime_strategy=mock_datetime) mock_datetime.mock_now = start self.assertFalse(token.ShouldRefresh()) mock_datetime.mock_now = start + datetime.timedelta(minutes=54) self.assertFalse(token.ShouldRefresh()) mock_datetime.mock_now = start + datetime.timedelta(minutes=55) self.assertFalse(token.ShouldRefresh()) mock_datetime.mock_now = start + datetime.timedelta(minutes=55, seconds=1) self.assertTrue(token.ShouldRefresh()) mock_datetime.mock_now = start + datetime.timedelta(minutes=61) self.assertTrue(token.ShouldRefresh()) mock_datetime.mock_now = start + datetime.timedelta(minutes=58) self.assertFalse(token.ShouldRefresh(time_delta=120)) mock_datetime.mock_now = start + datetime.timedelta(minutes=58, seconds=1) self.assertTrue(token.ShouldRefresh(time_delta=120))
def testSerialization(self): expiry = datetime.datetime(2011, 3, 1, 11, 25, 13, 300826) token = oauth2_client.AccessToken('foo', expiry) serialized_token = token.Serialize() LOG.debug('testSerialization: serialized_token=%s' % serialized_token) token2 = oauth2_client.AccessToken.UnSerialize(serialized_token) self.assertEquals(token, token2)
def testShouldRefreshNoExpiry(self): mock_datetime = MockDateTime() start = datetime.datetime(2011, 3, 1, 11, 25, 13, 300826) token = oauth2_client.AccessToken('foo', None, datetime_strategy=mock_datetime) mock_datetime.mock_now = start self.assertFalse(token.ShouldRefresh()) mock_datetime.mock_now = start + datetime.timedelta(minutes=472) self.assertFalse(token.ShouldRefresh())
def FetchAccessToken(self): self.fetched_token = True return oauth2_client.AccessToken( ACCESS_TOKEN, GetExpiry(self.datetime_strategy, 3600), datetime_strategy=self.datetime_strategy)