diff --git a/arkindex/images/serializers.py b/arkindex/images/serializers.py index de4ca0b73639f31e17020fad88744e3ae9cb280d..658ffdb58e675039463e97d9a461c04cc7236abb 100644 --- a/arkindex/images/serializers.py +++ b/arkindex/images/serializers.py @@ -4,6 +4,7 @@ from arkindex.images.models import ImageServer, Image, Zone from arkindex.dataimport.models import DataFile from arkindex.project.aws import S3FileStatus from django.db.models import F, Value, CharField +from requests.exceptions import RequestException from rest_framework.exceptions import ValidationError, APIException from rest_framework import serializers import re @@ -59,10 +60,12 @@ class ImageSerializer(serializers.ModelSerializer): elif value == S3FileStatus.Checked: # Perform image validation if we are updating an existing image to Checked try: - if not self.instance.server.read_only: + if self.instance.server.s3_bucket and not self.instance.server.read_only: self.instance.check_hash(raise_exc=True) self.instance.perform_check(raise_exc=True) - except (AssertionError, ValueError) as e: + except (AssertionError, ValueError, RequestException) as e: + self.instance.status = S3FileStatus.Error + self.instance.save() raise ValidationError(str(e)) return value diff --git a/arkindex/images/tests/test_image_api.py b/arkindex/images/tests/test_image_api.py index 2c4cceef0a86cad82beaa4e1625595717a6a26cf..b2298a5e7e50304e05618774a2505479f0bbfbe6 100644 --- a/arkindex/images/tests/test_image_api.py +++ b/arkindex/images/tests/test_image_api.py @@ -7,6 +7,7 @@ from django.test import override_settings from django.urls import reverse from unittest.mock import patch, call import random +import responses class ImageMethods: @@ -229,6 +230,26 @@ class TestImageApi(FixtureAPITestCase): self.assertEqual(check_hash_mock.call_args, call(raise_exc=True)) self.assertEqual(perform_check_mock.call_args, call(raise_exc=True)) + @responses.activate + def test_update_image_check_fail(self): + """ + Test setting an image's status to Checked runs the image checks + """ + url = 'https://test-server.eu/images/notfound/info.json' + responses.add(responses.GET, url, status=404) + img = self.server.images.create( + status=S3FileStatus.Unchecked, + path='notfound', + ) + response = self.client.put( + reverse('api:image-retrieve', kwargs={'pk': str(img.id)}), + {'status': 'checked'}, + ) + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertEqual(response.json(), {'status': [f'404 Client Error: Not Found for url: {url}']}) + img.refresh_from_db() + self.assertEqual(img.status, S3FileStatus.Error) + @patch('arkindex.images.serializers.Image.perform_check') @patch('arkindex.images.serializers.Image.check_hash') def test_update_image_server_read_only(self, check_hash_mock, perform_check_mock):