mirror of
https://github.com/ZwareBear/awx.git
synced 2026-04-24 00:41:48 -05:00
refactoring survey spec tests and fixtures
This commit is contained in:
@@ -5,7 +5,6 @@ from awx.api.serializers import JobLaunchSerializer
|
||||
from awx.main.models.credential import Credential
|
||||
from awx.main.models.inventory import Inventory
|
||||
from awx.main.models.jobs import Job, JobTemplate
|
||||
from awx.main.tests.factories.utils import generate_survey_spec
|
||||
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
@@ -278,10 +277,10 @@ def test_job_launch_JT_with_validation(machine_credential, deploy_jobtemplate):
|
||||
|
||||
@pytest.mark.django_db
|
||||
@pytest.mark.job_runtime_vars
|
||||
def test_job_launch_unprompted_vars_with_survey(mocker, job_template_prompts, post, admin_user):
|
||||
def test_job_launch_unprompted_vars_with_survey(mocker, survey_spec_factory, job_template_prompts, post, admin_user):
|
||||
job_template = job_template_prompts(False)
|
||||
job_template.survey_enabled = True
|
||||
job_template.survey_spec = generate_survey_spec('survey_var')
|
||||
job_template.survey_spec = survey_spec_factory('survey_var')
|
||||
job_template.save()
|
||||
|
||||
with mocker.patch('awx.main.access.BaseAccess.check_license'):
|
||||
|
||||
@@ -5,7 +5,6 @@ from django.core.urlresolvers import reverse
|
||||
|
||||
from awx.main.models.jobs import JobTemplate, Job
|
||||
from awx.api.license import LicenseForbids
|
||||
from awx.main.tests.factories.utils import generate_survey_spec
|
||||
from awx.main.access import JobTemplateAccess
|
||||
|
||||
|
||||
@@ -17,12 +16,8 @@ def mock_no_surveys(self, add_host=False, feature=None, check_expiration=True):
|
||||
|
||||
@pytest.fixture
|
||||
def job_template_with_survey(job_template_factory):
|
||||
objects = job_template_factory('jt', project='prj')
|
||||
obj = objects.job_template
|
||||
obj.survey_enabled = True
|
||||
obj.survey_spec = generate_survey_spec('submitter_email')
|
||||
obj.save()
|
||||
return obj
|
||||
objects = job_template_factory('jt', project='prj', survey='submitted_email')
|
||||
return objects.job_template
|
||||
|
||||
# Survey license-based denial tests
|
||||
@mock.patch('awx.api.views.feature_enabled', lambda feature: False)
|
||||
@@ -78,8 +73,8 @@ def test_survey_spec_view_allowed(deploy_jobtemplate, get, admin_user):
|
||||
@mock.patch('awx.api.views.feature_enabled', lambda feature: True)
|
||||
@pytest.mark.django_db
|
||||
@pytest.mark.survey
|
||||
def test_survey_spec_sucessful_creation(job_template, post, admin_user):
|
||||
survey_input_data = generate_survey_spec('new_question')
|
||||
def test_survey_spec_sucessful_creation(survey_spec_factory, job_template, post, admin_user):
|
||||
survey_input_data = survey_spec_factory('new_question')
|
||||
post(url=reverse('api:job_template_survey_spec', args=(job_template.id,)),
|
||||
data=survey_input_data, user=admin_user, expect=200)
|
||||
updated_jt = JobTemplate.objects.get(pk=job_template.pk)
|
||||
@@ -100,10 +95,10 @@ def test_survey_spec_non_dict_error(deploy_jobtemplate, post, admin_user):
|
||||
@mock.patch('awx.api.views.feature_enabled', lambda feature: True)
|
||||
@pytest.mark.django_db
|
||||
@pytest.mark.survey
|
||||
def test_survey_spec_dual_names_error(deploy_jobtemplate, post, user):
|
||||
def test_survey_spec_dual_names_error(survey_spec_factory, deploy_jobtemplate, post, user):
|
||||
response = post(
|
||||
url=reverse('api:job_template_survey_spec', args=(deploy_jobtemplate.id,)),
|
||||
data=generate_survey_spec(['submitter_email', 'submitter_email']),
|
||||
data=survey_spec_factory(['submitter_email', 'submitter_email']),
|
||||
user=user('admin', True), expect=400)
|
||||
assert response.data['error'] == "'variable' 'submitter_email' duplicated in survey question 1."
|
||||
|
||||
@@ -130,10 +125,9 @@ def test_delete_survey_access_without_license(job_template_with_survey, admin_us
|
||||
def test_job_start_allowed_with_survey_spec(job_template_factory, admin_user):
|
||||
"""After user downgrades survey license and disables survey on the JT,
|
||||
check that jobs still launch even if the survey_spec data persists."""
|
||||
objects = job_template_factory('jt', project='prj')
|
||||
objects = job_template_factory('jt', project='prj', survey='submitter_email')
|
||||
obj = objects.job_template
|
||||
obj.survey_enabled = False
|
||||
obj.survey_spec = generate_survey_spec('submitter_email')
|
||||
obj.save()
|
||||
access = JobTemplateAccess(admin_user)
|
||||
assert access.can_start(job_template_with_survey, {})
|
||||
@@ -180,12 +174,12 @@ def test_launch_survey_enabled_but_no_survey_spec(job_template_factory, post, ad
|
||||
@mock.patch('awx.api.serializers.JobSerializer.to_representation', lambda self, obj: {})
|
||||
@pytest.mark.django_db
|
||||
@pytest.mark.survey
|
||||
def test_launch_with_non_empty_survey_spec_no_license(job_template_factory, post, admin_user):
|
||||
def test_launch_with_non_empty_survey_spec_no_license(survey_spec_factory, job_template_factory, post, admin_user):
|
||||
"""Assure jobs can still be launched from JTs with a survey_spec
|
||||
when the survey is diabled."""
|
||||
objects = job_template_factory('jt', organization='org1', project='prj',
|
||||
inventory='inv', credential='cred')
|
||||
obj = objects.job_template
|
||||
obj.survey_spec = generate_survey_spec('survey_var')
|
||||
obj.survey_spec = survey_spec_factory('survey_var')
|
||||
obj.save()
|
||||
post(reverse('api:job_template_launch', args=[obj.pk]), {}, admin_user, expect=201)
|
||||
|
||||
@@ -42,6 +42,7 @@ from awx.main.tests.factories import (
|
||||
create_organization,
|
||||
create_job_template,
|
||||
create_notification_template,
|
||||
create_survey_spec,
|
||||
)
|
||||
|
||||
'''
|
||||
@@ -501,3 +502,7 @@ def organization_factory():
|
||||
def notification_template_factory():
|
||||
return create_notification_template
|
||||
|
||||
@pytest.fixture
|
||||
def survey_spec_factory():
|
||||
return create_survey_spec
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import pytest
|
||||
|
||||
from awx.main.tests.factories import NotUnique
|
||||
from awx.main.tests.factories.utils import generate_survey_spec
|
||||
|
||||
def test_roles_exc_not_persisted(organization_factory):
|
||||
with pytest.raises(RuntimeError) as exc:
|
||||
@@ -78,23 +77,25 @@ def test_org_factory(organization_factory):
|
||||
def test_job_template_factory(job_template_factory):
|
||||
jt_objects = job_template_factory('testJT', organization='org1',
|
||||
project='proj1', inventory='inventory1',
|
||||
credential='cred1')
|
||||
credential='cred1', survey='test-survey')
|
||||
assert jt_objects.job_template.name == 'testJT'
|
||||
assert jt_objects.project.name == 'proj1'
|
||||
assert jt_objects.inventory.name == 'inventory1'
|
||||
assert jt_objects.credential.name == 'cred1'
|
||||
assert jt_objects.inventory.organization.name == 'org1'
|
||||
assert jt_objects.job_template.survey_enabled is True
|
||||
assert jt_objects.job_template.survey_spec is not None
|
||||
|
||||
def test_survey_spec_generator_simple():
|
||||
survey_spec = generate_survey_spec('survey_variable')
|
||||
def test_survey_spec_generator_simple(survey_spec_factory):
|
||||
survey_spec = survey_spec_factory('survey_variable')
|
||||
assert 'name' in survey_spec
|
||||
assert 'spec' in survey_spec
|
||||
assert type(survey_spec['spec']) is list
|
||||
assert type(survey_spec['spec'][0]) is dict
|
||||
assert survey_spec['spec'][0]['type'] == 'integer'
|
||||
|
||||
def test_survey_spec_generator_mixed():
|
||||
survey_spec = generate_survey_spec(
|
||||
def test_survey_spec_generator_mixed(survey_spec_factory):
|
||||
survey_spec = survey_spec_factory(
|
||||
[{'variable': 'question1', 'type': 'integer', 'max': 87},
|
||||
{'variable': 'question2', 'type': 'str'},
|
||||
'some_variable'])
|
||||
|
||||
Reference in New Issue
Block a user