Merge pull request 'fix(auth): prevent duplicate phone numbers when updating user profiles' (#33) from fix/prevent-duplicate-profile-phone into main
Reviewed-on: https://git.zedobject.local/AcuPedia/acupedia-api/pulls/33 Reviewed-by: Amit Salunke <amit@noreply.localhost>
This commit is contained in:
commit
048ae6ff8c
|
|
@ -42,10 +42,9 @@ class AuthRepository:
|
|||
self,
|
||||
phone_number: str,
|
||||
) -> AppUser | None:
|
||||
# Preserves the existing auth API's LIKE lookup semantics.
|
||||
result = await self._session.execute(
|
||||
select(AppUser)
|
||||
.where(AppUser.phone_number.like(f"%{phone_number}%"))
|
||||
.where(AppUser.phone_number == phone_number)
|
||||
.order_by(AppUser.id.asc())
|
||||
.limit(1)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -222,6 +222,13 @@ class AuthService:
|
|||
)
|
||||
country = request.country.strip() if request.country else None
|
||||
|
||||
if phone:
|
||||
existing_user = await self.repository.get_user_by_phone(phone)
|
||||
if existing_user is not None and str(existing_user.id) != str(user_id):
|
||||
raise UserAlreadyExistsError(
|
||||
"An account with this phone number already exists."
|
||||
)
|
||||
|
||||
user = await self.repository.update_profile(
|
||||
user_id=user_id,
|
||||
display_name=full_name,
|
||||
|
|
|
|||
|
|
@ -187,3 +187,22 @@ async def test_change_password_same_password_returns_400(async_client: AsyncClie
|
|||
assert response.status_code == 400
|
||||
assert "New password cannot be the same as current password" in response.json()["detail"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_profile_duplicate_phone_returns_400(async_client: AsyncClient, mock_auth_service: MagicMock) -> None:
|
||||
mock_auth_service.update_profile = AsyncMock(side_effect=UserAlreadyExistsError("An account with this phone number already exists."))
|
||||
app.dependency_overrides[get_auth_service] = lambda: mock_auth_service
|
||||
|
||||
payload = {
|
||||
"full_name": "Dr. User Two",
|
||||
"phone_number": "+8809988776655",
|
||||
"country": "Bangladesh",
|
||||
}
|
||||
headers = {"Authorization": "Bearer sample_valid_token"}
|
||||
response = await async_client.put("/api/auth/profile", json=payload, headers=headers)
|
||||
app.dependency_overrides.pop(get_auth_service, None)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert "already exists" in response.json()["detail"]
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue