Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add redirect url to customer create mutation
fowczarek727f6f6f93fa
diff --git a/saleor/dashboard/customer/views.py b/saleor/dashboard/customer/views.pyindex ee75364a9c..6d54fdb798 100644--- a/saleor/dashboard/customer/views.py+++ b/saleor/dashboard/customer/views.py@@ -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.delay(customer.pk)+ send_set_password_customer_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 be32840ad3..86288e57e5 100644--- a/saleor/dashboard/emails.py+++ b/saleor/dashboard/emails.py@@ -16,35 +16,53 @@ from ..core.utils import build_absolute_uridef 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)- _send_set_password_staff_email_with_url.delay(staff.email, redirect_url, token)+ _send_set_user_password_email_with_url.delay(+ staff.email, redirect_url, token, "dashboard/staff/set_password"+ )+++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_password_staff_email.delay(staff.email, staff.pk, token)+ _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"+ )@app.task-def _send_set_password_staff_email_with_url(recipient_email, redirect_url, token):+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, "dashboard/staff/set_password"- )+ _send_set_password_email(recipient_email, password_set_url, template_name)@app.task-def _send_set_password_staff_email(recipient_email, staff_pk, token):- uid = urlsafe_base64_encode(force_bytes(staff_pk))+def _send_set_user_password_email(recipient_email, user_pk, token, template_name):+ uid = urlsafe_base64_encode(force_bytes(user_pk))password_set_url = build_absolute_uri(reverse("account:reset-password-confirm", kwargs={"token": token, "uidb64": uid}))- _send_set_password_email(- recipient_email, password_set_url, "dashboard/staff/set_password"- )+ _send_set_password_email(recipient_email, password_set_url, template_name)def _send_set_password_email(recipient_email, password_set_url, template_name):@@ -58,30 +76,6 @@ def _send_set_password_email(recipient_email, password_set_url, template_name):)-def send_set_password_customer_email(pk):- _old_send_set_password_email(pk, "dashboard/customer/set_password")---def _old_send_set_password_email(pk, template_name):- user = User.objects.get(pk=pk)- uid = urlsafe_base64_encode(force_bytes(user.pk))- token = default_token_generator.make_token(user)- password_set_url = build_absolute_uri(- reverse(- "account:reset-password-confirm", kwargs={"token": token, "uidb64": uid}- )- )- ctx = get_email_base_context()- ctx["password_set_url"] = password_set_url- send_templated_mail(- template_name=template_name,- from_email=settings.DEFAULT_FROM_EMAIL,- recipient_list=[user.email],- context=ctx,- )--@app.taskdef send_promote_customer_to_staff_email(staff_pk):staff = User.objects.get(pk=staff_pk)diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.pyindex b51fd43bfd..8e62b3cb60 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+from ....dashboard.emails import send_set_password_customer_email_with_urlfrom ...account.i18n import I18nMixinfrom ...account.types import Address, AddressInput, Userfrom ...core.mutations import (@@ -259,6 +259,12 @@ class UserCreateInput(CustomerInput):send_password_email = graphene.Boolean(description="Send an email with a link to set a password")+ redirect_url = graphene.String(+ description=(+ "URL of a view where users should be redirected to "+ "set the password. URL in RFC 1808 format.",+ )+ )class BaseCustomerCreate(ModelMutation, I18nMixin):@@ -290,6 +296,14 @@ class BaseCustomerCreate(ModelMutation, I18nMixin):billing_address_data, instance=getattr(instance, BILLING_ADDRESS_FIELD))cleaned_input[BILLING_ADDRESS_FIELD] = billing_address++ if cleaned_input.get("send_password_email"):+ if not cleaned_input.get("redirect_url"):+ raise ValidationError(+ {"redirect_url": "Redirect url is required to send a password."}+ )+ validate_storefront_url(cleaned_input.get("redirect_url"))+return cleaned_input@classmethod@@ -313,7 +327,9 @@ class BaseCustomerCreate(ModelMutation, I18nMixin):account_events.customer_account_created_event(user=instance)if cleaned_input.get("send_password_email"):- send_set_password_customer_email.delay(instance.pk)+ send_set_password_customer_email_with_url(+ cleaned_input.get("redirect_url"), instance+ )class UserUpdateMeta(UpdateMetaBaseMutation):diff --git a/tests/api/test_account.py b/tests/api/test_account.pyindex f88b06790d..e21f63d781 100644--- a/tests/api/test_account.py+++ b/tests/api/test_account.py@@ -576,18 +576,11 @@ def test_customer_register(user_api_client, query, mutation_name):assert customer_creation_event.user == new_user-@patch("saleor.dashboard.emails.send_set_password_customer_email.delay")-def test_customer_create(- send_set_password_customer_email_mock,- staff_api_client,- address,- permission_manage_users,-):- query = """+CUSTOMER_CREATE_MUTATION = """mutation CreateCustomer($email: String, $firstName: String, $lastName: String,$note: String, $billing: AddressInput, $shipping: AddressInput,- $send_mail: Boolean) {+ $send_mail: Boolean, $redirect_url: String) {customerCreate(input: {email: $email,firstName: $firstName,@@ -596,6 +589,7 @@ def test_customer_create(defaultShippingAddress: $shipping,defaultBillingAddress: $billingsendPasswordEmail: $send_mail+ redirectUrl: $redirect_url}) {errors {field@@ -618,7 +612,13 @@ def test_customer_create(}}}- """+"""+++@patch("saleor.dashboard.emails._send_set_password_email")+def test_customer_create(+ _send_set_password_email_mock, staff_api_client, address, permission_manage_users+):email = "[email protected]"first_name = "api_first_name"last_name = "api_last_name"@@ -633,10 +633,11 @@ def test_customer_create("shipping": address_data,"billing": address_data,"send_mail": True,+ "redirect_url": "https://www.example.com",}response = staff_api_client.post_graphql(- query, variables, permissions=[permission_manage_users]+ CUSTOMER_CREATE_MUTATION, variables, permissions=[permission_manage_users])content = get_graphql_content(response)@@ -660,16 +661,107 @@ def test_customer_create(assert not data["user"]["isStaff"]assert data["user"]["isActive"]- assert send_set_password_customer_email_mock.call_count == 1- args, kwargs = send_set_password_customer_email_mock.call_args- call_pk = args[0]- assert call_pk == new_customer.pk+ _send_set_password_email_mock.assert_called_once_with(+ new_customer.email, ANY, "dashboard/customer/set_password"+ )customer_creation_event = account_events.CustomerEvent.objects.get()assert customer_creation_event.type == account_events.CustomerEvents.ACCOUNT_CREATEDassert customer_creation_event.user == new_customer+@patch("saleor.dashboard.emails._send_set_user_password_email_with_url.delay")+def test_customer_create_send_password_with_url(+ _send_set_user_password_email_with_url_mock,+ staff_api_client,+ permission_manage_users,+):+ email = "[email protected]"+ variables = {+ "email": email,+ "send_mail": True,+ "redirect_url": "https://www.example.com",+ }++ response = staff_api_client.post_graphql(+ CUSTOMER_CREATE_MUTATION, variables, permissions=[permission_manage_users]+ )+ content = get_graphql_content(response)+ data = content["data"]["customerCreate"]+ assert not data["errors"]++ User = get_user_model()+ new_customer = User.objects.get(email=email)+ assert new_customer++ token = default_token_generator.make_token(new_customer)+ _send_set_user_password_email_with_url_mock.assert_called_once_with(+ new_customer.email, ANY, token, "dashboard/customer/set_password"+ )+++def test_customer_create_without_send_password(+ staff_api_client, permission_manage_users+):+ email = "[email protected]"+ variables = {"email": email}+ response = staff_api_client.post_graphql(+ CUSTOMER_CREATE_MUTATION, variables, permissions=[permission_manage_users]+ )+ content = get_graphql_content(response)+ data = content["data"]["customerCreate"]+ assert not data["errors"]+ staff_user = User.objects.get(email=email)+ assert staff_user+++def test_customer_create_without_redirect_url(+ staff_api_client, permission_manage_users+):+ email = "[email protected]"+ variables = {"email": email, "send_mail": True}+ response = staff_api_client.post_graphql(+ CUSTOMER_CREATE_MUTATION, variables, permissions=[permission_manage_users]+ )+ content = get_graphql_content(response)+ data = content["data"]["customerCreate"]+ assert data["errors"][0]["field"] == "redirectUrl"+ staff_user = User.objects.filter(email=email)+ assert not staff_user+++def test_customer_create_without_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(+ CUSTOMER_CREATE_MUTATION, variables, permissions=[permission_manage_users]+ )+ content = get_graphql_content(response)+ data = content["data"]["customerCreate"]+ assert data["errors"][0]["field"] == "redirectUrl"+ staff_user = User.objects.filter(email=email)+ assert not staff_user+++def test_customer_create_without_url_not_allowed(+ staff_api_client, permission_manage_users+):+ email = "[email protected]"+ variables = {+ "email": email,+ "send_mail": True,+ "redirectUrl": "https://www.fake.com",+ }+ response = staff_api_client.post_graphql(+ CUSTOMER_CREATE_MUTATION, variables, permissions=[permission_manage_users]+ )+ content = get_graphql_content(response)+ data = content["data"]["customerCreate"]+ assert data["errors"][0]["field"] == "redirectUrl"+ staff_user = User.objects.filter(email=email)+ assert not staff_user++def test_customer_update(staff_api_client, staff_user, customer_user, address, permission_manage_users):@@ -1272,6 +1364,37 @@ def test_staff_create()+@patch("saleor.dashboard.emails._send_set_user_password_email_with_url.delay")+def test_staff_create_send_password_with_url(+ _send_set_user_password_email_with_url_mock,+ staff_api_client,+ media_root,+ permission_manage_staff,+):+ email = "[email protected]"+ variables = {+ "email": email,+ "send_mail": True,+ "redirect_url": "https://www.example.com",+ }++ response = staff_api_client.post_graphql(+ STAFF_CREATE_MUTATION, variables, permissions=[permission_manage_staff]+ )+ content = get_graphql_content(response)+ data = content["data"]["staffCreate"]+ assert not data["errors"]++ User = get_user_model()+ staff_user = User.objects.get(email=email)+ assert staff_user.is_staff++ token = default_token_generator.make_token(staff_user)+ _send_set_user_password_email_with_url_mock.assert_called_once_with(+ staff_user.email, ANY, token, "dashboard/staff/set_password"+ )++def test_staff_create_without_send_password(staff_api_client, media_root, permission_manage_staff):