diff --git a/readthedocs/core/resolver.py b/readthedocs/core/resolver.py index 4e9b9a0b0b9..6a131bca332 100644 --- a/readthedocs/core/resolver.py +++ b/readthedocs/core/resolver.py @@ -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 diff --git a/readthedocs/rtd_tests/tests/test_resolver.py b/readthedocs/rtd_tests/tests/test_resolver.py index 28e415c2072..cab0d794025 100644 --- a/readthedocs/rtd_tests/tests/test_resolver.py +++ b/readthedocs/rtd_tests/tests/test_resolver.py @@ -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):