Skip to content

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

Fix email confirmation (#13367)

SzymJ76cde93243a6
diff --git a/saleor/graphql/account/mutations/account/account_register.py b/saleor/graphql/account/mutations/account/account_register.py
index 534689222d..7bfa16f575 100644
--- a/saleor/graphql/account/mutations/account/account_register.py
+++ b/saleor/graphql/account/mutations/account/account_register.py
@@ -146,7 +146,7 @@ class AccountRegister(ModelMutation):
)
manager = get_plugin_manager_promise(info.context).get()
site = get_site_promise(info.context).get()
- token = default_token_generator.make_token(user)
+ token = None
redirect_url = cleaned_input.get("redirect_url")
with traced_atomic_transaction():
@@ -155,6 +155,7 @@ class AccountRegister(ModelMutation):
user.save()
# Notifications will be deprecated in the future
+ token = default_token_generator.make_token(user)
notifications.send_account_confirmation(
user,
redirect_url,
@@ -166,7 +167,12 @@ class AccountRegister(ModelMutation):
user.save()
if redirect_url:
- params = urlencode({"email": user.email, "token": token})
+ params = urlencode(
+ {
+ "email": user.email,
+ "token": token or default_token_generator.make_token(user),
+ }
+ )
redirect_url = prepare_url(params, redirect_url)
cls.call_event(
diff --git a/saleor/graphql/account/tests/mutations/account/test_account_register.py b/saleor/graphql/account/tests/mutations/account/test_account_register.py
index e6567d48f8..61b0c72a6b 100644
--- a/saleor/graphql/account/tests/mutations/account/test_account_register.py
+++ b/saleor/graphql/account/tests/mutations/account/test_account_register.py
@@ -1,6 +1,7 @@
from unittest.mock import patch
from urllib.parse import urlencode
+from django.contrib.auth.tokens import default_token_generator
from django.test import override_settings
from ......account import events as account_events
@@ -122,6 +123,43 @@ def test_customer_register(
assert customer_creation_event.user == new_user
+@override_settings(
+ ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL=True, ALLOWED_CLIENT_HOSTS=["localhost"]
+)
+@patch("saleor.plugins.manager.PluginsManager.notify")
+def test_customer_register_generates_valid_token(
+ mocked_notify,
+ api_client,
+ channel_PLN,
+ order,
+ site_settings,
+):
+ # given
+ email = "[email protected]"
+ redirect_url = "http://localhost:3000"
+ variables = {
+ "email": email,
+ "password": "Password",
+ "redirectUrl": redirect_url,
+ "firstName": "saleor",
+ "lastName": "rocks",
+ "languageCode": "PL",
+ "metadata": [{"key": "meta", "value": "data"}],
+ "channel": channel_PLN.slug,
+ }
+
+ # when
+ response = api_client.post_graphql(ACCOUNT_REGISTER_MUTATION, variables)
+ new_user = User.objects.get(email=email)
+ content = get_graphql_content(response)
+ data = content["data"]["accountRegister"]
+
+ # then
+ token = mocked_notify.call_args.kwargs["payload"]["token"]
+ assert not data["errors"]
+ assert default_token_generator.check_token(new_user, token)
+
+
@patch("saleor.plugins.manager.PluginsManager.notify")
def test_customer_register_disabled_email_confirmation(
mocked_notify, api_client, site_settings