From a6822c7158e70e609a94c6b359d9394d8c1911e2 Mon Sep 17 00:00:00 2001 From: Erwan Rouchet <rouchet@teklia.com> Date: Thu, 30 Jan 2020 17:34:51 +0100 Subject: [PATCH] Handle HTTP errors on image checks --- arkindex/images/serializers.py | 7 +++++-- arkindex/images/tests/test_image_api.py | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/arkindex/images/serializers.py b/arkindex/images/serializers.py index de4ca0b736..658ffdb58e 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 2c4cceef0a..b2298a5e7e 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): -- GitLab