Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Reject empty-string channel ID in channelDelete mutation (#19222)

Lukasz Ostrowski6078e3dbe6dc
diff --git a/saleor/graphql/channel/mutations/channel_delete.py b/saleor/graphql/channel/mutations/channel_delete.py
index 3bc53c9b02..6539375668 100644
--- a/saleor/graphql/channel/mutations/channel_delete.py
+++ b/saleor/graphql/channel/mutations/channel_delete.py
@@ -125,6 +125,15 @@ class ChannelDelete(ModelDeleteMutation):
cls, root, info: ResolveInfo, /, *, id: str, input: dict | None = None
):
origin_channel = cls.get_node_or_error(info, id, only_type=Channel)
+ if origin_channel is None:
+ raise ValidationError(
+ {
+ "id": ValidationError(
+ "Couldn't resolve to a node: empty channel id.",
+ code=ChannelErrorCode.NOT_FOUND.value,
+ )
+ }
+ )
target_channel_global_id = input.get("channel_id") if input else None
if target_channel_global_id:
target_channel = cls.get_node_or_error(
diff --git a/saleor/graphql/channel/tests/mutations/test_channel_delete.py b/saleor/graphql/channel/tests/mutations/test_channel_delete.py
index 34d5de5c1e..96b3c3375f 100644
--- a/saleor/graphql/channel/tests/mutations/test_channel_delete.py
+++ b/saleor/graphql/channel/tests/mutations/test_channel_delete.py
@@ -355,3 +355,24 @@ def test_channel_delete_mutation_deletes_invalid_warehouse_to_zone_relations(
# ensure warehouse from index 1 has been deleted from shipping zone
# without JPY channel
assert warehouses[1] not in shipping_zones[1].warehouses.all()
+
+
+def test_channel_delete_with_empty_id_returns_validation_error(
+ permission_manage_channels, staff_api_client
+):
+ # given
+ variables = {"id": ""}
+
+ # when
+ response = staff_api_client.post_graphql(
+ CHANNEL_DELETE_MUTATION,
+ variables=variables,
+ permissions=(permission_manage_channels,),
+ )
+ content = get_graphql_content(response)
+
+ # then
+ errors = content["data"]["channelDelete"]["errors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "id"
+ assert errors[0]["code"] == ChannelErrorCode.NOT_FOUND.name