def setUp(self):
     self.tc = TokenCache()
     self.tc._clear()
 def test_token_cache_is_singleton(self):
     self.assertIs(TokenCache(), self.tc)
     self.assertIs(TokenCache(), TokenCache())
class TestTokenCache(unittest.TestCase):
    def setUp(self):
        self.tc = TokenCache()
        self.tc._clear()

    def tearDown(self):
        self.tc._clear()

    def test_token_cache_is_singleton(self):
        self.assertIs(TokenCache(), self.tc)
        self.assertIs(TokenCache(), TokenCache())

    def test_set_token_ttl_error(self):
        self.assertRaises(TokenError, self.tc.set_token, key='some_key', token='some_token', ttl=-1)

    def test_set_token_success(self):
        self.tc.set_token(key='some_key', token='some_token', ttl=0)
        self.tc.set_token(key='another_key', token='some_token', ttl=1)
        self.assertListEqual(self.tc.__class__._tokens['some_key'], ['some_token', None])
        self.assertEqual(self.tc.__class__._tokens['another_key'][0], 'some_token')
        self.assertIsInstance(self.tc.__class__._tokens['another_key'][1], float)

    def test_get_token_wrong_key(self):
        self.assertRaises(TokenError, self.tc.get_token, key='wrong_key')

    def test_get_token_expired(self):
        key = 'expired_key'
        self.tc.set_token(key=key, token='some_token', ttl=0.01)
        time.sleep(0.02)
        self.assertRaises(TokenError, self.tc.get_token, key=key)
        self.assertNotIn(key, self.tc.__class__._tokens)

    def test_get_token_success(self):
        k1 = 'no_expiry_key'
        k2 = 'valid_key'
        self.tc.set_token(key=k1, token='no_expiry_token', ttl=0)
        self.tc.set_token(key=k2, token='valid_token', ttl=float('inf'))

        self.assertEqual(self.tc.get_token(k1), 'no_expiry_token')
        self.assertEqual(self.tc.get_token(k2), 'valid_token')
 def setUp(self):
     self.tm = JWTTokenManager('https://www.some_url.com')
     self.tc = TokenCache()
     self.tc._clear()
class TestRetrieveToken(unittest.TestCase):
    def setUp(self):
        self.tm = JWTTokenManager('https://www.some_url.com')
        self.tc = TokenCache()
        self.tc._clear()

    def tearDown(self):
        if os.getenv(VCAP_SERVICES):
            del os.environ[VCAP_SERVICES]
        if os.getenv(MLP_FOUNDATION_SERVICE_NAME):
            del os.environ[MLP_FOUNDATION_SERVICE_NAME]
        if os.getenv(MLP_FOUNDATION_SERVICE_INSTANCE_NAME):
            del os.environ[MLP_FOUNDATION_SERVICE_INSTANCE_NAME]

    @patch('mlpkitsecurity.token_utils.urlopen',
           Mock(return_value=MockResponse('{"access_token":"some_token", "expires_in":100}')))
    def test_retrieve_token_str_with_cache(self):
        # cred is plain text
        cred = ['client', 'secret']
        token_str = self.tm.retrieve_token_str(token_retrieval_cred=cred,
                                               scopes=['scope'],
                                               use_cache=True)
        self.assertEqual(token_str, 'Bearer some_token')
        cache_key = 'mlp::{}scope'.format(str(cred))
        self.assertEqual(self.tc.__class__._tokens[cache_key][0], 'some_token')
        self.assertIsInstance(self.tc.__class__._tokens[cache_key][1], float)

        # cred is UaaBasicCredenticals
        cred = UaaBasicCredentials(self.tm.base_url, 'client', 'secret')
        token_str = self.tm.retrieve_token_str(token_retrieval_cred=cred,
                                               scopes=['scope'],
                                               use_cache=True)
        self.assertEqual(token_str, 'Bearer some_token')
        cache_key = 'mlp::{}scope'.format(str(cred))
        self.assertEqual(self.tc.__class__._tokens[cache_key][0], 'some_token')
        self.assertIsInstance(self.tc.__class__._tokens[cache_key][1], float)

    @patch('mlpkitsecurity.token_utils.urlopen',
           Mock(return_value=MockResponse('{"access_token":"some_token", "expires_in":100}')))
    def test_retrieve_token_str_without_cache(self):
        token_str = self.tm.retrieve_token_str(token_retrieval_cred=['client', 'secret'],
                                               scopes=['scope'],
                                               use_cache=False)
        self.assertEqual(token_str, 'Bearer some_token')

    @patch('mlpkitsecurity.token_utils.urlopen',
           Mock(return_value=MockResponse('{"access_token":"some_token", "expires_in":100}')))
    def test_parse_retrieve_response(self):
        res = self.tm.retrieve(token_retrieval_cred=['client', 'secret'])
        self.assertEqual(self.tm.parse_retrieve_response(res), 'some_token')

    @patch('mlpkitsecurity.token_utils.urlopen',
           Mock(return_value=MockResponse('{"access_token":"some_token", "expires_in":100}')))
    def test_retrieve_token_for_a_given_service_broker_instance_name(self):
        os.environ[VCAP_SERVICES] = VCAP_SERVICES_V2
        os.environ[MLP_FOUNDATION_SERVICE_NAME] = 'ml-foundation'
        os.environ[MLP_FOUNDATION_SERVICE_INSTANCE_NAME] = 'ml-foundation-std'
        token_str = retrieve_foundation_service_token()
        self.assertEqual(token_str, 'Bearer some_token')

    def test_retrieve_token_for_an_invalid_foundation_service_instance_name(self):
        os.environ[VCAP_SERVICES] = VCAP_SERVICES_V2
        os.environ[MLP_FOUNDATION_SERVICE_NAME] = 'ml-foundation'
        os.environ[MLP_FOUNDATION_SERVICE_INSTANCE_NAME] = 'invalid-name'
        with self.assertRaises(SecurityError) as context:
            retrieve_foundation_service_token()
        self.assertEqual(context.exception.code, 500)
        self.assertEqual(context.exception.detail_info,
                         'Unable to find service instance config for MLP_FOUNDATION_SERVICE_INSTANCE_NAME=invalid-name')

    def test_retrieve_token_expect_foundation_service_instance_name(self):
        os.environ[VCAP_SERVICES] = VCAP_SERVICES_V2
        os.environ[MLP_FOUNDATION_SERVICE_NAME] = 'ml-foundation'
        with self.assertRaises(SecurityError) as context:
            retrieve_foundation_service_token()
        self.assertEqual(context.exception.code, 500)
        self.assertEqual(context.exception.detail_info, 'MLP_FOUNDATION_SERVICE_INSTANCE_NAME is mandatory.')

    def test_retrieve_token_expect_foundation_service_name(self):
        os.environ[VCAP_SERVICES] = VCAP_SERVICES_V2
        os.environ[MLP_FOUNDATION_SERVICE_NAME] = 'invalid-foundation-service-name'
        os.environ[MLP_FOUNDATION_SERVICE_INSTANCE_NAME] = 'ml-foundation-std'
        with self.assertRaises(SecurityError) as context:
            retrieve_foundation_service_token()
        self.assertEqual(context.exception.code, 500)
        self.assertEqual(context.exception.detail_info,
                         'Unable to find service instance config for '
                         'MLP_FOUNDATION_SERVICE_NAME=invalid-foundation-service-name')
Example #6
0
 def tearDown(self):
     setup_env_vars_for_cf_uaa()
     TokenCache()._clear()
     os.environ.pop(MLP_UAA_PUBLIC_KEY, None)
Example #7
0
 def setUp(self):
     setup_env_vars_for_cf_uaa()
     TokenCache()._clear()
     os.environ[MLP_UAA_PUBLIC_KEY] = public_key
     self.app = webapp_cf_auth.app.test_client()