Skip to content

Commit bd7fa2a

Browse files
committed
Validate the slug generated is valid before importing a project
1 parent df85fef commit bd7fa2a

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

readthedocs/projects/forms.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ def clean_name(self):
118118
if Project.objects.filter(slug=potential_slug).exists():
119119
raise forms.ValidationError(
120120
_('Invalid project name, a project already exists with that name')) # yapf: disable # noqa
121+
if not potential_slug:
122+
# Check the generated slug won't be empty
123+
raise forms.ValidationError(
124+
_('Invalid project name'),
125+
)
126+
121127
return name
122128

123129
def clean_remote_repository(self):

readthedocs/projects/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def save(self, *args, **kwargs): # pylint: disable=arguments-differ
316316
if not self.slug:
317317
# Subdomains can't have underscores in them.
318318
self.slug = slugify(self.name)
319-
if self.slug == '':
319+
if not self.slug:
320320
raise Exception(_('Model must have slug'))
321321
if self.documentation_type == 'auto':
322322
# This used to determine the type and automatically set the

readthedocs/rtd_tests/tests/test_project_forms.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ def test_import_repo_url(self):
9393
form = ProjectBasicsForm(initial)
9494
self.assertEqual(form.is_valid(), valid, msg=url)
9595

96+
def test_empty_slug(self):
97+
initial = {
98+
'name': "''",
99+
}
100+
form = ProjectBasicsForm(initial)
101+
self.assertFalse(form.is_valid())
102+
96103

97104
class TestTranslationForms(TestCase):
98105

0 commit comments

Comments
 (0)