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

Handle non-JSON responses on image check

parent 0c890527
No related branches found
No related tags found
1 merge request!787Handle non-JSON responses on image check
......@@ -6,6 +6,7 @@ from django.db.models.functions import Concat, Substr
from django.utils.functional import cached_property
from django.utils.text import slugify
from enumfields import EnumField
from json import JSONDecodeError
from arkindex.images.managers import ImageServerManager
from arkindex.project.aws import S3FileMixin, S3FileStatus
from arkindex.project.fields import StripSlashURLField, LStripTextField, MD5HashField
......@@ -221,7 +222,11 @@ class Image(S3FileMixin, IndexableModel):
# Load info
resp = requests.get(info_url, timeout=15, allow_redirects=True)
resp.raise_for_status()
self.check_from_payload(resp.json())
try:
payload = resp.json()
except JSONDecodeError:
raise ValueError('Server returned an invalid JSON document')
self.check_from_payload(payload)
except Exception as e:
logger.warning('Image check failed: {}'.format(str(e)))
self.status = S3FileStatus.Error
......
......@@ -23,10 +23,32 @@ class TestImagePerformCheck(FixtureTestCase):
self.img.perform_check(raise_exc=True, save=False)
self.assertEqual(self.img.status, S3FileStatus.Error)
@responses.activate
def test_empty(self):
"""
Test Image.perform_check fails when the server returns nothing
"""
responses.add(responses.GET, 'http://server/img1/info.json', status=200)
self.img.status = S3FileStatus.Unchecked
with self.assertRaisesRegex(ValueError, 'Server returned an invalid JSON document'):
self.img.perform_check(raise_exc=True, save=False)
self.assertEqual(self.img.status, S3FileStatus.Error)
@responses.activate
def test_json(self):
"""
Test Image.perform_check fails when the server returns invalid JSON
"""
responses.add(responses.GET, 'http://server/img1/info.json', body='<blink>oh no</blink>')
self.img.status = S3FileStatus.Unchecked
with self.assertRaisesRegex(ValueError, 'Server returned an invalid JSON document'):
self.img.perform_check(raise_exc=True, save=False)
self.assertEqual(self.img.status, S3FileStatus.Error)
@responses.activate
def test_required_items(self):
"""
Test Image.perform_check fails on a HTTP error code
Test Image.perform_check fails when required items are missing from the JSON payload
"""
base_payload = {
'@id': 'http://server/img1',
......@@ -38,7 +60,7 @@ class TestImagePerformCheck(FixtureTestCase):
payload = base_payload.copy()
del payload[key]
responses.add(responses.GET, 'http://server/img1/info.json', json=payload)
with self.assertRaises(AssertionError, msg='Missing required properties'):
with self.assertRaisesRegex(AssertionError, 'Missing required properties'):
self.img.perform_check(raise_exc=True, save=False)
self.assertEqual(self.img.status, S3FileStatus.Error)
......
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