Refs #18620 -- Refactored ContentTypes view tests to group related field test cases.

This commit is contained in:
Paulo 2018-05-14 13:24:11 -04:00 committed by Tim Graham
parent d14850e525
commit fa679db1ff
1 changed files with 23 additions and 15 deletions

View File

@ -96,6 +96,18 @@ class ContentTypesViewsTests(TestCase):
response = self.client.get(short_url) response = self.client.get(short_url)
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
@override_settings(ROOT_URLCONF='contenttypes_tests.urls')
class ContentTypesViewsSiteRelTests(TestCase):
def setUp(self):
Site.objects.clear_cache()
@classmethod
def setUpTestData(cls):
cls.site_2 = Site.objects.create(domain='example2.com', name='example2.com')
cls.site_3 = Site.objects.create(domain='example3.com', name='example3.com')
@mock.patch('django.apps.apps.get_model') @mock.patch('django.apps.apps.get_model')
def test_shortcut_view_with_null_site_fk(self, get_model): def test_shortcut_view_with_null_site_fk(self, get_model):
""" """
@ -106,7 +118,7 @@ class ContentTypesViewsTests(TestCase):
obj = ModelWithNullFKToSite.objects.create(title='title') obj = ModelWithNullFKToSite.objects.create(title='title')
url = '/shortcut/%s/%s/' % (ContentType.objects.get_for_model(ModelWithNullFKToSite).id, obj.pk) url = '/shortcut/%s/%s/' % (ContentType.objects.get_for_model(ModelWithNullFKToSite).id, obj.pk)
response = self.client.get(url) response = self.client.get(url)
expected_url = 'http://testserver%s' % obj.get_absolute_url() expected_url = 'http://example.com%s' % obj.get_absolute_url()
self.assertRedirects(response, expected_url, fetch_redirect_response=False) self.assertRedirects(response, expected_url, fetch_redirect_response=False)
@mock.patch('django.apps.apps.get_model') @mock.patch('django.apps.apps.get_model')
@ -120,21 +132,17 @@ class ContentTypesViewsTests(TestCase):
# get_current_site() will lookup a Site object, so these must match the # get_current_site() will lookup a Site object, so these must match the
# domains in the MockSite model. # 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.objects.bulk_create([
MockSite(pk=1, domain='example1.com'), MockSite(pk=1, domain='example.com'),
MockSite(pk=2, domain='example2.com'), MockSite(pk=self.site_2.pk, domain=self.site_2.domain),
MockSite(pk=3, domain='example3.com'), MockSite(pk=self.site_3.pk, domain=self.site_3.domain),
]) ])
ct = ContentType.objects.get_for_model(ModelWithM2MToSite) ct = ContentType.objects.get_for_model(ModelWithM2MToSite)
site_3_obj = ModelWithM2MToSite.objects.create(title='Not Linked to Current Site') site_3_obj = ModelWithM2MToSite.objects.create(title='Not Linked to Current Site')
site_3_obj.sites.add(MockSite.objects.get(pk=3)) site_3_obj.sites.add(MockSite.objects.get(pk=self.site_3.pk))
expected_url = 'http://example3.com%s' % site_3_obj.get_absolute_url() expected_url = 'http://%s%s' % (self.site_3.domain, site_3_obj.get_absolute_url())
with self.settings(SITE_ID=2): with self.settings(SITE_ID=self.site_2.pk):
# Redirects to the domain of the first Site found in the m2m # Redirects to the domain of the first Site found in the m2m
# relationship (ordering is arbitrary). # relationship (ordering is arbitrary).
response = self.client.get('/shortcut/%s/%s/' % (ct.pk, site_3_obj.pk)) response = self.client.get('/shortcut/%s/%s/' % (ct.pk, site_3_obj.pk))
@ -143,18 +151,18 @@ class ContentTypesViewsTests(TestCase):
obj_with_sites = ModelWithM2MToSite.objects.create(title='Linked to Current Site') obj_with_sites = ModelWithM2MToSite.objects.create(title='Linked to Current Site')
obj_with_sites.sites.set(MockSite.objects.all()) obj_with_sites.sites.set(MockSite.objects.all())
shortcut_url = '/shortcut/%s/%s/' % (ct.pk, obj_with_sites.pk) shortcut_url = '/shortcut/%s/%s/' % (ct.pk, obj_with_sites.pk)
expected_url = 'http://example2.com%s' % obj_with_sites.get_absolute_url() expected_url = 'http://%s%s' % (self.site_2.domain, obj_with_sites.get_absolute_url())
with self.settings(SITE_ID=2): with self.settings(SITE_ID=self.site_2.pk):
# Redirects to the domain of the Site matching the current site's # Redirects to the domain of the Site matching the current site's
# domain. # domain.
response = self.client.get(shortcut_url) response = self.client.get(shortcut_url)
self.assertRedirects(response, expected_url, fetch_redirect_response=False) self.assertRedirects(response, expected_url, fetch_redirect_response=False)
with self.settings(SITE_ID=None, ALLOWED_HOSTS=['example2.com']): with self.settings(SITE_ID=None, ALLOWED_HOSTS=[self.site_2.domain]):
# Redirects to the domain of the Site matching the request's host # Redirects to the domain of the Site matching the request's host
# header. # header.
response = self.client.get(shortcut_url, SERVER_NAME='example2.com') response = self.client.get(shortcut_url, SERVER_NAME=self.site_2.domain)
self.assertRedirects(response, expected_url, fetch_redirect_response=False) self.assertRedirects(response, expected_url, fetch_redirect_response=False)