From 54056ab706872ab6993bb6bd9c2b56a93f42310a Mon Sep 17 00:00:00 2001 From: hitenvidhani Date: Sat, 20 Apr 2024 00:17:46 +0530 Subject: [PATCH] Add new test, resolve comments --- mathesar/api/ui/serializers/users.py | 5 +++-- mathesar/tests/api/test_user_api.py | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/mathesar/api/ui/serializers/users.py b/mathesar/api/ui/serializers/users.py index a8d363f707..80501c9c51 100644 --- a/mathesar/api/ui/serializers/users.py +++ b/mathesar/api/ui/serializers/users.py @@ -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 @@ -83,8 +84,8 @@ def validate_old_password(self, value): def validate_password(self, value): try: validate_password(value) - except serializers.ValidationError as exc: - raise serializers.ValidationError(str(exc)) + except DjangoValidationError as exc: + raise DjangoValidationError(str(exc)) return value def update(self, instance, validated_data): diff --git a/mathesar/tests/api/test_user_api.py b/mathesar/tests/api/test_user_api.py index 82ee072fe0..d655022188 100644 --- a/mathesar/tests/api/test_user_api.py +++ b/mathesar/tests/api/test_user_api.py @@ -64,7 +64,7 @@ def test_user_password_change(client_bob, user_bob): new_password = 'NewPass0!' old_password = 'password' data = { - 'password': 'NewPass0!', + 'password': new_password, 'old_password': old_password } response = client_bob.post('/api/ui/v0/users/password_change/', data=data) @@ -73,6 +73,19 @@ def test_user_password_change(client_bob, user_bob): 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()