mirror of
https://github.com/ansible/awx.git
synced 2026-05-08 09:57:35 -02:30
humble beginnings of survey question type validation
This commit is contained in:
@@ -285,9 +285,7 @@ def test_survey_spec_passwords_with_default_required(job_template_factory, post,
|
||||
@pytest.mark.django_db
|
||||
@pytest.mark.parametrize('default, status', [
|
||||
('SUPERSECRET', 200),
|
||||
(['some', 'invalid', 'list'], 400),
|
||||
({'some-invalid': 'dict'}, 400),
|
||||
(False, 400)
|
||||
])
|
||||
def test_survey_spec_default_passwords_are_encrypted(job_template, post, admin_user, default, status):
|
||||
job_template.survey_enabled = True
|
||||
@@ -317,7 +315,7 @@ def test_survey_spec_default_passwords_are_encrypted(job_template, post, admin_u
|
||||
'secret_value': default
|
||||
}
|
||||
else:
|
||||
assert "for 'secret_value' expected to be a string." in str(resp.data)
|
||||
assert "expected to be string." in str(resp.data)
|
||||
|
||||
|
||||
@mock.patch('awx.api.views.feature_enabled', lambda feature: True)
|
||||
@@ -346,37 +344,6 @@ def test_survey_spec_default_passwords_encrypted_on_update(job_template, post, p
|
||||
assert updated_jt.survey_spec == JobTemplate.objects.get(pk=job_template.pk).survey_spec
|
||||
|
||||
|
||||
# Tests related to survey content validation
|
||||
@mock.patch('awx.api.views.feature_enabled', lambda feature: True)
|
||||
@pytest.mark.django_db
|
||||
@pytest.mark.survey
|
||||
def test_survey_spec_non_dict_error(deploy_jobtemplate, post, admin_user):
|
||||
"""When a question doesn't follow the standard format, verify error thrown."""
|
||||
response = post(
|
||||
url=reverse('api:job_template_survey_spec', kwargs={'pk': deploy_jobtemplate.id}),
|
||||
data={
|
||||
"description": "Email of the submitter",
|
||||
"spec": ["What is your email?"], "name": "Email survey"
|
||||
},
|
||||
user=admin_user,
|
||||
expect=400
|
||||
)
|
||||
assert response.data['error'] == "Survey question 0 is not a json object."
|
||||
|
||||
|
||||
@mock.patch('awx.api.views.feature_enabled', lambda feature: True)
|
||||
@pytest.mark.django_db
|
||||
@pytest.mark.survey
|
||||
def test_survey_spec_dual_names_error(survey_spec_factory, deploy_jobtemplate, post, user):
|
||||
response = post(
|
||||
url=reverse('api:job_template_survey_spec', kwargs={'pk': deploy_jobtemplate.id}),
|
||||
data=survey_spec_factory(['submitter_email', 'submitter_email']),
|
||||
user=user('admin', True),
|
||||
expect=400
|
||||
)
|
||||
assert response.data['error'] == "'variable' 'submitter_email' duplicated in survey question 1."
|
||||
|
||||
|
||||
# Test actions that should be allowed with non-survey license
|
||||
@mock.patch('awx.main.access.BaseAccess.check_license', new=mock_no_surveys)
|
||||
@pytest.mark.django_db
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import mock
|
||||
import pytest
|
||||
import requests
|
||||
@@ -293,7 +294,7 @@ class TestSurveySpecValidation:
|
||||
new['spec'][0]['default'] = '$encrypted$'
|
||||
new['spec'][0]['required'] = False
|
||||
resp = view._validate_spec_data(new, old)
|
||||
assert resp is None
|
||||
assert resp is None, resp.data
|
||||
assert new == {
|
||||
"name": "old survey",
|
||||
"description": "foobar",
|
||||
@@ -357,3 +358,59 @@ class TestSurveySpecValidation:
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
@staticmethod
|
||||
def spec_from_element(survey_item):
|
||||
survey_item.setdefault('name', 'foo')
|
||||
survey_item.setdefault('variable', 'foo')
|
||||
survey_item.setdefault('required', False)
|
||||
survey_item.setdefault('question_name', 'foo')
|
||||
survey_item.setdefault('type', 'text')
|
||||
spec = {
|
||||
'name': 'test survey',
|
||||
'description': 'foo',
|
||||
'spec': [survey_item]
|
||||
}
|
||||
return spec
|
||||
|
||||
|
||||
@pytest.mark.parametrize("survey_item, error_text", [
|
||||
({'type': 'password', 'default': ['some', 'invalid', 'list']}, 'expected to be string'),
|
||||
({'type': 'password', 'default': False}, 'expected to be string'),
|
||||
({'type': 'integer', 'default': 'foo'}, 'expected to be int'),
|
||||
({'type': 'integer', 'default': u'🐉'}, 'expected to be int'),
|
||||
({'type': 'foo'}, 'allowed question types'),
|
||||
({'type': u'🐉'}, 'allowed question types'),
|
||||
({'type': 'multiplechoice'}, 'multiplechoice must specify choices'),
|
||||
({'type': 'multiplechoice', 'choices': 45}, 'Choices in survey question 0 expected to be string'),
|
||||
({'type': 'integer', 'min': 'foo'}, 'min limit in survey question 0 expected to be integer'),
|
||||
({'question_name': 42}, "'question_name' in survey question 0 expected to be string.")
|
||||
])
|
||||
def test_survey_question_element_validation(self, survey_item, error_text):
|
||||
spec = self.spec_from_element(survey_item)
|
||||
r = JobTemplateSurveySpec._validate_spec_data(spec, {})
|
||||
assert r is not None, (spec, error_text)
|
||||
assert 'error' in r.data
|
||||
assert error_text in r.data['error']
|
||||
|
||||
|
||||
def test_survey_spec_non_dict_error(self):
|
||||
spec = self.spec_from_element({})
|
||||
spec['spec'][0] = 'foo'
|
||||
r = JobTemplateSurveySpec._validate_spec_data(spec, {})
|
||||
assert 'Survey question 0 is not a json object' in r.data['error']
|
||||
|
||||
|
||||
def test_survey_spec_dual_names_error(self):
|
||||
spec = self.spec_from_element({})
|
||||
spec['spec'].append(spec['spec'][0].copy())
|
||||
r = JobTemplateSurveySpec._validate_spec_data(spec, {})
|
||||
assert "'variable' 'foo' duplicated in survey question 1." in r.data['error']
|
||||
|
||||
|
||||
def test_survey_spec_element_missing_property(self):
|
||||
spec = self.spec_from_element({})
|
||||
spec['spec'][0].pop('type')
|
||||
r = JobTemplateSurveySpec._validate_spec_data(spec, {})
|
||||
assert "'type' missing from survey question 0" in r.data['error']
|
||||
|
||||
Reference in New Issue
Block a user