def test_getIP_cached(): bc = BlessCache(None, None, BlessCache.CACHEMODE_ENABLED) bc.cache = {} bc.set('lastip', '1.1.1.1') bc.set('lastipchecktime', time.time()) user_ip = UserIP(bc, 10, IP_URLS) assert user_ip.getIP() == '1.1.1.1'
def test_getIP_fetched_fail(mocker): bc = BlessCache(None, None, BlessCache.CACHEMODE_ENABLED) bc.cache = {} mocker.patch.object(bc, 'save') user_ip = UserIP(bc, 10, IP_URLS) mocker.patch.object(user_ip, '_fetchIP') user_ip._fetchIP.return_value = None with pytest.raises(Exception): user_ip.getIP() user_ip._fetchIP.assert_called()
def test_getIP_fetched(mocker): bc = BlessCache(None, None, BlessCache.CACHEMODE_ENABLED) bc.cache = {} mocker.patch.object(bc, 'save') user_ip = UserIP(bc, 10, IP_URLS) mocker.patch.object(user_ip, '_fetchIP') user_ip._fetchIP.return_value = '1.1.1.1' assert user_ip.getIP() == '1.1.1.1' user_ip._fetchIP.assert_called_once() bc.save.assert_called_once()
def test_get_kmsauth_token_cached(): kmsconfig = {'awsregion': 'us-east-1', 'context': {}, 'kmskey': None} expiration = datetime.datetime.utcnow() + datetime.timedelta(minutes=60) kmsauth_cache = { 'token': 'KMSTOKEN', 'Expiration': expiration.strftime('%Y%m%dT%H%M%SZ') } bless_cache = BlessCache(None, None, BlessCache.CACHEMODE_ENABLED) bless_cache.cache = {} bless_cache.set('kmsauth-us-east-1', kmsauth_cache) token = client.get_kmsauth_token(None, kmsconfig, 'foouser', bless_cache) assert token == 'KMSTOKEN'
def null_bless_cache(): bless_cache = BlessCache(None, None, BlessCache.CACHEMODE_DISABLED) bless_cache.cache = {} return bless_cache
def autoupdate_cache(mocker): bless_cache = BlessCache(None, None, BlessCache.CACHEMODE_ENABLED) bless_cache.cache = {'last_updated': '20160101T000001Z'} mocker.patch.object(bless_cache, 'save') return bless_cache
def test_get(): bc = BlessCache(None, None, BlessCache.CACHEMODE_ENABLED) bc.cache = {'foo': 'bar'} assert bc.get('foo') == 'bar' assert bc.get('DOESNOTEXIST') is None
def test_save_cache(tmpdir): bc = BlessCache(str(tmpdir), 'cache', BlessCache.CACHEMODE_ENABLED) bc.cache = {'foo': 'bar'} bc.dirty = True bc.save() assert len(tmpdir.listdir()) == 1
def test_set(): bc = BlessCache(None, None, BlessCache.CACHEMODE_DISABLED) bc.cache = {} bc.set('foo', 'bar') assert bc.cache == {'foo': 'bar'} assert bc.dirty == True
def test_get_nocache(): bc = BlessCache(None, None, BlessCache.CACHEMODE_DISABLED) bc.cache = {'foo': 'bar'} assert bc.get('foo') is None