Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add validation to require variant price
Marcin Gębalab7072cfea6a4
diff --git a/saleor/graphql/core/tests/test_core.py b/saleor/graphql/core/tests/test_core.pyindex 0a9ac53eb7..1971be5d2c 100644--- a/saleor/graphql/core/tests/test_core.py+++ b/saleor/graphql/core/tests/test_core.py@@ -82,9 +82,9 @@ def test_mutation_returns_error_field_in_camel_case():# costPrice is snake case variable (cost_price) in the backendquery = """- mutation testCamel($id: ID!, $cost: Decimal) {+ mutation testCamel($id: ID!, $price: Decimal, $cost: Decimal) {productVariantUpdate(id: $id,- input: {costPrice: $cost, trackInventory: false}) {+ input: {costPrice: $cost, price: $price, trackInventory: false}) {errors {fieldmessage@@ -97,6 +97,7 @@ def test_mutation_returns_error_field_in_camel_case("""variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.id),+ "price": 15,"cost": 12.1234,}response = staff_api_client.post_graphql(@@ -164,9 +165,8 @@ def test_mutation_decimal_input(staff_api_client, variant, stock, permission_manage_products):query = """- mutation decimalInput($id: ID!, $cost: Decimal) {- productVariantUpdate(id: $id,- input: {costPrice: $cost}) {+ mutation decimalInput($id: ID!, $cost: Decimal, $price: Decimal) {+ productVariantUpdate(id: $id, input: {costPrice: $cost, price: $price}) {errors {fieldmessage@@ -181,6 +181,7 @@ def test_mutation_decimal_input("""variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.id),+ "price": 15,"cost": 12.12,"quantity": 17,}@@ -196,9 +197,8 @@ def test_mutation_decimal_input_without_arguments(staff_api_client, variant, permission_manage_products):query = """- mutation {- productVariantUpdate(id: "%(variant_id)s",- input: {costPrice: "%(cost)s"}) {+ mutation ProductVariantUpdate($id: ID!, $price: Decimal, $costPrice: Decimal) {+ productVariantUpdate(id: $id, input: {costPrice: $costPrice, price: $price}) {errors {fieldmessage@@ -210,12 +210,14 @@ def test_mutation_decimal_input_without_arguments(}}}- """ % {- "variant_id": graphene.Node.to_global_id("ProductVariant", variant.id),+ """+ variables = {+ "id": graphene.Node.to_global_id("ProductVariant", variant.id),"cost": 12.12,+ "price": 15,}response = staff_api_client.post_graphql(- query, permissions=[permission_manage_products]+ query, variables, permissions=[permission_manage_products])content = get_graphql_content(response)data = content["data"]["productVariantUpdate"]diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex eaf3d820e9..23aaabce9e 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -1191,6 +1191,18 @@ class ProductVariantCreate(ModelMutation):})cleaned_input["cost_price_amount"] = cost_price++ price = cleaned_input.get("price")+ if price is None:+ raise ValidationError(+ {+ "price": ValidationError(+ "Variant price is required.",+ code=ProductErrorCode.REQUIRED.value,+ )+ }+ )+if "price" in cleaned_input:price = cleaned_input.pop("price")if price is not None and price < 0:diff --git a/saleor/graphql/product/tests/test_variant.py b/saleor/graphql/product/tests/test_variant.pyindex 0f094af7dd..fbf53f74ed 100644--- a/saleor/graphql/product/tests/test_variant.py+++ b/saleor/graphql/product/tests/test_variant.py@@ -198,6 +198,42 @@ def test_create_variant(assert data["stocks"][0]["warehouse"]["slug"] == warehouse.slug+def test_create_product_variant_without_price(+ staff_api_client, product, product_type, permission_manage_products+):+ query = """+ mutation createVariant ($productId: ID!, $attributes: [AttributeValueInput]!) {+ productVariantCreate(+ input: {+ product: $productId,+ attributes: $attributes+ }) {+ productErrors {+ field+ code+ }+ }+ }+ """+ product_id = graphene.Node.to_global_id("Product", product.pk)+ attr_id = graphene.Node.to_global_id(+ "Attribute", product_type.variant_attributes.first().pk+ )+ attr_value = "test-value"+ variables = {+ "productId": product_id,+ "attributes": [{"id": attr_id, "values": [attr_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"] == "price"+ assert error["code"] == ProductErrorCode.REQUIRED.name++def test_create_product_variant_with_negative_weight(staff_api_client, product, product_type, permission_manage_products):@@ -248,11 +284,13 @@ def test_create_product_variant_not_all_attributes(query = """mutation createVariant ($productId: ID!,+ $price: Decimal,$sku: String!,$attributes: [AttributeValueInput]!) {productVariantCreate(input: {product: $productId,+ price: $price,sku: $sku,attributes: $attributes}) {@@ -275,6 +313,7 @@ def test_create_product_variant_not_all_attributes(variables = {"productId": product_id,+ "price": 15,"sku": sku,"attributes": [{"id": variant_id, "values": [variant_value]}],}@@ -301,12 +340,14 @@ def test_create_product_variant_duplicated_attributes(query = """mutation createVariant ($productId: ID!,+ $price: Decimal,$sku: String!,$attributes: [AttributeValueInput]!) {productVariantCreate(input: {product: $productId,+ price: $price,sku: $sku,attributes: $attributes}) {@@ -325,6 +366,7 @@ def test_create_product_variant_duplicated_attributes(sku = str(uuid4())[:12]variables = {"productId": product_id,+ "price": 15,"sku": sku,"attributes": [{"id": color_attribute_id, "values": ["red"]},@@ -424,12 +466,14 @@ def test_update_product_variant(staff_api_client, product, permission_manage_promutation updateVariant ($id: ID!,$sku: String!,+ $price: Decimal,$costPrice: Decimal,$trackInventory: Boolean!) {productVariantUpdate(id: $id,input: {sku: $sku,+ price: $price,costPrice: $costPrice,trackInventory: $trackInventory}) {@@ -441,6 +485,9 @@ def test_update_product_variant(staff_api_client, product, permission_manage_proamountlocalized}+ price {+ amount+ }}}}@@ -450,10 +497,12 @@ def test_update_product_variant(staff_api_client, product, permission_manage_provariant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)sku = "test sku"cost_price = 3.3+ price = 15variables = {"id": variant_id,"sku": sku,+ "price": price,"costPrice": cost_price,"trackInventory": True,}@@ -466,6 +515,7 @@ def test_update_product_variant(staff_api_client, product, permission_manage_prodata = content["data"]["productVariantUpdate"]["productVariant"]assert data["name"] == variant.nameassert data["costPrice"]["amount"] == cost_price+ assert data["price"]["amount"] == priceassert data["sku"] == sku@@ -475,12 +525,14 @@ def test_update_product_variant_with_negative_weight(query = """mutation updateVariant ($id: ID!,+ $price: Decimal,$weight: WeightScalar) {productVariantUpdate(id: $id,input: {- weight: $weight+ weight: $weight,+ price: $price}){productVariant {@@ -496,9 +548,7 @@ def test_update_product_variant_with_negative_weight("""variant = product.variants.first()variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)-- variables = {"id": variant_id, "weight": -1}-+ variables = {"id": variant_id, "weight": -1, "price": 15}response = staff_api_client.post_graphql(query, variables, permissions=[permission_manage_products])@@ -520,11 +570,13 @@ def test_update_product_variant_unset_cost_price(mutation updateVariant ($id: ID!,$sku: String!,+ $price: Decimal,$costPrice: Decimal) {productVariantUpdate(id: $id,input: {sku: $sku,+ price: $price,costPrice: $costPrice,}) {productVariant {@@ -544,7 +596,7 @@ def test_update_product_variant_unset_cost_price(variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)sku = variant.sku- variables = {"id": variant_id, "sku": sku, "costPrice": None}+ variables = {"id": variant_id, "sku": sku, "costPrice": None, "price": 15}response = staff_api_client.post_graphql(query, variables, permissions=[permission_manage_products]@@ -563,11 +615,13 @@ QUERY_UPDATE_VARIANT_ATTRIBUTES = """mutation updateVariant ($id: ID!,$sku: String,+ $price: Decimal,$attributes: [AttributeValueInput]!) {productVariantUpdate(id: $id,input: {sku: $sku,+ price: $price,attributes: $attributes}) {errors {@@ -603,6 +657,7 @@ def test_update_product_variant_not_all_attributes(variables = {"id": variant_id,"sku": sku,+ "price": 15,"attributes": [{"id": attr_id, "values": [variant_value]}],}@@ -640,6 +695,7 @@ def test_update_product_variant_with_current_attribut(variables = {"id": variant_id,"sku": sku,+ "price": 15,"attributes": [{"id": color_attribute_id, "values": ["red"]},{"id": size_attribute_id, "values": ["small"]},@@ -682,6 +738,7 @@ def test_update_product_variant_with_new_attribute(variables = {"id": variant_id,"sku": sku,+ "price": 15,"attributes": [{"id": color_attribute_id, "values": ["red"]},{"id": size_attribute_id, "values": ["big"]},@@ -735,6 +792,7 @@ def test_update_product_variant_with_duplicated_attribute(variables = {"id": variant_id,+ "price": 15,"attributes": [{"id": color_attribute_id, "values": ["blue"]},{"id": size_attribute_id, "values": ["big"]},@@ -783,6 +841,7 @@ def test_update_product_variant_requires_values(variables = {"id": variant_id,+ "price": 15,"attributes": [{"id": attr_id, "values": values}],"sku": sku,}