authentik.api.tests.test_throttle

 1"""Tests for ``authentik.api.throttle.LocalAnonRateThrottle``."""
 2
 3from django.conf import settings
 4from django.core.cache import caches
 5from django.test import TestCase
 6
 7from authentik.api.throttle import LocalAnonRateThrottle
 8
 9
10class TestLocalAnonRateThrottle(TestCase):
11    """The throttle must use the in-process LocMem cache, not the default
12    (PG-backed) cache — otherwise every API request incurs 2 cache ops
13    against PG just to make the throttle decision."""
14
15    def test_throttle_uses_locmem_cache_backend(self):
16        """The throttle's ``cache`` attribute is the ``throttle`` alias."""
17        throttle = LocalAnonRateThrottle()
18        self.assertIs(throttle.cache, caches["throttle"])
19
20    def test_throttle_cache_alias_is_locmem(self):
21        """The ``throttle`` cache alias is backed by LocMemCache."""
22        self.assertEqual(
23            settings.CACHES["throttle"]["BACKEND"],
24            "django.core.cache.backends.locmem.LocMemCache",
25        )
26
27    def test_throttle_cache_has_sufficient_max_entries(self):
28        """``MAX_ENTRIES`` is high enough to avoid LRU-evicting active
29        counters under realistic IP diversity."""
30        max_entries = settings.CACHES["throttle"].get("OPTIONS", {}).get("MAX_ENTRIES", 300)
31        self.assertGreaterEqual(max_entries, 1000)
32
33    def test_throttle_counter_isolated_from_default_cache(self):
34        """Throttle writes go to the LocMem cache, not the default cache."""
35        throttle = LocalAnonRateThrottle()
36        test_key = "test-throttle-isolation-key"
37        throttle.cache.set(test_key, ["a", "b", "c"], 30)
38        self.assertEqual(throttle.cache.get(test_key), ["a", "b", "c"])
39        self.assertIsNone(caches["default"].get(test_key))
40        throttle.cache.delete(test_key)
41
42    def test_default_throttle_class_is_local_throttle(self):
43        """REST_FRAMEWORK uses ``LocalAnonRateThrottle``, not DRF's stock one."""
44        self.assertEqual(
45            settings.REST_FRAMEWORK["DEFAULT_THROTTLE_CLASSES"],
46            ["authentik.api.throttle.LocalAnonRateThrottle"],
47        )
class TestLocalAnonRateThrottle(django.test.testcases.TestCase):
11class TestLocalAnonRateThrottle(TestCase):
12    """The throttle must use the in-process LocMem cache, not the default
13    (PG-backed) cache — otherwise every API request incurs 2 cache ops
14    against PG just to make the throttle decision."""
15
16    def test_throttle_uses_locmem_cache_backend(self):
17        """The throttle's ``cache`` attribute is the ``throttle`` alias."""
18        throttle = LocalAnonRateThrottle()
19        self.assertIs(throttle.cache, caches["throttle"])
20
21    def test_throttle_cache_alias_is_locmem(self):
22        """The ``throttle`` cache alias is backed by LocMemCache."""
23        self.assertEqual(
24            settings.CACHES["throttle"]["BACKEND"],
25            "django.core.cache.backends.locmem.LocMemCache",
26        )
27
28    def test_throttle_cache_has_sufficient_max_entries(self):
29        """``MAX_ENTRIES`` is high enough to avoid LRU-evicting active
30        counters under realistic IP diversity."""
31        max_entries = settings.CACHES["throttle"].get("OPTIONS", {}).get("MAX_ENTRIES", 300)
32        self.assertGreaterEqual(max_entries, 1000)
33
34    def test_throttle_counter_isolated_from_default_cache(self):
35        """Throttle writes go to the LocMem cache, not the default cache."""
36        throttle = LocalAnonRateThrottle()
37        test_key = "test-throttle-isolation-key"
38        throttle.cache.set(test_key, ["a", "b", "c"], 30)
39        self.assertEqual(throttle.cache.get(test_key), ["a", "b", "c"])
40        self.assertIsNone(caches["default"].get(test_key))
41        throttle.cache.delete(test_key)
42
43    def test_default_throttle_class_is_local_throttle(self):
44        """REST_FRAMEWORK uses ``LocalAnonRateThrottle``, not DRF's stock one."""
45        self.assertEqual(
46            settings.REST_FRAMEWORK["DEFAULT_THROTTLE_CLASSES"],
47            ["authentik.api.throttle.LocalAnonRateThrottle"],
48        )

The throttle must use the in-process LocMem cache, not the default (PG-backed) cache — otherwise every API request incurs 2 cache ops against PG just to make the throttle decision.

def test_throttle_uses_locmem_cache_backend(self):
16    def test_throttle_uses_locmem_cache_backend(self):
17        """The throttle's ``cache`` attribute is the ``throttle`` alias."""
18        throttle = LocalAnonRateThrottle()
19        self.assertIs(throttle.cache, caches["throttle"])

The throttle's cache attribute is the throttle alias.

def test_throttle_cache_alias_is_locmem(self):
21    def test_throttle_cache_alias_is_locmem(self):
22        """The ``throttle`` cache alias is backed by LocMemCache."""
23        self.assertEqual(
24            settings.CACHES["throttle"]["BACKEND"],
25            "django.core.cache.backends.locmem.LocMemCache",
26        )

The throttle cache alias is backed by LocMemCache.

def test_throttle_cache_has_sufficient_max_entries(self):
28    def test_throttle_cache_has_sufficient_max_entries(self):
29        """``MAX_ENTRIES`` is high enough to avoid LRU-evicting active
30        counters under realistic IP diversity."""
31        max_entries = settings.CACHES["throttle"].get("OPTIONS", {}).get("MAX_ENTRIES", 300)
32        self.assertGreaterEqual(max_entries, 1000)

MAX_ENTRIES is high enough to avoid LRU-evicting active counters under realistic IP diversity.

def test_throttle_counter_isolated_from_default_cache(self):
34    def test_throttle_counter_isolated_from_default_cache(self):
35        """Throttle writes go to the LocMem cache, not the default cache."""
36        throttle = LocalAnonRateThrottle()
37        test_key = "test-throttle-isolation-key"
38        throttle.cache.set(test_key, ["a", "b", "c"], 30)
39        self.assertEqual(throttle.cache.get(test_key), ["a", "b", "c"])
40        self.assertIsNone(caches["default"].get(test_key))
41        throttle.cache.delete(test_key)

Throttle writes go to the LocMem cache, not the default cache.

def test_default_throttle_class_is_local_throttle(self):
43    def test_default_throttle_class_is_local_throttle(self):
44        """REST_FRAMEWORK uses ``LocalAnonRateThrottle``, not DRF's stock one."""
45        self.assertEqual(
46            settings.REST_FRAMEWORK["DEFAULT_THROTTLE_CLASSES"],
47            ["authentik.api.throttle.LocalAnonRateThrottle"],
48        )

REST_FRAMEWORK uses LocalAnonRateThrottle, not DRF's stock one.