Skip to content

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

Fix CheckoutCustomerAttach mutation behaviour with possibility to be used by another user. (#7669)

Dawid Matuszczykd90b23072cda
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index 68a6de5564..611c8a8899 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -640,6 +640,11 @@ class CheckoutCustomerAttach(BaseMutation):
info, checkout_id or token, only_type=Checkout, field="checkout_id"
)
+ # Raise error when trying to attach a user to a checkout
+ # that is already owned by another user.
+ if checkout.user:
+ raise PermissionDenied()
+
checkout.user = info.context.user
checkout.email = info.context.user.email
checkout.save(update_fields=["email", "user", "last_change"])
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index 76500b7ed3..264e7e35fc 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -1506,6 +1506,43 @@ def test_checkout_customer_attach(
assert checkout.email == customer_user.email
+def test_checkout_customer_attach_user_to_checkout_with_user(
+ user_api_client, customer_user, user_checkout, address
+):
+ checkout = user_checkout
+
+ query = """
+ mutation checkoutCustomerAttach($checkoutId: ID, $token: UUID) {
+ checkoutCustomerAttach(checkoutId: $checkoutId, token: $token) {
+ checkout {
+ token
+ }
+ errors {
+ field
+ message
+ code
+ }
+ }
+ }
+"""
+
+ default_address = address.get_copy()
+ second_user = User.objects.create_user(
+ "password",
+ default_billing_address=default_address,
+ default_shipping_address=default_address,
+ first_name="Test2",
+ last_name="Tested",
+ )
+
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+ customer_id = graphene.Node.to_global_id("User", second_user.pk)
+ variables = {"checkoutId": checkout_id, "customerId": customer_id}
+ response = user_api_client.post_graphql(query, variables)
+ assert_no_permission(response)
+
+
MUTATION_CHECKOUT_CUSTOMER_DETACH = """
mutation checkoutCustomerDetach($token: UUID) {
checkoutCustomerDetach(token: $token) {