Ejemplo n.º 1
0
    def test_set_with_time_uses_now(self):
        """Verify we can add data with the current time."""
        store = dictstore.DictionaryStore()
        new_data = limit_data.LimitData(used=9999, remaining=1)

        set_data = store.set_with_time(key="mykey", data=new_data)
        assert isinstance(set_data.time, datetime.datetime)
        assert store.get("mykey") != {}
Ejemplo n.º 2
0
    def test_get_with_time_defaults_to_now(self):
        """Verify we can retrieve data with a default time."""
        data = limit_data.LimitData(used=9999, remaining=1)
        store = dictstore.DictionaryStore(store={"mykey": data})

        dt, retrieved_data = store.get_with_time("mykey")
        assert dt.replace(second=0, microsecond=0) == datetime.datetime.now(
            datetime.timezone.utc).replace(second=0, microsecond=0)
        assert retrieved_data == data.copy_with(time=dt)
Ejemplo n.º 3
0
def test_periodic_end_to_end():
    """Verify our periodic limiter works behind our throttle."""
    rate = quota.Quota.per_minute(5)
    store = dstore.DictionaryStore()
    limiter = periodic.PeriodicLimiter(store=store)
    periodic_throttle = throttle.Throttle(rate=rate, limiter=limiter)

    assert periodic_throttle.check("periodic-end-to-end", 5).limited is False
    assert (periodic_throttle.check("periodic-end-to-end-2", 5).limited is
            False)
    assert periodic_throttle.check("periodic-end-to-end", 5).limited is True
    assert periodic_throttle.check("periodic-end-to-end-2", 5).limited is True
Ejemplo n.º 4
0
    def test_get_with_time_uses_existing_time(self):
        """Verify we can retrieve data from our datastore with its time."""
        data = limit_data.LimitData(
            used=9999,
            remaining=1,
            time=datetime.datetime(
                year=2018,
                month=12,
                day=4,
                hour=9,
                minute=0,
                second=0,
                tzinfo=datetime.timezone.utc,
            ),
        )
        store = dictstore.DictionaryStore(store={"mykey": data})

        dt, retrieved_data = store.get_with_time("mykey")
        assert dt == data.time
        assert retrieved_data == data
Ejemplo n.º 5
0
    def test_set_with_time_uses_provided_value(self):
        """Verify we can add data with a specific time."""
        store = dictstore.DictionaryStore()
        new_data = limit_data.LimitData(
            used=9999,
            remaining=1,
            time=datetime.datetime(
                year=2018,
                month=12,
                day=4,
                hour=9,
                minute=0,
                second=0,
                tzinfo=datetime.timezone.utc,
            ),
        )

        set_data = store.set_with_time(key="mykey", data=new_data)
        assert set_data == new_data
        assert store.get("mykey") == new_data
Ejemplo n.º 6
0
 def test_begins_life_empty(self):
     """Verify that by default no data exists."""
     store = dictstore.DictionaryStore()
     assert store.store == {}
Ejemplo n.º 7
0
    def test_get(self):
        """Verify we can retrieve data from our datastore."""
        data = limit_data.LimitData(used=9999, remaining=1)
        store = dictstore.DictionaryStore(store={"mykey": data})

        assert store.get("mykey") == data
Ejemplo n.º 8
0
    def test_set(self):
        """Verify we can add data."""
        store = dictstore.DictionaryStore()
        new_data = limit_data.LimitData(used=9999, remaining=1)

        assert store.set(key="mykey", data=new_data) == new_data
Ejemplo n.º 9
0
    def test_may_begin_life_with_data(self):
        """Verify that we can give it initial data."""
        data = {"a_key": limit_data.LimitData(used=9999, remaining=1)}
        store = dictstore.DictionaryStore(store=data)

        assert store.store == data
Ejemplo n.º 10
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)