From 823707dd1200578130e73d03571034e4e2095df3 Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Tue, 17 Apr 2018 20:01:25 -0500 Subject: [PATCH] Make `resolve_domain` work when a project is subproject of itself 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. --- readthedocs/core/resolver.py | 5 ++++- readthedocs/rtd_tests/tests/test_resolver.py | 21 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) 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):