From 4ddf79211d864104802c976d128f9aedf2358563 Mon Sep 17 00:00:00 2001
From: James Turk <dev@jamesturk.net>
Date: Tue, 13 Oct 2020 11:45:55 -0400
Subject: [PATCH] don't allow calling get_usage_since unless track_daily_usage
 was on

---
 rrl.py            |  2 ++
 test_ratelimit.py | 17 +++++++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/rrl.py b/rrl.py
index 018a3c7..7a64629 100644
--- a/rrl.py
+++ b/rrl.py
@@ -108,6 +108,8 @@ class RateLimiter:
         start: datetime.date,
         end: typing.Optional[datetime.date] = None,
     ) -> typing.List[DailyUsage]:
+        if not self.track_daily_usage:
+            raise RuntimeError("track_daily_usage is not enabled")
         if not end:
             end = datetime.date.today()
         days = []
diff --git a/test_ratelimit.py b/test_ratelimit.py
index b9b5aeb..8faf29a 100644
--- a/test_ratelimit.py
+++ b/test_ratelimit.py
@@ -110,3 +110,20 @@ def test_get_daily_usage():
     assert usage[9] == DailyUsage(datetime.date(2020, 1, 10), 0)
     assert usage[14] == DailyUsage(datetime.date(2020, 1, 15), 0)
     assert len(usage) == 15
+
+
+def test_get_daily_usage_untracked():
+    redis.flushall()
+    rl = RateLimiter(
+        tiers=[unlimited_tier], use_redis_time=False, track_daily_usage=False
+    )
+
+    # make Nth day have N calls
+    for n in range(1, 10):
+        with freeze_time(f"2020-01-0{n}"):
+            for _ in range(n):
+                rl.check_limit("zone", "test-key", unlimited_tier.name)
+
+    # values would be incorrect (likely zero), warn the caller
+    with pytest.raises(RuntimeError):
+        rl.get_usage_since("zone", "test-key", datetime.date(2020, 1, 1))