From 776c3128b8e905a39eb678231015654664181ab5 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 27 Apr 2021 16:50:05 +0200 Subject: [PATCH] flows: add tests for stage type, component and ui_user_settings Signed-off-by: Jens Langhammer --- authentik/api/tests/test_config.py | 16 +++++++++++ authentik/core/api/utils.py | 6 ++-- authentik/flows/tests/test_stage_model.py | 35 +++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 authentik/api/tests/test_config.py create mode 100644 authentik/flows/tests/test_stage_model.py diff --git a/authentik/api/tests/test_config.py b/authentik/api/tests/test_config.py new file mode 100644 index 000000000..8e31e3ee0 --- /dev/null +++ b/authentik/api/tests/test_config.py @@ -0,0 +1,16 @@ +"""Test config API""" +from json import loads + +from django.urls import reverse +from rest_framework.test import APITestCase + + +class TestConfig(APITestCase): + """Test config API""" + + def test_config(self): + """Test YAML generation""" + response = self.client.get( + reverse("authentik_api:configs-list"), + ) + self.assertTrue(loads(response.content.decode())) diff --git a/authentik/core/api/utils.py b/authentik/core/api/utils.py index 5005090c3..7b8a98872 100644 --- a/authentik/core/api/utils.py +++ b/authentik/core/api/utils.py @@ -20,10 +20,12 @@ def is_dict(value: Any): class PassiveSerializer(Serializer): """Base serializer class which doesn't implement create/update methods""" - def create(self, validated_data: dict) -> Model: + def create(self, validated_data: dict) -> Model: # pragma: no cover return Model() - def update(self, instance: Model, validated_data: dict) -> Model: + def update( + self, instance: Model, validated_data: dict + ) -> Model: # pragma: no cover return Model() diff --git a/authentik/flows/tests/test_stage_model.py b/authentik/flows/tests/test_stage_model.py new file mode 100644 index 000000000..fe887ee89 --- /dev/null +++ b/authentik/flows/tests/test_stage_model.py @@ -0,0 +1,35 @@ +"""base model tests""" +from typing import Callable, Type + +from django.test import TestCase + +from authentik.flows.models import Stage +from authentik.flows.stage import StageView +from authentik.lib.utils.reflection import all_subclasses + + +class TestModels(TestCase): + """Generic model properties tests""" + + +def model_tester_factory(test_model: Type[Stage]) -> Callable: + """Test a form""" + + def tester(self: TestModels): + try: + model_class = None + if test_model._meta.abstract: + model_class = test_model.__bases__[0]() + else: + model_class = test_model() + self.assertTrue(issubclass(model_class.type, StageView)) + self.assertIsNotNone(test_model.component) + _ = test_model.ui_user_settings + except NotImplementedError: + pass + + return tester + + +for model in all_subclasses(Stage): + setattr(TestModels, f"test_model_{model.__name__}", model_tester_factory(model))