environment: test slugs are unique (#9154)

test if slugs are unique between different services
This commit is contained in:
Jean-Baptiste Jaillet 2016-07-01 12:16:54 +02:00
parent 441507ec0e
commit 5c37a4a6bc
4 changed files with 45 additions and 2 deletions

View File

@ -10,6 +10,7 @@ from .models import (Authentic, Wcs, Passerelle, Variable, Combo, Fargo, Welco,
EXCLUDED_FIELDS = ('last_operational_check_timestamp',
'last_operational_success_timestamp', 'secret_key', 'secondary')
class BaseForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
choices = self.get_template_choices()

View File

@ -27,6 +27,7 @@ from django.core.management.base import BaseCommand, CommandError
from django.core.management import call_command
from django.db import connection
from django.db.models import Max
from django.core.exceptions import ValidationError
from hobo.agent.common.management.commands.hobo_deploy import (
Command as HoboDeployCommand)
@ -130,6 +131,11 @@ class Command(BaseCommand):
setattr(obj, attr, locals().get(attr))
must_save = True
if must_save:
try:
obj.clean()
except ValidationError as e:
raise CommandError(str(e))
obj.save()
self.must_notify = True
variables = variables or {}

View File

@ -13,7 +13,7 @@ from django.core.exceptions import ValidationError
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from .utils import Zone, get_installed_services_dict
from .utils import Zone, get_installed_services
from .mandayejs_app_settings import APP_SETTINGS_CLASSES, DEFAULT_APP_SETTINGS
SECRET_CHARS = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
@ -117,6 +117,12 @@ class ServiceBase(models.Model):
def name(self):
return self.title
def clean(self, *args, **kwargs):
for service in get_installed_services():
if service.slug == self.slug and service.id != self.id:
raise ValidationError(_('This slug is already used. It must be unique.'))
return super(ServiceBase, self).clean(*args, **kwargs)
def save(self, *args, **kwargs):
self.base_url = self.base_url.strip().lower()
if not self.base_url.endswith('/'):

View File

@ -1,5 +1,35 @@
from hobo.environment.models import AVAILABLE_SERVICES
# -*- coding: utf-8 -*-
import pytest
from django.core.exceptions import ValidationError
from django.utils import timezone
from hobo.environment.models import AVAILABLE_SERVICES, Combo, Passerelle
pytestmark = pytest.mark.django_db
def test_service_id():
for service in AVAILABLE_SERVICES:
assert service.Extra.service_id
def test_unique_slug():
combo = Combo(title='Combo test',
slug='wesh',
last_operational_success_timestamp=timezone.now(),
last_operational_check_timestamp=timezone.now(),
secret_key='1nesüper5Cr!eteKAaY~',
base_url='http://example.com')
combo.save()
passerelle = Passerelle(title='Passerelle test',
slug='wesh',
last_operational_success_timestamp=timezone.now(),
last_operational_check_timestamp=timezone.now(),
secret_key='1nesüper5Cr!eteKAaY~',
base_url='http://example.com')
with pytest.raises(ValidationError) as e:
passerelle.clean()
assert e.value.messages[0] == u'This slug is already used. It must be unique.'