Skip to content

Validate the slug generated is valid before importing a project #4780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions readthedocs/projects/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions readthedocs/rtd_tests/tests/test_project_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ def test_import_repo_url(self):
form = ProjectBasicsForm(initial)
self.assertEqual(form.is_valid(), valid, msg=url)

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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should check that the error really comes from name

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, you are right.

In fact, I forget to add the rest of the fields in the initial data.


class TestTranslationForms(TestCase):

Expand Down