mirror of
https://github.com/ansible/awx.git
synced 2026-01-17 20:51:21 -03:30
generalize schedule prompts validation
This makes ScheduleSerializer behave same as WFJT nodes Prevents providing job_type for workflow jobs, as example
This commit is contained in:
parent
905ff7dad7
commit
a9aae91634
@ -3695,18 +3695,16 @@ class ScheduleSerializer(LaunchConfigurationBaseSerializer):
|
||||
return value
|
||||
|
||||
def validate(self, attrs):
|
||||
extra_data = parse_yaml_or_json(attrs.get('extra_data', {}))
|
||||
if extra_data:
|
||||
ujt = None
|
||||
if 'unified_job_template' in attrs:
|
||||
ujt = attrs['unified_job_template']
|
||||
elif self.instance:
|
||||
ujt = self.instance.unified_job_template
|
||||
accepted, rejected, errors = ujt.accept_or_ignore_variables(extra_data)
|
||||
if 'extra_vars' in errors:
|
||||
errors['extra_data'] = errors.pop('extra_vars')
|
||||
if errors:
|
||||
raise serializers.ValidationError(errors)
|
||||
ujt = None
|
||||
if 'unified_job_template' in attrs:
|
||||
ujt = attrs['unified_job_template']
|
||||
elif self.instance:
|
||||
ujt = self.instance.unified_job_template
|
||||
accepted, rejected, errors = ujt._accept_or_ignore_job_kwargs(**self._build_mock_obj(attrs).prompts_dict())
|
||||
if 'extra_vars' in errors:
|
||||
errors['extra_data'] = errors.pop('extra_vars')
|
||||
if errors:
|
||||
raise serializers.ValidationError(errors)
|
||||
return super(ScheduleSerializer, self).validate(attrs)
|
||||
|
||||
# We reject rrules if:
|
||||
|
||||
@ -431,7 +431,8 @@ class UnifiedJobTemplate(PolymorphicModel, CommonModelNameNotUnique, Notificatio
|
||||
'''
|
||||
errors = {}
|
||||
if kwargs:
|
||||
errors['all'] = [_("Fields {} are not allowed on launch.").format(kwargs.keys())]
|
||||
for field_name in kwargs.keys():
|
||||
errors[field_name] = [_("Field is not allowed on launch.")]
|
||||
return ({}, kwargs, errors)
|
||||
|
||||
def accept_or_ignore_variables(self, data, errors=None):
|
||||
|
||||
@ -2,6 +2,8 @@ import pytest
|
||||
|
||||
from awx.api.versioning import reverse
|
||||
|
||||
from awx.main.models import JobTemplate
|
||||
|
||||
|
||||
RRULE_EXAMPLE = 'DTSTART:20151117T050000Z RRULE:FREQ=DAILY;INTERVAL=1;COUNT=1'
|
||||
|
||||
@ -11,11 +13,17 @@ def test_non_job_extra_vars_prohibited(post, project, admin_user):
|
||||
url = reverse('api:project_schedules_list', kwargs={'pk': project.id})
|
||||
r = post(url, {'name': 'test sch', 'rrule': RRULE_EXAMPLE, 'extra_data': '{"a": 5}'},
|
||||
admin_user, expect=400)
|
||||
assert 'cannot accept variables' in str(r.data['extra_data'][0])
|
||||
assert 'not allowed on launch' in str(r.data['extra_data'][0])
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_valid_survey_answer(post, job_template, admin_user, survey_spec_factory):
|
||||
def test_valid_survey_answer(post, admin_user, project, inventory, survey_spec_factory):
|
||||
job_template = JobTemplate.objects.create(
|
||||
name='test-jt',
|
||||
project=project,
|
||||
playbook='helloworld.yml',
|
||||
inventory=inventory
|
||||
)
|
||||
job_template.ask_variables_on_launch = False
|
||||
job_template.survey_enabled = True
|
||||
job_template.survey_spec = survey_spec_factory('var1')
|
||||
|
||||
@ -50,7 +50,7 @@ def test_reject_other_prommpts():
|
||||
sjt = SystemJobTemplate()
|
||||
accepted, ignored, errors = sjt._accept_or_ignore_job_kwargs(limit="")
|
||||
assert accepted == {}
|
||||
assert 'not allowed on launch' in errors['all'][0]
|
||||
assert 'not allowed on launch' in errors['limit'][0]
|
||||
|
||||
|
||||
def test_reject_some_accept_some():
|
||||
@ -61,5 +61,5 @@ def test_reject_some_accept_some():
|
||||
})
|
||||
assert accepted == {"extra_vars": {"days": 34}}
|
||||
assert ignored == {"limit": "", "extra_vars": {"foobar": "baz"}}
|
||||
assert 'not allowed on launch' in errors['all'][0]
|
||||
assert 'not allowed on launch' in errors['limit'][0]
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user