Skip to content
Snippets Groups Projects
Commit f7ac455a authored by Erwan Rouchet's avatar Erwan Rouchet Committed by Bastien Abadie
Browse files

Prevent updating an element's zone to an invalid image

parent f48813bd
No related branches found
No related tags found
1 merge request!913Prevent updating an element's zone to an invalid image
......@@ -251,7 +251,6 @@ class ElementSerializer(ElementSlimSerializer):
return instance
def update(self, instance, validated_data):
image = validated_data.pop('image', None)
polygon = validated_data.pop('polygon', None)
if polygon or image:
......@@ -263,6 +262,9 @@ class ElementSerializer(ElementSlimSerializer):
})
image = instance.zone.image
if image.width == 0 or image.height == 0:
raise ValidationError({'image': ['This image does not have valid dimensions.']})
if not polygon:
if instance.zone:
polygon = instance.zone.polygon
......
......@@ -195,6 +195,27 @@ class TestPatchElements(FixtureAPITestCase):
self.assertEqual(self.vol.zone.polygon.width, 20)
self.assertEqual(self.vol.zone.polygon.height, 10)
def test_patch_element_invalid_dimensions(self):
self.client.force_login(self.user)
self.assertIsNone(self.vol.zone)
bad_image = self.imgsrv.images.create(
path='oh-no',
status=S3FileStatus.Unchecked,
width=0,
height=0,
)
response = self.client.patch(
reverse('api:element-retrieve', kwargs={'pk': str(self.vol.id)}),
data={
'image': str(bad_image.id),
'polygon': [[10, 20], [10, 30], [30, 30], [30, 20], [10, 20]],
},
format='json',
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertDictEqual(response.json(), {'image': ['This image does not have valid dimensions.']})
self.assertIsNone(self.vol.zone)
def test_element_patch_type_allowed_transcription(self):
"""
The base number of requests is increased by 1 because
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment