Skip to content

Commit

Permalink
Fix: Admin can update request when using approval workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Sispheor committed May 22, 2024
1 parent 46a2523 commit c70b36b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
6 changes: 4 additions & 2 deletions service_catalog/models/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,14 @@ def full_survey_user(self, approval_step_state=None):

@property
def full_survey(self):
# by default the survey is composed by what the end user provided, overriden by what the admin provided
full_survey = {k: v for k, v in {**self.fill_in_survey, **self.admin_fill_in_survey}.items() if v is not None}
# by default the survey is composed by what the end user provided
full_survey = {k: v for k, v in {**self.fill_in_survey}.items() if v is not None}
# when an approval workflow is used, we override with the content provided by each step
if self.approval_workflow_state is not None:
for step in self.approval_workflow_state.approval_step_states.all():
full_survey.update(step.fill_in_survey)
# the admin step always override what has been set in previous steps
full_survey.update({k: v for k, v in {**self.admin_fill_in_survey}.items() if v is not None})
return full_survey

def clean(self):
Expand Down
27 changes: 12 additions & 15 deletions templates/service_catalog/request_details/approval.html
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ <h3 class="timeline-header">Process the request</h3>
{% if object.get_state_display == "ACCEPTED" %}
<div class="row justify-content-md-center">
<div class="timeline-footer">
{% with args_filter="accept,"|addstr:object.id %}
{% if args_filter|can_proceed_request_action and can_accept_request %}
<a class="btn btn-primary"
title="Review"
href="{% url 'service_catalog:request_accept' object.id %}">
<i class="fas fa-clipboard-check"></i> Review
{% with args_filter="cancel,"|addstr:object.id %}
{% if args_filter|can_proceed_request_action and can_cancel_request %}
<a class="btn btn-secondary"
title="Cancel"
href="{% url 'service_catalog:request_cancel' object.id %}">
<i class="fas fa-window-close"></i> Cancel
</a>
{% endif %}
{% endwith %}
Expand All @@ -299,17 +299,15 @@ <h3 class="timeline-header">Process the request</h3>
href="{% url 'service_catalog:request_reject' object.id %}">
<i class="fas fa-ban"></i> Reject
</a>

{% endif %}
{% endwith %}
{% with args_filter="cancel,"|addstr:object.id %}
{% if args_filter|can_proceed_request_action and can_cancel_request %}
<a class="btn btn-secondary"
title="Cancel"
href="{% url 'service_catalog:request_cancel' object.id %}">
<i class="fas fa-window-close"></i> Cancel
{% with args_filter="accept,"|addstr:object.id %}
{% if args_filter|can_proceed_request_action and can_accept_request %}
<a class="btn btn-primary"
title="Review"
href="{% url 'service_catalog:request_accept' object.id %}">
<i class="fas fa-clipboard-check"></i> Review
</a>

{% endif %}
{% endwith %}
{% with args_filter="process,"|addstr:object.id %}
Expand All @@ -319,7 +317,6 @@ <h3 class="timeline-header">Process the request</h3>
href="{% url 'service_catalog:request_process' object.id %}">
<i class="fas fa-play"></i> Process
</a>

{% endif %}
{% endwith %}
</div>
Expand Down
54 changes: 30 additions & 24 deletions tests/test_service_catalog/test_models/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from profiles.api.serializers import ScopeSerializerNested
from profiles.api.serializers.user_serializers import UserSerializerNested
from service_catalog.models import ApprovalWorkflow, ApprovalStep
from service_catalog.models import ApprovalWorkflow, ApprovalStep, ApprovalWorkflowState, ApprovalStepState
from service_catalog.models.instance import InstanceState, Instance
from service_catalog.models.request import RequestState, Request
from tests.test_service_catalog.base_test_request import BaseTestRequest
Expand Down Expand Up @@ -383,31 +383,37 @@ def test_reprocess_after_failed(self):
self.assertTrue(can_proceed(self.test_request.process))

def test_get_full_survey(self):
fields_in_survey = ['text_variable', 'multiplechoice_variable', 'multiselect_var', 'textarea_var',
'password_var', 'float_var']
fields_not_in_survey = ['integer_var']
# end user survey
self.test_request.fill_in_survey = {
'text_variable': "myvar"
}
self.test_request.save()
self.assertEqual(self.test_request.full_survey['text_variable'], "myvar")

# add a step on the opereration
self.test_approval_workflow = ApprovalWorkflow.objects.create(name="test_approval_workflow",
operation=self.create_operation_test,
enabled=True)
self.test_approval_workflow_state = ApprovalWorkflowState.objects.create(approval_workflow=self.test_approval_workflow)
self.test_request.approval_workflow_state = self.test_approval_workflow_state
self.test_request.save()
self.test_approval_step_1 = ApprovalStep.objects.create(name="test_approval_step_1",
approval_workflow=self.test_approval_workflow)
self.test_approval_step_state_1 = ApprovalStepState.objects.create(
approval_workflow_state=self.test_approval_workflow_state,
approval_step=self.test_approval_step_1)
self.test_approval_step_state_1.fill_in_survey = {
'text_variable': "updated_by_step"
}
self.test_approval_step_state_1.save()
self.assertEqual(self.test_request.full_survey['text_variable'], "updated_by_step")

# Admin replace a value
self.test_request.admin_fill_in_survey = {
'multiplechoice_variable': "choice1",
'multiselect_var': [],
'textarea_var': "",
'password_var': "password_val",
'float_var': 0,
'integer_var': None
'text_variable': "updated_by_admin"
}
for field_name in fields_in_survey:
self.assertIn(field_name, self.test_request.full_survey.keys())
for field_name in fields_not_in_survey:
self.assertNotIn(field_name, self.test_request.full_survey.keys())
self.test_request.admin_fill_in_survey['integer_var'] = 1
self.test_request.fill_in_survey['text_variable'] = None
fields_not_in_survey.remove('integer_var')
fields_in_survey.append('integer_var')
fields_in_survey.remove('text_variable')
fields_not_in_survey.append('text_variable')
for field_name in fields_in_survey:
self.assertIn(field_name, self.test_request.full_survey.keys())
for field_name in fields_not_in_survey:
self.assertNotIn(field_name, self.test_request.full_survey.keys())
self.test_request.save()
self.assertEqual(self.test_request.full_survey['text_variable'], "updated_by_admin")

def test_update_fill_in_surveys_accept_request(self):
new_survey_config = {
Expand Down

0 comments on commit c70b36b

Please sign in to comment.