Beispiel #1
0
    def test_get_token_invalid_token_get_new_from_refresh(self):
        """
        create a new token, change the token and the last_verified. expect to use
        the refresh to get a new token and no referesh will be in the cache.
        """

        # create a new token
        token1 = get_token()

        # modify the token and change the last_verified in the cache
        # so the verify api will be called, the response will be false and
        # refresh api to be used

        last_verified = datetime.now() + timedelta(minutes=-11)

        cache.set('token', 'invalis_token')
        cache.set('last_verified', last_verified)

        token2 = get_token()
        refresh_token = cache.get('refresh')

        logger.debug("new_token: %s", token2)
        logger.debug("refresh_token: %s", refresh_token)

        assert_false(token1 == token2)
        assert_false(refresh_token)
Beispiel #2
0
    def test_get_token_existing_token_not_verified(self):
        """ 
        create a new token, change the last_verified to be more than 10 min. call get_token again.
        token returned should be the same in the cache.
        """

        # create a new token
        token1 = get_token()

        #change the last verified to be more than 10 minutes
        last_verified = datetime.now() + timedelta(minutes=-11)
        cache.set('last_verified', last_verified)

        token2 = get_token()
        logger.debug("token1: %s - token2: %s", token1, token2)

        assert_equal(token1, token2)
Beispiel #3
0
    def test_get_token_ok(self):
        """ expects to return a token from the token api. the token should be stored in the cache """

        token = get_token()

        cached_token = cache.get('token').decode('ASCII')
        refreshed_token = cache.get('refresh').decode('ASCII')

        assert_equal(token, cached_token)
Beispiel #4
0
    def test_get_token_false(self):
        """ 
        expects to return false because the user doesn't exist and there is no token in the cache 
        there should not be any token or refresh token stored in the cache.
        """

        token = get_token(user='******')

        cached_token = cache.get('token')
        refreshed_token = cache.get('refresh')

        logger.debug("cached_token: %s - refreshed_token: %s", cached_token,
                     refreshed_token)
        assert_false(token)
        assert_false(cached_token)
        assert_false(refreshed_token)
Beispiel #5
0
    def test_get_token_existing_token_and_verified(self):
        """ 
        sets a token and a last_verified records in the cache. expects to return a token from the token api. 
        the token should be stored in the cache. 
        """

        test_token = 'test_token'
        cache.set('token', test_token, ex=conf.CACHE['REDIS']['TTL'])
        cache.set('last_verified', datetime.now())

        token = get_token()

        logger.debug(
            "test_token created and token retrieved from get_token: %s - %s",
            test_token, token)

        assert_equal(token, test_token)
Beispiel #6
0
    def test_get_token_not_valid_token_not_verified(self):
        """ 
        set a token and a last_verified more than 10 minutes. expects to get a new token becasue 
        the token doesn't exist.
        """

        test_token = 'test_token'
        last_verified = datetime.now() + timedelta(minutes=-11)

        cache.set('token', test_token, ex=conf.CACHE['REDIS']['TTL'])
        cache.set('last_verified', last_verified)

        time_diff = get_token_cached_times_diff(cached_key='last_verified',
                                                unit='minutes')

        new_token = get_token()
        logger.debug("timediff min: %s", time_diff)
        logger.debug("old token: %s - new token: %s", test_token, new_token)

        assert_false(test_token == new_token)
Beispiel #7
0
    def test_get_token_with_invalid_user_no_refresh_token(self):
        """ 
        create a valid token, modify the token and remove the refresher from the cache 
        so they become invalid. change the last_verified so it will get an invalid 
        response from the verify api. expect it to raise an exception at the end.
        """

        token = set_token(
        )  #create a new token, refresher and set last_verified.
        new_token = 'foobar'  # modifing the token so it will become invalid.
        cache.set('token', new_token)  # set the invalid token
        cache.delete('refresh')  # remove the refresh from the cache.
        yesterday_date = datetime.now() + timedelta(
            days=-1)  # move the last_verified one dat back.
        cache.set('last_verified',
                  yesterday_date)  # set the date in the cache.

        # calling the
        #with assert_raises(Exception):
        invalid_token = get_token(user='******')
        logger.debug("invalid token response: %s", invalid_token)

        assert_false(invalid_token)