Skip to content

Commit

Permalink
Operation choice at creation only for approval workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
a-belhadj committed Nov 22, 2023
1 parent a1bbb35 commit c63069e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ def validate(self, data):
raise ValidationError({"scopes": f"The scope {scope} has already an approval workflow "
f"based on this operation"})
return data


class ApprovalWorkflowSerializerEdit(ApprovalWorkflowSerializer):
class Meta:
model = ApprovalWorkflow
fields = ['id', 'enabled', 'name', 'operation', 'scopes']
read_only_fields = ['id', "operation"]
5 changes: 3 additions & 2 deletions service_catalog/api/views/approval_workflow_api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from Squest.utils.squest_api_views import SquestListCreateAPIView, SquestRetrieveUpdateDestroyAPIView, \
SquestRetrieveAPIView
from service_catalog.api.serializers.approval_step_serializer import ApprovalStepPositionSerializer
from service_catalog.api.serializers.approval_workflow_serializer import ApprovalWorkflowSerializer
from service_catalog.api.serializers.approval_workflow_serializer import ApprovalWorkflowSerializer, \
ApprovalWorkflowSerializerEdit
from service_catalog.api.serializers.approval_workflow_state_serializer import ApprovalWorkflowStateSerializer
from service_catalog.api.serializers.approve_workflow_step_serializer import ApproveWorkflowStepSerializer
from service_catalog.filters.approval_workflow_filter import ApprovalWorkflowFilter
Expand All @@ -26,7 +27,7 @@ class ApprovalWorkflowListCreate(SquestListCreateAPIView):

class ApprovalWorkflowDetails(SquestRetrieveUpdateDestroyAPIView):
queryset = ApprovalWorkflow.objects.all()
serializer_class = ApprovalWorkflowSerializer
serializer_class = ApprovalWorkflowSerializerEdit


class ApprovalWorkflowStateDetails(SquestRetrieveAPIView):
Expand Down
6 changes: 6 additions & 0 deletions service_catalog/forms/approval_workflow_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ def clean(self):
if scope.approval_workflows.filter(operation=operation).exclude(id=exclude_id).exists():
raise ValidationError({"scopes": f"The scope {scope} has already an approval workflow "
f"based on this operation"})


class ApprovalWorkflowFormEdit(ApprovalWorkflowForm):
class Meta:
model = ApprovalWorkflow
fields = ['name', 'scopes', 'enabled']
4 changes: 2 additions & 2 deletions service_catalog/views/approval_workflow_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from Squest.utils.squest_views import SquestListView, SquestDetailView, SquestCreateView, SquestUpdateView, \
SquestDeleteView
from service_catalog.filters.approval_workflow_filter import ApprovalWorkflowFilter
from service_catalog.forms.approval_workflow_form import ApprovalWorkflowForm
from service_catalog.forms.approval_workflow_form import ApprovalWorkflowForm, ApprovalWorkflowFormEdit
from service_catalog.models import ApprovalWorkflow
from service_catalog.tables.approval_workflow_table import ApprovalWorkflowTable

Expand All @@ -26,7 +26,7 @@ class ApprovalWorkflowCreateView(SquestCreateView):

class ApprovalWorkflowEditView(SquestUpdateView):
model = ApprovalWorkflow
form_class = ApprovalWorkflowForm
form_class = ApprovalWorkflowFormEdit


class ApprovalWorkflowDeleteView(SquestDeleteView):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from profiles.models import GlobalScope
from service_catalog.api.serializers.approval_step_state_serializer import ApprovalStepStateSerializer
from service_catalog.api.serializers.approval_workflow_serializer import ApprovalWorkflowSerializer
from service_catalog.api.serializers.approval_workflow_serializer import ApprovalWorkflowSerializer, \
ApprovalWorkflowSerializerEdit
from service_catalog.models import ApprovalWorkflow
from tests.test_service_catalog.base_test_approval import BaseTestApprovalAPI

Expand Down Expand Up @@ -28,7 +27,7 @@ def test_can_add_another_scope_to_scopes(self):
"operation": self.create_operation_test.id,
"scopes": [self.test_quota_scope.id, self.test_quota_scope2.id]
}
serializer = ApprovalWorkflowSerializer(instance=self.test_approval_workflow, data=data)
serializer = ApprovalWorkflowSerializerEdit(instance=self.test_approval_workflow, data=data)
self.assertTrue(serializer.is_valid())

def test_cannot_valid_with_empty_scopes_if_already_exists(self):
Expand All @@ -41,3 +40,14 @@ def test_cannot_valid_with_empty_scopes_if_already_exists(self):
serializer = ApprovalWorkflowSerializer(data=data)
self.assertFalse(serializer.is_valid())
self.assertIn("An approval workflow for all scopes already exists", serializer.errors["scopes"][0])

def test_cannot_change_operation_when_edit(self):
existing_approval = ApprovalWorkflow.objects.create(name="test",
operation=self.create_operation_test_2)
data = {
"name": "test_workflow",
"operation": self.create_operation_test.id,
}
serializer = ApprovalWorkflowSerializerEdit(instance=existing_approval, data=data)
self.assertTrue(serializer.is_valid())
self.assertEqual(existing_approval.operation, self.create_operation_test_2)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from service_catalog.forms.approval_workflow_form import ApprovalWorkflowForm
from service_catalog.forms.approval_workflow_form import ApprovalWorkflowForm, ApprovalWorkflowFormEdit
from service_catalog.models import ApprovalWorkflow
from tests.test_service_catalog.base import BaseTest

Expand Down Expand Up @@ -39,9 +39,9 @@ def test_add_scope_to_scopes(self):
data = {
'name': 'test_approval_workflow',
'operation': self.create_operation_test,
'scopes': [self.test_quota_scope,self.test_quota_scope2]
'scopes': [self.test_quota_scope, self.test_quota_scope2]
}
form = ApprovalWorkflowForm(instance=existing_approval,data=data)
form = ApprovalWorkflowFormEdit(instance=existing_approval, data=data)
self.assertTrue(form.is_valid())

def test_clean_with_empty_scopes(self):
Expand Down Expand Up @@ -69,3 +69,16 @@ def test_create_specific_workflow_with_generic_already_there(self):
}
form = ApprovalWorkflowForm(data)
self.assertTrue(form.is_valid())

def test_cannot_change_operation_when_edit(self):
existing_approval = ApprovalWorkflow.objects.create(name="test",
operation=self.create_operation_test_2)

data = {
'name': "test",
'operation': self.update_operation_test
}
form = ApprovalWorkflowFormEdit(instance=existing_approval, data=data)
self.assertTrue(form.is_valid())
# Check that operation is not changed
self.assertEqual(existing_approval.operation, self.create_operation_test_2)

0 comments on commit c63069e

Please sign in to comment.