Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Allow skipping address validation in customerCreate and customerUpdate (#19729)
Lukasz Ostrowski3e1b49f05450
diff --git a/saleor/graphql/account/i18n.py b/saleor/graphql/account/i18n.pyindex 9e8803095c..9ae01992dd 100644--- a/saleor/graphql/account/i18n.py+++ b/saleor/graphql/account/i18n.py@@ -20,6 +20,8 @@ from ..site.dataloaders import get_site_promiseSKIP_ADDRESS_VALIDATION_PERMISSION_MAP: dict[str, list[BasePermissionEnum]] = {"addressCreate": [AccountPermissions.MANAGE_USERS],"addressUpdate": [AccountPermissions.MANAGE_USERS],+ "customerCreate": [AccountPermissions.MANAGE_USERS],+ "customerUpdate": [AccountPermissions.MANAGE_USERS],"customerBulkUpdate": [AccountPermissions.MANAGE_USERS],"draftOrderCreate": [OrderPermissions.MANAGE_ORDERS],"draftOrderUpdate": [OrderPermissions.MANAGE_ORDERS],diff --git a/saleor/graphql/account/tests/mutations/staff/test_customer_create.py b/saleor/graphql/account/tests/mutations/staff/test_customer_create.pyindex d20fada303..939cc22348 100644--- a/saleor/graphql/account/tests/mutations/staff/test_customer_create.py+++ b/saleor/graphql/account/tests/mutations/staff/test_customer_create.py@@ -2,6 +2,7 @@ from unittest.mock import ANY, patchfrom urllib.parse import urlencodeimport graphene+import pytestfrom ......account import events as account_eventsfrom ......account.error_codes import AccountErrorCode@@ -48,6 +49,7 @@ CUSTOMER_CREATE_MUTATION = """iddefaultBillingAddress {id+ postalCodemetadata {keyvalue@@ -873,3 +875,54 @@ def test_create_with_invalid_customer_type_id(assert error["field"] == "customerType"assert error["code"] == AccountErrorCode.GRAPHQL_ERROR.nameassert not User.objects.filter(email=email).exists()+++ ("_case", "skip_validation", "expected_error_field"),+ [+ ("validation skipped", True, None),+ ("validation enforced", False, "postalCode"),+ ],+)+def test_create_address_skip_validation(+ _case,+ skip_validation,+ expected_error_field,+ staff_api_client,+ permission_manage_users,+ graphql_address_data,+):+ # given+ email = "[email protected]"+ invalid_postal_code = "invalid_postal_code"+ address_data = {+ **graphql_address_data,+ "postalCode": invalid_postal_code,+ "skipValidation": skip_validation,+ }+ variables = {"email": email, "billing": address_data}++ # when+ response = staff_api_client.post_graphql(+ CUSTOMER_CREATE_MUTATION, variables, permissions=[permission_manage_users]+ )+ content = get_graphql_content(response)++ # then+ data = content["data"]["customerCreate"]+ if expected_error_field:+ assert len(data["errors"]) == 1+ assert data["errors"][0]["field"] == expected_error_field+ assert (+ data["errors"][0]["message"] == "This value is not valid for the address."+ )+ assert data["user"] is None+ assert User.objects.filter(email=email).exists() is False+ else:+ assert data["errors"] == []+ assert data["user"]["defaultBillingAddress"]["postalCode"] == (+ invalid_postal_code+ )+ address = User.objects.get(email=email).default_billing_address+ assert address.postal_code == invalid_postal_code+ assert address.validation_skipped is Truediff --git a/saleor/graphql/account/tests/mutations/staff/test_customer_update.py b/saleor/graphql/account/tests/mutations/staff/test_customer_update.pyindex 8c97131dba..4ce71a0aa6 100644--- a/saleor/graphql/account/tests/mutations/staff/test_customer_update.py+++ b/saleor/graphql/account/tests/mutations/staff/test_customer_update.py@@ -33,6 +33,7 @@ CUSTOMER_UPDATE_MUTATION = """lastNamedefaultBillingAddress {id+ postalCodemetadata {keyvalue@@ -40,6 +41,7 @@ CUSTOMER_UPDATE_MUTATION = """}defaultShippingAddress {id+ postalCodemetadata {keyvalue@@ -1169,3 +1171,63 @@ def test_update_with_attributes_replaces_existing_value(assert assigned_value.value == new_value# the old value remains a choice of the attributeassert loyalty_customer_attribute.values.filter(pk=old_value.pk).exists()+++ ("_case", "skip_validation", "expected_error_field"),+ [+ ("validation skipped", True, None),+ ("validation enforced", False, "postalCode"),+ ],+)+def test_update_address_skip_validation(+ _case,+ skip_validation,+ expected_error_field,+ staff_api_client,+ customer_user,+ permission_manage_users,+ graphql_address_data,+):+ # given+ invalid_postal_code = "invalid_postal_code"+ address_data = {+ **graphql_address_data,+ "postalCode": invalid_postal_code,+ "skipValidation": skip_validation,+ }+ previous_postal_code = customer_user.default_billing_address.postal_code+ assert previous_postal_code != invalid_postal_code++ variables = {+ "id": graphene.Node.to_global_id("User", customer_user.pk),+ "input": {"defaultBillingAddress": address_data},+ }++ # when+ response = staff_api_client.post_graphql(+ CUSTOMER_UPDATE_MUTATION, variables, permissions=[permission_manage_users]+ )+ content = get_graphql_content(response)++ # then+ data = content["data"]["customerUpdate"]+ customer_user.refresh_from_db()+ if expected_error_field:+ assert len(data["errors"]) == 1+ assert data["errors"][0]["field"] == expected_error_field+ assert (+ data["errors"][0]["message"] == "This value is not valid for the address."+ )+ assert data["user"] is None+ assert customer_user.default_billing_address.postal_code == (+ previous_postal_code+ )+ else:+ assert data["errors"] == []+ assert data["user"]["defaultBillingAddress"]["postalCode"] == (+ invalid_postal_code+ )+ address = customer_user.default_billing_address+ assert address.postal_code == invalid_postal_code+ assert address.validation_skipped is True