mirror of
https://github.com/ansible/awx.git
synced 2026-05-13 04:17:36 -02:30
exception for dual-name questions and more tests
This commit is contained in:
@@ -2229,6 +2229,7 @@ class JobTemplateSurveySpec(GenericAPIView):
|
|||||||
if len(obj.survey_spec["spec"]) < 1:
|
if len(obj.survey_spec["spec"]) < 1:
|
||||||
return Response(dict(error="'spec' doesn't contain any items"), status=status.HTTP_400_BAD_REQUEST)
|
return Response(dict(error="'spec' doesn't contain any items"), status=status.HTTP_400_BAD_REQUEST)
|
||||||
idx = 0
|
idx = 0
|
||||||
|
variable_set = set()
|
||||||
for survey_item in obj.survey_spec["spec"]:
|
for survey_item in obj.survey_spec["spec"]:
|
||||||
if not isinstance(survey_item, dict):
|
if not isinstance(survey_item, dict):
|
||||||
return Response(dict(error="survey element %s is not a json object" % str(idx)), status=status.HTTP_400_BAD_REQUEST)
|
return Response(dict(error="survey element %s is not a json object" % str(idx)), status=status.HTTP_400_BAD_REQUEST)
|
||||||
@@ -2238,6 +2239,10 @@ class JobTemplateSurveySpec(GenericAPIView):
|
|||||||
return Response(dict(error="'question_name' missing from survey element %s" % str(idx)), status=status.HTTP_400_BAD_REQUEST)
|
return Response(dict(error="'question_name' missing from survey element %s" % str(idx)), status=status.HTTP_400_BAD_REQUEST)
|
||||||
if "variable" not in survey_item:
|
if "variable" not in survey_item:
|
||||||
return Response(dict(error="'variable' missing from survey element %s" % str(idx)), status=status.HTTP_400_BAD_REQUEST)
|
return Response(dict(error="'variable' missing from survey element %s" % str(idx)), status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
if survey_item['variable'] in variable_set:
|
||||||
|
return Response(dict(error="'variable' name '%s' duplicated in survey element %s" % (survey_item['variable'], str(idx))), status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
else:
|
||||||
|
variable_set.add(survey_item['variable'])
|
||||||
if "required" not in survey_item:
|
if "required" not in survey_item:
|
||||||
return Response(dict(error="'required' missing from survey element %s" % str(idx)), status=status.HTTP_400_BAD_REQUEST)
|
return Response(dict(error="'required' missing from survey element %s" % str(idx)), status=status.HTTP_400_BAD_REQUEST)
|
||||||
idx += 1
|
idx += 1
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
import mock
|
import mock
|
||||||
import pytest
|
import pytest
|
||||||
import json
|
|
||||||
|
|
||||||
from awx.main.utils import timestamp_apiformat
|
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
from django.utils import timezone
|
from awx.main.models.jobs import JobTemplate
|
||||||
|
|
||||||
def mock_feature_enabled(feature, bypass_database=None):
|
def mock_feature_enabled(feature, bypass_database=None):
|
||||||
return True
|
return True
|
||||||
@@ -40,6 +38,65 @@ def test_survey_spec_view_allowed(deploy_jobtemplate, get, user):
|
|||||||
response = get(spec_url, user('admin', True))
|
response = get(spec_url, user('admin', True))
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
print response.data
|
|
||||||
|
|
||||||
|
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
|
||||||
|
@pytest.mark.django_db
|
||||||
|
@pytest.mark.survey
|
||||||
|
def test_survey_spec_sucessful_creation(deploy_jobtemplate, post, user):
|
||||||
|
spec_url = reverse('api:job_template_survey_spec', args=(deploy_jobtemplate.id,))
|
||||||
|
response = post(
|
||||||
|
url=spec_url,
|
||||||
|
data={
|
||||||
|
"description": "Email of the submitter",
|
||||||
|
"spec": [{
|
||||||
|
"variable": "submitter_email",
|
||||||
|
"question_name": "Enter your email",
|
||||||
|
"type": "text",
|
||||||
|
"required": False
|
||||||
|
}],
|
||||||
|
"name": "Email survey"
|
||||||
|
},
|
||||||
|
user=user('admin', True))
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
|
||||||
|
@pytest.mark.django_db
|
||||||
|
@pytest.mark.survey
|
||||||
|
def test_survey_spec_non_dict_error(deploy_jobtemplate, post, user):
|
||||||
|
spec_url = reverse('api:job_template_survey_spec', args=(deploy_jobtemplate.id,))
|
||||||
|
response = post(
|
||||||
|
url=spec_url,
|
||||||
|
data={"description": "Email of the submitter",
|
||||||
|
"spec": ["What is your email?"], "name": "Email survey"},
|
||||||
|
user=user('admin', True))
|
||||||
|
|
||||||
|
assert response.status_code == 400
|
||||||
|
assert response.data['error'] == "survey element 0 is not a json object"
|
||||||
|
|
||||||
|
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
|
||||||
|
@pytest.mark.django_db
|
||||||
|
@pytest.mark.survey
|
||||||
|
def test_survey_spec_dual_names_error(deploy_jobtemplate, post, user):
|
||||||
|
spec_url = reverse('api:job_template_survey_spec', args=(deploy_jobtemplate.id,))
|
||||||
|
response = post(
|
||||||
|
url=spec_url,
|
||||||
|
data={
|
||||||
|
"description": "Email of the submitter",
|
||||||
|
"spec": [{
|
||||||
|
"variable": "submitter_email",
|
||||||
|
"question_name": "Enter your email",
|
||||||
|
"type": "text",
|
||||||
|
"required": False
|
||||||
|
}, {
|
||||||
|
"variable": "submitter_email",
|
||||||
|
"question_name": "Same variable as last question",
|
||||||
|
"type": "integer",
|
||||||
|
"required": False
|
||||||
|
}],
|
||||||
|
"name": "Email survey"
|
||||||
|
},
|
||||||
|
user=user('admin', True))
|
||||||
|
|
||||||
|
assert response.status_code == 400
|
||||||
|
assert response.data['error'] == "'variable' name 'submitter_email' duplicated in survey element 1"
|
||||||
|
|||||||
Reference in New Issue
Block a user