diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/scripts/save_process_instance_metadata.py b/spiffworkflow-backend/src/spiffworkflow_backend/scripts/save_process_instance_metadata.py index 7176f6a5..ae5fe00e 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/scripts/save_process_instance_metadata.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/scripts/save_process_instance_metadata.py @@ -1,8 +1,11 @@ """Get_env.""" from typing import Any + from flask_bpmn.models.db import db -from spiffworkflow_backend.models.process_instance_metadata import ProcessInstanceMetadataModel +from spiffworkflow_backend.models.process_instance_metadata import ( + ProcessInstanceMetadataModel, +) from spiffworkflow_backend.models.script_attributes_context import ( ScriptAttributesContext, ) @@ -26,10 +29,14 @@ class SaveProcessInstanceMetadata(Script): metadata_dict = args[0] for key, value in metadata_dict.items(): pim = ProcessInstanceMetadataModel.query.filter_by( - process_instance_id=script_attributes_context.process_instance_id, key=key).first() + process_instance_id=script_attributes_context.process_instance_id, + key=key, + ).first() if pim is None: pim = ProcessInstanceMetadataModel( - process_instance_id=script_attributes_context.process_instance_id, key=key) + process_instance_id=script_attributes_context.process_instance_id, + key=key, + ) pim.value = value db.session.add(pim) db.session.commit() diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py index f6ac3be3..9b6ed01b 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py @@ -626,7 +626,7 @@ class ProcessInstanceProcessor: for at in active_tasks: if at.task_id == str(ready_or_waiting_task.id): active_task = at - active_tasks.pop(at) + active_tasks.remove(at) if active_task is None: active_task = ActiveTaskModel( @@ -644,12 +644,14 @@ class ProcessInstanceProcessor: db.session.add(active_task) db.session.commit() - for potential_owner_id in potential_owner_hash["potential_owner_ids"]: - active_task_user = ActiveTaskUserModel( - user_id=potential_owner_id, active_task_id=active_task.id - ) - db.session.add(active_task_user) - db.session.commit() + for potential_owner_id in potential_owner_hash[ + "potential_owner_ids" + ]: + active_task_user = ActiveTaskUserModel( + user_id=potential_owner_id, active_task_id=active_task.id + ) + db.session.add(active_task_user) + db.session.commit() if len(active_tasks) > 0: for at in active_tasks: diff --git a/spiffworkflow-backend/tests/spiffworkflow_backend/scripts/test_save_process_instance_metadata.py b/spiffworkflow-backend/tests/spiffworkflow_backend/scripts/test_save_process_instance_metadata.py index 62823f15..96eb6297 100644 --- a/spiffworkflow-backend/tests/spiffworkflow_backend/scripts/test_save_process_instance_metadata.py +++ b/spiffworkflow-backend/tests/spiffworkflow_backend/scripts/test_save_process_instance_metadata.py @@ -4,7 +4,9 @@ from flask.testing import FlaskClient from tests.spiffworkflow_backend.helpers.base_test import BaseTest from tests.spiffworkflow_backend.helpers.test_data import load_test_spec -from spiffworkflow_backend.models.process_instance_metadata import ProcessInstanceMetadataModel +from spiffworkflow_backend.models.process_instance_metadata import ( + ProcessInstanceMetadataModel, +) from spiffworkflow_backend.models.user import UserModel from spiffworkflow_backend.services.process_instance_processor import ( ProcessInstanceProcessor, @@ -38,5 +40,6 @@ class TestSaveProcessInstanceMetadata(BaseTest): processor.do_engine_steps(save=True) process_instance_metadata = ProcessInstanceMetadataModel.query.filter_by( - process_instance_id=process_instance.id).all() + process_instance_id=process_instance.id + ).all() assert len(process_instance_metadata) == 3 diff --git a/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_process_instance_processor.py b/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_process_instance_processor.py index f0de77aa..3e010795 100644 --- a/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_process_instance_processor.py +++ b/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_process_instance_processor.py @@ -161,6 +161,7 @@ class TestProcessInstanceProcessor(BaseTest): ) processor = ProcessInstanceProcessor(process_instance) processor.do_engine_steps(save=True) + processor.save() assert len(process_instance.active_tasks) == 1 active_task = process_instance.active_tasks[0] @@ -241,3 +242,42 @@ class TestProcessInstanceProcessor(BaseTest): ) assert process_instance.status == ProcessInstanceStatus.complete.value + + def test_does_not_recreate_active_tasks_on_multiple_saves( + self, + app: Flask, + client: FlaskClient, + with_db_and_bpmn_file_cleanup: None, + with_super_admin_user: UserModel, + ) -> None: + """Test_sets_permission_correctly_on_active_task_when_using_dict.""" + self.create_process_group( + client, with_super_admin_user, "test_group", "test_group" + ) + initiator_user = self.find_or_create_user("initiator_user") + finance_user_three = self.find_or_create_user("testuser3") + assert initiator_user.principal is not None + assert finance_user_three.principal is not None + AuthorizationService.import_permissions_from_yaml_file() + + finance_group = GroupModel.query.filter_by(identifier="Finance Team").first() + assert finance_group is not None + + process_model = load_test_spec( + process_model_id="test_group/model_with_lanes", + bpmn_file_name="lanes_with_owner_dict.bpmn", + process_model_source_directory="model_with_lanes", + ) + process_instance = self.create_process_instance_from_process_model( + process_model=process_model, user=initiator_user + ) + processor = ProcessInstanceProcessor(process_instance) + processor.do_engine_steps(save=True) + assert len(process_instance.active_tasks) == 1 + initial_active_task_id = process_instance.active_tasks[0].id + + # save again to ensure we go attempt to process the active tasks again + processor.save() + + assert len(process_instance.active_tasks) == 1 + assert initial_active_task_id == process_instance.active_tasks[0].id