Skip to content

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

Change id to email in input of setPassword mutation

fowczarek779706d4a75f
diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.py
index b4e1955dbc..08a9e92653 100644
--- a/saleor/graphql/account/mutations/base.py
+++ b/saleor/graphql/account/mutations/base.py
@@ -56,10 +56,12 @@ class SetPasswordInput(graphene.InputObjectType):
password = graphene.String(description="Password", required=True)
-class SetPassword(ModelMutation):
+class SetPassword(BaseMutation):
+ user = graphene.Field(User, description="An user instance with new password.")
+
class Arguments:
- id = graphene.ID(
- description="ID of a user to set password whom.", required=True
+ email = graphene.String(
+ required=True, description="Email of a user to set password whom."
)
input = SetPasswordInput(
description="Fields required to set password.", required=True
@@ -70,21 +72,21 @@ class SetPassword(ModelMutation):
"Sets the user's password from the token sent by email "
"using the RequestPasswordReset mutation."
)
- model = models.User
@classmethod
- def clean_input(cls, info, instance, data):
- cleaned_input = super().clean_input(info, instance, data)
- token = cleaned_input.pop("token")
- if not default_token_generator.check_token(instance, token):
+ def perform_mutation(cls, _root, info, **data):
+ email = data["email"]
+ try:
+ user = models.User.objects.get(email=email)
+ except ObjectDoesNotExist:
+ raise ValidationError({"email": "User doesn't exist"})
+ token = data["input"]["token"]
+ if not default_token_generator.check_token(user, token):
raise ValidationError({"token": INVALID_TOKEN})
- return cleaned_input
-
- @classmethod
- def save(cls, info, instance, cleaned_input):
- instance.set_password(cleaned_input["password"])
- instance.save(update_fields=["password"])
- account_events.customer_password_reset_event(user=instance)
+ user.set_password(data["input"]["password"])
+ user.save(update_fields=["password"])
+ account_events.customer_password_reset_event(user=user)
+ return cls(user=user)
class RequestPasswordReset(BaseMutation):
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index 6c34243eae..276a3ae8ab 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -1343,8 +1343,8 @@ def test_staff_update_errors(staff_user, customer_user, admin_user):
def test_set_password(user_api_client, customer_user):
query = """
- mutation SetPassword($id: ID!, $token: String!, $password: String!) {
- setPassword(id: $id, input: {token: $token, password: $password}) {
+ mutation SetPassword($email: String!, $token: String!, $password: String!) {
+ setPassword(email: $email, input: {token: $token, password: $password}) {
errors {
field
message
@@ -1355,12 +1355,11 @@ def test_set_password(user_api_client, customer_user):
}
}
"""
- id = graphene.Node.to_global_id("User", customer_user.id)
token = default_token_generator.make_token(customer_user)
password = "spanish-inquisition"
# check invalid token
- variables = {"id": id, "password": password, "token": "nope"}
+ variables = {"email": customer_user.email, "password": password, "token": "nope"}
response = user_api_client.post_graphql(query, variables)
content = get_graphql_content(response)
errors = content["data"]["setPassword"]["errors"]