Beispiel #1
0
    def test_invalid_url(self):
        """Verify we don't allow invalid URLs."""
        with pytest.raises(rexc.InvalidRedisURL) as excinfo:
            redstore.RedisStore(url="https://redis.io")

        err = excinfo.value
        assert err.url == "https://redis.io"
        assert isinstance(err.error, rfc3986.exceptions.ValidationError)
Beispiel #2
0
    def test_get_returns_none(self):
        """Verify we use the right method on our client to retrive data."""
        url = "redis://"
        client = mock.Mock()
        store = redstore.RedisStore(url=url, client=client)
        client.hgetall.return_value = {}

        data = store.get("test_key")

        assert data is None
        client.hgetall.assert_called_once_with("test_key")
Beispiel #3
0
    def test_set(self):
        """Verify we call the right method on our Redis client."""
        url = "redis://"
        client = mock.Mock()
        store = redstore.RedisStore(url=url, client=client)
        data = mock.Mock()
        data.asdict.return_value = {"remaining": "4", "used": "1"}

        store.set(key="test_key", data=data)

        client.hmset.assert_called_once_with("test_key", {
            "remaining": "4",
            "used": "1"
        })
Beispiel #4
0
    def test_get(self):
        """Verify we use the right method on our client to retrive data."""
        url = "redis://"
        client = mock.Mock()
        store = redstore.RedisStore(url=url, client=client)
        client.hgetall.return_value = {
            "remaining": "4",
            "used": "1",
            "created_at": "2018-12-11T12:12:15.123456+0000",
        }

        data = store.get("test_key")

        assert isinstance(data, limit_data.LimitData)
        client.hgetall.assert_called_once_with("test_key")
Beispiel #5
0
    def test_current_time_uses_redis_time(self):
        """Verify we retrieve the current time from Redis."""
        url = "redis://"
        client = mock.Mock()
        client.time.return_value = (1_545_740_827, 608_937)
        store = redstore.RedisStore(url=url, client=client)

        now = store.current_time()

        assert isinstance(now, datetime.datetime)
        assert now == datetime.datetime(2018,
                                        12,
                                        25,
                                        12,
                                        27,
                                        7,
                                        608_937,
                                        tzinfo=datetime.timezone.utc)
        client.time.assert_called_once_with()
Beispiel #6
0
 def test_valid_url(self, url):
     """Verify our valid urls work."""
     store = redstore.RedisStore(url=url)
     assert isinstance(store.url, rfc3986.ParseResult)
     assert isinstance(store.client, redis.StrictRedis)
Beispiel #7
0
__version__ = "2019.1.0.dev0"

logger = structlog.get_logger()
app = flask.Flask(__name__)

logging.basicConfig(format="%(message)s",
                    stream=sys.stdout,
                    level=logging.INFO)
structlog.configure(
    processors=[
        structlog.processors.KeyValueRenderer(
            key_order=["event", "request_id"])
    ],
    context_class=structlog.threadlocal.wrap_dict(dict),
    logger_factory=structlog.stdlib.LoggerFactory(),
)

REDIS_URL = os.environ.get("REDIS_URL")
if REDIS_URL:
    store = redis_store.RedisStore(url=REDIS_URL)
else:
    store = dict_store.DictionaryStore()

anonymous_quota = quota.Quota.per_hour(50)
authenticated_quota = quota.Quota.per_hour(5000, maximum_burst=500)
limiter = gcra.GenericCellRatelimiter(store=store)
anonymous_throttle = throttle.Throttle(rate=anonymous_quota, limiter=limiter)
authenticated_throttle = throttle.Throttle(rate=authenticated_quota,
                                           limiter=limiter)