authentik.enterprise.reports.tests.test_schema

 1from django.test.testcases import TestCase
 2from drf_spectacular.generators import SchemaGenerator
 3
 4from authentik.enterprise.reports.tests.utils import patch_license
 5
 6
 7@patch_license
 8class TestSchemaMatch(TestCase):
 9    def setUp(self) -> None:
10        generator = SchemaGenerator()
11        self.schema = generator.get_schema(request=None, public=True)
12
13    def _index_params_by_name(self, parameters):
14        result = {}
15        for p in parameters or []:
16            if p.get("in") != "query":
17                continue
18            schema = p.get("schema", {})
19            result[p["name"]] = {
20                "required": p.get("required", False),
21                "type": schema.get("type"),
22                "format": schema.get("format"),
23                "enum": tuple(schema.get("enum", [])),
24            }
25        return result
26
27    def _find_operation_by_operation_id(self, operation_id):
28        for path_item in self.schema.get("paths", {}).values():
29            for operation in path_item.values():
30                if isinstance(operation, dict) and operation.get("operationId") == operation_id:
31                    return operation
32        raise AssertionError(f"operationId '{operation_id}' not found in schema")
33
34    def _get_op_params(self, operation_id):
35        operation = self._find_operation_by_operation_id(operation_id)
36        return self._index_params_by_name(operation.get("parameters", []))
37
38    def test_user_export_action_query_params_match_list(self):
39        list_params = self._get_op_params("core_users_list")
40        del list_params["include_groups"]  # Not applicable for export
41        del list_params["include_roles"]  # Not applicable for export
42        export_params = self._get_op_params("core_users_export_create")
43        self.assertDictEqual(list_params, export_params)
44
45    def test_event_export_action_query_params_match_list(self):
46        list_params = self._get_op_params("events_events_list")
47        export_params = self._get_op_params("events_events_export_create")
48        self.assertDictEqual(list_params, export_params)
@patch_license
class TestSchemaMatch(django.test.testcases.TestCase):
 8@patch_license
 9class TestSchemaMatch(TestCase):
10    def setUp(self) -> None:
11        generator = SchemaGenerator()
12        self.schema = generator.get_schema(request=None, public=True)
13
14    def _index_params_by_name(self, parameters):
15        result = {}
16        for p in parameters or []:
17            if p.get("in") != "query":
18                continue
19            schema = p.get("schema", {})
20            result[p["name"]] = {
21                "required": p.get("required", False),
22                "type": schema.get("type"),
23                "format": schema.get("format"),
24                "enum": tuple(schema.get("enum", [])),
25            }
26        return result
27
28    def _find_operation_by_operation_id(self, operation_id):
29        for path_item in self.schema.get("paths", {}).values():
30            for operation in path_item.values():
31                if isinstance(operation, dict) and operation.get("operationId") == operation_id:
32                    return operation
33        raise AssertionError(f"operationId '{operation_id}' not found in schema")
34
35    def _get_op_params(self, operation_id):
36        operation = self._find_operation_by_operation_id(operation_id)
37        return self._index_params_by_name(operation.get("parameters", []))
38
39    def test_user_export_action_query_params_match_list(self):
40        list_params = self._get_op_params("core_users_list")
41        del list_params["include_groups"]  # Not applicable for export
42        del list_params["include_roles"]  # Not applicable for export
43        export_params = self._get_op_params("core_users_export_create")
44        self.assertDictEqual(list_params, export_params)
45
46    def test_event_export_action_query_params_match_list(self):
47        list_params = self._get_op_params("events_events_list")
48        export_params = self._get_op_params("events_events_export_create")
49        self.assertDictEqual(list_params, export_params)

Similar to TransactionTestCase, but use transaction.atomic() to achieve test isolation.

In most situations, TestCase should be preferred to TransactionTestCase as it allows faster execution. However, there are some situations where using TransactionTestCase might be necessary (e.g. testing some transactional behavior).

On database backends with no transaction support, TestCase behaves as TransactionTestCase.

def setUp(self) -> None:
10    def setUp(self) -> None:
11        generator = SchemaGenerator()
12        self.schema = generator.get_schema(request=None, public=True)

Hook method for setting up the test fixture before exercising it.

def test_user_export_action_query_params_match_list(self):
39    def test_user_export_action_query_params_match_list(self):
40        list_params = self._get_op_params("core_users_list")
41        del list_params["include_groups"]  # Not applicable for export
42        del list_params["include_roles"]  # Not applicable for export
43        export_params = self._get_op_params("core_users_export_create")
44        self.assertDictEqual(list_params, export_params)
def test_event_export_action_query_params_match_list(self):
46    def test_event_export_action_query_params_match_list(self):
47        list_params = self._get_op_params("events_events_list")
48        export_params = self._get_op_params("events_events_export_create")
49        self.assertDictEqual(list_params, export_params)