From fd09cc372606d16f14dfcb11c8fd7319c8203cdb Mon Sep 17 00:00:00 2001 From: Chris Church Date: Fri, 10 Apr 2015 16:20:57 -0400 Subject: [PATCH] Return survey_enabled as False for launch endpoint when no survey spec is defined for a job template; allow job template to be launched anyways. Fixes https://trello.com/c/zErQqf1L --- awx/api/views.py | 4 ++-- awx/main/models/jobs.py | 2 +- awx/main/tests/jobs/jobs_monolithic.py | 14 ++++++++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/awx/api/views.py b/awx/api/views.py index 10b72cc34e..d18c5ae6ea 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -1448,14 +1448,14 @@ class JobTemplateLaunch(GenericAPIView): data['ask_variables_on_launch'] = obj.ask_variables_on_launch data['variables_needed_to_start'] = obj.variables_needed_to_start data['credential_needed_to_start'] = obj.credential is None - data['survey_enabled'] = obj.survey_enabled + data['survey_enabled'] = obj.survey_enabled and 'spec' in obj.survey_spec return Response(data) def post(self, request, *args, **kwargs): obj = self.get_object() if not request.user.can_access(self.model, 'start', obj): raise PermissionDenied() - if obj.survey_enabled: + if obj.survey_enabled and 'spec' in obj.survey_spec: if request.DATA == "": request_data = {} else: diff --git a/awx/main/models/jobs.py b/awx/main/models/jobs.py index af73f2038c..78b45f5d12 100644 --- a/awx/main/models/jobs.py +++ b/awx/main/models/jobs.py @@ -229,7 +229,7 @@ class JobTemplate(UnifiedJobTemplate, JobOptions): errors.append("'name' missing from survey spec") if 'description' not in self.survey_spec: errors.append("'description' missing from survey spec") - for survey_element in self.survey_spec["spec"]: + for survey_element in self.survey_spec.get("spec", []): if survey_element['variable'] not in data and \ survey_element['required']: errors.append("'%s' value missing" % survey_element['variable']) diff --git a/awx/main/tests/jobs/jobs_monolithic.py b/awx/main/tests/jobs/jobs_monolithic.py index ac079fa7bd..a568fa6cda 100644 --- a/awx/main/tests/jobs/jobs_monolithic.py +++ b/awx/main/tests/jobs/jobs_monolithic.py @@ -1375,10 +1375,18 @@ class JobTemplateSurveyTest(BaseJobTestMixin, django.test.TestCase): args=(new_jt_id,)) self.assertEquals(response['url'], detail_url) url = reverse('api:job_template_survey_spec', args=(new_jt_id,)) + launch_url = reverse('api:job_template_launch', args=(new_jt_id,)) with self.current_user(self.user_sue): - response = self.post(url, json.loads(TEST_SIMPLE_REQUIRED_SURVEY), expect=200) - launch_url = reverse('api:job_template_launch', args=(new_jt_id,)) + # If no survey spec is available, survey_enabled on launch endpoint + # should return, and should be able to launch template without error. response = self.get(launch_url) + self.assertFalse(response['survey_enabled']) + self.post(launch_url, {}, expect=202) + # Now post a survey spec and check that the answer is set in the + # job's extra vars. + self.post(url, json.loads(TEST_SIMPLE_REQUIRED_SURVEY), expect=200) + response = self.get(launch_url) + self.assertTrue(response['survey_enabled']) self.assertTrue('favorite_color' in response['variables_needed_to_start']) response = self.post(launch_url, dict(extra_vars=dict(favorite_color="green")), expect=202) job = Job.objects.get(pk=response["job"]) @@ -1387,13 +1395,11 @@ class JobTemplateSurveyTest(BaseJobTestMixin, django.test.TestCase): with self.current_user(self.user_sue): response = self.post(url, json.loads(TEST_SIMPLE_NONREQUIRED_SURVEY), expect=200) - launch_url = reverse('api:job_template_launch', args=(new_jt_id,)) response = self.get(launch_url) self.assertTrue(len(response['variables_needed_to_start']) == 0) with self.current_user(self.user_sue): response = self.post(url, json.loads(TEST_SURVEY_REQUIREMENTS), expect=200) - launch_url = reverse('api:job_template_launch', args=(new_jt_id,)) # Just the required answer should work self.post(launch_url, dict(extra_vars=dict(reqd_answer="foo")), expect=202) # Short answer but requires a long answer