authentik.events.tests.test_utils

cleanse_dict/cleanse_item tests

 1"""cleanse_dict/cleanse_item tests"""
 2
 3from django.test import TestCase
 4
 5from authentik.events.utils import cleanse_dict
 6
 7
 8class TestCleanseDict(TestCase):
 9    """Test cleanse_dict/cleanse_item"""
10
11    def test_list_value_not_mutated_in_place(self):
12        """cleanse_dict must not mutate list/tuple/set values of the source dict it
13        is given -- callers rely on the passed-in dict remaining usable afterwards
14        (e.g. flow plan context, which is cleansed purely for tracing/audit purposes)."""
15        original_items = ["foo", "bar"]
16        # Any key containing "auth" (case-insensitive) matches Django's hidden_settings
17        # regex as a substring, which is true of every `goauthentik.io/...` flow
18        # plan context key.
19        source = {"goauthentik.io/some-key": original_items}
20
21        result = cleanse_dict(source)
22
23        self.assertEqual(original_items, ["foo", "bar"])
24        self.assertEqual(result["goauthentik.io/some-key"], ["********************"] * 2)
25
26    def test_set_value_not_mutated_in_place(self):
27        """Same as above, for a set value (which doesn't support item assignment at all)."""
28        original_items = {"foo"}
29        source = {"goauthentik.io/some-key": original_items}
30
31        result = cleanse_dict(source)
32
33        self.assertEqual(original_items, {"foo"})
34        self.assertEqual(result["goauthentik.io/some-key"], {"********************"})
class TestCleanseDict(django.test.testcases.TestCase):
 9class TestCleanseDict(TestCase):
10    """Test cleanse_dict/cleanse_item"""
11
12    def test_list_value_not_mutated_in_place(self):
13        """cleanse_dict must not mutate list/tuple/set values of the source dict it
14        is given -- callers rely on the passed-in dict remaining usable afterwards
15        (e.g. flow plan context, which is cleansed purely for tracing/audit purposes)."""
16        original_items = ["foo", "bar"]
17        # Any key containing "auth" (case-insensitive) matches Django's hidden_settings
18        # regex as a substring, which is true of every `goauthentik.io/...` flow
19        # plan context key.
20        source = {"goauthentik.io/some-key": original_items}
21
22        result = cleanse_dict(source)
23
24        self.assertEqual(original_items, ["foo", "bar"])
25        self.assertEqual(result["goauthentik.io/some-key"], ["********************"] * 2)
26
27    def test_set_value_not_mutated_in_place(self):
28        """Same as above, for a set value (which doesn't support item assignment at all)."""
29        original_items = {"foo"}
30        source = {"goauthentik.io/some-key": original_items}
31
32        result = cleanse_dict(source)
33
34        self.assertEqual(original_items, {"foo"})
35        self.assertEqual(result["goauthentik.io/some-key"], {"********************"})

Test cleanse_dict/cleanse_item

def test_list_value_not_mutated_in_place(self):
12    def test_list_value_not_mutated_in_place(self):
13        """cleanse_dict must not mutate list/tuple/set values of the source dict it
14        is given -- callers rely on the passed-in dict remaining usable afterwards
15        (e.g. flow plan context, which is cleansed purely for tracing/audit purposes)."""
16        original_items = ["foo", "bar"]
17        # Any key containing "auth" (case-insensitive) matches Django's hidden_settings
18        # regex as a substring, which is true of every `goauthentik.io/...` flow
19        # plan context key.
20        source = {"goauthentik.io/some-key": original_items}
21
22        result = cleanse_dict(source)
23
24        self.assertEqual(original_items, ["foo", "bar"])
25        self.assertEqual(result["goauthentik.io/some-key"], ["********************"] * 2)

cleanse_dict must not mutate list/tuple/set values of the source dict it is given -- callers rely on the passed-in dict remaining usable afterwards (e.g. flow plan context, which is cleansed purely for tracing/audit purposes).

def test_set_value_not_mutated_in_place(self):
27    def test_set_value_not_mutated_in_place(self):
28        """Same as above, for a set value (which doesn't support item assignment at all)."""
29        original_items = {"foo"}
30        source = {"goauthentik.io/some-key": original_items}
31
32        result = cleanse_dict(source)
33
34        self.assertEqual(original_items, {"foo"})
35        self.assertEqual(result["goauthentik.io/some-key"], {"********************"})

Same as above, for a set value (which doesn't support item assignment at all).