From bd7fa2ae10f4cb41ec3a9ba6651977f142fc3c35 Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Thu, 18 Oct 2018 16:50:51 +0200 Subject: [PATCH 1/2] Validate the slug generated is valid before importing a project --- readthedocs/projects/forms.py | 6 ++++++ readthedocs/projects/models.py | 2 +- readthedocs/rtd_tests/tests/test_project_forms.py | 7 +++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/readthedocs/projects/forms.py b/readthedocs/projects/forms.py index 5d0ab4c9229..73a30f85f8f 100644 --- a/readthedocs/projects/forms.py +++ b/readthedocs/projects/forms.py @@ -118,6 +118,12 @@ def clean_name(self): if Project.objects.filter(slug=potential_slug).exists(): raise forms.ValidationError( _('Invalid project name, a project already exists with that name')) # yapf: disable # noqa + if not potential_slug: + # Check the generated slug won't be empty + raise forms.ValidationError( + _('Invalid project name'), + ) + return name def clean_remote_repository(self): diff --git a/readthedocs/projects/models.py b/readthedocs/projects/models.py index 66f0cd97601..9fb780f2996 100644 --- a/readthedocs/projects/models.py +++ b/readthedocs/projects/models.py @@ -316,7 +316,7 @@ def save(self, *args, **kwargs): # pylint: disable=arguments-differ if not self.slug: # Subdomains can't have underscores in them. self.slug = slugify(self.name) - if self.slug == '': + if not self.slug: raise Exception(_('Model must have slug')) if self.documentation_type == 'auto': # This used to determine the type and automatically set the diff --git a/readthedocs/rtd_tests/tests/test_project_forms.py b/readthedocs/rtd_tests/tests/test_project_forms.py index 70167458a9a..0f78b365e15 100644 --- a/readthedocs/rtd_tests/tests/test_project_forms.py +++ b/readthedocs/rtd_tests/tests/test_project_forms.py @@ -93,6 +93,13 @@ def test_import_repo_url(self): form = ProjectBasicsForm(initial) self.assertEqual(form.is_valid(), valid, msg=url) + def test_empty_slug(self): + initial = { + 'name': "''", + } + form = ProjectBasicsForm(initial) + self.assertFalse(form.is_valid()) + class TestTranslationForms(TestCase): From 2726df4822238fe4647509b256c0af9df21545b5 Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Thu, 18 Oct 2018 17:50:10 +0200 Subject: [PATCH 2/2] Make test more robust --- readthedocs/rtd_tests/tests/test_project_forms.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/readthedocs/rtd_tests/tests/test_project_forms.py b/readthedocs/rtd_tests/tests/test_project_forms.py index 0f78b365e15..ff9ce834d19 100644 --- a/readthedocs/rtd_tests/tests/test_project_forms.py +++ b/readthedocs/rtd_tests/tests/test_project_forms.py @@ -96,9 +96,12 @@ def test_import_repo_url(self): def test_empty_slug(self): initial = { 'name': "''", + 'repo_type': 'git', + 'repo': 'https://github.com/user/repository', } form = ProjectBasicsForm(initial) self.assertFalse(form.is_valid()) + self.assertIn('name', form.errors) class TestTranslationForms(TestCase):