Skip to content

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

Add checks for graphql

NyanKiyoshi3712fbadd759
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a910b00e82..8ffce7efc5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,7 +28,8 @@ All notable, unreleased changes to this project will be documented in this file.
- Add bulk actions - #3955 by @dominik-zeglen
- Update file field styles with materializecss template filter - #3998 by @zodiacfireworks
- Add filtering interface for graphQL API - #3952 by @korycins
-- Ensure adding to checkout is respecting the limits set, so the user doesn't add too many - #4005 by @NyanKiyoshi
+- Ensure adding to quantities in checkout is respecting the limits set,
+ both in storefront 1.0 and in the API - #4005 by @NyanKiyoshi
## 2.5.0
diff --git a/saleor/checkout/forms.py b/saleor/checkout/forms.py
index a08b6b4cfa..24617acbb8 100644
--- a/saleor/checkout/forms.py
+++ b/saleor/checkout/forms.py
@@ -61,7 +61,7 @@ class AddToCheckoutForm(forms.Form):
self.taxes = kwargs.pop('taxes', {})
super().__init__(*args, **kwargs)
- def add_error_i18n(self, field, error_name, fmt: Any=tuple()):
+ def add_error_i18n(self, field, error_name, fmt: Any = tuple()):
self.add_error(field, self.error_messages[error_name] % fmt)
def clean(self):
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index 83745103fa..45262e9dc1 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -1,6 +1,7 @@
from datetime import date
import graphene
+from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import transaction
@@ -63,6 +64,10 @@ def clean_shipping_method(
def check_lines_quantity(variants, quantities):
"""Check if stock is sufficient for each line in the list of dicts."""
for variant, quantity in zip(variants, quantities):
+ if quantity > settings.MAX_CHECKOUT_LINE_QUANTITY:
+ raise ValidationError({
+ 'quantity': 'Cannot add more than %d times this item.'
+ '' % settings.MAX_CHECKOUT_LINE_QUANTITY})
try:
variant.check_quantity(quantity)
except InsufficientStock as e:
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index bb54fd5efe..00b9c5ceff 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -70,6 +70,29 @@ def test_checkout_create(api_client, variant, graphql_address_data):
assert new_checkout.shipping_address.city == shipping_address['city'].upper()
+def test_checkout_create_cannot_add_too_many_quantities(
+ api_client, variant, graphql_address_data):
+
+ variant_id = graphene.Node.to_global_id('ProductVariant', variant.id)
+ test_email = '[email protected]'
+ shipping_address = graphql_address_data
+ variables = {
+ 'checkoutInput': {
+ 'lines': [{
+ 'quantity': 51,
+ 'variantId': variant_id}],
+ 'email': test_email,
+ 'shippingAddress': shipping_address}}
+ 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': 'Cannot add more than 50 times this item.'}]
+
+
+
def test_checkout_create_reuse_checkout(checkout, user_api_client, variant):
# assign user to the checkout
checkout.user = user_api_client.user
@@ -360,6 +383,25 @@ def test_checkout_lines_add(user_api_client, checkout_with_item, variant):
assert line.quantity == 1
+def test_checkout_lines_add_too_many(
+ user_api_client, checkout_with_item, variant):
+ variant_id = graphene.Node.to_global_id('ProductVariant', variant.pk)
+ checkout_id = graphene.Node.to_global_id('Checkout', checkout_with_item.pk)
+
+ variables = {
+ 'checkoutId': checkout_id,
+ 'lines': [{
+ 'variantId': variant_id,
+ 'quantity': 51}]}
+ response = user_api_client.post_graphql(
+ MUTATION_CHECKOUT_LINES_ADD, variables)
+ content = get_graphql_content(response)['data']['checkoutLinesAdd']
+
+ assert content['errors']
+ assert content['errors'] == [{
+ 'field': 'quantity',
+ 'message': 'Cannot add more than 50 times this item.'}]
+
def test_checkout_lines_add_empty_checkout(user_api_client, checkout, variant):
variant_id = graphene.Node.to_global_id('ProductVariant', variant.pk)
checkout_id = graphene.Node.to_global_id('Checkout', checkout.pk)