mobile_ui-issue(58): updated change passworf validation #20
|
|
@ -14,6 +14,7 @@ from .errors import (
|
||||||
InvalidOrExpiredOtpError,
|
InvalidOrExpiredOtpError,
|
||||||
InvalidPhoneNumberError,
|
InvalidPhoneNumberError,
|
||||||
InvalidTokenError,
|
InvalidTokenError,
|
||||||
|
SamePasswordError,
|
||||||
UnauthorizedError,
|
UnauthorizedError,
|
||||||
UserAlreadyExistsError,
|
UserAlreadyExistsError,
|
||||||
UserInactiveError,
|
UserInactiveError,
|
||||||
|
|
@ -75,6 +76,7 @@ def _translate_auth_error(exc: Exception) -> HTTPException:
|
||||||
WeakPasswordError,
|
WeakPasswordError,
|
||||||
UserAlreadyExistsError,
|
UserAlreadyExistsError,
|
||||||
CurrentPasswordIncorrectError,
|
CurrentPasswordIncorrectError,
|
||||||
|
SamePasswordError,
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
return HTTPException(
|
return HTTPException(
|
||||||
|
|
|
||||||
|
|
@ -45,5 +45,9 @@ class CurrentPasswordIncorrectError(AuthError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class SamePasswordError(AuthError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class InvalidOrExpiredOtpError(AuthError):
|
class InvalidOrExpiredOtpError(AuthError):
|
||||||
pass
|
pass
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ from .errors import (
|
||||||
CurrentPasswordIncorrectError,
|
CurrentPasswordIncorrectError,
|
||||||
InvalidCredentialsError,
|
InvalidCredentialsError,
|
||||||
InvalidOrExpiredOtpError,
|
InvalidOrExpiredOtpError,
|
||||||
|
SamePasswordError,
|
||||||
UnauthorizedError,
|
UnauthorizedError,
|
||||||
UserAlreadyExistsError,
|
UserAlreadyExistsError,
|
||||||
UserInactiveError,
|
UserInactiveError,
|
||||||
|
|
@ -260,6 +261,14 @@ class AuthService:
|
||||||
"Current password is incorrect. Please enter your valid current password."
|
"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)
|
validate_password_strength(request.new_password)
|
||||||
|
|
||||||
await self.repository.update_password_hash(
|
await self.repository.update_password_hash(
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ from httpx import AsyncClient
|
||||||
|
|
||||||
from app.main import app
|
from app.main import app
|
||||||
from app.features.auth.deps import get_auth_service
|
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 (
|
from app.features.auth.schema import (
|
||||||
AuthTokenResponse,
|
AuthTokenResponse,
|
||||||
AuthTokenUserResponse,
|
AuthTokenUserResponse,
|
||||||
|
|
@ -169,3 +169,21 @@ async def test_forgot_password_success(async_client: AsyncClient, mock_auth_serv
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.json()["status"] == "success"
|
assert response.json()["status"] == "success"
|
||||||
assert mock_auth_service.send_password_reset_email.await_count == 1
|
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"]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -170,3 +170,30 @@ class TestJWTTokens:
|
||||||
def test_verify_password_catches_invalid_salt_format(self) -> None:
|
def test_verify_password_catches_invalid_salt_format(self) -> None:
|
||||||
# Format passes startswith $2b$ but is invalid for bcrypt.checkpw
|
# Format passes startswith $2b$ but is invalid for bcrypt.checkpw
|
||||||
assert verify_password("pass", "$2b$invalid_format_hash") is False
|
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)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue