From 4154b625657715dc4960bed8309eacfa630468f6 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 22 Mar 2022 20:34:00 +0100 Subject: [PATCH] stages/prompt: fix non-required fields not allowing blank values, add more tests closes #2544 Signed-off-by: Jens Langhammer --- authentik/stages/prompt/models.py | 2 ++ authentik/stages/prompt/tests.py | 11 ++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/authentik/stages/prompt/models.py b/authentik/stages/prompt/models.py index 264f3b06d..bf196381b 100644 --- a/authentik/stages/prompt/models.py +++ b/authentik/stages/prompt/models.py @@ -119,10 +119,12 @@ class Prompt(SerializerModel): } if self.type == FieldTypes.TEXT: kwargs["trim_whitespace"] = False + kwargs["allow_blank"] = not self.required if self.type == FieldTypes.TEXT_READ_ONLY: field_class = ReadOnlyField if self.type == FieldTypes.EMAIL: field_class = EmailField + kwargs["allow_blank"] = not self.required if self.type == FieldTypes.NUMBER: field_class = IntegerField if self.type == FieldTypes.CHECKBOX: diff --git a/authentik/stages/prompt/tests.py b/authentik/stages/prompt/tests.py index cabb6e6a2..6e0ef2491 100644 --- a/authentik/stages/prompt/tests.py +++ b/authentik/stages/prompt/tests.py @@ -292,7 +292,7 @@ class TestPromptStage(FlowTestCase): prompt.save() -def field_type_tester_factory(field_type: FieldTypes): +def field_type_tester_factory(field_type: FieldTypes, required: bool): """Test field for field_type""" def tester(self: TestPromptStage): @@ -304,11 +304,16 @@ def field_type_tester_factory(field_type: FieldTypes): placeholder_expression=False, sub_text="test", order=123, + required=required, ) self.assertIsNotNone(prompt.field("foo")) return tester -for _type in FieldTypes: - setattr(TestPromptStage, f"test_field_type_{_type}", field_type_tester_factory(_type)) +for _required in (True, False): + for _type in FieldTypes: + test_name = f"test_field_type_{_type}" + if _required: + test_name += "_required" + setattr(TestPromptStage, test_name, field_type_tester_factory(_type, _required))