Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Adding a non existent variant to a checkout no longer crashes
NyanKiyoshi42db9bfd1268
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 71fb8354ba..fde745d768 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -22,6 +22,7 @@ All notable, unreleased changes to this project will be documented in this file.- GraphQL now prints exceptions to stderr as well as returning them or not - #4148 by @NyanKiyoshi- Refactored API resolvers to staticmethods with root typing - #4155 by @NyanKiyoshi- Users can how add multiple "Add to Cart" forms in a single page - #4165 by @NyanKiyoshi+- Adding a non existent variant to a checkout no longer crashes - #4166 by @NyanKiyoshi## 2.6.0diff --git a/saleor/checkout/forms.py b/saleor/checkout/forms.pyindex 760275c1b6..79631eff6a 100644--- a/saleor/checkout/forms.py+++ b/saleor/checkout/forms.py@@ -4,7 +4,7 @@ from typing import Anyfrom django import formsfrom django.conf import settings-from django.core.exceptions import NON_FIELD_ERRORS, ObjectDoesNotExist+from django.core.exceptions import NON_FIELD_ERRORSfrom django.utils.encoding import smart_textfrom django.utils.safestring import mark_safefrom django.utils.translation import npgettext_lazy, pgettext_lazy@@ -85,9 +85,8 @@ class AddToCheckoutForm(forms.Form):quantity = cleaned_data.get("quantity")if quantity is None:return cleaned_data- try:- variant = self.get_variant(cleaned_data)- except ObjectDoesNotExist:+ variant = self.get_variant(cleaned_data)+ if variant is None:self.add_error_i18n(NON_FIELD_ERRORS, "variant-does-not-exists")else:line = self.checkout.get_line(variant)diff --git a/tests/test_cart.py b/tests/test_cart.pyindex 6feb5cdbe9..5ad23d8af3 100644--- a/tests/test_cart.py+++ b/tests/test_cart.py@@ -5,7 +5,6 @@ from uuid import uuid4import pytestfrom django.contrib.auth.models import AnonymousUserfrom django.core import signing-from django.core.exceptions import ObjectDoesNotExistfrom django.urls import reversefrom measurement.measures import Weightfrom prices import Money, TaxedMoney@@ -382,20 +381,6 @@ def test_add_to_checkout_form(checkout, product):assert not form.is_valid()-def test_form_when_variant_does_not_exist():- checkout_lines = []- checkout = Mock(- add=lambda variant, quantity: checkout_lines.append(Mock()),- get_line=Mock(return_value=Mock(quantity=1)),- )-- form = forms.AddToCheckoutForm(- data={"quantity": 1}, checkout=checkout, product=Mock()- )- form.get_variant = Mock(side_effect=ObjectDoesNotExist)- assert not form.is_valid()--@pytest.mark.parametrize("track_inventory", (True, False))def test_add_to_checkout_form_when_insufficient_stock(product, track_inventory):variant = product.variants.first()diff --git a/tests/test_product.py b/tests/test_product.pyindex 5c547f17cc..7bbf019a29 100644--- a/tests/test_product.py+++ b/tests/test_product.py@@ -209,6 +209,19 @@ def test_view_invalid_add_to_checkout(client, product, request_checkout):assert request_checkout.quantity == 2+def test_view_add_to_checkout_invalid_variant(client, product, request_checkout):+ response = client.post(+ reverse(+ "product:add-to-checkout",+ kwargs={"slug": product.get_slug(), "product_id": product.pk},+ ),+ {"variant": 1234, "quantity": 1},+ HTTP_X_REQUESTED_WITH="XMLHttpRequest",+ )+ assert response.status_code == 400+ assert request_checkout.quantity == 0++def test_view_add_to_checkout(authorized_client, product, user_checkout):variant = product.variants.first()