Projet

Général

Profil

0001-manager-raise-a-ValidationError-on-duplicated-slug-2.patch

Nicolas Roche, 16 octobre 2019 17:02

Télécharger (2,57 ko)

Voir les différences:

Subject: [PATCH] manager: raise a ValidationError on duplicated slug (#21800)

 passerelle/forms.py   | 12 ++++++++++++
 tests/test_manager.py | 20 ++++++++++++++++++++
 2 files changed, 32 insertions(+)
passerelle/forms.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17
from django.core.exceptions import ValidationError
17 18
from django.utils.text import slugify
19
from django.utils.translation import ugettext_lazy as _
18 20
from django import forms
19 21

  
20 22

  
21 23
class GenericConnectorForm(forms.ModelForm):
24
    def clean_title(self):
25
        title = self.cleaned_data['title']
26
        if not self.instance.slug:
27
            slug = slugify(title)
28
            if self.instance.__class__.objects.filter(slug=slug).exists():
29
                raise ValidationError(
30
                    _("'%(slug)s' slug is already used"),
31
                    params={'slug': slug})
32
        return title
33

  
22 34
    def save(self, commit=True):
23 35
        if not self.instance.slug:
24 36
            self.instance.slug = slugify(self.instance.title)
tests/test_manager.py
77 77
    resp = app.get('/manage/', status=200)
78 78
    assert 'Test Connector' in resp.body
79 79

  
80
def test_add_connector_unique_slug(app, admin_user):
81
    app = login(app)
82
    resp = app.get('/manage/', status=200)
83
    resp = resp.click('Add Connector')
84
    resp = resp.click('Base Adresse Web Service')
85
    resp.forms[0]['title'] = 'Test Connector'
86
    resp.forms[0]['description'] = 'Connector for a simple test'
87
    resp.forms[0]['service_url'] = 'https://api-adresse.data.gouv.fr/'
88
    resp1 = resp.forms[0].submit()
89
    assert resp1.status_int == 302
90

  
91
    resp2 = resp.forms[0].submit()
92
    assert 'There were errors processing your form.' in resp2.body
93
    assert '<div class="error">' in resp2.body
94
    assert 'slug is already used' in resp2.body
95

  
96
    resp.forms[0]['title'] = 'Other Test Connector'
97
    resp2 = resp.forms[0].submit()
98
    assert resp2.status_int == 302
99

  
80 100
def test_visit_connectors(app, admin_user):
81 101
    app = login(app)
82 102
    resp = app.get('/manage/', status=200)
83
-