Skip to content

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

Allow using PasswordReset mutation for accounts

fowczareke3a4a5bb344d
diff --git a/saleor/graphql/account/mutations/account.py b/saleor/graphql/account/mutations/account.py
index 60da8d0c0a..b5bd0ccb39 100644
--- a/saleor/graphql/account/mutations/account.py
+++ b/saleor/graphql/account/mutations/account.py
@@ -1,11 +1,12 @@
import graphene
-from django.core.exceptions import ValidationError
+from django.core.exceptions import ObjectDoesNotExist, ValidationError
from ....account import emails, events as account_events, models, utils
from ....checkout import AddressType
from ...account.enums import AddressTypeEnum
from ...account.types import Address, AddressInput, User
from ...core.mutations import BaseMutation, ModelMutation
+from .base import send_user_password_reset_email
from .staff import CustomerCreate, UserAddressInput
@@ -184,3 +185,33 @@ class CustomerSetDefaultAddress(BaseMutation):
utils.change_user_default_address(user, address, address_type)
return cls(user=user)
+
+
+class CustomerPasswordResetInput(graphene.InputObjectType):
+ email = graphene.String(
+ required=True,
+ description=("Email of the user that will be used for password recovery."),
+ )
+
+
+class CustomerPasswordReset(BaseMutation):
+ class Arguments:
+ input = CustomerPasswordResetInput(
+ description="Fields required to reset customer's password", required=True
+ )
+
+ class Meta:
+ description = (
+ "DEPRECATED: Use SetPassword instead. Resets the customer's password."
+ )
+
+ @classmethod
+ def perform_mutation(cls, _root, info, **data):
+ email = data.get("input")["email"]
+ try:
+ user = models.User.objects.get(email=email)
+ except ObjectDoesNotExist:
+ raise ValidationError({"email": "User with this email doesn't exist"})
+ site = info.context.site
+ send_user_password_reset_email(user, site)
+ return CustomerPasswordReset()
diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.py
index ff8a536fca..332858640b 100644
--- a/saleor/graphql/account/mutations/base.py
+++ b/saleor/graphql/account/mutations/base.py
@@ -78,32 +78,22 @@ class SetPassword(ModelMutation):
account_events.customer_password_reset_event(user=instance)
-class CustomerPasswordResetInput(graphene.InputObjectType):
- email = graphene.String(
- required=True,
- description=("Email of the user that will be used for password recovery."),
- )
-
-
-class CustomerPasswordReset(BaseMutation):
+class PasswordReset(BaseMutation):
class Arguments:
- input = CustomerPasswordResetInput(
- description="Fields required to reset customer's password", required=True
- )
+ email = graphene.String(description="Email", required=True)
class Meta:
- description = "Resets the customer's password."
+ description = "Sends password reset email"
@classmethod
- def perform_mutation(cls, _root, info, **data):
- email = data.get("input")["email"]
+ def perform_mutation(cls, _root, info, email):
try:
user = models.User.objects.get(email=email)
except ObjectDoesNotExist:
raise ValidationError({"email": "User with this email doesn't exist"})
site = info.context.site
send_user_password_reset_email(user, site)
- return CustomerPasswordReset()
+ return PasswordReset()
class AddressUpdate(ModelMutation):
diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.py
index aee56354a3..1a59710416 100644
--- a/saleor/graphql/account/mutations/staff.py
+++ b/saleor/graphql/account/mutations/staff.py
@@ -1,7 +1,7 @@
from copy import copy
import graphene
-from django.core.exceptions import ObjectDoesNotExist, ValidationError
+from django.core.exceptions import ValidationError
from graphql_jwt.decorators import staff_member_required
from graphql_jwt.exceptions import PermissionDenied
@@ -310,25 +310,6 @@ class StaffDelete(StaffDeleteMixin, UserDelete):
return cls.success_response(instance)
-class PasswordReset(BaseMutation):
- class Arguments:
- email = graphene.String(description="Email", required=True)
-
- class Meta:
- description = "Sends password reset email"
- permissions = ("account.manage_users",)
-
- @classmethod
- def perform_mutation(cls, _root, info, email):
- try:
- user = models.User.objects.get(email=email)
- except ObjectDoesNotExist:
- raise ValidationError({"email": "User with this email doesn't exist"})
- site = info.context.site
- send_user_password_reset_email(user, site)
- return PasswordReset()
-
-
class AddressCreate(ModelMutation):
user = graphene.Field(
User, description="A user instance for which the address was created."
diff --git a/saleor/graphql/account/schema.py b/saleor/graphql/account/schema.py
index 8478d5bb7f..670eb61214 100644
--- a/saleor/graphql/account/schema.py
+++ b/saleor/graphql/account/schema.py
@@ -11,6 +11,7 @@ from .mutations.account import (
AccountRequestDeletion,
AccountUpdate,
CustomerAddressCreate,
+ CustomerPasswordReset,
CustomerRegister,
CustomerSetDefaultAddress,
LoggedUserUpdate,
@@ -18,7 +19,7 @@ from .mutations.account import (
from .mutations.base import (
AddressDelete,
AddressUpdate,
- CustomerPasswordReset,
+ PasswordReset,
SetPassword,
UserClearStoredMeta,
UserUpdateMeta,
@@ -29,7 +30,6 @@ from .mutations.staff import (
CustomerCreate,
CustomerDelete,
CustomerUpdate,
- PasswordReset,
StaffCreate,
StaffDelete,
StaffUpdate,
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index e9b044c1f8..66e28fe83a 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -1289,7 +1289,7 @@ def test_set_password(user_api_client, customer_user):
@patch("saleor.account.emails.send_password_reset_email.delay")
def test_password_reset_email(
- send_password_reset_mock, staff_api_client, customer_user, permission_manage_users
+ send_password_reset_mock, staff_api_client, customer_user
):
query = """
mutation ResetPassword($email: String!) {
@@ -1303,9 +1303,7 @@ def test_password_reset_email(
"""
email = customer_user.email
variables = {"email": email}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_users]
- )
+ response = staff_api_client.post_graphql(query, variables)
content = get_graphql_content(response)
data = content["data"]["passwordReset"]
assert data == {"errors": []}
@@ -1321,7 +1319,7 @@ def test_password_reset_email(
@patch("saleor.account.emails.send_password_reset_email.delay")
def test_password_reset_email_non_existing_user(
- send_password_reset_mock, staff_api_client, permission_manage_users
+ send_password_reset_mock, staff_api_client
):
query = """
mutation ResetPassword($email: String!) {
@@ -1335,9 +1333,7 @@ def test_password_reset_email_non_existing_user(
"""
variables = {"email": email}
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_users]
- )
+ response = staff_api_client.post_graphql(query, variables)
content = get_graphql_content(response)
data = content["data"]["passwordReset"]
assert data["errors"] == [