Skip to content

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

Refactor checkout customer detach graphql mutation

fowczarek7dd3328d2989
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index 358063ec7b..ac1c36a56c 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -279,12 +279,11 @@ class CheckoutCustomerDetach(BaseMutation):
errors = []
checkout = cls.get_node_or_error(
info, checkout_id, errors, 'checkout_id', only_type=Checkout)
- if checkout is None:
- 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
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index 82a5250031..787c6e1f8b 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -310,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']
@@ -341,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