Skip to content

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

Add available for purchase validation in CheckoutCreate mutation

IKarbowiak09caf09fe159
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index c5c9be5fb7..7ec5ab39e4 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -138,6 +138,29 @@ def check_lines_quantity(variants, quantities, country):
raise ValidationError({"quantity": ValidationError(message, code=e.code)})
+def validate_variants_available_for_purchase(variants):
+ not_available_variants = [
+ variant.pk
+ for variant in variants
+ if not variant.product.available_for_purchase
+ or datetime.date.today() < variant.product.available_for_purchase
+ ]
+ if not_available_variants:
+ variant_ids = [
+ graphene.Node.to_global_id("ProductVariant", pk)
+ for pk in not_available_variants
+ ]
+ raise ValidationError(
+ {
+ "lines": ValidationError(
+ "Cannot add lines for unavailable for purchase variants.",
+ code=CheckoutErrorCode.PRODUCT_UNAVAILABLE_FOR_PURCHASE,
+ params={"variants": variant_ids},
+ )
+ }
+ )
+
+
class CheckoutLineInput(graphene.InputObjectType):
quantity = graphene.Int(required=True, description="The number of items purchased.")
variant_id = graphene.ID(required=True, description="ID of the product variant.")
@@ -200,6 +223,7 @@ class CheckoutCreate(ModelMutation, I18nMixin):
)
quantities = [line.get("quantity") for line in lines]
+ validate_variants_available_for_purchase(variants)
check_lines_quantity(variants, quantities, country)
return variants, quantities
@@ -346,29 +370,6 @@ class CheckoutLinesAdd(BaseMutation):
error_type_class = CheckoutError
error_type_field = "checkout_errors"
- @classmethod
- def validate_variants_available_for_purchase(cls, variants):
- not_available_variants = [
- variant.pk
- for variant in variants
- if not variant.product.available_for_purchase
- or datetime.date.today() < variant.product.available_for_purchase
- ]
- if not_available_variants:
- variant_ids = [
- graphene.Node.to_global_id("ProductVariant", pk)
- for pk in not_available_variants
- ]
- raise ValidationError(
- {
- "lines": ValidationError(
- "Cannot add lines for unavailable for purchase variants.",
- code=CheckoutErrorCode.PRODUCT_UNAVAILABLE_FOR_PURCHASE,
- params={"variants": variant_ids},
- )
- }
- )
-
@classmethod
def perform_mutation(cls, _root, info, checkout_id, lines, replace=False):
checkout = cls.get_node_or_error(
@@ -380,7 +381,7 @@ class CheckoutLinesAdd(BaseMutation):
quantities = [line.get("quantity") for line in lines]
check_lines_quantity(variants, quantities, checkout.get_country())
- cls.validate_variants_available_for_purchase(variants)
+ validate_variants_available_for_purchase(variants)
if variants and quantities:
for variant, quantity in zip(variants, quantities):
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index 27da25a23d..b57db0002d 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -98,14 +98,11 @@ MUTATION_CHECKOUT_CREATE = """
quantity
}
}
- errors {
- field
- message
- }
checkoutErrors {
field
message
code
+ variants
}
}
}
@@ -314,16 +311,13 @@ def test_checkout_create_cannot_add_invalid_quantities(
assert not Checkout.objects.exists()
response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
content = get_graphql_content(response)["data"]["checkoutCreate"]
- assert content["errors"]
- assert content["errors"] == [
- {"field": "quantity", "message": expected_error_message}
- ]
-
+ assert content["checkoutErrors"]
assert content["checkoutErrors"] == [
{
"field": "quantity",
"message": expected_error_message,
"code": error_code.name,
+ "variants": None,
}
]
@@ -364,7 +358,7 @@ def test_checkout_create_required_email(api_client, stock):
response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
content = get_graphql_content(response)
- errors = content["data"]["checkoutCreate"]["errors"]
+ errors = content["data"]["checkoutCreate"]["checkoutErrors"]
assert errors
assert errors[0]["field"] == "email"
assert errors[0]["message"] == "This field cannot be blank."
@@ -542,10 +536,10 @@ def test_checkout_create_check_lines_quantity_multiple_warehouse(
response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
content = get_graphql_content(response)
data = content["data"]["checkoutCreate"]
- assert data["errors"][0]["message"] == (
+ assert data["checkoutErrors"][0]["message"] == (
"Could not add item Test product (SKU_A). Only 7 remaining in stock."
)
- assert data["errors"][0]["field"] == "quantity"
+ assert data["checkoutErrors"][0]["field"] == "quantity"
@override_settings(DEFAULT_COUNTRY="DE")
@@ -631,10 +625,46 @@ def test_checkout_create_check_lines_quantity(
response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
content = get_graphql_content(response)
data = content["data"]["checkoutCreate"]
- assert data["errors"][0]["message"] == (
+ assert data["checkoutErrors"][0]["message"] == (
"Could not add item Test product (SKU_A). Only 15 remaining in stock."
)
- assert data["errors"][0]["field"] == "quantity"
+ assert data["checkoutErrors"][0]["field"] == "quantity"
+
+
+def test_checkout_create_anavailable_for_purchase_product(
+ user_api_client, stock, graphql_address_data
+):
+ # given
+ variant = stock.product_variant
+ product = variant.product
+
+ product.available_for_purchase = None
+ product.save(update_fields=["available_for_purchase"])
+
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 10, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+
+ # when
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutCreate"]
+
+ errors = data["checkoutErrors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "lines"
+ assert errors[0]["code"] == CheckoutErrorCode.PRODUCT_UNAVAILABLE_FOR_PURCHASE.name
+ assert errors[0]["variants"] == [variant_id]
@pytest.fixture
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index fea1419ed2..63cd07d205 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -1988,6 +1988,7 @@ def digital_content(category, media_root, warehouse) -> DigitalContent:
product_type=product_type,
category=category,
is_published=True,
+ available_for_purchase=datetime.date(1999, 1, 1),
)
product_variant = ProductVariant.objects.create(
product=product,