diff --git a/tests/contenttypes_tests/models.py b/tests/contenttypes_tests/models.py index 5475c4aade..eeab6296ee 100644 --- a/tests/contenttypes_tests/models.py +++ b/tests/contenttypes_tests/models.py @@ -128,3 +128,11 @@ class ModelWithNullFKToSite(models.Model): def get_absolute_url(self): return '/title/%s/' % quote(self.title) + + +class ModelWithM2MToSite(models.Model): + title = models.CharField(max_length=200) + sites = models.ManyToManyField(Site) + + def get_absolute_url(self): + return '/title/%s/' % quote(self.title) diff --git a/tests/contenttypes_tests/test_views.py b/tests/contenttypes_tests/test_views.py index 1e4da28538..72e87550d0 100644 --- a/tests/contenttypes_tests/test_views.py +++ b/tests/contenttypes_tests/test_views.py @@ -10,7 +10,8 @@ from django.test import TestCase, override_settings from .models import ( Article, Author, FooWithBrokenAbsoluteUrl, FooWithoutUrl, FooWithUrl, - ModelWithNullFKToSite, SchemeIncludedURL, Site as MockSite, + ModelWithM2MToSite, ModelWithNullFKToSite, SchemeIncludedURL, + Site as MockSite, ) @@ -107,6 +108,36 @@ class ContentTypesViewsTests(TestCase): response = self.client.get(url) self.assertRedirects(response, '%s' % obj.get_absolute_url(), fetch_redirect_response=False) + @mock.patch('django.apps.apps.get_model') + def test_shortcut_view_with_site_m2m(self, get_model): + """ + When the object has a ManyToManyField to Site, redirect to the + domain of the first site found in the m2m relationship. + """ + get_model.side_effect = lambda *args, **kwargs: MockSite if args[0] == 'sites.Site' else ModelWithM2MToSite + + # get_current_site() will lookup a Site object, so these must match the + # domains in the MockSite model. + Site.objects.bulk_create([ + Site(domain='example2.com', name='example2.com'), + Site(domain='example3.com', name='example3.com'), + ]) + MockSite.objects.bulk_create([ + MockSite(pk=1, domain='example1.com'), + MockSite(pk=2, domain='example2.com'), + MockSite(pk=3, domain='example3.com'), + ]) + ct = ContentType.objects.get_for_model(ModelWithM2MToSite) + site_3_obj = ModelWithM2MToSite.objects.create(title='Not Linked to Current Site') + site_3_obj.sites.add(MockSite.objects.get(pk=3)) + expected_url = 'http://example3.com%s' % site_3_obj.get_absolute_url() + + with self.settings(SITE_ID=2): + # Redirects to the domain of the first Site found in the m2m + # relationship (ordering is arbitrary). + response = self.client.get('/shortcut/%s/%s/' % (ct.pk, site_3_obj.pk)) + self.assertRedirects(response, expected_url, fetch_redirect_response=False) + class ShortcutViewTests(TestCase):