diff --git a/awx/main/models/mixins.py b/awx/main/models/mixins.py index cb0a2236d4..e13b56fa87 100644 --- a/awx/main/models/mixins.py +++ b/awx/main/models/mixins.py @@ -170,13 +170,14 @@ class SurveyJobTemplateMixin(models.Model): errors.append("Value %s for '%s' expected to be a string." % (data[survey_element['variable']], survey_element['variable'])) return errors - if not data[survey_element['variable']] == '$encrypted$' and not survey_element['type'] == 'password': - if 'min' in survey_element and survey_element['min'] not in ["", None] and len(data[survey_element['variable']]) < int(survey_element['min']): - errors.append("'%s' value %s is too small (length is %s must be at least %s)." % - (survey_element['variable'], data[survey_element['variable']], len(data[survey_element['variable']]), survey_element['min'])) - if 'max' in survey_element and survey_element['max'] not in ["", None] and len(data[survey_element['variable']]) > int(survey_element['max']): - errors.append("'%s' value %s is too large (must be no more than %s)." % - (survey_element['variable'], data[survey_element['variable']], survey_element['max'])) + + if 'min' in survey_element and survey_element['min'] not in ["", None] and len(data[survey_element['variable']]) < int(survey_element['min']): + errors.append("'%s' value %s is too small (length is %s must be at least %s)." % + (survey_element['variable'], data[survey_element['variable']], len(data[survey_element['variable']]), survey_element['min'])) + if 'max' in survey_element and survey_element['max'] not in ["", None] and len(data[survey_element['variable']]) > int(survey_element['max']): + errors.append("'%s' value %s is too large (must be no more than %s)." % + (survey_element['variable'], data[survey_element['variable']], survey_element['max'])) + elif survey_element['type'] == 'integer': if survey_element['variable'] in data: if type(data[survey_element['variable']]) != int: diff --git a/awx/main/tests/factories/tower.py b/awx/main/tests/factories/tower.py index aa273227d2..a8f20f941f 100644 --- a/awx/main/tests/factories/tower.py +++ b/awx/main/tests/factories/tower.py @@ -139,7 +139,7 @@ def create_instance_group(name, instances=None): return mk_instance_group(name=name, instance=instances) -def create_survey_spec(variables=None, default_type='integer', required=True): +def create_survey_spec(variables=None, default_type='integer', required=True, min=None, max=None): ''' Returns a valid survey spec for a job template, based on the input argument specifying variable name(s) @@ -174,10 +174,14 @@ def create_survey_spec(variables=None, default_type='integer', required=True): spec_item.setdefault('question_description', "A question about %s." % var_name) if spec_item['type'] == 'integer': spec_item.setdefault('default', 0) - spec_item.setdefault('max', spec_item['default'] + 100) - spec_item.setdefault('min', spec_item['default'] - 100) + spec_item.setdefault('max', max or spec_item['default'] + 100) + spec_item.setdefault('min', min or spec_item['default'] - 100) else: spec_item.setdefault('default', '') + if min: + spec_item.setdefault('min', min) + if max: + spec_item.setdefault('max', max) spec.append(spec_item) survey_spec = {} diff --git a/awx/main/tests/unit/models/test_survey_models.py b/awx/main/tests/unit/models/test_survey_models.py index d9642a9e17..967ac406c1 100644 --- a/awx/main/tests/unit/models/test_survey_models.py +++ b/awx/main/tests/unit/models/test_survey_models.py @@ -93,24 +93,29 @@ def test_update_kwargs_survey_invalid_default(survey_spec_factory): @pytest.mark.survey -@pytest.mark.parametrize("question_type,default,expect_use,expect_value", [ - ("multiplechoice", "", False, 'N/A'), # historical bug - ("multiplechoice", "zeb", False, 'N/A'), # zeb not in choices - ("multiplechoice", "coffee", True, 'coffee'), - ("multiselect", None, False, 'N/A'), # NOTE: Behavior is arguable, value of [] may be prefered - ("multiselect", "", False, 'N/A'), - ("multiselect", ["zeb"], False, 'N/A'), - ("multiselect", ["milk"], True, ["milk"]), - ("multiselect", ["orange\nmilk"], False, 'N/A'), # historical bug +@pytest.mark.parametrize("question_type,default,min,max,expect_use,expect_value", [ + ("text", "", 0, 0, True, ''), # default used + ("text", "", 1, 0, False, 'N/A'), # value less than min length + ("password", "", 1, 0, False, 'N/A'), # passwords behave the same as text + ("multiplechoice", "", 0, 0, False, 'N/A'), # historical bug + ("multiplechoice", "zeb", 0, 0, False, 'N/A'), # zeb not in choices + ("multiplechoice", "coffee", 0, 0, True, 'coffee'), + ("multiselect", None, 0, 0, False, 'N/A'), # NOTE: Behavior is arguable, value of [] may be prefered + ("multiselect", "", 0, 0, False, 'N/A'), + ("multiselect", ["zeb"], 0, 0, False, 'N/A'), + ("multiselect", ["milk"], 0, 0, True, ["milk"]), + ("multiselect", ["orange\nmilk"], 0, 0, False, 'N/A'), # historical bug ]) def test_optional_survey_question_defaults( - survey_spec_factory, question_type, default, expect_use, expect_value): + survey_spec_factory, question_type, default, min, max, expect_use, expect_value): spec = survey_spec_factory([ { "required": False, "default": default, "choices": "orange\nmilk\nchocolate\ncoffee", "variable": "c", + "min": min, + "max": max, "type": question_type }, ])