Skip to content

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

Fix attaching params for address form errors (#7485)

Iga Karbowiak5f97c747adbf
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0c201e6bd5..df1a9d6f03 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -115,6 +115,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Order events performance - #7424 by tomaszszymanski129
- Add hash to uploading images #7453 by @IKarbowiak
- Add file format validation for uploaded images - #7447 by @IKarbowiak
+- Fix attaching params for address form errors - #7485 by @IKarbowiak
### Breaking
- Multichannel MVP: Multicurrency - #6242 by @fowczarek @d-wysocki
diff --git a/saleor/graphql/account/i18n.py b/saleor/graphql/account/i18n.py
index 3f3a98fe18..af57c0e716 100644
--- a/saleor/graphql/account/i18n.py
+++ b/saleor/graphql/account/i18n.py
@@ -54,7 +54,10 @@ class I18nMixin:
errors_dict = address_form.errors.as_data()
for errors in errors_dict.values():
for error in errors:
- error.params = params
+ if not error.params:
+ error.params = params
+ else:
+ error.params.update(params)
return errors_dict
@classmethod
diff --git a/saleor/graphql/account/tests/test_i18n.py b/saleor/graphql/account/tests/test_i18n.py
new file mode 100644
index 0000000000..a8d5bd9d9b
--- /dev/null
+++ b/saleor/graphql/account/tests/test_i18n.py
@@ -0,0 +1,97 @@
+import pytest
+from django.core.exceptions import ValidationError
+
+from ....checkout import AddressType
+from ..i18n import I18nMixin
+
+
+def test_validate_address():
+ # given
+ address_data = {
+ "first_name": "John Saleor",
+ "last_name": "Doe Mirumee",
+ "company_name": "Mirumee Software",
+ "street_address_1": "Tęczowa 7",
+ "street_address_2": "",
+ "postal_code": "53-601",
+ "country": "PL",
+ "city": "Wrocław",
+ "country_area": "",
+ "phone": "+48321321888",
+ }
+ # when
+ address = I18nMixin.validate_address(
+ address_data, address_type=AddressType.SHIPPING
+ )
+
+ # then
+ assert address
+
+
+def test_validate_address_invalid_postal_code():
+ # given
+ address_data = {
+ "first_name": "John Saleor",
+ "last_name": "Doe Mirumee",
+ "company_name": "Mirumee Software",
+ "street_address_1": "Tęczowa 7",
+ "street_address_2": "",
+ "postal_code": "Test Test 111 Test Road BEDFORD AM23 0UP",
+ "country": "PL",
+ "city": "Wrocław",
+ "country_area": "",
+ "phone": "+48321321888",
+ }
+
+ # when
+ with pytest.raises(ValidationError) as error:
+ I18nMixin.validate_address(address_data, address_type=AddressType.SHIPPING)
+
+ # then
+ assert len(error.value.error_dict["postal_code"]) == 2
+
+
+def test_validate_address_no_country_code():
+ # given
+ address_data = {
+ "first_name": "John Saleor",
+ "last_name": "Doe Mirumee",
+ "company_name": "Mirumee Software",
+ "street_address_1": "Tęczowa 7",
+ "street_address_2": "",
+ "postal_code": "53-601",
+ "country": "",
+ "city": "Wrocław",
+ "country_area": "",
+ "phone": "+48321321888",
+ }
+
+ # when
+ with pytest.raises(ValidationError) as error:
+ I18nMixin.validate_address(address_data, address_type=AddressType.SHIPPING)
+
+ # then
+ assert len(error.value.error_dict["country"]) == 1
+
+
+def test_validate_address_no_city():
+ # given
+ address_data = {
+ "first_name": "John Saleor",
+ "last_name": "Doe Mirumee",
+ "company_name": "Mirumee Software",
+ "street_address_1": "Tęczowa 7",
+ "street_address_2": "",
+ "postal_code": "53-601",
+ "country": "PL",
+ "city": "",
+ "country_area": "",
+ "phone": "+48321321888",
+ }
+
+ # when
+ with pytest.raises(ValidationError) as error:
+ I18nMixin.validate_address(address_data, address_type=AddressType.SHIPPING)
+
+ # then
+ assert len(error.value.error_dict["city"]) == 1
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.py
index 94fa0db9f3..d7e6655959 100644
--- a/saleor/graphql/order/tests/test_order.py
+++ b/saleor/graphql/order/tests/test_order.py
@@ -2320,6 +2320,41 @@ def test_draft_order_update_tax_error(
assert not order.customer_note
+def test_draft_order_update_invalid_address(
+ staff_api_client,
+ permission_manage_orders,
+ draft_order,
+ voucher,
+ graphql_address_data,
+):
+ order = draft_order
+ assert not order.voucher
+ assert not order.customer_note
+ graphql_address_data["postalCode"] = "TEST TEST invalid postal code 12345"
+ query = DRAFT_ORDER_UPDATE_MUTATION
+ order_id = graphene.Node.to_global_id("Order", order.id)
+ voucher_id = graphene.Node.to_global_id("Voucher", voucher.id)
+
+ variables = {
+ "id": order_id,
+ "voucher": voucher_id,
+ "shippingAddress": graphql_address_data,
+ }
+
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_orders]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["draftOrderUpdate"]
+ assert len(data["errors"]) == 2
+ assert not data["order"]
+ assert {error["code"] for error in data["errors"]} == {
+ OrderErrorCode.INVALID.name,
+ OrderErrorCode.REQUIRED.name,
+ }
+ assert {error["field"] for error in data["errors"]} == {"postalCode"}
+
+
def test_draft_order_update_doing_nothing_generates_no_events(
staff_api_client, permission_manage_orders, order_with_lines
):