From 00ee398c58b25ce46cd7604d3c6185c1d52a1090 Mon Sep 17 00:00:00 2001 From: Urvish Date: Mon, 26 Oct 2026 11:56:41 +0530 Subject: [PATCH] mobile_ui-issue(58): updated change passworf validation --- app/features/auth/api.py | 2 ++ app/features/auth/errors.py | 4 ++++ app/features/auth/service.py | 9 +++++++++ tests/api/test_auth_api.py | 20 +++++++++++++++++++- tests/unit/test_security.py | 27 +++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 1 deletion(-) diff --git a/app/features/auth/api.py b/app/features/auth/api.py index ef943c5..5836cb6 100644 --- a/app/features/auth/api.py +++ b/app/features/auth/api.py @@ -14,6 +14,7 @@ from .errors import ( InvalidOrExpiredOtpError, InvalidPhoneNumberError, InvalidTokenError, + SamePasswordError, UnauthorizedError, UserAlreadyExistsError, UserInactiveError, @@ -75,6 +76,7 @@ def _translate_auth_error(exc: Exception) -> HTTPException: WeakPasswordError, UserAlreadyExistsError, CurrentPasswordIncorrectError, + SamePasswordError, ), ): return HTTPException( diff --git a/app/features/auth/errors.py b/app/features/auth/errors.py index 0372cea..829cc88 100644 --- a/app/features/auth/errors.py +++ b/app/features/auth/errors.py @@ -45,5 +45,9 @@ class CurrentPasswordIncorrectError(AuthError): pass +class SamePasswordError(AuthError): + pass + + class InvalidOrExpiredOtpError(AuthError): pass diff --git a/app/features/auth/service.py b/app/features/auth/service.py index 162512f..02194f1 100644 --- a/app/features/auth/service.py +++ b/app/features/auth/service.py @@ -12,6 +12,7 @@ from .errors import ( CurrentPasswordIncorrectError, InvalidCredentialsError, InvalidOrExpiredOtpError, + SamePasswordError, UnauthorizedError, UserAlreadyExistsError, UserInactiveError, @@ -260,6 +261,14 @@ class AuthService: "Current password is incorrect. Please enter your valid current password." ) + if ( + request.old_password == request.new_password + or verify_password(request.new_password, user.password_hash) + ): + raise SamePasswordError( + "New password cannot be the same as current password." + ) + validate_password_strength(request.new_password) await self.repository.update_password_hash( diff --git a/tests/api/test_auth_api.py b/tests/api/test_auth_api.py index c71e5ad..e818151 100644 --- a/tests/api/test_auth_api.py +++ b/tests/api/test_auth_api.py @@ -7,7 +7,7 @@ from httpx import AsyncClient from app.main import app from app.features.auth.deps import get_auth_service -from app.features.auth.errors import InvalidCredentialsError, UserAlreadyExistsError +from app.features.auth.errors import InvalidCredentialsError, SamePasswordError, UserAlreadyExistsError from app.features.auth.schema import ( AuthTokenResponse, AuthTokenUserResponse, @@ -169,3 +169,21 @@ async def test_forgot_password_success(async_client: AsyncClient, mock_auth_serv assert response.status_code == 200 assert response.json()["status"] == "success" assert mock_auth_service.send_password_reset_email.await_count == 1 + + +@pytest.mark.asyncio +async def test_change_password_same_password_returns_400(async_client: AsyncClient, mock_auth_service: MagicMock) -> None: + mock_auth_service.change_password = AsyncMock(side_effect=SamePasswordError("New password cannot be the same as current password.")) + app.dependency_overrides[get_auth_service] = lambda: mock_auth_service + + payload = { + "old_password": "OldPassword123!", + "new_password": "OldPassword123!", + } + headers = {"Authorization": "Bearer sample_valid_token"} + response = await async_client.post("/api/auth/change-password", json=payload, headers=headers) + app.dependency_overrides.pop(get_auth_service, None) + + assert response.status_code == 400 + assert "New password cannot be the same as current password" in response.json()["detail"] + diff --git a/tests/unit/test_security.py b/tests/unit/test_security.py index f5d4d96..623585b 100644 --- a/tests/unit/test_security.py +++ b/tests/unit/test_security.py @@ -170,3 +170,30 @@ class TestJWTTokens: def test_verify_password_catches_invalid_salt_format(self) -> None: # Format passes startswith $2b$ but is invalid for bcrypt.checkpw assert verify_password("pass", "$2b$invalid_format_hash") is False + + @pytest.mark.asyncio + async def test_change_password_same_password_raises(self) -> None: + from unittest.mock import AsyncMock, MagicMock + from uuid import uuid4 + from app.features.auth.service import AuthService + from app.features.auth.schema import ChangePasswordRequest + from app.features.auth.errors import SamePasswordError + + mock_session = AsyncMock() + mock_repo = AsyncMock() + + user = MagicMock() + user.id = uuid4() + user.password_hash = hash_password("ValidPass123!") + mock_repo.get_user_by_id = AsyncMock(return_value=user) + + token = create_access_token({"sub": str(user.id)}) + service = AuthService( + session=mock_session, + repository=mock_repo, + password_reset_email_sender=MagicMock(), + ) + + req = ChangePasswordRequest(old_password="ValidPass123!", new_password="ValidPass123!") + with pytest.raises(SamePasswordError, match="cannot be the same"): + await service.change_password(access_token=token, request=req)