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

Handle HTTP errors on image checks

parent 02de936e
No related branches found
No related tags found
1 merge request!654Handle HTTP errors on image checks
......@@ -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
......
......@@ -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):
......
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