Skip to content

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

Change addressValidator signature

Aleksander Spyraedf6ae1059ed
diff --git a/saleor/graphql/account/resolvers.py b/saleor/graphql/account/resolvers.py
index 5923f876b3..795e3ddd3a 100644
--- a/saleor/graphql/account/resolvers.py
+++ b/saleor/graphql/account/resolvers.py
@@ -1,11 +1,11 @@
from itertools import chain
+from typing import Optional
import graphene_django_optimizer as gql_optimizer
from django.db.models import Q
from i18naddress import get_validation_rules
from ...account import models
-from ...core.utils import get_client_ip, get_country_by_ip
from ...payment.utils import (
fetch_customer_id,
list_enabled_gateways,
@@ -47,17 +47,17 @@ def resolve_staff_users(info, query):
return gql_optimizer.query(qs, info)
-def resolve_address_validator(info, country_code, country_area, city_area):
- if not country_code:
- client_ip = get_client_ip(info.context)
- country = get_country_by_ip(client_ip)
- if country:
- country_code = country.code
- else:
- return None
+def resolve_address_validator(
+ info,
+ country_code: str,
+ country_area: Optional[str],
+ city: Optional[str],
+ city_area: Optional[str],
+):
params = {
"country_code": country_code,
"country_area": country_area,
+ "city": city,
"city_area": city_area,
}
rules = get_validation_rules(params)
diff --git a/saleor/graphql/account/schema.py b/saleor/graphql/account/schema.py
index 3a6e326b9d..dfd9c0747b 100644
--- a/saleor/graphql/account/schema.py
+++ b/saleor/graphql/account/schema.py
@@ -65,9 +65,10 @@ class StaffUserInput(FilterInputObjectType):
class AccountQueries(graphene.ObjectType):
address_validation_rules = graphene.Field(
AddressValidationData,
- country_code=graphene.Argument(CountryCodeEnum, required=False),
- country_area=graphene.String(required=False),
- city_area=graphene.String(required=False),
+ country_code=graphene.Argument(CountryCodeEnum, required=True),
+ country_area=graphene.Argument(graphene.String),
+ city=graphene.Argument(graphene.String),
+ city_area=graphene.Argument(graphene.String),
)
customers = FilterInputConnectionField(
User,
@@ -89,12 +90,13 @@ class AccountQueries(graphene.ObjectType):
)
def resolve_address_validation_rules(
- self, info, country_code=None, country_area=None, city_area=None
+ self, info, country_code, country_area=None, city=None, city_area=None
):
return resolve_address_validator(
info,
- country_code=country_code,
+ country_code,
country_area=country_area,
+ city=city,
city_area=city_area,
)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 0f56f398da..dbcef44bcf 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -2974,7 +2974,7 @@ type Query {
checkouts(before: String, after: String, first: Int, last: Int): CheckoutCountableConnection
checkoutLine(id: ID): CheckoutLine
checkoutLines(before: String, after: String, first: Int, last: Int): CheckoutLineCountableConnection
- addressValidationRules(countryCode: CountryCode, countryArea: String, cityArea: String): AddressValidationData
+ addressValidationRules(countryCode: CountryCode!, countryArea: String, city: String, cityArea: String): AddressValidationData
customers(filter: CustomerFilterInput, query: String, before: String, after: String, first: Int, last: Int): UserCountableConnection
me: User
staffUsers(filter: StaffUserInput, query: String, before: String, after: String, first: Int, last: Int): UserCountableConnection
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index 456c2f2399..32fbcf6087 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -1796,7 +1796,7 @@ def test_set_default_address(
def test_address_validator(user_api_client):
query = """
query getValidator(
- $country_code: CountryCode, $country_area: String, $city_area: String) {
+ $country_code: CountryCode!, $country_area: String, $city_area: String) {
addressValidationRules(
countryCode: $country_code,
countryArea: $country_area,
@@ -1822,42 +1822,10 @@ def test_address_validator(user_api_client):
assert matcher.match("00-123")
-def test_address_validator_uses_geip_when_country_code_missing(
- user_api_client, monkeypatch
-):
- query = """
- query getValidator(
- $country_code: CountryCode, $country_area: String, $city_area: String) {
- addressValidationRules(
- countryCode: $country_code,
- countryArea: $country_area,
- cityArea: $city_area) {
- countryCode,
- countryName
- }
- }
- """
- variables = {"country_code": None, "country_area": None, "city_area": None}
- mock_country_by_ip = Mock(return_value=Mock(code="US"))
- monkeypatch.setattr(
- "saleor.graphql.account.resolvers.get_client_ip",
- lambda request: Mock(return_value="127.0.0.1"),
- )
- monkeypatch.setattr(
- "saleor.graphql.account.resolvers.get_country_by_ip", mock_country_by_ip
- )
- response = user_api_client.post_graphql(query, variables)
- content = get_graphql_content(response)
- assert mock_country_by_ip.called
- data = content["data"]["addressValidationRules"]
- assert data["countryCode"] == "US"
- assert data["countryName"] == "UNITED STATES"
-
-
def test_address_validator_with_country_area(user_api_client):
query = """
query getValidator(
- $country_code: CountryCode, $country_area: String, $city_area: String) {
+ $country_code: CountryCode!, $country_area: String, $city_area: String) {
addressValidationRules(
countryCode: $country_code,
countryArea: $country_area,