Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix revies remarks
fowczarek5ccd99ac6b77
diff --git a/saleor/account/emails.py b/saleor/account/emails.pyindex ba4d378995..b9afea2c23 100644--- a/saleor/account/emails.py+++ b/saleor/account/emails.py@@ -1,4 +1,4 @@-from urllib.parse import urlencode+from urllib.parse import urlencode, urlsplitfrom django.conf import settingsfrom django.contrib.auth.tokens import default_token_generator@@ -36,11 +36,9 @@ def _send_user_password_reset_email(recipient_email, context, user_id):@app.taskdef _send_password_reset_email_with_url(recipient_email, redirect_url, user_id, token):params = urlencode({"email": recipient_email, "token": token})- reset_url = "%(redirect_url)s?%(params)s" % {- "redirect_url": redirect_url,- "params": params,- }- _send_password_reset_email(recipient_email, reset_url, user_id)+ reset_url = urlsplit(redirect_url)+ reset_url = reset_url._replace(query=params)+ _send_password_reset_email(recipient_email, reset_url.geturl(), user_id)def _send_password_reset_email(recipient_email, reset_url, user_id):diff --git a/saleor/core/utils/url.py b/saleor/core/utils/url.pyindex 93f1c5df74..7913c92306 100644--- a/saleor/core/utils/url.py+++ b/saleor/core/utils/url.py@@ -9,12 +9,16 @@ def validate_storefront_url(url):"""Validate the storefront URL.Raise ValidationError if URL isn't in RFC 1808 format- or it isn't allowed by ALLOWED_STOREFRONT_HOSTS in settings.+ or it isn't allowed by ALLOWED_CLIENT_HOSTS in settings."""try:parsed_url = urlparse(url)domain, _ = split_domain_port(parsed_url.netloc)except ValueError as error:raise ValidationError({"redirectUrl": str(error)})- if not validate_host(domain, settings.ALLOWED_STOREFRONT_HOSTS):- raise ValidationError({"redirectUrl": f"{domain} this is not allowed address."})+ if not validate_host(domain, settings.ALLOWED_CLIENT_HOSTS):+ error_message = (+ f"{domain or url} is not allowed. Please check "+ "`ALLOWED_CLIENT_HOSTS` configuration."+ )+ raise ValidationError({"redirectUrl": error_message})diff --git a/saleor/dashboard/customer/views.py b/saleor/dashboard/customer/views.pyindex 6d54fdb798..7b74f17d02 100644--- a/saleor/dashboard/customer/views.py+++ b/saleor/dashboard/customer/views.py@@ -11,7 +11,7 @@ from django.utils.translation import pgettext_lazyfrom ...account import events as account_eventsfrom ...account.models import CustomerNote, Userfrom ...core.utils import get_paginator_items-from ..emails import send_set_password_customer_email+from ..emails import send_set_password_emailfrom ..views import staff_member_requiredfrom .filters import UserFilterfrom .forms import CustomerDeleteForm, CustomerForm, CustomerNoteForm@@ -62,7 +62,7 @@ def customer_create(request):if form.is_valid():form.save()msg = pgettext_lazy("Dashboard message", "Added customer %s") % customer- send_set_password_customer_email(customer)+ send_set_password_email(customer)messages.success(request, msg)return redirect("dashboard:customer-details", pk=customer.pk)ctx = {"form": form, "customer": customer}diff --git a/saleor/dashboard/emails.py b/saleor/dashboard/emails.pyindex 86288e57e5..bb6bfb726a 100644--- a/saleor/dashboard/emails.py+++ b/saleor/dashboard/emails.py@@ -1,4 +1,4 @@-from urllib.parse import urlencode+from urllib.parse import urlencode, urlsplitfrom django.conf import settingsfrom django.contrib.auth.tokens import default_token_generator@@ -13,36 +13,22 @@ from ..core.emails import get_email_base_contextfrom ..core.utils import build_absolute_uri-def send_set_password_staff_email_with_url(redirect_url, staff):- """Trigger sending a set password email for the given staff."""- token = default_token_generator.make_token(staff)+def send_set_password_email_with_url(redirect_url, user, staff=False):+ """Trigger sending a set password email for the given customer/staff."""+ template_type = "staff" if staff else "customer"+ template = f"dashboard/{template_type}/set_password"+ token = default_token_generator.make_token(user)_send_set_user_password_email_with_url.delay(- staff.email, redirect_url, token, "dashboard/staff/set_password"+ user.email, redirect_url, token, template)-def send_set_password_customer_email_with_url(redirect_url, customer):- """Trigger sending a set password email for the given customer."""- token = default_token_generator.make_token(customer)- _send_set_user_password_email_with_url.delay(- customer.email, redirect_url, token, "dashboard/customer/set_password"- )---def send_set_password_staff_email(staff):- """Trigger sending a set password email for the given staff."""- token = default_token_generator.make_token(staff)- _send_set_user_password_email.delay(- staff.email, staff.pk, token, "dashboard/staff/set_password"- )---def send_set_password_customer_email(customer):- """Trigger sending a set password email for the given customer."""- token = default_token_generator.make_token(customer)- _send_set_user_password_email.delay(- customer.email, customer.pk, token, "dashboard/customer/set_password"- )+def send_set_password_email(user, staff=False):+ """Trigger sending a set password email for the given customer/staff."""+ template_type = "staff" if staff else "customer"+ template = f"dashboard/{template_type}/set_password"+ token = default_token_generator.make_token(user)+ _send_set_user_password_email.delay(user.email, user.pk, token, template)@app.task@@ -50,8 +36,9 @@ def _send_set_user_password_email_with_url(recipient_email, redirect_url, token, template_name):params = urlencode({"email": recipient_email, "token": token})- password_set_url = f"{redirect_url}?%{params}"- _send_set_password_email(recipient_email, password_set_url, template_name)+ password_set_url = urlsplit(redirect_url)+ password_set_url = password_set_url._replace(query=params)+ _send_set_password_email(recipient_email, password_set_url.geturl(), template_name)@app.taskdiff --git a/saleor/dashboard/staff/views.py b/saleor/dashboard/staff/views.pyindex 25ff30e84f..8449b88258 100644--- a/saleor/dashboard/staff/views.py+++ b/saleor/dashboard/staff/views.py@@ -7,7 +7,7 @@ from django.utils.translation import pgettext_lazyfrom ...account.models import Userfrom ...core.utils import get_paginator_items-from ..emails import send_promote_customer_to_staff_email, send_set_password_staff_email+from ..emails import send_promote_customer_to_staff_email, send_set_password_emailfrom ..views import staff_member_requiredfrom .filters import StaffFilterfrom .forms import StaffForm@@ -71,7 +71,7 @@ def staff_create(request):msg = pgettext_lazy("Dashboard message", "Added staff member %s") % (staff,)messages.success(request, msg)if created:- send_set_password_staff_email(staff)+ send_set_password_email(user=staff, staff=True)else:send_promote_customer_to_staff_email.delay(staff.pk)return redirect("dashboard:staff-list")diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.pyindex 8e62b3cb60..d0a9c7d8c1 100644--- a/saleor/graphql/account/mutations/base.py+++ b/saleor/graphql/account/mutations/base.py@@ -8,7 +8,7 @@ from graphql_jwt.exceptions import PermissionDeniedfrom ....account import events as account_events, modelsfrom ....account.emails import send_user_password_reset_email_with_urlfrom ....core.utils.url import validate_storefront_url-from ....dashboard.emails import send_set_password_customer_email_with_url+from ....dashboard.emails import send_set_password_email_with_urlfrom ...account.i18n import I18nMixinfrom ...account.types import Address, AddressInput, Userfrom ...core.mutations import (@@ -327,7 +327,7 @@ class BaseCustomerCreate(ModelMutation, I18nMixin):account_events.customer_account_created_event(user=instance)if cleaned_input.get("send_password_email"):- send_set_password_customer_email_with_url(+ send_set_password_email_with_url(cleaned_input.get("redirect_url"), instance)diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.pyindex 8ace56714e..f71628db5d 100644--- a/saleor/graphql/account/mutations/staff.py+++ b/saleor/graphql/account/mutations/staff.py@@ -11,7 +11,7 @@ from ....account.utils import get_random_avatarfrom ....checkout import AddressTypefrom ....core.permissions import get_permissionsfrom ....core.utils.url import validate_storefront_url-from ....dashboard.emails import send_set_password_staff_email_with_url+from ....dashboard.emails import send_set_password_email_with_urlfrom ....dashboard.staff.utils import remove_staff_memberfrom ...account.enums import AddressTypeEnumfrom ...account.types import Address, AddressInput, User@@ -187,8 +187,8 @@ class StaffCreate(ModelMutation):if create_avatar:create_user_avatar_thumbnails.delay(user_id=user.pk)if cleaned_input.get("send_password_email"):- send_set_password_staff_email_with_url(- cleaned_input.get("redirect_url"), user+ send_set_password_email_with_url(+ redirect_url=cleaned_input.get("redirect_url"), user=user, staff=True)diff --git a/saleor/settings.py b/saleor/settings.pyindex b2477287b9..b9144205ab 100644--- a/saleor/settings.py+++ b/saleor/settings.py@@ -41,8 +41,8 @@ ADMINS = ()MANAGERS = ADMINS-ALLOWED_STOREFRONT_HOSTS = get_list(- os.environ.get("ALLOWED_STOREFRONT_HOSTS", "localhost,127.0.0.1")+ALLOWED_CLIENT_HOSTS = get_list(+ os.environ.get("ALLOWED_CLIENT_HOSTS", "localhost,127.0.0.1"))INTERNAL_IPS = get_list(os.environ.get("INTERNAL_IPS", "127.0.0.1"))diff --git a/tests/api/test_account.py b/tests/api/test_account.pyindex e21f63d781..a15dc4df10 100644--- a/tests/api/test_account.py+++ b/tests/api/test_account.py@@ -5,7 +5,6 @@ from unittest.mock import ANY, MagicMock, Mock, patchimport grapheneimport pytest-from django.contrib.auth import get_user_modelfrom django.contrib.auth.tokens import default_token_generatorfrom django.core.exceptions import ValidationErrorfrom django.core.files import File@@ -641,7 +640,6 @@ def test_customer_create()content = get_graphql_content(response)- User = get_user_model()new_customer = User.objects.get(email=email)shipping_address, billing_address = (@@ -690,7 +688,6 @@ def test_customer_create_send_password_with_url(data = content["data"]["customerCreate"]assert not data["errors"]- User = get_user_model()new_customer = User.objects.get(email=email)assert new_customer@@ -711,8 +708,7 @@ def test_customer_create_without_send_password(content = get_graphql_content(response)data = content["data"]["customerCreate"]assert not data["errors"]- staff_user = User.objects.get(email=email)- assert staff_user+ User.objects.get(email=email)def test_customer_create_without_redirect_url(@@ -730,7 +726,7 @@ def test_customer_create_without_redirect_url(assert not staff_user-def test_customer_create_without_invalid_url(staff_api_client, permission_manage_users):+def test_customer_create_with_invalid_url(staff_api_client, permission_manage_users):email = "[email protected]"variables = {"email": email, "send_mail": True, "redirect_url": "invalid"}response = staff_api_client.post_graphql(@@ -743,7 +739,7 @@ def test_customer_create_without_invalid_url(staff_api_client, permission_manageassert not staff_user-def test_customer_create_without_url_not_allowed(+def test_customer_create_with_not_allowed_url(staff_api_client, permission_manage_users):email = "[email protected]"@@ -829,7 +825,6 @@ def test_customer_update()content = get_graphql_content(response)- User = get_user_model()customer = User.objects.get(email=customer_user.email)# check that existing instances are updated@@ -1136,7 +1131,7 @@ def test_account_request_deletion_all_storefront_hosts_allowed():user = user_api_client.usertoken = default_token_generator.make_token(user)- settings.ALLOWED_STOREFRONT_HOSTS = ["*"]+ settings.ALLOWED_CLIENT_HOSTS = ["*"]variables = {"redirectUrl": "https://www.test.com"}response = user_api_client.post_graphql(ACCOUNT_REQUEST_DELETION_MUTATION, variables@@ -1158,7 +1153,7 @@ def test_account_request_deletion_subdomain():user = user_api_client.usertoken = default_token_generator.make_token(user)- settings.ALLOWED_STOREFRONT_HOSTS = [".example.com"]+ settings.ALLOWED_CLIENT_HOSTS = [".example.com"]variables = {"redirectUrl": "https://sub.example.com"}response = user_api_client.post_graphql(ACCOUNT_REQUEST_DELETION_MUTATION, variables@@ -1354,7 +1349,6 @@ def test_staff_create(permissions = data["user"]["permissions"]assert permissions[0]["code"] == "MANAGE_PRODUCTS"- User = get_user_model()staff_user = User.objects.get(email=email)assert staff_user.is_staff@@ -1385,7 +1379,6 @@ def test_staff_create_send_password_with_url(data = content["data"]["staffCreate"]assert not data["errors"]- User = get_user_model()staff_user = User.objects.get(email=email)assert staff_user.is_staff@@ -1406,8 +1399,7 @@ def test_staff_create_without_send_password(content = get_graphql_content(response)data = content["data"]["staffCreate"]assert not data["errors"]- staff_user = User.objects.get(email=email)- assert staff_user+ User.objects.get(email=email)def test_staff_create_without_redirect_url(@@ -1425,7 +1417,7 @@ def test_staff_create_without_redirect_url(assert not staff_user-def test_staff_create_without_invalid_url(+def test_staff_create_with_invalid_url(staff_api_client, media_root, permission_manage_staff):email = "[email protected]"@@ -1440,7 +1432,7 @@ def test_staff_create_without_invalid_url(assert not staff_user-def test_staff_create_without_url_not_allowed(+def test_staff_create_with_not_allowed_url(staff_api_client, media_root, permission_manage_staff):email = "[email protected]"@@ -2259,6 +2251,7 @@ def test_account_reset_password_storefront_hosts_not_allowed(content = get_graphql_content(response)data = content["data"]["requestPasswordReset"]assert len(data["errors"]) == 1+ assert data["errors"][0]["field"] == "redirectUrl"assert not send_password_reset_email_mock.called@@ -2266,7 +2259,7 @@ def test_account_reset_password_storefront_hosts_not_allowed(def test_account_reset_password_all_storefront_hosts_allowed(send_password_reset_email_mock, user_api_client, customer_user, settings):- settings.ALLOWED_STOREFRONT_HOSTS = ["*"]+ settings.ALLOWED_CLIENT_HOSTS = ["*"]variables = {"email": customer_user.email, "redirectUrl": "https://www.test.com"}response = user_api_client.post_graphql(REQUEST_PASSWORD_RESET_MUTATION, variables)content = get_graphql_content(response)@@ -2285,7 +2278,7 @@ def test_account_reset_password_all_storefront_hosts_allowed(def test_account_reset_password_subdomain(send_password_reset_email_mock, user_api_client, customer_user, settings):- settings.ALLOWED_STOREFRONT_HOSTS = [".example.com"]+ settings.ALLOWED_CLIENT_HOSTS = [".example.com"]variables = {"email": customer_user.email, "redirectUrl": "https://sub.example.com"}response = user_api_client.post_graphql(REQUEST_PASSWORD_RESET_MUTATION, variables)content = get_graphql_content(response)diff --git a/tests/settings.py b/tests/settings.pyindex 012e5d2705..7e7ff43371 100644--- a/tests/settings.py+++ b/tests/settings.py@@ -6,7 +6,7 @@ CELERY_TASK_ALWAYS_EAGER = TrueSECRET_KEY = "NOTREALLY"-ALLOWED_STOREFRONT_HOSTS = ["www.example.com"]+ALLOWED_CLIENT_HOSTS = ["www.example.com"]DEFAULT_CURRENCY = "USD"