Skip to content
Snippets Groups Projects
Commit 69b4ca4b authored by Bastien Abadie's avatar Bastien Abadie
Browse files

Merge branch 'image-check-json' into 'master'

Handle non-JSON responses on image check

Closes #274

See merge request !787
parents 0c890527 b705301d
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 ...@@ -6,6 +6,7 @@ from django.db.models.functions import Concat, Substr
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.utils.text import slugify from django.utils.text import slugify
from enumfields import EnumField from enumfields import EnumField
from json import JSONDecodeError
from arkindex.images.managers import ImageServerManager from arkindex.images.managers import ImageServerManager
from arkindex.project.aws import S3FileMixin, S3FileStatus from arkindex.project.aws import S3FileMixin, S3FileStatus
from arkindex.project.fields import StripSlashURLField, LStripTextField, MD5HashField from arkindex.project.fields import StripSlashURLField, LStripTextField, MD5HashField
...@@ -221,7 +222,11 @@ class Image(S3FileMixin, IndexableModel): ...@@ -221,7 +222,11 @@ class Image(S3FileMixin, IndexableModel):
# Load info # Load info
resp = requests.get(info_url, timeout=15, allow_redirects=True) resp = requests.get(info_url, timeout=15, allow_redirects=True)
resp.raise_for_status() 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: except Exception as e:
logger.warning('Image check failed: {}'.format(str(e))) logger.warning('Image check failed: {}'.format(str(e)))
self.status = S3FileStatus.Error self.status = S3FileStatus.Error
......
...@@ -23,10 +23,32 @@ class TestImagePerformCheck(FixtureTestCase): ...@@ -23,10 +23,32 @@ class TestImagePerformCheck(FixtureTestCase):
self.img.perform_check(raise_exc=True, save=False) self.img.perform_check(raise_exc=True, save=False)
self.assertEqual(self.img.status, S3FileStatus.Error) 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 @responses.activate
def test_required_items(self): 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 = { base_payload = {
'@id': 'http://server/img1', '@id': 'http://server/img1',
...@@ -38,7 +60,7 @@ class TestImagePerformCheck(FixtureTestCase): ...@@ -38,7 +60,7 @@ class TestImagePerformCheck(FixtureTestCase):
payload = base_payload.copy() payload = base_payload.copy()
del payload[key] del payload[key]
responses.add(responses.GET, 'http://server/img1/info.json', json=payload) 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.img.perform_check(raise_exc=True, save=False)
self.assertEqual(self.img.status, S3FileStatus.Error) 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