Skip to content

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

3.1 Do not put street_address_2 into errors in billing address update (#9079)

L3str4nge4467893f0572
diff --git a/saleor/account/i18n.py b/saleor/account/i18n.py
index 53efa45147..cd27aae1a6 100644
--- a/saleor/account/i18n.py
+++ b/saleor/account/i18n.py
@@ -157,6 +157,8 @@ class CountryAwareAddressForm(AddressForm):
("country_code", ["country"]),
]
+ NOT_REQUIRED_FIELDS = ("street_address_2",)
+
class Meta:
model = Address
exclude = []
@@ -166,6 +168,10 @@ class CountryAwareAddressForm(AddressForm):
for field_name, error_code in errors.items():
local_fields = field_mapping[field_name]
for field in local_fields:
+
+ if field in self.NOT_REQUIRED_FIELDS:
+ continue
+
try:
error_msg = self.fields[field].error_messages[error_code]
except KeyError:
diff --git a/saleor/graphql/checkout/tests/deprecated/test_checkout_billing_address_update.py b/saleor/graphql/checkout/tests/deprecated/test_checkout_billing_address_update_deprecated.py
similarity index 100%
rename from saleor/graphql/checkout/tests/deprecated/test_checkout_billing_address_update.py
rename to saleor/graphql/checkout/tests/deprecated/test_checkout_billing_address_update_deprecated.py
diff --git a/saleor/graphql/checkout/tests/test_checkout_billing_address_update.py b/saleor/graphql/checkout/tests/test_checkout_billing_address_update.py
new file mode 100644
index 0000000000..6bebc2679b
--- /dev/null
+++ b/saleor/graphql/checkout/tests/test_checkout_billing_address_update.py
@@ -0,0 +1,121 @@
+from ...tests.utils import get_graphql_content
+
+MUTATION_CHECKOUT_BILLING_ADDRESS_UPDATE = """
+ mutation checkoutBillingAddressUpdate(
+ $checkoutId: ID, $token: UUID, $billingAddress: AddressInput!) {
+ checkoutBillingAddressUpdate(
+ checkoutId: $checkoutId,
+ token: $token,
+ billingAddress: $billingAddress
+ ){
+ checkout {
+ token,
+ id
+ },
+ errors {
+ field
+ code
+ message
+ }
+ }
+ }
+"""
+
+
+def test_checkout_billing_address_update_by_token(
+ user_api_client, checkout_with_item, graphql_address_data
+):
+ checkout = checkout_with_item
+ assert checkout.shipping_address is None
+
+ query = MUTATION_CHECKOUT_BILLING_ADDRESS_UPDATE
+ billing_address = graphql_address_data
+
+ variables = {"token": checkout_with_item.token, "billingAddress": billing_address}
+
+ response = user_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutBillingAddressUpdate"]
+ assert not data["errors"]
+ checkout.refresh_from_db()
+ assert checkout.billing_address is not None
+ assert checkout.billing_address.first_name == billing_address["firstName"]
+ assert checkout.billing_address.last_name == billing_address["lastName"]
+ assert (
+ checkout.billing_address.street_address_1 == billing_address["streetAddress1"]
+ )
+ assert (
+ checkout.billing_address.street_address_2 == billing_address["streetAddress2"]
+ )
+ assert checkout.billing_address.postal_code == billing_address["postalCode"]
+ assert checkout.billing_address.country == billing_address["country"]
+ assert checkout.billing_address.city == billing_address["city"].upper()
+
+
+def test_checkout_billing_address_update_by_token_without_required_fields(
+ user_api_client, checkout_with_item, graphql_address_data
+):
+ checkout = checkout_with_item
+ assert checkout.shipping_address is None
+
+ query = MUTATION_CHECKOUT_BILLING_ADDRESS_UPDATE
+
+ graphql_address_data["streetAddress1"] = ""
+ graphql_address_data["streetAddress2"] = ""
+ graphql_address_data["postalCode"] = ""
+
+ billing_address = graphql_address_data
+
+ variables = {"token": checkout_with_item.token, "billingAddress": billing_address}
+
+ response = user_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutBillingAddressUpdate"]
+ assert data["errors"]
+ assert data["errors"] == [
+ {
+ "code": "REQUIRED",
+ "field": "postalCode",
+ "message": "This field is required.",
+ },
+ {
+ "code": "REQUIRED",
+ "field": "streetAddress1",
+ "message": "This field is required.",
+ },
+ ]
+
+
+def test_checkout_billing_address_update_by_token_without_street_address_2(
+ user_api_client, checkout_with_item, graphql_address_data
+):
+ checkout = checkout_with_item
+ assert checkout.shipping_address is None
+
+ query = MUTATION_CHECKOUT_BILLING_ADDRESS_UPDATE
+
+ graphql_address_data["streetAddress2"] = ""
+
+ billing_address = graphql_address_data
+
+ variables = {"token": checkout_with_item.token, "billingAddress": billing_address}
+
+ response = user_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutBillingAddressUpdate"]
+ assert not data["errors"]
+ checkout.refresh_from_db()
+ assert checkout.billing_address is not None
+ assert checkout.billing_address.first_name == billing_address["firstName"]
+ assert checkout.billing_address.last_name == billing_address["lastName"]
+ assert (
+ checkout.billing_address.street_address_1 == billing_address["streetAddress1"]
+ )
+ assert (
+ checkout.billing_address.street_address_2
+ == billing_address["streetAddress2"]
+ == ""
+ )
+ assert checkout.billing_address.postal_code == billing_address["postalCode"]
+ assert checkout.billing_address.country == billing_address["country"]
+ assert checkout.billing_address.city == billing_address["city"].upper()