Skip to content

Commit

Permalink
Subprojects: fix form
Browse files Browse the repository at this point in the history
Fixes #7229
  • Loading branch information
stsewd committed Oct 6, 2020
1 parent 1b54393 commit da26dc7
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
21 changes: 15 additions & 6 deletions readthedocs/projects/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,17 +375,26 @@ def clean_parent(self):
return self.project

def clean_child(self):
child = self.cleaned_data['child']
"""
Validate child is a valid subproject.
child.is_valid_as_subproject(
self.project, forms.ValidationError
)
Validation is done on creation only,
when editing users can't change the child.
"""
child = self.cleaned_data['child']
if self.instance.pk is None:
child.is_valid_as_subproject(
self.project, forms.ValidationError
)
return child

def clean_alias(self):
alias = self.cleaned_data['alias']
subproject = self.project.subprojects.filter(
alias=alias).exclude(id=self.instance.pk)
subproject = (
self.project.subprojects
.filter(alias=alias)
.exclude(id=self.instance.pk)
)

if subproject.exists():
raise forms.ValidationError(
Expand Down
61 changes: 59 additions & 2 deletions readthedocs/rtd_tests/tests/test_subprojects.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
import django_dynamic_fixture as fixture
from unittest import mock

import django_dynamic_fixture as fixture
from django.contrib.auth.models import User
from django.test import TestCase
from django.test.utils import override_settings
Expand All @@ -12,6 +12,13 @@

class SubprojectFormTests(TestCase):

def setUp(self):
self.user = fixture.get(User)
self.project = fixture.get(Project, users=[self.user])
self.subproject = fixture.get(Project, users=[self.user])
self.project.add_subproject(self.subproject, 'subproject')
self.relation = self.subproject.superprojects.first()

def test_empty_child(self):
user = fixture.get(User)
project = fixture.get(Project, slug='mainproject')
Expand Down Expand Up @@ -254,3 +261,53 @@ def test_edit_only_lists_instance_project_in_child_choices(self):
[proj_id for (proj_id, __) in form.fields['child'].choices],
['', relation.child.id],
)

def test_change_alias(self):
subproject_2 = fixture.get(Project, users=[self.user])
self.project.add_subproject(subproject_2, 'another-subproject')

relation = subproject_2.superprojects.first()

# Change alias to an existing alias
form = ProjectRelationshipForm(
{
'child': subproject_2.id,
'alias': 'subproject'
},
instance=relation,
project=self.project,
user=self.user,
)
self.assertFalse(form.is_valid())
error_msg = 'A subproject with this alias already exists'
self.assertDictEqual(form.errors, {'alias': [error_msg]})

# Change alias to a new alias
form = ProjectRelationshipForm(
{
'child': subproject_2.id,
'alias': 'other-subproject'
},
instance=relation,
project=self.project,
user=self.user,
)
self.assertTrue(form.is_valid())
form.save()
relation.refresh_from_db()
self.assertEqual(relation.alias, 'other-subproject')

def test_change_alias_to_same_alias(self):
form = ProjectRelationshipForm(
{
'child': self.subproject.id,
'alias': 'subproject'
},
instance=self.relation,
project=self.project,
user=self.user,
)
self.assertTrue(form.is_valid())
form.save()
self.relation.refresh_from_db()
self.assertEqual(self.relation.alias, 'subproject')

0 comments on commit da26dc7

Please sign in to comment.