datasources: check template on form validation (#33350) #599
|
@ -263,6 +263,35 @@ def test_data_sources_type_options(pub):
|
|||
]
|
||||
|
||||
|
||||
def test_data_sources_type_json(pub):
|
||||
create_superuser(pub)
|
||||
NamedDataSource.wipe()
|
||||
|
||||
data_source = NamedDataSource(name='foobar')
|
||||
data_source.store()
|
||||
app = login(get_app(pub))
|
||||
resp = app.get('/backoffice/settings/data-sources/%s/edit' % data_source.id)
|
||||
resp.form['data_source$type'] = 'json'
|
||||
resp.form['data_source$value'] = 'plop' # invalid URL
|
||||
resp = resp.form.submit('submit')
|
||||
assert 'Value must be a full URL.' in resp.text
|
||||
resp.form['data_source$value'] = 'http://localhost/'
|
||||
resp = resp.form.submit('submit')
|
||||
assert 'Value must be a full URL.' not in resp.text
|
||||
data_source.refresh_from_storage()
|
||||
assert data_source.data_source == {'type': 'json', 'value': 'http://localhost/'}
|
||||
|
||||
resp = app.get('/backoffice/settings/data-sources/%s/edit' % data_source.id)
|
||||
resp.form['data_source$value'] = '{{ [passerelle_url }}/test' # invalid template
|
||||
resp = resp.form.submit('submit')
|
||||
assert 'syntax error in Django template' in resp.text
|
||||
resp.form['data_source$value'] = '{{ passerelle_url }}/test'
|
||||
resp = resp.form.submit('submit')
|
||||
assert 'syntax error in Django template' not in resp.text
|
||||
data_source.refresh_from_storage()
|
||||
assert data_source.data_source == {'type': 'json', 'value': '{{ passerelle_url }}/test'}
|
||||
|
||||
|
||||
def test_data_sources_type_options_jsonp(pub):
|
||||
create_superuser(pub)
|
||||
|
||||
|
|
|
@ -33,7 +33,14 @@ from .categories import DataSourceCategory
|
|||
from .qommon import _, get_logger, misc, pgettext
|
||||
from .qommon.afterjobs import AfterJob
|
||||
from .qommon.cron import CronJob
|
||||
from .qommon.form import CompositeWidget, OptGroup, SingleSelectWidget, StringWidget
|
||||
from .qommon.form import (
|
||||
CompositeWidget,
|
||||
ComputedExpressionWidget,
|
||||
OptGroup,
|
||||
SingleSelectWidget,
|
||||
StringWidget,
|
||||
ValidationError,
|
||||
)
|
||||
from .qommon.humantime import seconds2humanduration
|
||||
from .qommon.misc import get_variadic_url
|
||||
from .qommon.publisher import get_publisher_class
|
||||
|
@ -218,10 +225,16 @@ class DataSourceSelectionWidget(CompositeWidget):
|
|||
|
||||
if values.get('type') in ('json', 'jsonp', 'geojson'):
|
||||
url = values.get('value') or ''
|
||||
if url and not Template.is_template_string(url):
|
||||
parsed = urllib.parse.urlparse(url)
|
||||
if not (parsed.scheme and parsed.netloc):
|
||||
self.error = _('Value must be a full URL.')
|
||||
if url:
|
||||
if Template.is_template_string(url):
|
||||
try:
|
||||
ComputedExpressionWidget.validate_template(url)
|
||||
except ValidationError as e:
|
||||
self.error = str(e)
|
||||
else:
|
||||
parsed = urllib.parse.urlparse(url)
|
||||
if not (parsed.scheme and parsed.netloc):
|
||||
self.error = _('Value must be a full URL.')
|
||||
|
||||
if values.get('type', '') in ('none', ''):
|
||||
values = None
|
||||
|
|
Loading…
Reference in New Issue