|
5 | 5 | from testapp.factories import CustomImageFactory |
6 | 6 | from wagtail.test.utils import WagtailTestUtils |
7 | 7 |
|
| 8 | +from wagtail_bynder.exceptions import BynderAssetDownloadError |
| 9 | + |
8 | 10 | from .utils import TEST_ASSET_ID |
9 | 11 |
|
10 | 12 |
|
@@ -106,3 +108,45 @@ def test_uses_existing_image_and_updates_it(self, update_object_mock): |
106 | 108 | }, |
107 | 109 | }, |
108 | 110 | ) |
| 111 | + |
| 112 | + def test_returns_error_step_when_download_fails(self): |
| 113 | + """Test that download errors return an error step instead of crashing""" |
| 114 | + with mock.patch( |
| 115 | + "wagtail_bynder.views.image.ImageChosenView.create_object", |
| 116 | + ) as create_object_mock: |
| 117 | + # Mock create_object to raise download error |
| 118 | + create_object_mock.side_effect = BynderAssetDownloadError( |
| 119 | + "Server error downloading 'test.jpg' from Bynder. " |
| 120 | + ) |
| 121 | + |
| 122 | + response = self.client.get(str(self.url)) |
| 123 | + |
| 124 | + # Should return error step, not crash |
| 125 | + self.assertEqual(response.status_code, 200) |
| 126 | + response_data = response.json() |
| 127 | + self.assertEqual(response_data["step"], "error") |
| 128 | + self.assertIn("error_message", response_data) |
| 129 | + self.assertIn("Server error downloading", response_data["error_message"]) |
| 130 | + |
| 131 | + @override_settings(BYNDER_SYNC_EXISTING_IMAGES_ON_CHOOSE=True) |
| 132 | + def test_returns_error_step_when_update_fails(self): |
| 133 | + """Test that download errors during update return an error step""" |
| 134 | + # Create an image with a matching bynder_id |
| 135 | + CustomImageFactory.create(bynder_id=TEST_ASSET_ID) |
| 136 | + |
| 137 | + with mock.patch( |
| 138 | + "wagtail_bynder.views.image.ImageChosenView.update_object", |
| 139 | + ) as update_object_mock: |
| 140 | + # Mock update_object to raise download error |
| 141 | + update_object_mock.side_effect = BynderAssetDownloadError( |
| 142 | + "Server error downloading 'test.jpg' from Bynder. " |
| 143 | + ) |
| 144 | + |
| 145 | + response = self.client.get(str(self.url)) |
| 146 | + |
| 147 | + # Should return error step, not crash |
| 148 | + self.assertEqual(response.status_code, 200) |
| 149 | + response_data = response.json() |
| 150 | + self.assertEqual(response_data["step"], "error") |
| 151 | + self.assertIn("error_message", response_data) |
| 152 | + self.assertIn("Server error downloading", response_data["error_message"]) |
0 commit comments