From 52996af891b27e7599ccdad614e6320ec7979d0b Mon Sep 17 00:00:00 2001 From: sumit_chaturvedi Date: Tue, 15 Jul 2025 16:42:09 +0530 Subject: [PATCH] test(api): add test for unexpected exception during user deletion --- api/src/backend/api/tests/test_views.py | 13 +++++++++++++ api/src/backend/api/v1/views.py | 4 +--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/api/src/backend/api/tests/test_views.py b/api/src/backend/api/tests/test_views.py index 2d59cffddd..bda84a89a1 100644 --- a/api/src/backend/api/tests/test_views.py +++ b/api/src/backend/api/tests/test_views.py @@ -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", [ diff --git a/api/src/backend/api/v1/views.py b/api/src/backend/api/v1/views.py index a613eca866..b388452d43 100644 --- a/api/src/backend/api/v1/views.py +++ b/api/src/backend/api/v1/views.py @@ -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( [ {