Skip to content

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

Move Error code and field outside validate storefront url

fowczarekda31ff57f82c
diff --git a/saleor/core/utils/url.py b/saleor/core/utils/url.py
index 85ec5b4710..8a8ab526fb 100644
--- a/saleor/core/utils/url.py
+++ b/saleor/core/utils/url.py
@@ -4,8 +4,6 @@ from django.conf import settings
from django.core.exceptions import ValidationError
from django.http.request import split_domain_port, validate_host
-from ...account.error_codes import AccountErrorCode
-
def validate_storefront_url(url):
"""Validate the storefront URL.
@@ -16,18 +14,15 @@ def validate_storefront_url(url):
try:
parsed_url = urlparse(url)
domain, _ = split_domain_port(parsed_url.netloc)
- # TODO: Check if parsed_url.netloc is empty
+ if not parsed_url.netloc:
+ raise ValidationError(
+ "Invalid URL. Please check if URL is in RFC 1808 format."
+ )
except ValueError as error:
- # TODO: Move Error code and field outside function
- raise ValidationError(
- {"redirectUrl": str(error)}, code=AccountErrorCode.INVALID
- )
+ raise ValidationError(error)
if not validate_host(domain, settings.ALLOWED_CLIENT_HOSTS):
error_message = (
f"{domain or url} is not allowed. Please check "
"`ALLOWED_CLIENT_HOSTS` configuration."
)
- # TODO: Move Error code and field outside function
- raise ValidationError(
- {"redirectUrl": error_message}, code=AccountErrorCode.INVALID
- )
+ raise ValidationError(error_message)
diff --git a/saleor/graphql/account/mutations/account.py b/saleor/graphql/account/mutations/account.py
index bdcbeb01d9..ffcfbab71b 100644
--- a/saleor/graphql/account/mutations/account.py
+++ b/saleor/graphql/account/mutations/account.py
@@ -106,7 +106,12 @@ class AccountRequestDeletion(BaseMutation):
def perform_mutation(cls, root, info, **data):
user = info.context.user
redirect_url = data["redirect_url"]
- validate_storefront_url(redirect_url)
+ try:
+ validate_storefront_url(redirect_url)
+ except ValidationError as error:
+ raise ValidationError(
+ {"redirect_url": error}, code=AccountErrorCode.INVALID
+ )
emails.send_account_delete_confirmation_email_with_url(redirect_url, user)
return AccountRequestDeletion()
diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.py
index 55d819c394..2c8ea1d2b1 100644
--- a/saleor/graphql/account/mutations/base.py
+++ b/saleor/graphql/account/mutations/base.py
@@ -128,7 +128,12 @@ class RequestPasswordReset(BaseMutation):
def perform_mutation(cls, _root, info, **data):
email = data["email"]
redirect_url = data["redirect_url"]
- validate_storefront_url(redirect_url)
+ try:
+ validate_storefront_url(redirect_url)
+ except ValidationError as error:
+ raise ValidationError(
+ {"redirect_url": error}, code=AccountErrorCode.INVALID
+ )
try:
user = models.User.objects.get(email=email)
@@ -351,7 +356,12 @@ class BaseCustomerCreate(ModelMutation, I18nMixin):
}
)
if cleaned_input.get("redirect_url"):
- validate_storefront_url(cleaned_input.get("redirect_url"))
+ try:
+ validate_storefront_url(cleaned_input.get("redirect_url"))
+ except ValidationError as error:
+ raise ValidationError(
+ {"redirect_url": error}, code=AccountErrorCode.INVALID
+ )
return cleaned_input
diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.py
index 738d71c154..c017026d3b 100644
--- a/saleor/graphql/account/mutations/staff.py
+++ b/saleor/graphql/account/mutations/staff.py
@@ -186,7 +186,12 @@ class StaffCreate(ModelMutation):
}
)
if cleaned_input.get("redirect_url"):
- validate_storefront_url(cleaned_input.get("redirect_url"))
+ try:
+ validate_storefront_url(cleaned_input.get("redirect_url"))
+ except ValidationError as error:
+ raise ValidationError(
+ {"redirect_url": error}, code=AccountErrorCode.INVALID
+ )
# set is_staff to True to create a staff user
cleaned_input["is_staff"] = True
diff --git a/saleor/graphql/shop/mutations.py b/saleor/graphql/shop/mutations.py
index 4123248239..e3fdcf5929 100644
--- a/saleor/graphql/shop/mutations.py
+++ b/saleor/graphql/shop/mutations.py
@@ -69,7 +69,12 @@ class ShopSettingsUpdate(BaseMutation):
@classmethod
def clean_input(cls, _info, _instance, data):
if data.get("customer_set_password_url"):
- validate_storefront_url(data["customer_set_password_url"])
+ try:
+ validate_storefront_url(data["customer_set_password_url"])
+ except ValidationError as error:
+ raise ValidationError(
+ {"customer_set_password_url": error}, code=ShopErrorCode.INVALID
+ )
return data
@classmethod
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index c0d334ec08..0d0dea254b 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -646,9 +646,8 @@ CUSTOMER_CREATE_MUTATION = """
field
message
}
- accountErrors{
+ accountErrors {
field
- message
code
}
user {
@@ -776,7 +775,6 @@ def test_customer_create_without_redirect_url_deprecated_send_mail_flag(
assert data["accountErrors"][0] == {
"field": "redirectUrl",
"code": AccountErrorCode.REQUIRED.name,
- "message": ANY,
}
staff_user = User.objects.filter(email=email)
assert not staff_user
@@ -793,7 +791,6 @@ def test_customer_create_with_invalid_url(staff_api_client, permission_manage_us
assert data["accountErrors"][0] == {
"field": "redirectUrl",
"code": AccountErrorCode.INVALID.name,
- "message": ANY,
}
staff_user = User.objects.filter(email=email)
assert not staff_user
@@ -812,7 +809,6 @@ def test_customer_create_with_not_allowed_url(
assert data["accountErrors"][0] == {
"field": "redirectUrl",
"code": AccountErrorCode.INVALID.name,
- "message": ANY,
}
staff_user = User.objects.filter(email=email)
assert not staff_user
@@ -1118,6 +1114,10 @@ ACCOUNT_REQUEST_DELETION_MUTATION = """
field
message
}
+ accountErrors {
+ code
+ field
+ }
}
}
"""
@@ -1181,7 +1181,10 @@ def test_account_request_deletion_storefront_hosts_not_allowed(
content = get_graphql_content(response)
data = content["data"]["accountRequestDeletion"]
assert len(data["errors"]) == 1
- assert data["errors"][0]["field"] == "redirectUrl"
+ assert data["accountErrors"][0] == {
+ "field": "redirectUrl",
+ "code": AccountErrorCode.INVALID.name,
+ }
send_account_delete_confirmation_email_with_url_mock.assert_not_called()
@@ -1366,7 +1369,6 @@ STAFF_CREATE_MUTATION = """
}
accountErrors {
field
- message
code
}
user {
@@ -1475,7 +1477,6 @@ def test_staff_create_without_redirect_url_deprecated_send_mail_flag(
assert data["accountErrors"][0] == {
"field": "redirectUrl",
"code": AccountErrorCode.REQUIRED.name,
- "message": ANY,
}
staff_user = User.objects.filter(email=email)
assert not staff_user
@@ -1494,7 +1495,6 @@ def test_staff_create_with_invalid_url(
assert data["accountErrors"][0] == {
"field": "redirectUrl",
"code": AccountErrorCode.INVALID.name,
- "message": ANY,
}
staff_user = User.objects.filter(email=email)
assert not staff_user
@@ -1513,7 +1513,6 @@ def test_staff_create_with_not_allowed_url(
assert data["accountErrors"][0] == {
"field": "redirectUrl",
"code": AccountErrorCode.INVALID.name,
- "message": ANY,
}
staff_user = User.objects.filter(email=email)
assert not staff_user
@@ -1569,7 +1568,6 @@ def test_staff_update_doesnt_change_existing_avatar(
}
accountErrors {
field
- message
code
}
}
diff --git a/tests/api/test_shop.py b/tests/api/test_shop.py
index 2797cde209..ebb2d25e0a 100644
--- a/tests/api/test_shop.py
+++ b/tests/api/test_shop.py
@@ -4,6 +4,7 @@ import graphene
import pytest
from django_countries import countries
+from saleor.core.error_codes import ShopErrorCode
from saleor.core.permissions import MODELS_PERMISSIONS
from saleor.graphql.core.utils import str_to_enum
from saleor.site import AuthenticationBackends
@@ -572,8 +573,11 @@ def test_shop_customer_set_password_url_update_invalid_url(
)
content = get_graphql_content(response)
data = content["data"]["shopSettingsUpdate"]
- # TODO Add field and Code After fix errors in valitaion
- assert data["shopErrors"][0] == {"field": ANY, "code": ANY, "message": ANY}
+ assert data["shopErrors"][0] == {
+ "field": "customerSetPasswordUrl",
+ "code": ShopErrorCode.INVALID.name,
+ "message": ANY,
+ }
site_settings.refresh_from_db()
assert not site_settings.customer_set_password_url