def test_get_redis_conn_sentinel(self): mock_settings = patch( 'globomap_auth_manager.redis_client.settings').start() mock_sentinel = patch( 'globomap_auth_manager.redis_client.Sentinel').start() mock_redis = patch('globomap_auth_manager.redis_client.redis').start() mock_settings.REDIS_SENTINEL_ENDPOINT_SIMPLE = '1' mock_settings.REDIS_SENTINELS_PORT = '8000' mock_settings.REDIS_SENTINELS = 'localhost1,localhost2,localhost3' mock_settings.REDIS_SENTINEL_SERVICE_NAME = 'redis_service' mock_settings.REDIS_SENTINEL_PASSWORD = '******' mock_sentinel.return_value.discover_master.return_value = [ 'localhost1', '8000' ] RedisClient() mock_sentinel.assert_called_once_with([('localhost1', '8000'), ('localhost2', '8000'), ('localhost3', '8000')], socket_timeout=0.1) mock_redis.StrictRedis.assert_called_once_with(host='localhost1', port='8000', password='******') mock_sentinel().discover_master.assert_called_once_with( 'redis_service')
def test_set_cache_token(self): mock_settings = patch( 'globomap_auth_manager.redis_client.settings').start() patch('globomap_auth_manager.redis_client.redis').start() mock_datetime = patch( 'globomap_auth_manager.redis_client.datetime').start() mock_settings.REDIS_SENTINEL_ENDPOINT_SIMPLE = None mock_settings.REDIS_HOST = 'localhost' mock_settings.REDIS_PORT = '8000' mock_settings.REDIS_PASSWORD = '******' redis_client = RedisClient() mock_datetime.utcnow.return_value.now.return_value.__sub__ = MagicMock( return_value=MagicMock(seconds=79802)) redis_client.conn = Mock() redis_client.set_cache_token({ 'expires_at': '2018-04-11T19:17:49.870116Z', 'auth_token': 'token', 'roles': [{ 'name': 'role1', 'id': '123' }], 'user': { 'name': 'test' } }) token_data = { 'expires_at': '2018-04-11T19:17:49.870116Z', 'roles': [{ 'name': 'role1', 'id': '123' }], 'user': { 'name': 'test' } } redis_client.conn.set.assert_called_once_with('token', json.dumps(token_data), ex=79802)
def test_get_cache_token(self): mock_settings = patch( 'globomap_auth_manager.redis_client.settings').start() patch('globomap_auth_manager.redis_client.redis').start() mock_settings.REDIS_SENTINEL_ENDPOINT_SIMPLE = None mock_settings.REDIS_HOST = 'localhost' mock_settings.REDIS_PORT = '8000' mock_settings.REDIS_PASSWORD = '******' redis_client = RedisClient() data = {'expires_at': '2018-04-11T19:17:49.870116Z', 'token': 'token'} redis_client.conn = Mock() redis_client.conn.get.return_value = json.dumps(data) self.assertDictEqual(data, redis_client.get_cache_token('123'))
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 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_get_redis_conn_redis(self): mock_settings = patch( 'globomap_auth_manager.redis_client.settings').start() mock_redis = patch('globomap_auth_manager.redis_client.redis').start() mock_settings.REDIS_SENTINEL_ENDPOINT_SIMPLE = None mock_settings.REDIS_HOST = 'localhost' mock_settings.REDIS_PORT = '8000' mock_settings.REDIS_PASSWORD = '******' RedisClient() mock_redis.Redis.assert_called_once_with(host='localhost', port='8000', password='******')
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_no_cache_exception(self): mock_settings = patch( 'globomap_auth_manager.redis_client.settings').start() patch('globomap_auth_manager.redis_client.redis').start() mock_datetime = patch( 'globomap_auth_manager.redis_client.datetime').start() mock_settings.REDIS_SENTINEL_ENDPOINT_SIMPLE = None mock_settings.REDIS_HOST = 'localhost' mock_settings.REDIS_PORT = '8000' mock_settings.REDIS_PASSWORD = '******' redis_client = RedisClient() mock_datetime.utcnow.return_value.now.return_value.__sub__ = MagicMock( return_value=MagicMock(seconds=79802)) redis_client.conn = None with self.assertRaises(CacheException): redis_client.get_cache_token('token') with self.assertRaises(CacheException): redis_client.set_cache_token('token')
def configure_cache(self): if settings.USE_REDIS == '1': self.cache = RedisClient()