Skip to content

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

Add customer set password url to site settings

fowczarek43b51c59c3df
diff --git a/saleor/core/utils/url.py b/saleor/core/utils/url.py
index a4ef24d873..85ec5b4710 100644
--- a/saleor/core/utils/url.py
+++ b/saleor/core/utils/url.py
@@ -16,7 +16,9 @@ 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
except ValueError as error:
+ # TODO: Move Error code and field outside function
raise ValidationError(
{"redirectUrl": str(error)}, code=AccountErrorCode.INVALID
)
@@ -25,6 +27,7 @@ def validate_storefront_url(url):
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
)
diff --git a/saleor/graphql/shop/mutations.py b/saleor/graphql/shop/mutations.py
index 81dff20673..4123248239 100644
--- a/saleor/graphql/shop/mutations.py
+++ b/saleor/graphql/shop/mutations.py
@@ -5,6 +5,7 @@ from django.core.management import call_command
from ...account.models import Address
from ...core.error_codes import ShopErrorCode
+from ...core.utils.url import validate_storefront_url
from ...site import models as site_models
from ..account.i18n import I18nMixin
from ..account.types import AddressInput
@@ -42,6 +43,8 @@ class ShopSettingsInput(graphene.InputObjectType):
default_mail_sender_address = graphene.String(
description="Default email sender's address."
)
+ # TODO: Add description
+ customer_set_password_url = graphene.String(description="XXXXX")
class SiteDomainInput(graphene.InputObjectType):
@@ -64,13 +67,25 @@ class ShopSettingsUpdate(BaseMutation):
error_type_field = "shop_errors"
@classmethod
- def perform_mutation(cls, _root, info, **data):
- instance = info.context.site.settings
- data = data.get("input")
- for field_name, desired_value in data.items():
+ def clean_input(cls, _info, _instance, data):
+ if data.get("customer_set_password_url"):
+ validate_storefront_url(data["customer_set_password_url"])
+ return data
+
+ @classmethod
+ def construct_instance(cls, instance, cleaned_data):
+ for field_name, desired_value in cleaned_data.items():
current_value = getattr(instance, field_name)
if current_value != desired_value:
setattr(instance, field_name, desired_value)
+ return instance
+
+ @classmethod
+ def perform_mutation(cls, _root, info, **data):
+ instance = info.context.site.settings
+ data = data.get("input")
+ cleaned_input = cls.clean_input(info, instance, data)
+ instance = cls.construct_instance(instance, cleaned_input)
cls.clean_instance(instance)
instance.save()
return ShopSettingsUpdate(shop=Shop())
diff --git a/saleor/graphql/shop/types.py b/saleor/graphql/shop/types.py
index b43ab1c50c..16c82e0bc0 100644
--- a/saleor/graphql/shop/types.py
+++ b/saleor/graphql/shop/types.py
@@ -149,6 +149,8 @@ class Shop(graphene.ObjectType):
company_address = graphene.Field(
Address, description="Company address.", required=False
)
+ # TODO: Add description
+ customer_set_password_url = graphene.String(description="XXXXX", required=False)
class Meta:
description = (
@@ -289,6 +291,10 @@ class Shop(graphene.ObjectType):
def resolve_company_address(_, info):
return info.context.site.settings.company_address
+ @staticmethod
+ def resolve_customer_set_password_url(_, info):
+ return info.context.site.settings.customer_set_password_url
+
@staticmethod
def resolve_translation(_, info, language_code):
return resolve_translation(info.context.site.settings, info, language_code)
diff --git a/saleor/site/models.py b/saleor/site/models.py
index 145fdf0157..4eaba2be38 100644
--- a/saleor/site/models.py
+++ b/saleor/site/models.py
@@ -61,7 +61,6 @@ class SiteSettings(models.Model):
company_address = models.ForeignKey(
"account.Address", blank=True, null=True, on_delete=models.CASCADE
)
-
default_mail_sender_name = models.CharField(
max_length=settings.DEFAULT_MAX_EMAIL_DISPLAY_NAME_LENGTH,
blank=True,
@@ -69,7 +68,7 @@ class SiteSettings(models.Model):
validators=EMAIL_SENDER_NAME_VALIDATORS,
)
default_mail_sender_address = models.EmailField(blank=True, null=True)
-
+ customer_set_password_url = models.CharField(max_length=255, blank=True, null=True)
translated = TranslationProxy()
class Meta:
diff --git a/tests/api/test_shop.py b/tests/api/test_shop.py
index 43d42948e2..2797cde209 100644
--- a/tests/api/test_shop.py
+++ b/tests/api/test_shop.py
@@ -1,3 +1,5 @@
+from unittest.mock import ANY
+
import graphene
import pytest
from django_countries import countries
@@ -513,6 +515,69 @@ def test_shop_domain_update(staff_api_client, permission_manage_settings):
assert site.name == new_name
+MUTATION_CUSTOMER_SET_PASSWORD_URL_UPDATE = """
+ mutation updateSettings($customerSetPasswordUrl: String!) {
+ shopSettingsUpdate(input: {customerSetPasswordUrl: $customerSetPasswordUrl}){
+ shop {
+ customerSetPasswordUrl
+ }
+ shopErrors {
+ message
+ field
+ code
+ }
+ }
+ }
+"""
+
+
+def test_shop_customer_set_password_url_update(
+ staff_api_client, site_settings, permission_manage_settings
+):
+ customer_set_password_url = "http://www.example.com/set_pass/"
+ variables = {"customerSetPasswordUrl": customer_set_password_url}
+ assert site_settings.customer_set_password_url != customer_set_password_url
+ response = staff_api_client.post_graphql(
+ MUTATION_CUSTOMER_SET_PASSWORD_URL_UPDATE,
+ variables,
+ permissions=[permission_manage_settings],
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["shopSettingsUpdate"]
+ assert not data["shopErrors"]
+ site_settings = Site.objects.get_current().settings
+ assert site_settings.customer_set_password_url == customer_set_password_url
+
+
+ "customer_set_password_url",
+ [
+ ("http://not-allowed-storefron.com/pass"),
+ ("http://[value-error-in-urlparse@test/pass"),
+ ("without-protocole.com/pass"),
+ ],
+)
+def test_shop_customer_set_password_url_update_invalid_url(
+ staff_api_client,
+ site_settings,
+ permission_manage_settings,
+ customer_set_password_url,
+):
+ variables = {"customerSetPasswordUrl": customer_set_password_url}
+ assert not site_settings.customer_set_password_url
+ response = staff_api_client.post_graphql(
+ MUTATION_CUSTOMER_SET_PASSWORD_URL_UPDATE,
+ variables,
+ permissions=[permission_manage_settings],
+ )
+ 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}
+ site_settings.refresh_from_db()
+ assert not site_settings.customer_set_password_url
+
+
def test_homepage_collection_update(
staff_api_client, collection, permission_manage_settings
):