1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 16:51:11 +03:00

fix up tests from workflow copy work

This commit is contained in:
AlanCoding 2016-11-15 14:57:05 -05:00
parent 8cda12c020
commit 31a95e5c04
4 changed files with 29 additions and 27 deletions

View File

@ -2916,6 +2916,7 @@ class WorkflowJobTemplateDetail(RetrieveUpdateDestroyAPIView):
always_allow_superuser = False always_allow_superuser = False
new_in_310 = True new_in_310 = True
class WorkflowJobTemplateCopy(GenericAPIView): class WorkflowJobTemplateCopy(GenericAPIView):
model = WorkflowJobTemplate model = WorkflowJobTemplate
@ -2999,7 +3000,7 @@ class WorkflowJobRelaunch(GenericAPIView):
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
obj = self.get_object() obj = self.get_object()
new_workflow_job = obj.create_relaunch_workflow_job() new_workflow_job = obj.create_relaunch_workflow_job()
result = new_workflow_job.signal_start() new_workflow_job.signal_start()
data = WorkflowJobSerializer(new_workflow_job, context=self.get_serializer_context()).data data = WorkflowJobSerializer(new_workflow_job, context=self.get_serializer_context()).data
headers = {'Location': new_workflow_job.get_absolute_url()} headers = {'Location': new_workflow_job.get_absolute_url()}

View File

@ -341,8 +341,6 @@ class BaseAccess(object):
# Aliases for going form UI language to API language # Aliases for going form UI language to API language
if display_method == 'edit': if display_method == 'edit':
method = 'change' method = 'change'
elif display_method == 'copy':
method = 'add'
elif display_method == 'adhoc': elif display_method == 'adhoc':
method = 'run_ad_hoc_commands' method = 'run_ad_hoc_commands'
else: else:
@ -368,7 +366,7 @@ class BaseAccess(object):
access_method = getattr(self, "can_%s" % method) access_method = getattr(self, "can_%s" % method)
if method in ['change']: # 3 args if method in ['change']: # 3 args
user_capabilities[display_method] = access_method(obj, data) user_capabilities[display_method] = access_method(obj, data)
elif method in ['delete', 'run_ad_hoc_commands']: elif method in ['delete', 'run_ad_hoc_commands', 'copy']:
user_capabilities[display_method] = access_method(obj) user_capabilities[display_method] = access_method(obj)
elif method in ['start']: elif method in ['start']:
user_capabilities[display_method] = access_method(obj, validate_license=False) user_capabilities[display_method] = access_method(obj, validate_license=False)

View File

@ -3,7 +3,7 @@ import pytest
from awx.main.models.jobs import JobTemplate from awx.main.models.jobs import JobTemplate
from awx.main.models import Inventory, Credential, Project from awx.main.models import Inventory, Credential, Project
from awx.main.models.workflow import ( from awx.main.models.workflow import (
WorkflowJobTemplate, WorkflowJobTemplateNode, WorkflowJobInheritNodesMixin, WorkflowJobTemplate, WorkflowJobTemplateNode, WorkflowJobOptions,
WorkflowJob, WorkflowJobNode WorkflowJob, WorkflowJobNode
) )
import mock import mock
@ -22,9 +22,9 @@ class TestWorkflowJobInheritNodesMixin():
def test__create_workflow_job_nodes(self, mocker, job_template_nodes): def test__create_workflow_job_nodes(self, mocker, job_template_nodes):
workflow_job_node_create = mocker.patch('awx.main.models.WorkflowJobTemplateNode.create_workflow_job_node') workflow_job_node_create = mocker.patch('awx.main.models.WorkflowJobTemplateNode.create_workflow_job_node')
mixin = WorkflowJobInheritNodesMixin() mixin = WorkflowJobOptions()
mixin._create_workflow_job_nodes(job_template_nodes) mixin._create_workflow_nodes(job_template_nodes)
for job_template_node in job_template_nodes: for job_template_node in job_template_nodes:
workflow_job_node_create.assert_any_call(workflow_job=mixin) workflow_job_node_create.assert_any_call(workflow_job=mixin)
@ -37,22 +37,30 @@ class TestWorkflowJobInheritNodesMixin():
def job_nodes(self): def job_nodes(self):
return [WorkflowJobNode(id=i) for i in range(100, 120)] return [WorkflowJobNode(id=i) for i in range(100, 120)]
def test__map_workflow_job_nodes(self, job_template_nodes, job_nodes): def test__map_workflow_job_nodes(self, job_template_nodes, job_nodes, mocker):
mixin = WorkflowJobInheritNodesMixin() mixin = WorkflowJob()
wj_node = WorkflowJobNode()
mocker.patch('awx.main.models.workflow.WorkflowJobTemplateNode.create_workflow_job_node',
return_value=wj_node)
node_ids_map = mixin._map_workflow_job_nodes(job_template_nodes, job_nodes) node_ids_map = mixin._create_workflow_nodes(job_template_nodes, user=None)
assert len(node_ids_map) == len(job_template_nodes) assert len(node_ids_map) == len(job_template_nodes)
for i, job_template_node in enumerate(job_template_nodes): for i, job_template_node in enumerate(job_template_nodes):
assert node_ids_map[job_template_node.id] == job_nodes[i].id assert node_ids_map[job_template_node.id] == wj_node
class TestInheritRelationship(): class TestInheritRelationship():
@pytest.fixture @pytest.fixture
def job_template_nodes(self, mocker): def job_template_nodes(self, mocker):
nodes = [mocker.MagicMock(id=i) for i in range(0, 10)] nodes = [mocker.MagicMock(id=i, pk=i) for i in range(0, 10)]
for i in range(0, 9): for i in range(0, 9):
nodes[i].success_nodes = [mocker.MagicMock(id=i + 1)] nodes[i].success_nodes = mocker.MagicMock(
all=mocker.MagicMock(return_value=[mocker.MagicMock(id=i + 1, pk=i + 1)]))
nodes[i].always_nodes = mocker.MagicMock(all=mocker.MagicMock(return_value=[]))
nodes[i].failure_nodes = mocker.MagicMock(all=mocker.MagicMock(return_value=[]))
new_wj_node = mocker.MagicMock(success_nodes=mocker.MagicMock())
nodes[i].create_workflow_job_node = mocker.MagicMock(return_value=new_wj_node)
return nodes return nodes
@ -70,18 +78,13 @@ class TestWorkflowJobInheritNodesMixin():
def test__inherit_relationship(self, mocker, job_template_nodes, job_nodes, job_nodes_dict): def test__inherit_relationship(self, mocker, job_template_nodes, job_nodes, job_nodes_dict):
mixin = WorkflowJobInheritNodesMixin() wj = WorkflowJob()
mixin._get_workflow_job_node_by_id = lambda x: job_nodes_dict[x] node_ids_map = wj._create_workflow_nodes(job_template_nodes)
mixin._get_all_by_type = lambda x,node_type: x.success_nodes wj._inherit_node_relationships(job_template_nodes, node_ids_map)
node_ids_map = mixin._map_workflow_job_nodes(job_template_nodes, job_nodes) for i in range(0, 8):
node_ids_map[i].success_nodes.add.assert_any_call(node_ids_map[i + 1])
for i, job_template_node in enumerate(job_template_nodes):
mixin._inherit_relationship(job_template_node, job_nodes[i], node_ids_map, 'success_nodes')
for i in range(0, 9):
job_nodes[i].success_nodes.add.assert_any_call(job_nodes[i + 1])
@pytest.fixture @pytest.fixture

View File

@ -258,8 +258,8 @@ def test_user_capabilities_method():
def can_change(self, obj, data): def can_change(self, obj, data):
return 'bar' return 'bar'
def can_add(self, data): def can_copy(self, obj):
return 'foobar' return 'foo'
user = User(username='auser') user = User(username='auser')
foo_access = FooAccess(user) foo_access = FooAccess(user)
@ -267,7 +267,7 @@ def test_user_capabilities_method():
foo_capabilities = foo_access.get_user_capabilities(foo, ['edit', 'copy']) foo_capabilities = foo_access.get_user_capabilities(foo, ['edit', 'copy'])
assert foo_capabilities == { assert foo_capabilities == {
'edit': 'bar', 'edit': 'bar',
'copy': 'foobar' 'copy': 'foo'
} }