def test_limits_with_buckets(self, mock_decode, mock_time): db = mock.Mock(**{'zrange.return_value': [ dict( uuid='uuid', params={}, bucket=dict(messages=2, expire=1000001.0), ), dict( uuid='uuid2', params=dict(unused='foo'), bucket=dict(messages=5, expire=999999.0), ), dict( uuid='uuid3', params=dict(param='foo'), bucket=dict(messages=10, expire=1000005.0), ), dict( uuid='uuid3', params=dict(param='bar'), bucket=dict(messages=5, expire=1000001.0), ), ]}) limits = [ self._make_limit( uuid='uuid', queries=[], verbs=['GET'], unit='minute', uri='/spam/{uri}', value=7, ), self._make_limit( uuid='uuid2', queries=[], verbs=['GET'], unit='minute', uri='/spam/{uri2}/{used}', value=20, ), self._make_limit( uuid='uuid3', queries=[], verbs=['GET'], unit='minute', uri='/spam/{uri3}/{param}', value=50, ), self._make_limit( uuid='uuid4', queries=[], verbs=['GET'], unit='minute', uri='/spam/{uri4}/{param}', value=50, ), self._make_limit( uuid='uuid5', queries=[], verbs=['GET'], unit='minute', uri='/spam/{uri5}', value=10, ), ] midware = mock.Mock(db=db, limits=limits) environ = { 'turnstile.nova.limitclass': 'lim_class', 'turnstile.bucket_set': 'bucket_set:spam', } nova_limits.nova_postprocess(midware, environ) self.assertEqual(environ['nova.limits'], [ dict( verb='GET', URI='/spam/{uri}', regex='/spam/{uri}', value=7, unit='MINUTE', remaining=2, resetTime=1000001.0, ), dict( verb='GET', URI='/spam/{uri2}/{used}', regex='/spam/{uri2}/{used}', value=20, unit='MINUTE', remaining=5, resetTime=999999.0, ), dict( verb='GET', URI='/spam/{uri3}/foo', regex='/spam/{uri3}/foo', value=50, unit='MINUTE', remaining=10, resetTime=1000005.0, ), dict( verb='GET', URI='/spam/{uri3}/bar', regex='/spam/{uri3}/bar', value=50, unit='MINUTE', remaining=5, resetTime=1000001.0, ), dict( verb='GET', URI='/spam/{uri3}/{param}', regex='/spam/{uri3}/{param}', value=50, unit='MINUTE', remaining=5, resetTime=1000005.0, ), dict( verb='GET', URI='/spam/{uri4}/{param}', regex='/spam/{uri4}/{param}', value=50, unit='MINUTE', remaining=50, resetTime=1000000.0, ), dict( verb='GET', URI='/spam/{uri5}', regex='/spam/{uri5}', value=10, unit='MINUTE', remaining=10, resetTime=1000000.0, ), ])
def test_limits(self, mock_decode, mock_time): db = mock.Mock(**{'zrange.return_value': []}) limits = [ self._make_limit( uuid='uuid', queries=[], verbs=['GET', 'PUT'], unit='minute', uri='/spam/uri', value=23, ), self._make_limit( uuid='uuid2', queries=[], verbs=[], unit='second', uri='/spam/uri2', value=18, ), self._make_limit( uuid='uuid3', rate_class='spam', queries=[], verbs=['GET'], unit='hour', uri='/spam/uri3', value=17, ), self._make_limit( uuid='uuid4', rate_class='lim_class', queries=[], verbs=['GET'], unit='day', uri='/spam/uri4', value=1, ), self._make_limit( uuid='uuid5', queries=[], verbs=['GET'], unit='1234', uri='/spam/uri5', value=183, ), self._make_limit( uuid='uuid6', queries=['bravo', 'alfa'], verbs=['GET'], unit='day', uri='/spam/uri6', value=1, ), ] midware = mock.Mock(db=db, limits=limits) environ = { 'turnstile.nova.limitclass': 'lim_class', 'turnstile.bucket_set': 'bucket_set:spam', } nova_limits.nova_postprocess(midware, environ) self.assertEqual(environ['nova.limits'], [ dict( verb='GET', URI='/spam/uri', regex='/spam/uri', value=23, unit='MINUTE', remaining=23, resetTime=1000000.0, ), dict( verb='PUT', URI='/spam/uri', regex='/spam/uri', value=23, unit='MINUTE', remaining=23, resetTime=1000000.0, ), dict( verb='GET', URI='/spam/uri2', regex='/spam/uri2', value=18, unit='SECOND', remaining=18, resetTime=1000000.0, ), dict( verb='HEAD', URI='/spam/uri2', regex='/spam/uri2', value=18, unit='SECOND', remaining=18, resetTime=1000000.0, ), dict( verb='POST', URI='/spam/uri2', regex='/spam/uri2', value=18, unit='SECOND', remaining=18, resetTime=1000000.0, ), dict( verb='PUT', URI='/spam/uri2', regex='/spam/uri2', value=18, unit='SECOND', remaining=18, resetTime=1000000.0, ), dict( verb='DELETE', URI='/spam/uri2', regex='/spam/uri2', value=18, unit='SECOND', remaining=18, resetTime=1000000.0, ), dict( verb='GET', URI='/spam/uri4', regex='/spam/uri4', value=1, unit='DAY', remaining=1, resetTime=1000000.0, ), dict( verb='GET', URI='/spam/uri5', regex='/spam/uri5', value=183, unit='UNKNOWN', remaining=183, resetTime=1000000.0, ), dict( verb='GET', URI='/spam/uri6?alfa={alfa}&bravo={bravo}', regex='/spam/uri6?alfa={alfa}&bravo={bravo}', value=1, unit='DAY', remaining=1, resetTime=1000000.0, ), ]) db.zrange.assert_called_once_with('bucket_set:spam', 0, -1)