mirror of
https://github.com/ansible/awx.git
synced 2026-03-25 04:45:03 -02:30
Merge pull request #3557 from jangsutsr/3344_add_extra_vars_type_verification
Refactor and patch extra vars verification.
This commit is contained in:
@@ -340,6 +340,10 @@ class JobTemplate(UnifiedJobTemplate, JobOptions, ResourceMixin):
|
|||||||
errors.append("'%s' value missing" % survey_element['variable'])
|
errors.append("'%s' value missing" % survey_element['variable'])
|
||||||
elif survey_element['type'] in ["textarea", "text", "password"]:
|
elif survey_element['type'] in ["textarea", "text", "password"]:
|
||||||
if survey_element['variable'] in data:
|
if survey_element['variable'] in data:
|
||||||
|
if type(data[survey_element['variable']]) not in (str, unicode):
|
||||||
|
errors.append("Value %s for '%s' expected to be a string." % (data[survey_element['variable']],
|
||||||
|
survey_element['variable']))
|
||||||
|
continue
|
||||||
if 'min' in survey_element and survey_element['min'] not in ["", None] and len(data[survey_element['variable']]) < int(survey_element['min']):
|
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)." %
|
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']))
|
(survey_element['variable'], data[survey_element['variable']], len(data[survey_element['variable']]), survey_element['min']))
|
||||||
@@ -348,6 +352,10 @@ class JobTemplate(UnifiedJobTemplate, JobOptions, ResourceMixin):
|
|||||||
(survey_element['variable'], data[survey_element['variable']], survey_element['max']))
|
(survey_element['variable'], data[survey_element['variable']], survey_element['max']))
|
||||||
elif survey_element['type'] == 'integer':
|
elif survey_element['type'] == 'integer':
|
||||||
if survey_element['variable'] in data:
|
if survey_element['variable'] in data:
|
||||||
|
if type(data[survey_element['variable']]) != int:
|
||||||
|
errors.append("Value %s for '%s' expected to be an integer." % (data[survey_element['variable']],
|
||||||
|
survey_element['variable']))
|
||||||
|
continue
|
||||||
if 'min' in survey_element and survey_element['min'] not in ["", None] and survey_element['variable'] in data and \
|
if 'min' in survey_element and survey_element['min'] not in ["", None] and survey_element['variable'] in data and \
|
||||||
data[survey_element['variable']] < int(survey_element['min']):
|
data[survey_element['variable']] < int(survey_element['min']):
|
||||||
errors.append("'%s' value %s is too small (must be at least %s)." %
|
errors.append("'%s' value %s is too small (must be at least %s)." %
|
||||||
@@ -356,20 +364,18 @@ class JobTemplate(UnifiedJobTemplate, JobOptions, ResourceMixin):
|
|||||||
data[survey_element['variable']] > int(survey_element['max']):
|
data[survey_element['variable']] > int(survey_element['max']):
|
||||||
errors.append("'%s' value %s is too large (must be no more than %s)." %
|
errors.append("'%s' value %s is too large (must be no more than %s)." %
|
||||||
(survey_element['variable'], data[survey_element['variable']], survey_element['max']))
|
(survey_element['variable'], data[survey_element['variable']], survey_element['max']))
|
||||||
if type(data[survey_element['variable']]) != int:
|
|
||||||
errors.append("Value %s for '%s' expected to be an integer." % (data[survey_element['variable']],
|
|
||||||
survey_element['variable']))
|
|
||||||
elif survey_element['type'] == 'float':
|
elif survey_element['type'] == 'float':
|
||||||
if survey_element['variable'] in data:
|
if survey_element['variable'] in data:
|
||||||
|
if type(data[survey_element['variable']]) not in (float, int):
|
||||||
|
errors.append("Value %s for '%s' expected to be a numeric type." % (data[survey_element['variable']],
|
||||||
|
survey_element['variable']))
|
||||||
|
continue
|
||||||
if 'min' in survey_element and survey_element['min'] not in ["", None] and data[survey_element['variable']] < float(survey_element['min']):
|
if 'min' in survey_element and survey_element['min'] not in ["", None] and data[survey_element['variable']] < float(survey_element['min']):
|
||||||
errors.append("'%s' value %s is too small (must be at least %s)." %
|
errors.append("'%s' value %s is too small (must be at least %s)." %
|
||||||
(survey_element['variable'], data[survey_element['variable']], survey_element['min']))
|
(survey_element['variable'], data[survey_element['variable']], survey_element['min']))
|
||||||
if 'max' in survey_element and survey_element['max'] not in ["", None] and data[survey_element['variable']] > float(survey_element['max']):
|
if 'max' in survey_element and survey_element['max'] not in ["", None] and data[survey_element['variable']] > float(survey_element['max']):
|
||||||
errors.append("'%s' value %s is too large (must be no more than %s)." %
|
errors.append("'%s' value %s is too large (must be no more than %s)." %
|
||||||
(survey_element['variable'], data[survey_element['variable']], survey_element['max']))
|
(survey_element['variable'], data[survey_element['variable']], survey_element['max']))
|
||||||
if type(data[survey_element['variable']]) not in (float, int):
|
|
||||||
errors.append("Value %s for '%s' expected to be a numeric type." % (data[survey_element['variable']],
|
|
||||||
survey_element['variable']))
|
|
||||||
elif survey_element['type'] == 'multiselect':
|
elif survey_element['type'] == 'multiselect':
|
||||||
if survey_element['variable'] in data:
|
if survey_element['variable'] in data:
|
||||||
if type(data[survey_element['variable']]) != list:
|
if type(data[survey_element['variable']]) != list:
|
||||||
|
|||||||
@@ -50,3 +50,32 @@ def test_job_template_survey_password_redaction(job_template_with_survey_passwor
|
|||||||
"""Tests the JobTemplate model's funciton to redact passwords from
|
"""Tests the JobTemplate model's funciton to redact passwords from
|
||||||
extra_vars - used when creating a new job"""
|
extra_vars - used when creating a new job"""
|
||||||
assert job_template_with_survey_passwords_unit.survey_password_variables() == ['secret_key', 'SSN']
|
assert job_template_with_survey_passwords_unit.survey_password_variables() == ['secret_key', 'SSN']
|
||||||
|
|
||||||
|
def test_job_template_survey_variable_validation(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": "5",
|
||||||
|
"max": 1024,
|
||||||
|
"question_description": "",
|
||||||
|
"choices": "",
|
||||||
|
"variable": "a",
|
||||||
|
"question_name": "Whosyourdaddy",
|
||||||
|
"type": "text"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"name": ""
|
||||||
|
}
|
||||||
|
obj.survey_enabled = True
|
||||||
|
assert obj.survey_variable_validation({"a": 5}) == ["Value 5 for 'a' expected to be a string."]
|
||||||
|
|||||||
Reference in New Issue
Block a user