diff --git a/tests/test_image_chooser_views.py b/tests/test_image_chooser_views.py index 22d1797..4298deb 100644 --- a/tests/test_image_chooser_views.py +++ b/tests/test_image_chooser_views.py @@ -1,11 +1,15 @@ from unittest import mock +from django.db import IntegrityError +from django.http import HttpRequest from django.test import TestCase, TransactionTestCase, override_settings from django.urls import reverse, reverse_lazy from testapp.factories import CustomImageFactory from wagtail.test.utils import WagtailTestUtils -from .utils import TEST_ASSET_ID +from wagtail_bynder.views.image import ImageChosenView + +from .utils import TEST_ASSET_ID, get_test_asset_data class TestImageChooseView(TestCase, WagtailTestUtils): @@ -21,6 +25,11 @@ def test_view_loads_without_errors(self): class TestImageChosenView(TransactionTestCase, WagtailTestUtils): url = reverse_lazy("wagtailimages_chooser:chosen", args=[TEST_ASSET_ID]) + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.asset_data = get_test_asset_data() + def setUp(self): # Always log in as an admin user self.user = self.create_test_user() @@ -106,3 +115,81 @@ def test_uses_existing_image_and_updates_it(self, update_object_mock): }, }, ) + + def _get_view_instance(self) -> ImageChosenView: + request = HttpRequest() + request.method = "post" + request.path = self.url + view = ImageChosenView() + view.setup(request, TEST_ASSET_ID) + return view + + def test_create_object_clash_handling_after_update(self): + view = self._get_view_instance() + + # Create an image with a matching bynder_id + existing_image = CustomImageFactory.create(bynder_id=TEST_ASSET_ID) + + # Trigger the test target directly + with ( + mock.patch.object( + view.asset_client, "media_info", return_value=self.asset_data + ) as media_info_mock, + ): + result = view.create_object(TEST_ASSET_ID) + + # Check behavior and result + media_info_mock.assert_called_once_with(TEST_ASSET_ID) + self.assertEqual(result, existing_image) + + def test_create_object_clash_handling_on_save(self): + view = self._get_view_instance() + + def problematic_save_method(): + # Create an image with a matching bynder_id + CustomImageFactory.create(bynder_id=TEST_ASSET_ID) + raise IntegrityError("bynder_id must be unique") + + patched_obj = mock.MagicMock() + patched_obj.save = problematic_save_method + + # Trigger the test target directly + with ( + mock.patch.object( + view.asset_client, "media_info", return_value=self.asset_data + ) as media_info_mock, + mock.patch.object( + view, "_update_object_details", return_value=patched_obj + ) as update_object_details_mock, + ): + result = view.create_object(TEST_ASSET_ID) + + # Check behavior and result + media_info_mock.assert_called_once_with(TEST_ASSET_ID) + update_object_details_mock.assert_called_once() + self.assertEqual(result.bynder_id, TEST_ASSET_ID) + + def test_create_object_clash_handling_on_save_when_bynder_id_match_not_found(self): + view = self._get_view_instance() + + def problematic_save_method(): + raise IntegrityError("Some other field must be unique") + + patched_obj = mock.MagicMock() + patched_obj.save = problematic_save_method + + # Trigger the test target directly + with ( + mock.patch.object( + view.asset_client, "media_info", return_value=self.asset_data + ) as media_info_mock, + mock.patch.object( + view, "_update_object_details", return_value=patched_obj + ) as update_object_details_mock, + self.assertRaises(IntegrityError), + ): + view.create_object(TEST_ASSET_ID) + + # Check behavior + media_info_mock.assert_called_once_with(TEST_ASSET_ID) + update_object_details_mock.assert_called_once()