def create_token(username=None, password=None): auth_inst = Auth() auth_inst.set_credentials(username, password) token = auth_inst.get_token_data() return token
def test_validate_token_exception(self): auth_inst = Auth() auth_inst.cache = None mock_keystoneauth = patch( 'globomap_auth_manager.auth.KeystoneAuth').start() mock_keystoneauth.return_value.validate_token.return_value = None with self.assertRaises(exceptions.InvalidToken): auth_inst.validate_token()
def test_validate_token_with_cache_exception(self): mock_keystoneauth = patch( 'globomap_auth_manager.auth.KeystoneAuth').start() mock_keystoneauth.return_value.validate_token.return_value = '' auth_inst = Auth() auth_inst._set_config_keystone = Mock() auth_inst.cache = RedisClient() auth_inst.cache.is_redis_ok = Mock(return_value=False) auth_inst.cache.get_cache_token = Mock(return_value='') auth_inst.token = 'token123' auth_inst._keystone_auth = mock_keystoneauth() with self.assertRaises(exceptions.InvalidToken): auth_inst.validate_token()
def test_validate_token_with_cache(self): auth_inst = Auth() auth_inst.cache = Mock() auth_inst.token = 'token123' auth_inst.cache.get_cache_token.return_value = 'token_data' auth_inst.validate_token() self.assertEqual('token_data', auth_inst.token_data)
def test_validate_token(self): auth_inst = Auth() auth_inst.cache = RedisClient() auth_inst.cache.is_redis_ok = Mock(return_value=False) auth_inst.token = 'token123' mock_keystoneauth = patch( 'globomap_auth_manager.auth.KeystoneAuth').start() mock_keystoneauth.return_value.validate_token.return_value = 'token_data' auth_inst.validate_token() self.assertEqual('token_data', auth_inst.token_data)
def get_roles(token): auth_inst = Auth() try: auth_inst.set_token(token) auth_inst.validate_token() token_data = auth_inst.get_token_data_details() except exceptions.InvalidToken: app.logger.warning('Invalid Token') api.abort(401, errors='Invalid Token') except exceptions.AuthException: err_msg = 'Error to validate token' app.logger.exception(err_msg) api.abort(503) return token_data
def test_set_credentials(self): mock_settings = patch('globomap_auth_manager.auth.settings').start() mock_keystoneauth = patch( 'globomap_auth_manager.auth.KeystoneAuth').start() mock_settings.KEYSTONE_USERNAME = '******' mock_settings.KEYSTONE_PASSWORD = '******' mock_settings.KEYSTONE_PROJECT_NAME = 'project_name' mock_settings.KEYSTONE_AUTH_URL = 'auth_url' mock_settings.KEYSTONE_USER_DOMAIN_NAME = 'user_domain_name' mock_settings.KEYSTONE_PROJECT_DOMAIN_NAME = 'project_domain_name' mock_settings.KEYSTONE_TIMEOUT = 3 Auth().set_credentials('user', 'pass') mock_keystoneauth.assert_called_once_with('auth_url', 'project_name', 'user', 'pass', 'user_domain_name', 'project_domain_name', 3)
def validate_token(token): auth_inst = Auth() try: auth_inst.set_token(token) auth_inst.validate_token() return auth_inst except exceptions.InvalidToken: app.logger.error('Invalid Token') api.abort(401, errors='Invalid Token') except exceptions.AuthException: err_msg = 'Error to validate token' app.logger.exception(err_msg) api.abort(503)
def test_get_token_data(self): auth_inst = Auth() auth_inst.cache = RedisClient() auth_inst.cache.is_redis_ok = Mock(return_value=False) auth_inst._keystone_auth = Mock() auth_inst._keystone_auth.conn.auth_ref = { 'expires_at': '2018-04-11T19:17:49.870116Z', 'auth_token': 'token', 'roles': [{ 'name': 'role1', 'id': '123' }], 'user': { 'name': 'test' }, 'useless': 1 } data = {'expires_at': '2018-04-11T19:17:49.870116Z', 'token': 'token'} auth_inst.token = 'token' self.assertEqual(data, auth_inst.get_token_data())
def test_get_token_data_with_cache(self): auth_inst = Auth() auth_inst.cache = Mock() auth_inst._keystone_auth = Mock() auth_ref = { 'expires_at': '2018-04-11T19:17:49.870116Z', 'auth_token': 'token', 'roles': [{ 'name': 'role1', 'id': '123' }], 'user': { 'name': 'test' }, 'useless': 1 } auth_inst._keystone_auth.conn.auth_ref = auth_ref data = {'expires_at': '2018-04-11T19:17:49.870116Z', 'token': 'token'} auth_inst.token = 'token' self.assertEqual(data, auth_inst.get_token_data()) auth_inst.cache.set_cache_token.assert_called_once_with(auth_ref)
def validate_token(token): auth_inst = Auth() auth_inst.set_token(token) auth_inst.validate_token() return auth_inst
def test_set_token(self): auth_inst = Auth() auth_inst.set_token('Token token=token123') self.assertEqual(auth_inst.token, 'token123')
def test_validate_token_without_token(self): auth_inst = Auth() with self.assertRaises(exceptions.InvalidToken): auth_inst.validate_token()