From 92efb2d3172d153ae0949eb078c24b4459056635 Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Thu, 5 May 2016 15:53:35 -0400 Subject: [PATCH] exception for dual-name questions and more tests --- awx/api/views.py | 5 ++ .../functional/api/test_survey_spec_view.py | 65 +++++++++++++++++-- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/awx/api/views.py b/awx/api/views.py index aa476f18c6..29f9cea95d 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -2229,6 +2229,7 @@ class JobTemplateSurveySpec(GenericAPIView): if len(obj.survey_spec["spec"]) < 1: return Response(dict(error="'spec' doesn't contain any items"), status=status.HTTP_400_BAD_REQUEST) idx = 0 + variable_set = set() for survey_item in obj.survey_spec["spec"]: 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) @@ -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) if "variable" not in survey_item: 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: return Response(dict(error="'required' missing from survey element %s" % str(idx)), status=status.HTTP_400_BAD_REQUEST) idx += 1 diff --git a/awx/main/tests/functional/api/test_survey_spec_view.py b/awx/main/tests/functional/api/test_survey_spec_view.py index 87d67e7b2e..27e93a2a5b 100644 --- a/awx/main/tests/functional/api/test_survey_spec_view.py +++ b/awx/main/tests/functional/api/test_survey_spec_view.py @@ -1,10 +1,8 @@ import mock import pytest -import json -from awx.main.utils import timestamp_apiformat 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): return True @@ -40,6 +38,65 @@ def test_survey_spec_view_allowed(deploy_jobtemplate, get, user): response = get(spec_url, user('admin', True)) 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"