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

This commit is contained in:
Chris Church 2015-04-10 16:20:57 -04:00
parent 3b66dbed03
commit fd09cc3726
3 changed files with 13 additions and 7 deletions

View File

@ -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:

View File

@ -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'])

View File

@ -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