def test_cache_key(self): value_input = ('1', '2', '3', '4') value_hash = hashlib.sha1() value_hash.update("(1,2,3,4)") value_output = value_hash.hexdigest() test_result = auth._tuple_to_cache_key(value_input) self.assertEqual(test_result, value_output)
def test_cache_key(self): value_input = ('1', '2', '3', '4') value_hash = hashlib.sha1() value_hash.update("(1,2,3,4)") value_output = value_hash.hexdigest() test_result = auth._tuple_to_cache_key(value_input) self.assertEqual(test_result, value_output)
def test_cache_key(self): value_input = ('1', '2', '3', '4') value_hash = hashlib.sha1() if six.PY3: value_hash.update('(1,2,3,4)'.encode('utf-8')) else: value_hash.update('(1,2,3,4)') value_output = value_hash.hexdigest() test_result = auth._tuple_to_cache_key(value_input) self.assertEqual(test_result, value_output)
def test_store_data_to_cache(self): url = 'myfakeurl' tenant_id = '0987654321' token = 'fedcbaFEDCBA' key_data = (tenant_id, token, url) key_value = auth._tuple_to_cache_key(key_data) redis_client = fakeredis_connection() # The data that will get cached access_data = fake_catalog(tenant_id, token) packed_data = msgpack.packb(access_data, use_bin_type=True, encoding='utf-8') # Redis fails the expiration time with mock.patch( 'fakeredis.FakeRedis.pexpireat') as MockRedisExpire: MockRedisExpire.side_effect = Exception( 'mock redis expire failure') redis_error = auth._send_data_to_cache(redis_client, url, access_data, self.default_max_cache_life) self.assertFalse(redis_error) # Redis fails to set the data with mock.patch( 'fakeredis.FakeRedis.set') as MockRedisSet: MockRedisSet.side_effect = Exception('mock redis set data failed') redis_error = auth._send_data_to_cache(redis_client, url, access_data, self.default_max_cache_life) self.assertFalse(redis_error) # Happy Path store_result = auth._send_data_to_cache(redis_client, url, access_data, self.default_max_cache_life) self.assertTrue(store_result) stored_data = redis_client.get(key_value) self.assertIsNotNone(stored_data) self.assertEqual(stored_data, packed_data) stored_data_original = msgpack.unpackb(stored_data, encoding='utf-8') self.assertEqual(stored_data_original, access_data)
def test_retrieve_cache_data(self): url = 'myurl' tenant_id = '123456890' token = 'ABCDEFabcdef' key_data = (tenant_id, token, url) key_value = auth._tuple_to_cache_key(key_data) data = fake_catalog(tenant_id, token) data_packed = msgpack.packb(data, encoding='utf-8', use_bin_type=True) redis_client = fakeredis_connection() self.assertTrue(redis_client.set(key_value, data_packed)) # Invalid Cache Error # - we use a random url for the cache conflict portion invalid_cached_data = auth._retrieve_data_from_cache(redis_client, '/random/url', tenant_id, token) self.assertIsNone(invalid_cached_data) # Test: Redis Client tosses exception def redis_toss_exception(*args, **kwargs): raise Exception('mock redis exception') redis_exception_result = auth._retrieve_data_from_cache( redis_toss_exception, url, tenant_id, token) self.assertEqual(redis_exception_result, None) # msgpack error with mock.patch('eom.auth.__unpacker') as MockMsgPacker: MockMsgPacker.side_effect = msgpack.exceptions.UnpackException( 'mock') msgpack_error = auth._retrieve_data_from_cache(redis_client, url, tenant_id, token) self.assertIsNone(msgpack_error) # Test: Happy case V2 data happy_v2_result = auth._retrieve_data_from_cache(redis_client, url, tenant_id, token) self.assertEqual(happy_v2_result, data)
def test_store_data_to_cache(self): url = 'myfakeurl' tenant_id = '0987654321' token = 'fedcbaFEDCBA' key_data = (tenant_id, token, url) key_value = auth._tuple_to_cache_key(key_data) redis_client = fakeredis_connection() # The data that will get cached access_data = fake_catalog(tenant_id, token) packed_data = msgpack.packb(access_data, use_bin_type=True, encoding='utf-8') # Redis fails the expiration time with mock.patch('fakeredis.FakeRedis.pexpireat') as MockRedisExpire: MockRedisExpire.side_effect = Exception( 'mock redis expire failure') redis_error = auth._send_data_to_cache(redis_client, url, access_data, self.default_max_cache_life) self.assertFalse(redis_error) # Redis fails to set the data with mock.patch('fakeredis.FakeRedis.set') as MockRedisSet: MockRedisSet.side_effect = Exception('mock redis set data failed') redis_error = auth._send_data_to_cache(redis_client, url, access_data, self.default_max_cache_life) self.assertFalse(redis_error) # Happy Path store_result = auth._send_data_to_cache(redis_client, url, access_data, self.default_max_cache_life) self.assertTrue(store_result) stored_data = redis_client.get(key_value) self.assertIsNotNone(stored_data) self.assertEqual(stored_data, packed_data) stored_data_original = msgpack.unpackb(stored_data, encoding='utf-8') self.assertEqual(stored_data_original, access_data)
def test_retrieve_cache_data(self): url = 'myurl' tenant_id = '123456890' token = 'ABCDEFabcdef' key_data = (tenant_id, token, url) key_value = auth._tuple_to_cache_key(key_data) data = fake_catalog(tenant_id, token) data_packed = msgpack.packb(data, encoding='utf-8', use_bin_type=True) redis_client = fakeredis_connection() self.assertTrue(redis_client.set(key_value, data_packed)) # Invalid Cache Error # - we use a random url for the cache conflict portion invalid_cached_data = auth._retrieve_data_from_cache( redis_client, '/random/url', tenant_id, token) self.assertIsNone(invalid_cached_data) # Test: Redis Client tosses exception def redis_toss_exception(*args, **kwargs): raise Exception('mock redis exception') redis_exception_result = auth._retrieve_data_from_cache( redis_toss_exception, url, tenant_id, token) self.assertEqual(redis_exception_result, None) # msgpack error with mock.patch('eom.auth.__unpacker') as MockMsgPacker: MockMsgPacker.side_effect = msgpack.exceptions.UnpackException( 'mock') msgpack_error = auth._retrieve_data_from_cache( redis_client, url, tenant_id, token) self.assertIsNone(msgpack_error) # Test: Happy case V2 data happy_v2_result = auth._retrieve_data_from_cache( redis_client, url, tenant_id, token) self.assertEqual(happy_v2_result, data)
def test_cache_key(self): value_input = ('1', '2', '3', '4') value_output = "(1,2,3,4)" test_result = auth._tuple_to_cache_key(value_input) self.assertEqual(test_result, value_output)