From 5530ffe355b035751b81f4cb0d333258262f15c3 Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Sun, 29 Nov 2026 14:43:32 -0800 Subject: [PATCH] Fix: reject the bare custom field display name, incomplete nested email and webhook rows on patch, and falsy non-object custom field values --- src/documents/serialisers.py | 53 ++++++---- src/documents/tests/test_api_input_shapes.py | 103 ++++++++++++++++++- 2 files changed, 137 insertions(+), 19 deletions(-) diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index c677417b9..1e1fa106b 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -1504,7 +1504,10 @@ class SavedViewSerializer(OwnedObjectSerializer): raise serializers.ValidationError( f"Invalid field: {field}", ) - elif field not in SavedView.DisplayFields.values: + elif ( + field == SavedView.DisplayFields.CUSTOM_FIELD + or field not in SavedView.DisplayFields.values + ): # the bare 'custom_field_%d' template names no custom field raise serializers.ValidationError( f"Invalid field: {field}", ) @@ -3435,7 +3438,7 @@ class WorkflowActionSerializer(serializers.ModelSerializer[WorkflowAction]): {"assign_title": f"{e.args[0]}"}, ) - if attrs.get("assign_custom_fields_values"): + if attrs.get("assign_custom_fields_values") is not None: if not isinstance(attrs["assign_custom_fields_values"], dict): raise serializers.ValidationError( {"assign_custom_fields_values": "Must be an object"}, @@ -3446,23 +3449,37 @@ class WorkflowActionSerializer(serializers.ModelSerializer[WorkflowAction]): for field_id, value in attrs["assign_custom_fields_values"].items() } - if ( - "type" in attrs - and attrs["type"] == WorkflowAction.WorkflowActionType.EMAIL - and "email" not in attrs + action_type = attrs.get("type", self.instance.type if self.instance else None) + for attribute, nested_type, serializer_class, label in ( + ( + "email", + WorkflowAction.WorkflowActionType.EMAIL, + WorkflowActionEmailSerializer, + "Email", + ), + ( + "webhook", + WorkflowAction.WorkflowActionType.WEBHOOK, + WorkflowActionWebhookSerializer, + "Webhook", + ), ): - raise serializers.ValidationError( - "Email data is required for email actions", - ) - - if ( - "type" in attrs - and attrs["type"] == WorkflowAction.WorkflowActionType.WEBHOOK - and "webhook" not in attrs - ): - raise serializers.ValidationError( - "Webhook data is required for webhook actions", - ) + if action_type != nested_type or not ( + "type" in attrs or attribute in attrs + ): + continue + if self.instance is not None and getattr(self.instance, attribute): + # An existing row can be updated partially + continue + data = attrs.get(attribute) + if not isinstance(data, dict): + raise serializers.ValidationError( + f"{label} data is required for {attribute} actions", + ) + # A partial update skipped the required fields of this new row + nested = serializer_class(data=data, context=self.context) + if not nested.is_valid(): + raise serializers.ValidationError({attribute: nested.errors}) if ( "type" in attrs diff --git a/src/documents/tests/test_api_input_shapes.py b/src/documents/tests/test_api_input_shapes.py index 29c1169d6..961bda050 100644 --- a/src/documents/tests/test_api_input_shapes.py +++ b/src/documents/tests/test_api_input_shapes.py @@ -190,7 +190,12 @@ class TestNullAndWrongTypeFields: @pytest.mark.parametrize( "display_fields", - [["custom_field_abc"], ["custom_field_"], ["xcustom_field_1"]], + [ + ["custom_field_abc"], + ["custom_field_"], + ["xcustom_field_1"], + ["custom_field_%d"], + ], ) def test_saved_view_custom_field_name_must_be_well_formed( self, @@ -594,6 +599,102 @@ class TestStandaloneWorkflowActions: assert action.email is not None assert action.email.subject == "Subject" + @pytest.mark.parametrize( + ("action_type", "nested", "payload"), + [ + pytest.param( + WorkflowAction.WorkflowActionType.EMAIL, + "email", + {"email": {}}, + id="email-empty", + ), + pytest.param( + WorkflowAction.WorkflowActionType.EMAIL, + "email", + {"email": None}, + id="email-null", + ), + pytest.param( + WorkflowAction.WorkflowActionType.EMAIL, + "email", + {"email": {"subject": "Only a subject"}}, + id="email-incomplete", + ), + pytest.param( + WorkflowAction.WorkflowActionType.WEBHOOK, + "webhook", + {"webhook": {}}, + id="webhook-empty", + ), + pytest.param( + WorkflowAction.WorkflowActionType.WEBHOOK, + "webhook", + {"webhook": None}, + id="webhook-null", + ), + ], + ) + def test_patch_cannot_make_an_incomplete_nested_row( + self, + admin_client: APIClient, + action_type: int, + nested: str, + payload: dict, + ) -> None: + """ + GIVEN: + - An action that has no email or webhook + WHEN: + - It is patched to an email or webhook action with no, or incomplete, + email or webhook data + THEN: + - The response is a 400 and nothing is stored, a partial update only + applies to a row that already exists + """ + action = WorkflowAction.objects.create( + type=WorkflowAction.WorkflowActionType.ASSIGNMENT, + ) + + response = send_json( + admin_client, + "PATCH", + f"/api/workflow_actions/{action.pk}/", + {"type": action_type, **payload}, + ) + + assert response.status_code == status.HTTP_400_BAD_REQUEST + action.refresh_from_db() + assert getattr(action, nested) is None + assert action.type == WorkflowAction.WorkflowActionType.ASSIGNMENT + + @pytest.mark.parametrize("value", [[], "", 0, False, "text", 5]) + def test_custom_field_values_must_be_an_object( + self, + admin_client: APIClient, + value: object, + ) -> None: + """ + GIVEN: + - An authenticated client + WHEN: + - An action is created with custom field values that are not an object, + including values that are falsy + THEN: + - The response is a 400 and nothing is stored + """ + response = send_json( + admin_client, + "POST", + "/api/workflow_actions/", + { + "type": WorkflowAction.WorkflowActionType.ASSIGNMENT, + "assign_custom_fields_values": value, + }, + ) + + assert response.status_code == status.HTTP_400_BAD_REQUEST + assert WorkflowAction.objects.count() == 0 + def test_an_id_in_the_payload_cannot_reach_another_actions_email( self, admin_client: APIClient,