ServiceBase: validate base_url (#31384)
This commit is contained in:
parent
73848f82f3
commit
e2ffab609d
|
@ -14,6 +14,7 @@ from django.utils.six.moves.urllib.parse import urlparse
|
|||
from django.utils.timezone import now
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.validators import URLValidator
|
||||
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
|
||||
|
@ -71,7 +72,7 @@ class ServiceBase(models.Model):
|
|||
|
||||
title = models.CharField(_('Title'), max_length=50)
|
||||
slug = models.SlugField(_('Slug'))
|
||||
base_url = models.CharField(_('Base URL'), max_length=200)
|
||||
base_url = models.CharField(_('Base URL'), max_length=200, validators=[URLValidator()])
|
||||
secret_key = models.CharField(_('Secret Key'), max_length=60)
|
||||
template_name = models.CharField(_('Template'), max_length=60, blank=True)
|
||||
secondary = models.BooleanField(_('Secondary Service'), default=False)
|
||||
|
|
|
@ -33,3 +33,28 @@ def test_unique_slug():
|
|||
|
||||
assert e.value.messages[0] == u'This slug is already used. It must be unique.'
|
||||
|
||||
|
||||
def test_base_url_field_validator():
|
||||
kwargs = {'title': 'Combo test',
|
||||
'last_operational_success_timestamp': timezone.now(),
|
||||
'last_operational_check_timestamp': timezone.now(),
|
||||
'secret_key': '1nesüper5Cr!eteKAaY~'}
|
||||
cpt = 0 # slugs must be unique
|
||||
|
||||
# accept well formated url
|
||||
for url in ('https://example.com', 'http://example.com'):
|
||||
cpt += 1
|
||||
slug = 'wesh' + str(cpt)
|
||||
combo = Combo(base_url=url, slug=slug ,**kwargs)
|
||||
combo.full_clean()
|
||||
combo.save()
|
||||
assert True # no exception raised
|
||||
|
||||
# reject baddly formated url
|
||||
for url in ('example.com', 'http:/example.com', 'file:///home/me'):
|
||||
cpt += 1
|
||||
slug = 'wesh' + str(cpt)
|
||||
with pytest.raises(ValidationError):
|
||||
combo = Combo(base_url=url, slug='wesh_'+str(cpt), **kwargs)
|
||||
combo.full_clean()
|
||||
combo.save()
|
||||
|
|
Loading…
Reference in New Issue