Skip to content

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

Add required country in addres validation

fowczrek10a414e50adf
diff --git a/saleor/graphql/account/i18n.py b/saleor/graphql/account/i18n.py
index 15beb7af71..282d303015 100644
--- a/saleor/graphql/account/i18n.py
+++ b/saleor/graphql/account/i18n.py
@@ -43,6 +43,10 @@ class I18nMixin:
@classmethod
def validate_address(cls, address_data: dict, instance=None, info=None):
+ if address_data.get("country") is None:
+ raise ValidationError(
+ {"country": ValidationError("This field is required.", code="required")}
+ )
address_form = cls.validate_address_form(address_data)
if not instance:
instance = Address()
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index 975cb5ffc6..1201bba3e2 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -331,6 +331,52 @@ def test_checkout_create_required_email(api_client, stock):
assert checkout_errors[0]["code"] == CheckoutErrorCode.REQUIRED.name
+def test_checkout_create_required_country_shipping_address(
+ api_client, stock, graphql_address_data
+):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ shipping_address = graphql_address_data
+ del shipping_address["country"]
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": "[email protected]",
+ "shippingAddress": shipping_address,
+ }
+ }
+
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+
+ checkout_errors = content["data"]["checkoutCreate"]["checkoutErrors"]
+ assert checkout_errors[0]["field"] == "country"
+ assert checkout_errors[0]["code"] == CheckoutErrorCode.REQUIRED.name
+
+
+def test_checkout_create_required_country_billing_address(
+ api_client, stock, graphql_address_data
+):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ billing_address = graphql_address_data
+ del billing_address["country"]
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": "[email protected]",
+ "billingAddress": billing_address,
+ }
+ }
+
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+
+ checkout_errors = content["data"]["checkoutCreate"]["checkoutErrors"]
+ assert checkout_errors[0]["field"] == "country"
+ assert checkout_errors[0]["code"] == CheckoutErrorCode.REQUIRED.name
+
+
def test_checkout_create_default_email_for_logged_in_customer(user_api_client, stock):
variant = stock.product_variant
variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)