Skip to content

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

Add waight validation

fowczrek7a761011b27a
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 2b920a74d0..28c3f0817c 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -802,6 +802,18 @@ class ProductCreate(ModelMutation):
@classmethod
def clean_input(cls, info, instance, data):
cleaned_input = super().clean_input(info, instance, data)
+
+ weight = cleaned_input.get("weight")
+ if weight and weight.value < 0:
+ raise ValidationError(
+ {
+ "weight": ValidationError(
+ "Product can't have negative weight.",
+ code=ProductErrorCode.INVALID,
+ )
+ }
+ )
+
# Attributes are provided as list of `AttributeValueInput` objects.
# We need to transform them into the format they're stored in the
# `Product` model, which is HStore field that maps attribute's PK to
@@ -1158,6 +1170,17 @@ class ProductVariantCreate(ModelMutation):
):
cleaned_input = super().clean_input(info, instance, data)
+ weight = cleaned_input.get("weight")
+ if weight and weight.value < 0:
+ raise ValidationError(
+ {
+ "weight": ValidationError(
+ "Product variant can't have negative weight.",
+ code=ProductErrorCode.INVALID.value,
+ )
+ }
+ )
+
if "cost_price" in cleaned_input:
cost_price = cleaned_input.pop("cost_price")
if cost_price and cost_price < 0:
@@ -1419,6 +1442,18 @@ class ProductTypeCreate(ModelMutation):
@classmethod
def clean_input(cls, info, instance, data):
cleaned_input = super().clean_input(info, instance, data)
+
+ weight = cleaned_input.get("weight")
+ if weight and weight.value < 0:
+ raise ValidationError(
+ {
+ "weight": ValidationError(
+ "Product type can't have negative weight.",
+ code=ProductErrorCode.INVALID,
+ )
+ }
+ )
+
try:
cleaned_input = validate_slug_and_generate_if_needed(
instance, "name", cleaned_input
diff --git a/saleor/graphql/shipping/mutations.py b/saleor/graphql/shipping/mutations.py
index 760074cd4a..0ae355a287 100644
--- a/saleor/graphql/shipping/mutations.py
+++ b/saleor/graphql/shipping/mutations.py
@@ -207,6 +207,27 @@ class ShippingPriceMixin:
else:
min_weight = cleaned_input.get("minimum_order_weight")
max_weight = cleaned_input.get("maximum_order_weight")
+
+ if min_weight and min_weight.value < 0:
+ raise ValidationError(
+ {
+ "minimum_order_weight": ValidationError(
+ "Shipping can't have negative weight.",
+ code=ShippingErrorCode.INVALID,
+ )
+ }
+ )
+
+ if max_weight and max_weight.value < 0:
+ raise ValidationError(
+ {
+ "maximum_order_weight": ValidationError(
+ "Shipping can't have negative weight.",
+ code=ShippingErrorCode.INVALID,
+ )
+ }
+ )
+
if (
min_weight is not None
and max_weight is not None
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index d62c9a29d3..a762915bfc 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -901,6 +901,11 @@ CREATE_PRODUCT_MUTATION = """
}
}
}
+ productErrors {
+ field
+ code
+ message
+ }
errors {
message
field
@@ -1049,6 +1054,40 @@ def test_create_product_no_slug_in_input(
assert str(data["product"]["basePrice"]["amount"]) == product_price
+def test_create_product_with_negative_weight(
+ staff_api_client,
+ product_type,
+ category,
+ description_json,
+ permission_manage_products,
+):
+ query = CREATE_PRODUCT_MUTATION
+
+ description_json = json.dumps(description_json)
+
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+ product_name = "test name"
+
+ variables = {
+ "input": {
+ "productType": product_type_id,
+ "category": category_id,
+ "name": product_name,
+ "weight": -1,
+ }
+ }
+
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productCreate"]
+ error = data["productErrors"][0]
+ assert error["field"] == "weight"
+ assert error["code"] == ProductErrorCode.INVALID.name
+
+
QUERY_CREATE_PRODUCT_WITHOUT_VARIANTS = """
mutation createProduct(
$productTypeId: ID!,
@@ -1900,6 +1939,46 @@ def test_update_product_without_variants_sku_duplication(
assert data["errors"][0]["message"] == "Product with this SKU already exists."
+def test_update_product_with_negative_weight(
+ staff_api_client, product_with_default_variant, permission_manage_products, product
+):
+ query = """
+ mutation updateProduct(
+ $productId: ID!,
+ $weight: WeightScalar)
+ {
+ productUpdate(
+ id: $productId,
+ input: {
+ weight: $weight
+ })
+ {
+ product {
+ id
+ }
+ productErrors {
+ field
+ message
+ code
+ }
+ }
+ }
+ """
+ product = product_with_default_variant
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+
+ variables = {"productId": product_id, "weight": -1}
+
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productUpdate"]
+ error = data["productErrors"][0]
+ assert error["field"] == "weight"
+ assert error["code"] == ProductErrorCode.INVALID.name
+
+
def test_update_product_without_category_and_true_is_published_value(
staff_api_client, permission_manage_products, product
):
@@ -2260,6 +2339,42 @@ def test_create_product_type_with_given_slug(
assert data["productType"]["slug"] == expected_slug
+def test_create_product_type_create_with_negative_weight(
+ staff_api_client, permission_manage_products
+):
+ query = """
+ mutation(
+ $name: String, $weight: WeightScalar) {
+ productTypeCreate(
+ input: {
+ name: $name
+ weight: $weight
+ }
+ ) {
+ productType {
+ id
+ name
+ }
+ productErrors {
+ field
+ message
+ code
+ }
+ }
+ }
+ """
+ name = "Test product type"
+ variables = {"name": name, "weight": -1.1}
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productTypeCreate"]
+ error = data["productErrors"][0]
+ assert error["field"] == "weight"
+ assert error["code"] == ProductErrorCode.INVALID.name
+
+
def test_product_type_update_mutation(
staff_api_client, product_type, permission_manage_products
):
@@ -2481,6 +2596,42 @@ def test_update_product_type_slug_and_name(
assert errors[0]["code"] == ProductErrorCode.REQUIRED.name
+def test_update_product_type_with_negative_weight(
+ staff_api_client, product_type, permission_manage_products,
+):
+ query = """
+ mutation($id: ID!, $weight: WeightScalar) {
+ productTypeUpdate(
+ id: $id
+ input: {
+ weight: $weight
+ }
+ ) {
+ productType{
+ name
+ }
+ productErrors {
+ field
+ message
+ code
+ }
+ }
+ }
+ """
+
+ node_id = graphene.Node.to_global_id("ProductType", product_type.id)
+ variables = {"id": node_id, "weight": "-1"}
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ product_type.refresh_from_db()
+ data = content["data"]["productTypeUpdate"]
+ error = data["productErrors"][0]
+ assert error["field"] == "weight"
+ assert error["code"] == ProductErrorCode.INVALID.name
+
+
def test_product_type_delete_mutation(
staff_api_client, product_type, permission_manage_products
):
diff --git a/tests/api/test_shipping_method.py b/tests/api/test_shipping_method.py
index 613ecaffb0..253c9aa199 100644
--- a/tests/api/test_shipping_method.py
+++ b/tests/api/test_shipping_method.py
@@ -665,6 +665,48 @@ def test_create_weight_shipping_method_errors(
assert data["shippingErrors"][0]["code"] == ShippingErrorCode.MAX_LESS_THAN_MIN.name
+def test_create_shipping_method_with_negative_min_weight(
+ shipping_zone, staff_api_client, permission_manage_shipping
+):
+ shipping_zone_id = graphene.Node.to_global_id("ShippingZone", shipping_zone.pk)
+ variables = {
+ "shippingZone": shipping_zone_id,
+ "name": "DHL",
+ "price": 12.34,
+ "minimumOrderWeight": -20,
+ "type": ShippingMethodTypeEnum.WEIGHT.name,
+ }
+ response = staff_api_client.post_graphql(
+ WEIGHT_BASED_SHIPPING_QUERY, variables, permissions=[permission_manage_shipping]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["shippingPriceCreate"]
+ error = data["shippingErrors"][0]
+ assert error["field"] == "minimumOrderWeight"
+ assert error["code"] == ShippingErrorCode.INVALID.name
+
+
+def test_create_shipping_method_with_negative_max_weight(
+ shipping_zone, staff_api_client, permission_manage_shipping
+):
+ shipping_zone_id = graphene.Node.to_global_id("ShippingZone", shipping_zone.pk)
+ variables = {
+ "shippingZone": shipping_zone_id,
+ "name": "DHL",
+ "price": 12.34,
+ "maximumOrderWeight": -15,
+ "type": ShippingMethodTypeEnum.WEIGHT.name,
+ }
+ response = staff_api_client.post_graphql(
+ WEIGHT_BASED_SHIPPING_QUERY, variables, permissions=[permission_manage_shipping]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["shippingPriceCreate"]
+ error = data["shippingErrors"][0]
+ assert error["field"] == "maximumOrderWeight"
+ assert error["code"] == ShippingErrorCode.INVALID.name
+
+
def test_update_shipping_method(
staff_api_client, shipping_zone, permission_manage_shipping
):
diff --git a/tests/api/test_variant.py b/tests/api/test_variant.py
index 28a94e15bc..134459850d 100644
--- a/tests/api/test_variant.py
+++ b/tests/api/test_variant.py
@@ -178,6 +178,50 @@ def test_create_variant(
assert data["stocks"][0]["warehouse"]["slug"] == warehouse.slug
+def test_create_product_variant_with_negative_weight(
+ staff_api_client, product, product_type, permission_manage_products
+):
+ query = """
+ mutation createVariant (
+ $productId: ID!,
+ $attributes: [AttributeValueInput]!,
+ $weight: WeightScalar) {
+ productVariantCreate(
+ input: {
+ product: $productId,
+ attributes: $attributes,
+ weight: $weight
+ }) {
+ productErrors {
+ field
+ code
+ message
+ }
+ }
+ }
+ """
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+
+ variant_id = graphene.Node.to_global_id(
+ "Attribute", product_type.variant_attributes.first().pk
+ )
+ variant_value = "test-value"
+
+ variables = {
+ "productId": product_id,
+ "weight": -1,
+ "attributes": [{"id": variant_id, "values": [variant_value]}],
+ }
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productVariantCreate"]
+ error = data["productErrors"][0]
+ assert error["field"] == "weight"
+ assert error["code"] == ProductErrorCode.INVALID.name
+
+
def test_create_product_variant_not_all_attributes(
staff_api_client, product, product_type, color_attribute, permission_manage_products
):
@@ -405,6 +449,47 @@ def test_update_product_variant(staff_api_client, product, permission_manage_pro
assert data["sku"] == sku
+def test_update_product_variant_with_negative_weight(
+ staff_api_client, product, permission_manage_products
+):
+ query = """
+ mutation updateVariant (
+ $id: ID!,
+ $weight: WeightScalar
+ ) {
+ productVariantUpdate(
+ id: $id,
+ input: {
+ weight: $weight
+ }
+ ){
+ productVariant {
+ name
+ }
+ productErrors {
+ field
+ message
+ code
+ }
+ }
+ }
+ """
+ variant = product.variants.first()
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+
+ variables = {"id": variant_id, "weight": -1}
+
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ variant.refresh_from_db()
+ content = get_graphql_content(response)
+ data = content["data"]["productVariantUpdate"]
+ error = data["productErrors"][0]
+ assert error["field"] == "weight"
+ assert error["code"] == ProductErrorCode.INVALID.name
+
+
@pytest.mark.parametrize("field", ("cost_price", "price_override"))
def test_update_product_variant_unset_amounts(
staff_api_client, product, permission_manage_products, field