Repository: saleor/saleor

Issue:
Mutation checkoutLinesAdd doesn't work when empty checkout
### What I'm trying to achieve
Add a new product to empty checkout.

### Steps to reproduce the problem
1. Create new checkout.
2. If checkout has any product then remove it.
3. Add a new product to empty checkout.



Discussion:

Resolving patch:
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -63,17 +63,17 @@ def check_lines_quantity(variants, quantities):
     """Check if stock is sufficient for each line in the list of dicts.
     Return list of errors.
     """
-    # FIXME Add tests
     errors = []
     for variant, quantity in zip(variants, quantities):
         try:
             variant.check_quantity(quantity)
         except InsufficientStock as e:
             message = (
-                'Add line mutation error',
-                'Could not add item. Only %(remaining)d remaining in stock.' %
-                {'remaining': e.item.quantity_available})
-            errors.append((variant.name, message))
+                'Could not add item '
+                + '%(item_name)s. Only %(remaining)d remaining in stock.' % {
+                    'remaining': e.item.quantity_available,
+                    'item_name': e.item.display_product()})
+            errors.append(('quantity', message))
     return errors
 
 
@@ -166,34 +166,33 @@ def mutate(cls, root, info, checkout_id, lines, replace=False):
         errors = []
         checkout = cls.get_node_or_error(
             info, checkout_id, errors, 'checkout_id', only_type=Checkout)
-        if not checkout:
-            return CheckoutLinesAdd(errors=errors)
-
         variants, quantities = None, None
-        if lines:
-            variant_ids = [line.get('variant_id') for line in lines]
-            variants = cls.get_nodes_or_error(
-                ids=variant_ids, errors=errors, field='variant_id',
-                only_type=ProductVariant)
-            quantities = [line.get('quantity') for line in lines]
-            line_errors = check_lines_quantity(variants, quantities)
-            if line_errors:
-                for err in line_errors:
-                    cls.add_error(errors, field=err[0], message=err[1])
+        if checkout is not None:
+            if lines:
+                variant_ids = [line.get('variant_id') for line in lines]
+                variants = cls.get_nodes_or_error(
+                    ids=variant_ids, errors=errors, field='variant_id',
+                    only_type=ProductVariant)
+                quantities = [line.get('quantity') for line in lines]
+                line_errors = check_lines_quantity(variants, quantities)
+                if line_errors:
+                    for err in line_errors:
+                        cls.add_error(errors, field=err[0], message=err[1])
+
+            # FIXME test if below function is called
+            clean_shipping_method(
+                checkout=checkout, method=checkout.shipping_method,
+                errors=errors, discounts=info.context.discounts,
+                taxes=get_taxes_for_address(checkout.shipping_address))
+
+        if errors:
+            return CheckoutLinesAdd(errors=errors)
 
         if variants and quantities:
             for variant, quantity in zip(variants, quantities):
                 add_variant_to_cart(
                     checkout, variant, quantity, replace=replace)
 
-        # FIXME test if below function is called
-        clean_shipping_method(
-            checkout=checkout, method=checkout.shipping_method, errors=errors,
-            discounts=info.context.discounts,
-            taxes=get_taxes_for_address(checkout.shipping_address))
-        if errors:
-            return CheckoutLinesAdd(errors=errors)
-
         return CheckoutLinesAdd(checkout=checkout, errors=errors)
 
 
@@ -260,7 +259,7 @@ def mutate(cls, root, info, checkout_id, customer_id):
             info, checkout_id, errors, 'checkout_id', only_type=Checkout)
         customer = cls.get_node_or_error(
             info, customer_id, errors, 'customer_id', only_type=User)
-        if checkout and customer:
+        if checkout is not None and customer:
             checkout.user = customer
             checkout.save(update_fields=['user'])
         return CheckoutCustomerAttach(checkout=checkout, errors=errors)
@@ -280,12 +279,11 @@ def mutate(cls, root, info, checkout_id):
         errors = []
         checkout = cls.get_node_or_error(
             info, checkout_id, errors, 'checkout_id', only_type=Checkout)
-        if not checkout:
-            return cls(errors=errors)
-        if not checkout.user:
+        if checkout is not None and not checkout.user:
             cls.add_error(
                 errors, field=None,
                 message='There\'s no customer assigned to this Checkout.')
+        if errors:
             return CheckoutCustomerDetach(errors=errors)
 
         checkout.user = None
@@ -311,21 +309,20 @@ def mutate(cls, root, info, checkout_id, shipping_address):
         checkout = cls.get_node_or_error(
             info, checkout_id, errors, 'checkout_id', only_type=Checkout)
 
-        shipping_address, errors = cls.validate_address(
-            shipping_address, errors, instance=checkout.shipping_address)
-        if errors:
-            return CheckoutShippingAddressUpdate(errors=errors)
-
-        clean_shipping_method(
-            checkout, checkout.shipping_method, errors,
-            info.context.discounts, get_taxes_for_address(shipping_address))
-        if errors:
-            CheckoutShippingAddressUpdate(errors=errors)
-
-        if checkout and shipping_address:
-            with transaction.atomic():
-                shipping_address.save()
-                change_shipping_address_in_cart(checkout, shipping_address)
+        if checkout is not None:
+            shipping_address, errors = cls.validate_address(
+                shipping_address, errors, instance=checkout.shipping_address)
+            clean_shipping_method(
+                checkout, checkout.shipping_method, errors,
+                info.context.discounts,
+                get_taxes_for_address(shipping_address))
+            if errors:
+                CheckoutShippingAddressUpdate(errors=errors)
+
+            if shipping_address:
+                with transaction.atomic():
+                    shipping_address.save()
+                    change_shipping_address_in_cart(checkout, shipping_address)
         return CheckoutShippingAddressUpdate(checkout=checkout, errors=errors)
 
 
@@ -353,7 +350,7 @@ def mutate(cls, root, info, checkout_id, billing_address):
         if errors:
             return CheckoutBillingAddressUpdate(errors=errors)
 
-        if checkout and billing_address:
+        if checkout is not None and billing_address:
             with transaction.atomic():
                 billing_address.save()
                 change_billing_address_in_cart(checkout, billing_address)
@@ -375,7 +372,7 @@ def mutate(cls, root, info, checkout_id, email):
         errors = []
         checkout = cls.get_node_or_error(
             info, checkout_id, errors, 'checkout_id', only_type=Checkout)
-        if checkout:
+        if checkout is not None:
             checkout.email = email
             checkout.save(update_fields=['email'])
 
@@ -402,9 +399,10 @@ def mutate(cls, root, info, checkout_id, shipping_method_id):
             info, shipping_method_id, errors, 'shipping_method_id',
             only_type=ShippingMethod)
 
-        clean_shipping_method(
-            checkout, shipping_method, errors, info.context.discounts,
-            info.context.taxes, remove=False)
+        if checkout is not None and shipping_method:
+            clean_shipping_method(
+                checkout, shipping_method, errors, info.context.discounts,
+                info.context.taxes, remove=False)
         if errors:
             return CheckoutShippingMethodUpdate(errors=errors)
 
@@ -429,7 +427,7 @@ def mutate(cls, root, info, checkout_id):
         errors = []
         checkout = cls.get_node_or_error(
             info, checkout_id, errors, 'checkout_id', only_type=Checkout)
-        if not checkout:
+        if checkout is None:
             return CheckoutComplete(errors=errors)
         taxes = get_taxes_for_cart(checkout, info.context.taxes)
         ready, checkout_error = ready_to_place_order(


Test patch:
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -9,18 +9,24 @@
 from tests.api.utils import get_graphql_content
 
 
-def test_checkout_create(user_api_client, variant, graphql_address_data):
-    """Create checkout object using GraphQL API."""
-    query = """
+MUTATION_CHECKOUT_CREATE = """
     mutation createCheckout($checkoutInput: CheckoutCreateInput!) {
         checkoutCreate(input: $checkoutInput) {
             checkout {
                 token,
                 id
             }
+            errors {
+                field
+                message
+            }
         }
     }
     """
+
+
+def test_checkout_create(user_api_client, variant, graphql_address_data):
+    """Create checkout object using GraphQL API."""
     variant_id = graphene.Node.to_global_id('ProductVariant', variant.id)
     test_email = 'test@example.com'
     shipping_address = graphql_address_data
@@ -32,7 +38,8 @@ def test_checkout_create(user_api_client, variant, graphql_address_data):
             'email': test_email,
             'shippingAddress': shipping_address}}
     assert not Cart.objects.exists()
-    response = user_api_client.post_graphql(query, variables)
+    response = user_api_client.post_graphql(
+        MUTATION_CHECKOUT_CREATE, variables)
     content = get_graphql_content(response)
 
     new_cart = Cart.objects.first()
@@ -57,39 +64,58 @@ def test_checkout_create(user_api_client, variant, graphql_address_data):
     assert new_cart.shipping_address.city == shipping_address['city']
 
 
-def test_checkout_lines_add(user_api_client, cart_with_item, variant):
-    """
-    Create checkout object using GraphQL API
-    """
-    cart = cart_with_item
-    line = cart.lines.first()
-    assert line.quantity == 3
-    query = """
-        mutation checkoutLinesAdd($checkoutId: ID!, $lines: [CheckoutLineInput!]!) {
-            checkoutLinesAdd(checkoutId: $checkoutId, lines: $lines) {
-                checkout {
-                    token
-                    lines {
-                        quantity
-                        variant {
-                            id
-                        }
+def test_checkout_create_check_lines_quantity(
+        user_api_client, variant, graphql_address_data):
+    variant_id = graphene.Node.to_global_id('ProductVariant', variant.id)
+    test_email = 'test@example.com'
+    shipping_address = graphql_address_data
+    variables = {
+        'checkoutInput': {
+            'lines': [{
+                'quantity': 3,
+                'variantId': variant_id}],
+            'email': test_email,
+            'shippingAddress': shipping_address}}
+    response = user_api_client.post_graphql(
+        MUTATION_CHECKOUT_CREATE, variables)
+    content = get_graphql_content(response)
+    data = content['data']['checkoutCreate']
+    assert data['errors'][0]['message'] == 'Could not add item Test product (SKU_A). Only 2 remaining in stock.'
+    assert data['errors'][0]['field'] == 'quantity'
+
+
+MUTATION_CHECKOUT_LINES_ADD = """
+    mutation checkoutLinesAdd($checkoutId: ID!, $lines: [CheckoutLineInput!]!) {
+        checkoutLinesAdd(checkoutId: $checkoutId, lines: $lines) {
+            checkout {
+                token
+                lines {
+                    quantity
+                    variant {
+                        id
                     }
                 }
-                errors {
-                    field
-                    message
-                }
+            }
+            errors {
+                field
+                message
             }
         }
-    """
+    }"""
+
+
+def test_checkout_lines_add(user_api_client, cart_with_item, variant):
+    cart = cart_with_item
+    line = cart.lines.first()
+    assert line.quantity == 3
     variant_id = graphene.Node.to_global_id('ProductVariant', variant.pk)
     checkout_id = graphene.Node.to_global_id('Checkout', cart.pk)
 
     variables = {
         'checkoutId': checkout_id,
         'lines': [{'variantId': variant_id, 'quantity': 1}]}
-    response = user_api_client.post_graphql(query, variables)
+    response = user_api_client.post_graphql(
+        MUTATION_CHECKOUT_LINES_ADD, variables)
     content = get_graphql_content(response)
     data = content['data']['checkoutLinesAdd']
     assert not data['errors']
@@ -99,31 +125,69 @@ def test_checkout_lines_add(user_api_client, cart_with_item, variant):
     assert line.quantity == 1
 
 
-def test_checkout_lines_update(user_api_client, cart_with_item):
-    cart = cart_with_item
-    assert cart.lines.count() == 1
+def test_checkout_lines_add_empty_checkout(
+        user_api_client, cart, variant):
+    variant_id = graphene.Node.to_global_id('ProductVariant', variant.pk)
+    checkout_id = graphene.Node.to_global_id('Checkout', cart.pk)
+
+    variables = {
+        'checkoutId': checkout_id,
+        'lines': [{'variantId': variant_id, 'quantity': 1}]}
+    response = user_api_client.post_graphql(
+        MUTATION_CHECKOUT_LINES_ADD, variables)
+    content = get_graphql_content(response)
+    data = content['data']['checkoutLinesAdd']
+    assert not data['errors']
+    cart.refresh_from_db()
     line = cart.lines.first()
-    variant = line.variant
-    assert line.quantity == 3
-    query = """
-        mutation checkoutLinesUpdate($checkoutId: ID!, $lines: [CheckoutLineInput!]!) {
-            checkoutLinesUpdate(checkoutId: $checkoutId, lines: $lines) {
-                checkout {
-                    token
-                    lines {
-                        quantity
-                        variant {
-                            id
-                        }
+    assert line.variant == variant
+    assert line.quantity == 1
+
+
+def test_checkout_lines_add_check_lines_quantity(
+        user_api_client, cart, variant):
+    variant_id = graphene.Node.to_global_id('ProductVariant', variant.pk)
+    checkout_id = graphene.Node.to_global_id('Checkout', cart.pk)
+
+    variables = {
+        'checkoutId': checkout_id,
+        'lines': [{'variantId': variant_id, 'quantity': 3}]}
+    response = user_api_client.post_graphql(
+        MUTATION_CHECKOUT_LINES_ADD, variables)
+    content = get_graphql_content(response)
+    data = content['data']['checkoutLinesAdd']
+    assert data['errors'][0]['message'] == 'Could not add item Test product (SKU_A). Only 2 remaining in stock.'
+    assert data['errors'][0]['field'] == 'quantity'
+
+
+MUTATION_CHECKOUT_LINES_UPDATE = """
+    mutation checkoutLinesUpdate($checkoutId: ID!, $lines: [CheckoutLineInput!]!) {
+        checkoutLinesUpdate(checkoutId: $checkoutId, lines: $lines) {
+            checkout {
+                token
+                lines {
+                    quantity
+                    variant {
+                        id
                     }
                 }
-                errors {
-                    field
-                    message
-                }
+            }
+            errors {
+                field
+                message
             }
         }
+    }
     """
+
+
+def test_checkout_lines_update(user_api_client, cart_with_item):
+    cart = cart_with_item
+    assert cart.lines.count() == 1
+    line = cart.lines.first()
+    variant = line.variant
+    assert line.quantity == 3
+
     variant_id = graphene.Node.to_global_id('ProductVariant', variant.pk)
     checkout_id = graphene.Node.to_global_id('Checkout', cart.pk)
 
@@ -132,7 +196,8 @@ def test_checkout_lines_update(user_api_client, cart_with_item):
         'lines': [{
             'variantId': variant_id,
             'quantity': 1}]}
-    response = user_api_client.post_graphql(query, variables)
+    response = user_api_client.post_graphql(
+        MUTATION_CHECKOUT_LINES_UPDATE, variables)
     content = get_graphql_content(response)
 
     data = content['data']['checkoutLinesUpdate']
@@ -143,11 +208,38 @@ def test_checkout_lines_update(user_api_client, cart_with_item):
     assert line.variant == variant
     assert line.quantity == 1
 
-    # test invalid checkout_id results in error
+
+def test_checkout_lines_update_invalid_checkout_id(
+        user_api_client):
     variables = {'checkoutId': 'test', 'lines': []}
-    response = user_api_client.post_graphql(query, variables)
+    response = user_api_client.post_graphql(
+        MUTATION_CHECKOUT_LINES_UPDATE, variables)
     content = get_graphql_content(response)
-    assert content['data']['checkoutLinesUpdate']['errors'][0]['field'] == 'checkoutId'
+    data = content['data']['checkoutLinesUpdate']
+    assert data['errors'][0]['field'] == 'checkoutId'
+
+
+def test_checkout_lines_update_check_lines_quantity(
+        user_api_client, cart_with_item):
+    cart = cart_with_item
+    line = cart.lines.first()
+    variant = line.variant
+
+    variant_id = graphene.Node.to_global_id('ProductVariant', variant.pk)
+    checkout_id = graphene.Node.to_global_id('Checkout', cart.pk)
+
+    variables = {
+        'checkoutId': checkout_id,
+        'lines': [{
+            'variantId': variant_id,
+            'quantity': 10}]}
+    response = user_api_client.post_graphql(
+        MUTATION_CHECKOUT_LINES_UPDATE, variables)
+    content = get_graphql_content(response)
+
+    data = content['data']['checkoutLinesUpdate']
+    assert data['errors'][0]['message'] == 'Could not add item Test product (123). Only 9 remaining in stock.'
+    assert data['errors'][0]['field'] == 'quantity'
 
 
 def test_checkout_line_delete(user_api_client, cart_with_item):
@@ -218,29 +310,32 @@ def test_checkout_customer_attach(
     assert cart.user == customer_user
 
 
+MUTATION_CHECKOUT_CUSTOMER_DETACH = """
+    mutation checkoutCustomerDetach($checkoutId: ID!) {
+        checkoutCustomerDetach(checkoutId: $checkoutId) {
+            checkout {
+                token
+            }
+            errors {
+                field
+                message
+            }
+        }
+    }
+    """
+
+
 def test_checkout_customer_detach(
         user_api_client, cart_with_item, customer_user):
     cart = cart_with_item
     cart.user = customer_user
     cart.save(update_fields=['user'])
 
-    query = """
-        mutation checkoutCustomerDetach($checkoutId: ID!) {
-            checkoutCustomerDetach(checkoutId: $checkoutId) {
-                checkout {
-                    token
-                }
-                errors {
-                    field
-                    message
-                }
-            }
-        }
-    """
     checkout_id = graphene.Node.to_global_id('Checkout', cart.pk)
     variables = {
         'checkoutId': checkout_id, }
-    response = user_api_client.post_graphql(query, variables)
+    response = user_api_client.post_graphql(
+        MUTATION_CHECKOUT_CUSTOMER_DETACH, variables)
     content = get_graphql_content(response)
 
     data = content['data']['checkoutCustomerDetach']
@@ -249,6 +344,20 @@ def test_checkout_customer_detach(
     assert cart.user is None
 
 
+def test_checkout_customer_detach_without_customer(
+        user_api_client, cart_with_item, customer_user):
+    cart = cart_with_item
+
+    checkout_id = graphene.Node.to_global_id('Checkout', cart.pk)
+    variables = {
+        'checkoutId': checkout_id, }
+    response = user_api_client.post_graphql(
+        MUTATION_CHECKOUT_CUSTOMER_DETACH, variables)
+    content = get_graphql_content(response)
+    data = content['data']['checkoutCustomerDetach']
+    assert data['errors'][0]['message'] == 'There\'s no customer assigned to this Checkout.'
+
+
 def test_checkout_shipping_address_update(
         user_api_client, cart_with_item, graphql_address_data):
     cart = cart_with_item