applications: don't fail if wcs is not responding (#86520)
gitea/combo/pipeline/head This commit looks good Details

This commit is contained in:
Lauréline Guérin 2024-02-06 10:35:48 +01:00
parent fd0d9c6fb7
commit 150f6e954f
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
3 changed files with 51 additions and 5 deletions

View File

@ -30,6 +30,7 @@ from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from combo.apps.export_import.models import Application, ApplicationAsyncJob, ApplicationElement
from combo.apps.wcs.utils import WCSError
from combo.data.models import Page, PageSnapshot
from combo.utils.misc import is_portal_agent
@ -166,7 +167,10 @@ class ComponentDependencies(GenericAPIView):
return component
return get_component_bundle_entry(request, component, 0)
dependencies = [dependency_dict(x) for x in component.get_dependencies() if x]
try:
dependencies = [dependency_dict(x) for x in component.get_dependencies() if x]
except WCSError as e:
return Response({'err': 1, 'err_desc': str(e)}, status=400)
return Response({'err': 0, 'data': dependencies})

View File

@ -69,8 +69,12 @@ def get_wcs_json(wcs_site, path, log_errors=True):
# return json if available (on 404 responses by example)
return e.response.json()
except json.JSONDecodeError:
pass
return {'err': 1, 'data': None}
return {
'err': 1,
'err_desc': 'request-error-status-%s' % e.response.status_code,
'data': None,
}
return {'err': 1, 'err_desc': 'request-error', 'data': None}
return response.json()

View File

@ -13,6 +13,8 @@ from django.contrib.auth.models import Group
from django.contrib.contenttypes.models import ContentType
from django.core.files import File
from django.core.files.storage import default_storage
from requests.exceptions import ConnectionError
from requests.models import Response
from combo.apps.export_import.models import Application, ApplicationAsyncJob, ApplicationElement
from combo.apps.family.models import WeeklyAgendaCell
@ -21,7 +23,7 @@ from combo.apps.search.models import SearchCell
from combo.apps.wcs.models import WcsCardCell, WcsCategoryCell, WcsFormCell
from combo.data.models import LinkCell, LinkListCell, Page, PageSnapshot, TextCell
from .wcs.utils import mocked_requests_send
from .wcs.utils import MockedRequestResponse, mocked_requests_send
pytestmark = pytest.mark.django_db
@ -567,7 +569,7 @@ def test_bundle_check(app, john_doe):
@mock.patch('requests.Session.send', side_effect=mocked_requests_send)
def test_page_dependencies_card_models(mock_send):
def test_page_dependencies_card_models(mock_send, app, john_doe):
page = Page.objects.create(
title='Test',
slug='test',
@ -586,6 +588,42 @@ def test_page_dependencies_card_models(mock_send):
}
assert card_dep in page.get_dependencies()
app.authorization = ('Basic', (john_doe.username, john_doe.username))
with mock.patch('requests.Session.get') as requests_get:
requests_get.side_effect = ConnectionError()
resp = app.get(f'/api/export-import/pages/{page.uuid}/dependencies/', status=400)
assert resp.json['err'] == 1
assert resp.json['err_desc'] == 'Unable to get WCS service (request-error)'
with mock.patch('requests.Session.get') as requests_get:
mock_resp = Response()
mock_resp.status_code = 500
requests_get.return_value = mock_resp
resp = app.get(f'/api/export-import/pages/{page.uuid}/dependencies/', status=400)
assert resp.json['err'] == 1
assert resp.json['err_desc'] == 'Unable to get WCS service (request-error-status-500)'
with mock.patch('requests.Session.get') as requests_get:
mock_resp = Response()
mock_resp.status_code = 404
requests_get.return_value = mock_resp
resp = app.get(f'/api/export-import/pages/{page.uuid}/dependencies/', status=400)
assert resp.json['err'] == 1
assert resp.json['err_desc'] == 'Unable to get WCS service (request-error-status-404)'
with mock.patch('requests.Session.get') as requests_get:
requests_get.return_value = MockedRequestResponse(content=json.dumps({'foo': 'bar'}))
resp = app.get(f'/api/export-import/pages/{page.uuid}/dependencies/', status=400)
assert resp.json['err'] == 1
assert resp.json['err_desc'] == 'Unable to get WCS data'
data = {'data': []}
with mock.patch('requests.Session.get') as requests_get:
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
resp = app.get(f'/api/export-import/pages/{page.uuid}/dependencies/', status=400)
assert resp.json['err'] == 1
assert resp.json['err_desc'] == 'Unable to get WCS data'
page.extra_variables = {}
page.sub_slug = 'foo'
page.save()