Repository: saleor/saleor

Issue:
Bug: "streetAddress1", "streetAddress2" field validation 
### What are you trying to achieve?

I want to standardize the validation messages


### Steps to reproduce the problem

Update address with too long streetAddress1 or streetAddress2 value: 

```
{
  "id": "VXNlcjoxMzY5",
  "input": {
    "city": "test",
    "cityArea": "test",
    "companyName": "test",
    "country": "US",
    "countryArea": "IN",
    "firstName": "test",
    "lastName": "test",
    "phone": "",
    "postalCode": "46802",
    "streetAddress1": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum",
    "streetAddress2": ""
  }
}
```

Result: 
```
{
  "errors": [
    {
      "message": "Internal Server Error",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "addressUpdate"
      ],
      "extensions": {
        "exception": {
          "code": "KeyError"
        }
      }
    }
  ],
  "data": {
    "addressUpdate": null
  }
}
``` 

### What did you expect to happen?

Instead of Internal Server Error, a clear validation message that provided value is too long
e.g.:
```
    "addressUpdate": {
      "errors": [
        {
          "code": "INVALID",
          "field": "firstName",
          "addressType": null,
          "message": "Ensure this value has at most 256 characters (it has 447).",
          "__typename": "AccountError"
        },
```

### Logs

_No response_

### Environment

Saleor version: `core v3.13.0-a`
OS and version: …

Bug: "streetAddress1", "streetAddress2" field validation 
### What are you trying to achieve?

I want to standardize the validation messages


### Steps to reproduce the problem

Update address with too long streetAddress1 or streetAddress2 value: 

```
{
  "id": "VXNlcjoxMzY5",
  "input": {
    "city": "test",
    "cityArea": "test",
    "companyName": "test",
    "country": "US",
    "countryArea": "IN",
    "firstName": "test",
    "lastName": "test",
    "phone": "",
    "postalCode": "46802",
    "streetAddress1": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum",
    "streetAddress2": ""
  }
}
```

Result: 
```
{
  "errors": [
    {
      "message": "Internal Server Error",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "addressUpdate"
      ],
      "extensions": {
        "exception": {
          "code": "KeyError"
        }
      }
    }
  ],
  "data": {
    "addressUpdate": null
  }
}
``` 

### What did you expect to happen?

Instead of Internal Server Error, a clear validation message that provided value is too long
e.g.:
```
    "addressUpdate": {
      "errors": [
        {
          "code": "INVALID",
          "field": "firstName",
          "addressType": null,
          "message": "Ensure this value has at most 256 characters (it has 447).",
          "__typename": "AccountError"
        },
```

### Logs

_No response_

### Environment

Saleor version: `core v3.13.0-a`
OS and version: …



Discussion:
Hi can I work on this bug?
@FremahA Sure!
Wish to help with the bug
I'll be glad to help!
Hi can I work on this bug?
@FremahA Sure!
Wish to help with the bug
I'll be glad to help!

Resolving patch:
diff --git a/saleor/account/i18n.py b/saleor/account/i18n.py
--- a/saleor/account/i18n.py
+++ b/saleor/account/i18n.py
@@ -173,10 +173,12 @@ def add_field_errors(self, errors):
     def validate_address(self, data):
         try:
             data["country_code"] = data.get("country", "")
-            if data["street_address_1"] or data["street_address_2"]:
-                data[
-                    "street_address"
-                ] = f'{data["street_address_1"]}\n{data["street_address_2"]}'
+
+            street_address_1 = data.get("street_address_1", "")
+            street_address_2 = data.get("street_address_2", "")
+            if street_address_1 or street_address_2:
+                data["street_address"] = f"{street_address_1}\n{street_address_2}"
+
             normalized_data = i18naddress.normalize_address(data)
             if getattr(self, "enable_normalization", True):
                 data = normalized_data


Test patch:
diff --git a/saleor/account/tests/test_account.py b/saleor/account/tests/test_account.py
--- a/saleor/account/tests/test_account.py
+++ b/saleor/account/tests/test_account.py
@@ -14,6 +14,7 @@
 
 @pytest.mark.parametrize("country", ["CN", "PL", "US", "IE"])
 def test_address_form_for_country(country):
+    # given
     data = {
         "first_name": "John",
         "last_name": "Doe",
@@ -21,10 +22,13 @@ def test_address_form_for_country(country):
         "phone": "123456789",
     }
 
+    # when
     form = forms.get_address_form(data, country_code=country)
     errors = form.errors
     rules = i18naddress.get_validation_rules({"country_code": country})
     required = rules.required_fields
+
+    # then
     if "street_address" in required:
         assert "street_address_1" in errors
     else:
@@ -48,17 +52,50 @@ def test_address_form_for_country(country):
 
 
 def test_address_form_postal_code_validation():
+    # given
     data = {
         "first_name": "John",
         "last_name": "Doe",
         "country": "PL",
         "postal_code": "XXX",
     }
+
+    # when
     form = forms.get_address_form(data, country_code="PL")
     errors = form.errors
+    # then
     assert "postal_code" in errors
 
 
+def test_address_form_long_street_address_validation():
+    # given
+    data = {
+        "city": "test",
+        "city_area": "test",
+        "company_name": "test",
+        "first_name": "Amelia",
+        "last_name": "Doe",
+        "country": "US",
+        "country_area": "IN",
+        "phone": "",
+        "postal_code": "46802",
+        "street_address_1": (
+            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut "
+            "labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris "
+            "nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit "
+            "esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt "
+            "in culpa qui officia deserunt mollit anim id est laborum"
+        ),
+    }
+
+    # when
+    form = forms.get_address_form(data, country_code="US")
+    errors = form.errors
+
+    # then
+    assert "street_address_1" in errors
+
+
 @pytest.mark.parametrize(
     ("country", "phone", "is_valid"),
     [
@@ -183,11 +220,6 @@ def test_copy_address(address):
     assert copied_address == address
 
 
-def test_compare_addresses(address):
-    copied_address = address.get_copy()
-    assert address == copied_address
-
-
 def test_compare_addresses_with_country_object(address):
     copied_address = address.get_copy()
     copied_address.country = Country("PL")