Skip to content

Commit

Permalink
Make resolve_domain work when a project is subproject of itself (#3962
Browse files Browse the repository at this point in the history
)

This is an invalid db state since this relationship is not possible,
but there are some old project that has this relationship and we want
to avoid breaking the code for this cases.
  • Loading branch information
humitos authored and agjohnson committed Apr 18, 2018
1 parent 0bf1737 commit 3c581d8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 4 additions & 1 deletion readthedocs/core/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ def _get_canonical_project(self, project):
relation = project.superprojects.first()
if project.main_language_project:
return self._get_canonical_project(project.main_language_project)
elif relation:
# If ``relation.parent == project`` means that the project has an
# inconsistent relationship and was added when this restriction didn't
# exist
elif relation and relation.parent != project:
return self._get_canonical_project(relation.parent)
return project

Expand Down
21 changes: 21 additions & 0 deletions readthedocs/rtd_tests/tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,27 @@ def test_domain_resolver_subproject(self):
url = resolve_domain(project=self.subproject)
self.assertEqual(url, 'pip.readthedocs.org')

@override_settings(PRODUCTION_DOMAIN='readthedocs.org')
def test_domain_resolver_subproject_itself(self):
"""
Test inconsistent project/subproject relationship.
If a project is subproject of itself (inconsistent relationship) we
still resolves the proper domain.
"""
# remove all possible subproject relationships
self.pip.subprojects.all().delete()

# add the project as subproject of itself
self.pip.add_subproject(self.pip)

with override_settings(USE_SUBDOMAIN=False):
url = resolve_domain(project=self.pip)
self.assertEqual(url, 'readthedocs.org')
with override_settings(USE_SUBDOMAIN=True):
url = resolve_domain(project=self.pip)
self.assertEqual(url, 'pip.readthedocs.org')

@override_settings(PRODUCTION_DOMAIN='readthedocs.org')
def test_domain_resolver_translation(self):
with override_settings(USE_SUBDOMAIN=False):
Expand Down

0 comments on commit 3c581d8

Please sign in to comment.