Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix change password error #3536

Merged
12 changes: 10 additions & 2 deletions mathesar/api/ui/serializers/users.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib.auth.password_validation import validate_password
from django.core.exceptions import ValidationError as DjangoValidationError
from rest_access_policy import FieldAccessMixin, PermittedPkRelatedField
from rest_framework import serializers

Expand Down Expand Up @@ -71,7 +72,7 @@ def create(self, validated_data):


class ChangePasswordSerializer(MathesarErrorMessageMixin, serializers.Serializer):
password = serializers.CharField(write_only=True, required=True, validators=[validate_password])
password = serializers.CharField(write_only=True, required=True)
old_password = serializers.CharField(write_only=True, required=True)

def validate_old_password(self, value):
Expand All @@ -80,14 +81,21 @@ def validate_old_password(self, value):
return value
raise IncorrectOldPassword(field='old_password')

def validate_password(self, value):
try:
validate_password(value)
except DjangoValidationError as e:
raise e
return value

def update(self, instance, validated_data):
instance.set_password(validated_data['password'])
instance.save()
return instance


class PasswordResetSerializer(MathesarErrorMessageMixin, serializers.Serializer):
password = serializers.CharField(write_only=True, required=True, validators=[validate_password])
password = serializers.CharField(write_only=True, required=True)


class DatabaseRoleSerializer(MathesarErrorMessageMixin, serializers.ModelSerializer):
Expand Down
18 changes: 16 additions & 2 deletions mathesar/tests/api/test_user_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,31 @@ def test_user_password_reset_non_superuser(client_bob, user_bob):


def test_user_password_change(client_bob, user_bob):
hitenvidhani marked this conversation as resolved.
Show resolved Hide resolved
new_password = 'new_password'
new_password = 'NewPass0!'
old_password = 'password'
data = {
'password': new_password,
'old_password': 'password'
'old_password': old_password
}
response = client_bob.post('/api/ui/v0/users/password_change/', data=data)
assert response.status_code == 200
user_bob.refresh_from_db()
assert user_bob.check_password(new_password) is True


def test_user_password_change_invalid(client_bob, user_bob):
new_password = 'new_pwd'
old_password = 'password'
data = {
'password': new_password,
'old_password': old_password
}
response = client_bob.post('/api/ui/v0/users/password_change/', data=data)
assert response.status_code == 400
user_bob.refresh_from_db()
assert user_bob.check_password(new_password) is False


def test_diff_user_detail_as_non_superuser(client_bob, admin_user):
response = client_bob.get(f'/api/ui/v0/users/{admin_user.id}/')
response_data = response.json()
Expand Down
Loading