Skip to content

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

Add django password validation to set password mutation

fowczarekf28520917763
diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.py
index 08a9e92653..6990ecc9ec 100644
--- a/saleor/graphql/account/mutations/base.py
+++ b/saleor/graphql/account/mutations/base.py
@@ -1,5 +1,6 @@
import graphene
from django.conf import settings
+from django.contrib.auth import password_validation
from django.contrib.auth.tokens import default_token_generator
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db import transaction
@@ -76,14 +77,21 @@ class SetPassword(BaseMutation):
@classmethod
def perform_mutation(cls, _root, info, **data):
email = data["email"]
+ token = data["input"]["token"]
+ password = data["input"]["password"]
+
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})
- user.set_password(data["input"]["password"])
+ try:
+ password_validation.validate_password(password, user)
+ except ValidationError as error:
+ raise ValidationError({"password": error.messages})
+
+ user.set_password(password)
user.save(update_fields=["password"])
account_events.customer_password_reset_event(user=user)
return cls(user=user)
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index 276a3ae8ab..52761402e6 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -1341,32 +1341,27 @@ def test_staff_update_errors(staff_user, customer_user, admin_user):
StaffUpdate.clean_is_active(False, customer_user, staff_user)
-def test_set_password(user_api_client, customer_user):
- query = """
+SET_PASSWORD_MUTATION = """
mutation SetPassword($email: String!, $token: String!, $password: String!) {
setPassword(email: $email, input: {token: $token, password: $password}) {
errors {
- field
- message
- }
- user {
- id
- }
+ field
+ message
+ }
+ user {
+ id
}
}
- """
+ }
+"""
+
+
+def test_set_password(user_api_client, customer_user):
token = default_token_generator.make_token(customer_user)
password = "spanish-inquisition"
- # check invalid token
- 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"]
- assert errors[0]["message"] == INVALID_TOKEN
-
- variables["token"] = token
- response = user_api_client.post_graphql(query, variables)
+ variables = {"email": customer_user.email, "password": password, "token": token}
+ response = user_api_client.post_graphql(SET_PASSWORD_MUTATION, variables)
content = get_graphql_content(response)
data = content["data"]["setPassword"]
assert data["user"]["id"]
@@ -1379,6 +1374,45 @@ def test_set_password(user_api_client, customer_user):
assert password_resent_event.user == customer_user
+def test_set_password_invalid_token(user_api_client, customer_user):
+ variables = {"email": customer_user.email, "password": "pass", "token": "token"}
+ response = user_api_client.post_graphql(SET_PASSWORD_MUTATION, variables)
+ content = get_graphql_content(response)
+ errors = content["data"]["setPassword"]["errors"]
+ assert errors[0]["message"] == INVALID_TOKEN
+
+
+def test_set_password_invalid_email(user_api_client):
+ variables = {"email": "[email protected]", "password": "pass", "token": "token"}
+ response = user_api_client.post_graphql(SET_PASSWORD_MUTATION, variables)
+ content = get_graphql_content(response)
+ errors = content["data"]["setPassword"]["errors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "email"
+
+
+def test_set_password_invalid_password(user_api_client, customer_user, settings):
+ settings.AUTH_PASSWORD_VALIDATORS = [
+ {
+ "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
+ "OPTIONS": {"min_length": 5},
+ },
+ {"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"},
+ ]
+
+ token = default_token_generator.make_token(customer_user)
+ variables = {"email": customer_user.email, "password": "1234", "token": token}
+ response = user_api_client.post_graphql(SET_PASSWORD_MUTATION, variables)
+ content = get_graphql_content(response)
+ errors = content["data"]["setPassword"]["errors"]
+ assert len(errors) == 2
+ assert (
+ errors[0]["message"]
+ == "This password is too short. It must contain at least 5 characters."
+ )
+ assert errors[1]["message"] == "This password is entirely numeric."
+
+
@patch("saleor.account.emails.send_password_reset_email.delay")
def test_request_password_reset_email_for_staff(
send_password_reset_mock, staff_api_client