From c6209df1e0c6247817747a17b01f3b65f756fdb1 Mon Sep 17 00:00:00 2001 From: Kersom <9053044+nixocio@users.noreply.github.com> Date: Fri, 4 Mar 2022 14:03:17 -0500 Subject: [PATCH] Api issue float (#11757) * Fix integer/float errors in survey * Add SURVEY_TYPE_MAPPING to constants Add SURVEY_TYPE_MAPPING to constants, and replace usage in a couple of files. Co-authored-by: Alexander Komarov --- awx/api/views/__init__.py | 10 +++--- awx/main/constants.py | 2 ++ .../tests/unit/models/test_survey_models.py | 32 +++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/awx/api/views/__init__.py b/awx/api/views/__init__.py index 51ab4c9dd2..163fa4e727 100644 --- a/awx/api/views/__init__.py +++ b/awx/api/views/__init__.py @@ -113,7 +113,7 @@ from awx.api.permissions import ( from awx.api import renderers from awx.api import serializers from awx.api.metadata import RoleMetadata -from awx.main.constants import ACTIVE_STATES +from awx.main.constants import ACTIVE_STATES, SURVEY_TYPE_MAPPING from awx.main.scheduler.dag_workflow import WorkflowDAG from awx.api.views.mixin import ( ControlledByScmMixin, @@ -2468,8 +2468,6 @@ class JobTemplateSurveySpec(GenericAPIView): obj_permission_type = 'admin' serializer_class = serializers.EmptySerializer - ALLOWED_TYPES = {'text': str, 'textarea': str, 'password': str, 'multiplechoice': str, 'multiselect': str, 'integer': int, 'float': float} - def get(self, request, *args, **kwargs): obj = self.get_object() return Response(obj.display_survey_spec()) @@ -2540,17 +2538,17 @@ class JobTemplateSurveySpec(GenericAPIView): # Type-specific validation # validate question type <-> default type qtype = survey_item["type"] - if qtype not in JobTemplateSurveySpec.ALLOWED_TYPES: + if qtype not in SURVEY_TYPE_MAPPING: return Response( dict( error=_("'{survey_item[type]}' in survey question {idx} is not one of '{allowed_types}' allowed question types.").format( - allowed_types=', '.join(JobTemplateSurveySpec.ALLOWED_TYPES.keys()), **context + allowed_types=', '.join(SURVEY_TYPE_MAPPING.keys()), **context ) ), status=status.HTTP_400_BAD_REQUEST, ) if 'default' in survey_item and survey_item['default'] != '': - if not isinstance(survey_item['default'], JobTemplateSurveySpec.ALLOWED_TYPES[qtype]): + if not isinstance(survey_item['default'], SURVEY_TYPE_MAPPING[qtype]): type_label = 'string' if qtype in ['integer', 'float']: type_label = qtype diff --git a/awx/main/constants.py b/awx/main/constants.py index 9074d9bd7f..d87bf82983 100644 --- a/awx/main/constants.py +++ b/awx/main/constants.py @@ -93,3 +93,5 @@ JOB_FOLDER_PREFIX = 'awx_%s_' # /HOST-DIR:/CONTAINER-DIR:OPTIONS CONTAINER_VOLUMES_MOUNT_TYPES = ['z', 'O', 'ro', 'rw'] MAX_ISOLATED_PATH_COLON_DELIMITER = 2 + +SURVEY_TYPE_MAPPING = {'text': str, 'textarea': str, 'password': str, 'multiplechoice': str, 'multiselect': str, 'integer': int, 'float': (float, int)} diff --git a/awx/main/tests/unit/models/test_survey_models.py b/awx/main/tests/unit/models/test_survey_models.py index c3c9a8723f..9ec5673cd8 100644 --- a/awx/main/tests/unit/models/test_survey_models.py +++ b/awx/main/tests/unit/models/test_survey_models.py @@ -59,6 +59,38 @@ class SurveyVariableValidation: assert accepted == {} assert str(errors[0]) == "Value 5 for 'a' expected to be a string." + def test_job_template_survey_default_variable_validation(self, job_template_factory): + objects = job_template_factory( + "survey_variable_validation", + organization="org1", + inventory="inventory1", + credential="cred1", + persisted=False, + ) + obj = objects.job_template + obj.survey_spec = { + "description": "", + "spec": [ + { + "required": True, + "min": 0, + "default": "2", + "max": 1024, + "question_description": "", + "choices": "", + "variable": "a", + "question_name": "float_number", + "type": "float", + } + ], + "name": "", + } + + obj.survey_enabled = True + accepted, _, errors = obj.accept_or_ignore_variables({"a": 2}) + assert accepted == {{"a": 2.0}} + assert not errors + @pytest.fixture def job(mocker):