def test_storage_should_respect_datetime_format(self): token = Token(access_token='access_token', expires_on=Token.calc_expires_on(10)) token_storage = TokenStorage(self.storage_obj) token_storage(token) expires = token_storage.request_token()[TOKEN_EXPIRES] expected = datetime(2015, 1, 1, 10, 12, 20, microsecond=0) self.assertEqual(expires, expected)
def test_storage_should_add_token(self): token = Token(access_token='access_token', expires_on=Token.calc_expires_on(10)) token_storage = TokenStorage(self.storage_obj) token_storage(token) self.assertEqual(self.storage_obj.get(TOKEN_KEY), 'access_token') expires = str(token.expires_on) self.assertEqual(self.storage_obj.get(TOKEN_EXPIRES), expires, self.storage_obj)
def test_storage_should_add_retrieve_token(self): token = Token(access_token='access_token', expires_on=Token.calc_expires_on(10)) expires = str(token.expires_on) token_storage = TokenStorage(self.storage_obj) token_storage(token) token_requested = token_storage.request_token() self.assertEqual(len(token_requested), 2) self.assertEqual(self.storage_obj.get(token_storage.token_key), 'access_token') self.assertEqual(self.storage_obj.get(token_storage.expires_key), expires, self.storage_obj)
def test_storage_should_add_token(self): token = Token(access_token='access_token', expires_on=Token.calc_expires_on(10)) token_storage = TokenStorage(self.storage_obj, 'test') token_storage(token) self.assertEqual(self.storage_obj.get(token_storage.token_key), 'access_token') expires = str(token.expires_on) self.assertEqual(self.storage_obj.get(token_storage.expires_key), expires, self.storage_obj) self.assertEqual('test_access_token', token_storage.token_key) self.assertEqual('test_expires_on', token_storage.expires_key)
def test_update_token_should_set_a_token_with_data_retrieved_from_storage( self, _request_token): expires = Token.calc_expires_on(100) _request_token.return_value = dict() self.manager._token = Token('new_access_token', expires_on=expires) self.manager._token_storage(self.manager._token) self.manager._update_token() self.assertFalse(_request_token.called) self.assertEqual(self.manager._token.access_token, 'new_access_token') self.assertEqual(self.manager._token.expires_on, expires)
def test_storage_should_add_just_once_same_value(self): expires_datetime = Token.calc_expires_on(10) token = Token(access_token='access_token', expires_on=expires_datetime) token2 = Token(access_token='new_access_token', expires_on=expires_datetime) expires = str(token.expires_on) token_storage = TokenStorage(self.storage_obj) token_storage(token) token_storage._access_token = 'new_access_token' token_storage(token2) token_requested = token_storage.request_token() self.assertEqual(len(token_requested), 2) self.assertEqual(token_requested.get('access_token'), 'access_token') self.assertEqual(self.storage_obj.get('expires_on'), expires, self.storage_obj)
def test_storage_should_respect_datetime_format(self): token = Token(access_token='access_token', expires_on=Token.calc_expires_on(10)) token_storage = TokenStorage(self.storage_obj) token_storage(token) expires = token_storage.request_token()['expires_on'] expected = datetime(2015, 1, 1, 10, 12, 20, microsecond=0) self.assertEqual(expires, expected)
def test_update_token_should_set_a_token_with_data_retrieved( self, _request_token): self.manager.reset_token() expires = 100 _request_token.return_value = { 'access_token': 'new_access_token', 'expires_in': expires } self.manager._token = Token('access_token', expires_on=0) self.manager._update_token() self.assertTrue(_request_token.called) self.assertEqual(self.manager._token.access_token, 'new_access_token') self.assertEqual(self.manager._token.expires_on, Token.calc_expires_on(expires))
def test_update_token_should_set_a_token_with_data_retrieved(self, _request_token): _request_token.return_value = {'access_token': 'new_access_token', 'expires_in': 10} self.manager._token = Token('access_token', expires_in=100) self.manager._update_token() self.assertTrue(_request_token.called) self.assertEqual(self.manager._token.access_token, 'new_access_token') self.assertEqual(self.manager._token._expires_in, 10)
def test_should_be_able_to_request_a_new_token(self, post): post.return_value.json.return_value = { 'access_token': 'accesstoken', 'expires_on': Token.calc_expires_on(10), } self.manager._request_token() post.assert_called_with(self.END_POINT, data={'grant_type': 'client_credentials'}, auth=(self.CLIENT_ID, self.CLIENT_SECRET))
def test_should_be_able_to_request_a_new_token(self, post): post.return_value.json.return_value = { 'access_token': 'accesstoken', 'expires_on': Token.calc_expires_on(10), } self.manager._request_token() post.assert_called_with(self.end_point, data={'grant_type': 'client_credentials'}, auth=(self.client_id, self.client_secret))
def test_should_be_able_to_request_a_new_token(self, post): post.return_value.json.return_value = { 'access_token': 'accesstoken', 'expires_on': Token.calc_expires_on(10), } self.manager._request_token() post.assert_called_with(self.END_POINT, timeout=None, data={'grant_type': 'client_credentials'}, auth=(self.CLIENT_ID, self.CLIENT_SECRET))
def test_update_token_should_set_a_token_with_data_retrieved_from_storage(self, _request_token): expires = Token.calc_expires_on(100) _request_token.return_value = dict() self.manager._token = Token('new_access_token', expires_on=expires) self.manager._token_storage(self.manager._token) self.manager._update_token() self.assertFalse(_request_token.called) self.assertEqual(self.manager._token.access_token, 'new_access_token') self.assertEqual(self.manager._token.expires_on, expires)
def test_storage_should_add_just_once_same_value(self): expires_datetime = Token.calc_expires_on(10) token = Token(access_token='access_token', expires_on=expires_datetime) token2 = Token(access_token='new_access_token', expires_on=expires_datetime) expires = token.expires_on token_storage = TokenStorage(self.storage_obj) token_storage(token) token_storage._access_token = 'new_access_token' token_storage(token2) token_requested = token_storage.request_token() self.assertEqual(len(token_requested), 2) self.assertEqual(token_requested.get('access_token'), 'access_token') self.assertEqual(token_requested.get('expires_on'), expires, self.storage_obj)
def test_update_token_should_set_a_token_with_data_retrieved(self, _request_token): self.manager.reset_token() expires = 100 _request_token.return_value = {'access_token': 'new_access_token', 'expires_in': expires} self.manager._token = Token('access_token', expires_on=0) self.manager._update_token() self.assertTrue(_request_token.called) self.assertEqual(self.manager._token.access_token, 'new_access_token') self.assertEqual(self.manager._token.expires_on, Token.calc_expires_on(expires))
def test_should_know_when_it_has_expired(self): token = Token(access_token='access_token', expires_on=0) self.assertFalse(token.is_valid(), self.storage_obj)
def test_should_have_an_access_token(self): token = Token(access_token='access_token') self.assertEqual(token.access_token, 'access_token')
def test_expires_on_works(self): expires = Token.calc_expires_on(10) self.assertEqual(datetime.now() + timedelta(seconds=10), expires)
def test_should_reset_token(self): self.manager.reset_token() self.assertEqual(self.manager._token.access_token, '') self.assertEqual(self.manager._token.expires_on, Token.calc_expires_on(0))
def test_should_return_token_value(self): self.manager._token = Token('access_token', expires_on=Token.calc_expires_on(10)) self.assertEqual(self.manager.get_token(), 'access_token')
def test_should_know_when_it_is_valid(self): token = Token(access_token='access_token', expires_on=Token.calc_expires_on(10)) self.assertTrue(token.is_valid(), self.storage_obj)
def test_should_respect_valid_token(self): self.manager._token = Token('', expires_in=10) self.assertTrue(self.manager._has_token())
def test_should_know_when_it_has_expired(self): token = Token(access_token='access_token', expires_in=0) self.assertFalse(token.is_valid())
def test_should_know_when_it_is_valid(self): token = Token(access_token='access_token', expires_in=10) self.assertTrue(token.is_valid())
def test_should_detect_expired_token(self): self.manager._token = Token('', expires_on=Token.calc_expires_on(0)) self.assertFalse(self.manager._has_token())
def test_should_respect_valid_token(self): self.manager._token = Token('', expires_on=Token.calc_expires_on(10)) self.assertTrue(self.manager._has_token())