Skip to content

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

Check if image exists befeore validating

Krzysztof Świątek755e9f42e69d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c1c1df8fd7..d28e5132b4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -89,6 +89,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Remove unused settings and environment variables - #5420 by @maarcingebala
- Add product price validation - #5413 by @kswiatek92
- Add attribute validation to attributeAssign - #5423 by @kswiatek92
+- Check if image exists befeore validating - #5425 by @kswiatek92
## 2.9.0
diff --git a/saleor/graphql/core/utils/__init__.py b/saleor/graphql/core/utils/__init__.py
index 9391b938b5..ad66639873 100644
--- a/saleor/graphql/core/utils/__init__.py
+++ b/saleor/graphql/core/utils/__init__.py
@@ -36,6 +36,11 @@ def str_to_enum(name):
def validate_image_file(file, field_name):
"""Validate if the file is an image."""
+ print(type(file))
+ if not file:
+ raise ValidationError(
+ {field_name: ValidationError("File is required", code="invalid")}
+ )
if not file.content_type.startswith("image/"):
raise ValidationError(
{field_name: ValidationError("Invalid file type", code="invalid")}
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 93c95741d9..aed2455477 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -2519,6 +2519,33 @@ def test_product_image_create_mutation(
mock_create_thumbnails.assert_called_once_with(product_image.pk)
+def test_product_image_create_mutation_without_file(
+ monkeypatch, staff_api_client, product, permission_manage_products, media_root
+):
+ query = """
+ mutation createProductImage($image: Upload!, $product: ID!) {
+ productImageCreate(input: {image: $image, product: $product}) {
+ productErrors {
+ code
+ field
+ }
+ }
+ }
+ """
+ variables = {
+ "product": graphene.Node.to_global_id("Product", product.id),
+ "image": "image name",
+ }
+ body = get_multipart_request_body(query, variables, file="", file_name="name")
+ response = staff_api_client.post_multipart(
+ body, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ errors = content["data"]["productImageCreate"]["productErrors"]
+ assert errors[0]["field"] == "image"
+ assert errors[0]["code"] == ProductErrorCode.INVALID.name
+
+
def test_invalid_product_image_create_mutation(
staff_api_client, product, permission_manage_products
):