diff --git a/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.html b/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.html index b4d4b02ca..76d788e59 100644 --- a/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.html +++ b/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.html @@ -99,7 +99,7 @@
- + diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index 233c56367..5825fb387 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -1386,12 +1386,38 @@ class WorkflowActionSerializer(serializers.ModelSerializer): def validate(self, attrs): # Empty strings treated as None to avoid unexpected behavior - if ( - "assign_title" in attrs - and attrs["assign_title"] is not None - and len(attrs["assign_title"]) == 0 - ): - attrs["assign_title"] = None + if "assign_title" in attrs: + if attrs["assign_title"] is not None and len(attrs["assign_title"]) == 0: + attrs["assign_title"] = None + else: + try: + # test against all placeholders, see consumer.py `parse_doc_title_w_placeholders` + attrs["assign_title"].format( + correspondent="", + document_type="", + added="", + added_year="", + added_year_short="", + added_month="", + added_month_name="", + added_month_name_short="", + added_day="", + added_time="", + owner_username="", + original_filename="", + created="", + created_year="", + created_year_short="", + created_month="", + created_month_name="", + created_month_name_short="", + created_day="", + created_time="", + ) + except ValueError as e: + raise serializers.ValidationError( + {"assign_title": f"Invalid f-string detected: {e.args[0]}"}, + ) return attrs diff --git a/src/documents/tests/test_api_workflows.py b/src/documents/tests/test_api_workflows.py index d7a7ad6ff..21e887c24 100644 --- a/src/documents/tests/test_api_workflows.py +++ b/src/documents/tests/test_api_workflows.py @@ -248,6 +248,45 @@ class TestApiWorkflows(DirectoriesMixin, APITestCase): self.assertEqual(WorkflowTrigger.objects.count(), 1) + def test_api_create_invalid_assign_title(self): + """ + GIVEN: + - API request to create a workflow + - Invalid f-string for assign_title + WHEN: + - API is called + THEN: + - Correct HTTP 400 response + - No objects are created + """ + response = self.client.post( + self.ENDPOINT, + json.dumps( + { + "name": "Workflow 1", + "order": 1, + "triggers": [ + { + "type": WorkflowTrigger.WorkflowTriggerType.DOCUMENT_UPDATED, + }, + ], + "actions": [ + { + "assign_title": "{created_year]", + }, + ], + }, + ), + content_type="application/json", + ) + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertIn( + "Invalid f-string detected", + response.data["actions"][0]["assign_title"][0], + ) + + self.assertEqual(Workflow.objects.count(), 1) + def test_api_create_workflow_trigger_action_empty_fields(self): """ GIVEN: