Projet

Général

Profil

0001-environment-Service-title-unicity-35392.patch

Lauréline Guérin, 04 février 2020 10:40

Télécharger (3,67 ko)

Voir les différences:

Subject: [PATCH] environment: Service title unicity (#35392)

 hobo/environment/models.py |  6 +++++-
 tests/test_environment.py  | 35 ++++++++++++++++++++++++++++++-----
 2 files changed, 35 insertions(+), 6 deletions(-)
hobo/environment/models.py
163 163

  
164 164
    def clean(self, *args, **kwargs):
165 165
        for service in get_installed_services():
166
            if service.slug == self.slug and service.id != self.id:
166
            if self.pk == service.pk:
167
                continue
168
            if service.slug == self.slug:
167 169
                raise ValidationError(_('This slug is already used. It must be unique.'))
170
            if service.title == self.title:
171
                raise ValidationError(_('This title is already used. It must be unique.'))
168 172
        return super(ServiceBase, self).clean(*args, **kwargs)
169 173

  
170 174
    def save(self, *args, **kwargs):
tests/test_environment.py
1 1
# -*- coding: utf-8 -*-
2 2
import pytest
3 3

  
4
from django.contrib.auth.models import User
5 4
from django.core.exceptions import ValidationError
6 5
from django.utils import timezone
7 6

  
......
47 46
    assert e.value.messages[0] == u'This slug is already used. It must be unique.'
48 47

  
49 48

  
49
def test_unique_title():
50
    Combo.objects.create(
51
        title='Combo test',
52
        slug='bar',
53
        last_operational_success_timestamp=timezone.now(),
54
        last_operational_check_timestamp=timezone.now(),
55
        secret_key='1nesüper5Cr!eteKAaY~',
56
        base_url='http://example.com'
57
    )
58

  
59
    combo = Combo(
60
        title='Combo test',
61
        slug='foo',
62
        last_operational_success_timestamp=timezone.now(),
63
        last_operational_check_timestamp=timezone.now(),
64
        secret_key='1nesüper5Cr!eteKAaY~',
65
        base_url='http://example.com'
66
    )
67

  
68
    with pytest.raises(ValidationError) as e:
69
        combo.clean()
70

  
71
    assert e.value.messages[0] == u'This title is already used. It must be unique.'
72

  
73

  
50 74
def test_base_url_field_validator():
51
    kwargs = {'title': 'Combo test',
52
              'last_operational_success_timestamp': timezone.now(),
75
    kwargs = {'last_operational_success_timestamp': timezone.now(),
53 76
              'last_operational_check_timestamp': timezone.now(),
54 77
              'secret_key': '1nesüper5Cr!eteKAaY~'}
55 78
    cpt = 0  # slugs must be unique
......
58 81
    for url in ('https://example.com', 'http://example.com'):
59 82
        cpt += 1
60 83
        slug = 'wesh' + str(cpt)
61
        combo = Combo(base_url=url, slug=slug ,**kwargs)
84
        title = 'Combo test ' + str(cpt)
85
        combo = Combo(base_url=url, slug=slug, title=title, **kwargs)
62 86
        combo.full_clean()
63 87
        combo.save()
64 88
        assert True  # no exception raised
......
67 91
    for url in ('example.com', 'http:/example.com', 'file:///home/me'):
68 92
        cpt += 1
69 93
        slug = 'wesh' + str(cpt)
94
        title = 'Combo test ' + str(cpt)
70 95
        with pytest.raises(ValidationError):
71
            combo = Combo(base_url=url, slug='wesh_'+str(cpt), **kwargs)
96
            combo = Combo(base_url=url, slug=slug, title=title, **kwargs)
72 97
            combo.full_clean()
73 98
            combo.save()
74 99

  
75
-