From 43aef6c63082c0263e49c6aca00908ee08b635f0 Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Thu, 15 Mar 2018 07:43:03 -0400 Subject: [PATCH] allow normal users to relaunch jobs w survey answers --- awx/main/models/jobs.py | 12 +++++++++- .../tests/unit/models/test_survey_models.py | 22 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/awx/main/models/jobs.py b/awx/main/models/jobs.py index 43b74a84a7..0738755b81 100644 --- a/awx/main/models/jobs.py +++ b/awx/main/models/jobs.py @@ -950,7 +950,17 @@ class JobLaunchConfig(LaunchTimeConfig): launching with those prompts ''' prompts = self.prompts_dict() - for field_name, ask_field_name in template.get_ask_mapping().items(): + ask_mapping = template.get_ask_mapping() + if template.survey_enabled and (not template.ask_variables_on_launch): + ask_mapping.pop('extra_vars') + provided_vars = set(prompts['extra_vars'].keys()) + survey_vars = set( + element.get('variable') for element in + template.survey_spec.get('spec', {}) if 'variable' in element + ) + if provided_vars - survey_vars: + return True + for field_name, ask_field_name in ask_mapping.items(): if field_name in prompts and not getattr(template, ask_field_name): return True else: diff --git a/awx/main/tests/unit/models/test_survey_models.py b/awx/main/tests/unit/models/test_survey_models.py index 8a93902f9e..3b7cf0d7e6 100644 --- a/awx/main/tests/unit/models/test_survey_models.py +++ b/awx/main/tests/unit/models/test_survey_models.py @@ -7,6 +7,7 @@ from awx.main.tasks import RunJob from awx.main.models import ( Job, JobTemplate, + JobLaunchConfig, WorkflowJobTemplate ) @@ -137,6 +138,27 @@ def test_job_args_unredacted_passwords(job, tmpdir_factory): assert extra_vars['secret_key'] == 'my_password' +def test_launch_config_has_unprompted_vars(survey_spec_factory): + jt = JobTemplate( + survey_enabled = True, + survey_spec = survey_spec_factory(['question1', 'question2']) + ) + unprompted_config = JobLaunchConfig( + extra_data = { + 'question1': 'foobar', + 'question4': 'foobar' + } + ) + assert unprompted_config.has_unprompted(jt) + allowed_config = JobLaunchConfig( + extra_data = { + 'question1': 'foobar', + 'question2': 'foobar' + } + ) + assert not allowed_config.has_unprompted(jt) + + @pytest.mark.survey def test_update_kwargs_survey_invalid_default(survey_spec_factory): spec = survey_spec_factory('var2')