wcs: fix tracking code with unknown wcs_site (#54195)
gitea-wip/combo/pipeline/head There was a failure building this commit Details
gitea/combo/pipeline/head Build started... Details

This commit is contained in:
Lauréline Guérin 2021-08-12 17:04:58 +02:00
parent 42e11b6cef
commit 7c1821ce35
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
3 changed files with 17 additions and 3 deletions

View File

@ -1032,7 +1032,7 @@ class TrackingCodeInputCell(CellBase):
extra_context = super().get_cell_extra_context(context)
if not self.wcs_site:
self.wcs_site = list(get_wcs_services().keys())[0]
extra_context['url'] = get_wcs_services().get(self.wcs_site).get('url')
extra_context['url'] = (get_wcs_services().get(self.wcs_site) or {}).get('url')
return extra_context

View File

@ -21,7 +21,6 @@ from django.conf import settings
from django.contrib import messages
from django.core.exceptions import DisallowedRedirect, PermissionDenied
from django.http import HttpResponseBadRequest, HttpResponseRedirect, JsonResponse
from django.urls import reverse
from django.utils.http import urlquote
from django.utils.six.moves.urllib import parse as urlparse
from django.utils.translation import ugettext_lazy as _
@ -62,6 +61,8 @@ class TrackingCodeView(View):
raise PermissionDenied('rate limit reached (%s)' % rate_limit)
for wcs_site in wcs_sites:
if not wcs_site:
continue
response = requests.get('/api/code/' + urlquote(code), remote_service=wcs_site, log_errors=False)
if response.status_code == 200 and response.json().get('err') == 0:
return response.json().get('load_url')
@ -73,7 +74,7 @@ class TrackingCodeView(View):
cell = TrackingCodeInputCell.objects.get(id=request.POST['cell'])
except (KeyError, ValueError, TrackingCodeInputCell.DoesNotExist):
return HttpResponseBadRequest('Invalid cell id')
if not 'code' in request.POST:
if 'code' not in request.POST:
return HttpResponseBadRequest('Missing code')
code = request.POST['code']

View File

@ -1904,6 +1904,19 @@ def test_tracking_code_cell(app, nocache):
resp = resp.follow()
assert '<li class="error">The tracking code could not been found.</li>' in resp.text
# unknown wcs_site
cell.wcs_site = 'unknown'
cell.save()
resp = app.get('/')
resp.form['code'] = 'CNPHNTFB'
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
mock_json = mock.Mock(status_code=200)
requests_get.return_value = mock_json
resp = resp.form.submit()
assert len(requests_get.call_args_list) == 0
resp = resp.follow()
assert '<li class="error">The tracking code could not been found.</li>' in resp.text
# simulate cell being displayed on a different site
resp = app.get('/')
resp.form['url'] = 'http://example.org/'