test(api): add test for unexpected exception during user deletion

This commit is contained in:
sumit_chaturvedi
2025-07-15 16:42:09 +05:30
parent dd16d896a9
commit 52996af891
2 changed files with 14 additions and 3 deletions
+13
View File
@@ -326,6 +326,19 @@ class TestUserViewSet:
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert User.objects.filter(id=another_user.id).exists()
def test_users_destroy_unexpected_exception(
self, authenticated_client, create_test_user
):
with patch(
"api.v1.views.UserViewSet.perform_destroy",
side_effect=Exception("Unexpected"),
):
response = authenticated_client.delete(
reverse("user-detail", kwargs={"pk": create_test_user.id})
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json()["errors"][0]["detail"] == "Failed to delete the user"
@pytest.mark.parametrize(
"attribute_key, attribute_value, error_field",
[
+1 -3
View File
@@ -782,9 +782,7 @@ class UserViewSet(BaseUserViewset):
try:
return super().destroy(request, *args, **kwargs)
except Exception as e:
print("Exception caught in destroy():", e)
except Exception:
raise ValidationError(
[
{