Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix assigning an email address that does not belong to an existing user to draft order (#10320)
SzymJ97129cf0c960
diff --git a/saleor/graphql/order/mutations/draft_order_create.py b/saleor/graphql/order/mutations/draft_order_create.pyindex 3b4384be7b..836223796e 100644--- a/saleor/graphql/order/mutations/draft_order_create.py+++ b/saleor/graphql/order/mutations/draft_order_create.py@@ -6,6 +6,7 @@ from django.core.exceptions import ValidationErrorfrom django.db import transactionfrom graphene.types import InputObjectType+from ....account.models import Userfrom ....checkout import AddressTypefrom ....core.permissions import OrderPermissionsfrom ....core.taxes import TaxError@@ -117,6 +118,13 @@ class DraftOrderCreate(ModelMutation, I18nMixin):object_id=data.pop("shipping_method", None), raise_error=False)+ if email := data.get("user_email", None):+ try:+ user = User.objects.get(email=email, is_active=True)+ data["user"] = graphene.Node.to_global_id("User", user.id)+ except User.DoesNotExist:+ data["user"] = None+cleaned_input = super().clean_input(info, instance, data)channel = cls.clean_channel_id(info, instance, cleaned_input, channel_id)diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.pyindex 18d3bcd404..60a5c3e9cb 100644--- a/saleor/graphql/order/tests/test_order.py+++ b/saleor/graphql/order/tests/test_order.py@@ -3883,6 +3883,85 @@ def test_draft_order_update_free_shipping_voucher(assert order.voucher+DRAFT_ORDER_UPDATE_USER_EMAIL_MUTATION = """+ mutation draftUpdate(+ $id: ID!+ $userEmail: String!+ ) {+ draftOrderUpdate(+ id: $id+ input: {+ userEmail: $userEmail+ }+ ) {+ errors {+ field+ message+ code+ }+ order {+ id+ }+ }+ }+ """+++def test_draft_order_update_when_not_existing_customer_email_provided(+ staff_api_client, permission_manage_orders, draft_order+):+ # given+ order = draft_order+ assert order.user++ query = DRAFT_ORDER_UPDATE_USER_EMAIL_MUTATION+ order_id = graphene.Node.to_global_id("Order", order.id)+ email = "[email protected]"+ variables = {"id": order_id, "userEmail": email}++ # when+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders]+ )+ content = get_graphql_content(response)+ data = content["data"]["draftOrderUpdate"]+ order.refresh_from_db()++ # then+ assert not data["errors"]+ assert not order.user+ assert order.user_email == email+++def test_draft_order_update_assign_user_when_existing_customer_email_provided(+ staff_api_client, permission_manage_orders, draft_order+):+ # given+ order = draft_order+ user = order.user+ user_email = user.email+ order.user = None+ order.save(update_fields=["user"])+ assert not order.user++ query = DRAFT_ORDER_UPDATE_USER_EMAIL_MUTATION+ order_id = graphene.Node.to_global_id("Order", order.id)+ variables = {"id": order_id, "userEmail": user_email}++ # when+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders]+ )+ content = get_graphql_content(response)+ data = content["data"]["draftOrderUpdate"]+ order.refresh_from_db()++ # then+ assert not data["errors"]+ assert order.user == user+ assert order.user_email == user_email++def test_draft_order_delete(staff_api_client, permission_manage_orders, draft_order):order = draft_orderquery = """