Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
    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)
Пример #4
0
    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)
Пример #5
0
 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)
Пример #6
0
    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))
Пример #7
0
    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))
Пример #8
0
    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)
Пример #9
0
    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))
Пример #10
0
    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)
Пример #11
0
 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)
Пример #12
0
 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)
Пример #13
0
    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))
Пример #14
0
    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)
Пример #15
0
    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))
Пример #16
0
    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))
Пример #17
0
 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)
Пример #18
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')
Пример #19
0
 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)
Пример #20
0
 def test_expires_on_works(self):
     expires = Token.calc_expires_on(10)
     self.assertEqual(datetime.now() + timedelta(seconds=10), expires)
Пример #21
0
    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))
Пример #22
0
 def test_should_respect_valid_token(self):
     self.manager._token = Token('', expires_on=Token.calc_expires_on(10))
     self.assertTrue(self.manager._has_token())
Пример #23
0
 def test_should_detect_expired_token(self):
     self.manager._token = Token('', expires_on=Token.calc_expires_on(0))
     self.assertFalse(self.manager._has_token())
Пример #24
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')
Пример #25
0
 def test_should_detect_expired_token(self):
     self.manager._token = Token('', expires_on=Token.calc_expires_on(0))
     self.assertFalse(self.manager._has_token())
Пример #26
0
 def test_expires_on_works(self):
     expires = Token.calc_expires_on(10)
     self.assertEqual(datetime.now() + timedelta(seconds=10), expires)
Пример #27
0
 def test_should_respect_valid_token(self):
     self.manager._token = Token('', expires_on=Token.calc_expires_on(10))
     self.assertTrue(self.manager._has_token())