Skip to content

Commit 3c581d8

Browse files
humitosagjohnson
authored andcommitted
Make resolve_domain work when a project is subproject of itself (#3962)
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.
1 parent 0bf1737 commit 3c581d8

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

readthedocs/core/resolver.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ def _get_canonical_project(self, project):
164164
relation = project.superprojects.first()
165165
if project.main_language_project:
166166
return self._get_canonical_project(project.main_language_project)
167-
elif relation:
167+
# If ``relation.parent == project`` means that the project has an
168+
# inconsistent relationship and was added when this restriction didn't
169+
# exist
170+
elif relation and relation.parent != project:
168171
return self._get_canonical_project(relation.parent)
169172
return project
170173

readthedocs/rtd_tests/tests/test_resolver.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,27 @@ def test_domain_resolver_subproject(self):
291291
url = resolve_domain(project=self.subproject)
292292
self.assertEqual(url, 'pip.readthedocs.org')
293293

294+
@override_settings(PRODUCTION_DOMAIN='readthedocs.org')
295+
def test_domain_resolver_subproject_itself(self):
296+
"""
297+
Test inconsistent project/subproject relationship.
298+
299+
If a project is subproject of itself (inconsistent relationship) we
300+
still resolves the proper domain.
301+
"""
302+
# remove all possible subproject relationships
303+
self.pip.subprojects.all().delete()
304+
305+
# add the project as subproject of itself
306+
self.pip.add_subproject(self.pip)
307+
308+
with override_settings(USE_SUBDOMAIN=False):
309+
url = resolve_domain(project=self.pip)
310+
self.assertEqual(url, 'readthedocs.org')
311+
with override_settings(USE_SUBDOMAIN=True):
312+
url = resolve_domain(project=self.pip)
313+
self.assertEqual(url, 'pip.readthedocs.org')
314+
294315
@override_settings(PRODUCTION_DOMAIN='readthedocs.org')
295316
def test_domain_resolver_translation(self):
296317
with override_settings(USE_SUBDOMAIN=False):

0 commit comments

Comments
 (0)