wcs: report card info cell as invalid if 404 (#50663)
gitea-wip/combo/pipeline/head Build started... Details
gitea/combo/pipeline/head Build started... Details

This commit is contained in:
Lauréline Guérin 2021-02-05 09:52:18 +01:00
parent 7b690fb0b6
commit 2b5cfec6f9
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
3 changed files with 20 additions and 7 deletions

View File

@ -862,7 +862,7 @@ class WcsCardInfosCell(CardMixin, CellBase):
if self.carddef_reference:
wcs_key, card_slug = self.carddef_reference.split(':')
wcs_site = get_wcs_services().get(wcs_key)
card_schema = get_wcs_json(wcs_site, 'api/cards/%s/@schema' % card_slug)
card_schema = get_wcs_json(wcs_site, 'api/cards/%s/@schema' % card_slug, log_errors='warn')
if not card_schema:
# can not retrieve data, don't report cell as invalid

View File

@ -14,6 +14,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
from django.conf import settings
from requests.exceptions import RequestException
@ -31,7 +33,7 @@ def get_wcs_services():
return settings.KNOWN_SERVICES.get('wcs')
def get_wcs_json(wcs_site, path):
def get_wcs_json(wcs_site, path, log_errors=True):
if wcs_site is None:
# no site specified (probably an import referencing a not yet deployed
# site)
@ -39,9 +41,16 @@ def get_wcs_json(wcs_site, path):
try:
response = requests.get(
path, remote_service=wcs_site, without_user=True,
headers={'accept': 'application/json'})
headers={'accept': 'application/json'},
log_errors=log_errors)
response.raise_for_status()
except RequestException:
except RequestException as e:
if e.response is not None:
try:
# return json if available (on 404 responses by example)
return e.response.json()
except json.JSONDecodeError:
pass
return {'err': 1, 'data': None}
return response.json()

View File

@ -1317,9 +1317,13 @@ def test_card_cell_validity(mock_send):
cell.save()
assert ValidityInfo.objects.exists() is False
mock_send.side_effect = lambda *a, **k: MockedRequestResponse(content=json.dumps({'err': 1, 'err_class': 'Page not found'}))
cell.carddef_reference = 'default:foobar'
cell.save()
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
mock_resp = Response()
mock_resp.json = lambda *a, **k: {'err': 1, 'err_class': 'Page not found'}
mock_resp.status_code = 404
requests_get.return_value = mock_resp
cell.carddef_reference = 'default:foobar'
cell.save()
validity_info = ValidityInfo.objects.latest('pk')
assert validity_info.invalid_reason_code == 'wcs_card_not_found'
assert validity_info.invalid_since is not None