Compare commits

...
2 changed files with 137 additions and 19 deletions

No files matched your search

+35 -18
View File
@@ -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
+102 -1
View File
@@ -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,