diff options
| author | dani poni <459630@gmail.com> | 2016-04-16 09:16:48 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-04-16 17:27:44 -0400 |
| commit | d29d11b026e8acea4778dd596aaf82d55b11f44d (patch) | |
| tree | 0b1b3a8c7f39b02fa44b3ff227b959b3fa8394fa /tests/contenttypes_tests | |
| parent | e494b9ffb60215bb303e81049bd67e8aa36a504d (diff) | |
Fixed #26085 -- Fixed contenttypes shortcut() view crash with a null fk to Site.
Thanks Fabien Schwob for the initial patch.
Diffstat (limited to 'tests/contenttypes_tests')
| -rw-r--r-- | tests/contenttypes_tests/models.py | 22 | ||||
| -rw-r--r-- | tests/contenttypes_tests/tests.py | 22 |
2 files changed, 42 insertions, 2 deletions
diff --git a/tests/contenttypes_tests/models.py b/tests/contenttypes_tests/models.py index a302fe97f0..4b9d3d31e6 100644 --- a/tests/contenttypes_tests/models.py +++ b/tests/contenttypes_tests/models.py @@ -4,12 +4,22 @@ from django.contrib.contenttypes.fields import ( GenericForeignKey, GenericRelation, ) from django.contrib.contenttypes.models import ContentType +from django.contrib.sites.models import SiteManager from django.db import models from django.utils.encoding import python_2_unicode_compatible from django.utils.http import urlquote @python_2_unicode_compatible +class Site(models.Model): + domain = models.CharField(max_length=100) + objects = SiteManager() + + def __str__(self): + return self.domain + + +@python_2_unicode_compatible class Author(models.Model): name = models.CharField(max_length=100) @@ -115,3 +125,15 @@ class Post(models.Model): def __str__(self): return self.title + + +@python_2_unicode_compatible +class ModelWithNullFKToSite(models.Model): + title = models.CharField(max_length=200) + site = models.ForeignKey(Site, null=True, on_delete=models.CASCADE) + + def __str__(self): + return self.title + + def get_absolute_url(self): + return '/title/%s/' % urlquote(self.title) diff --git a/tests/contenttypes_tests/tests.py b/tests/contenttypes_tests/tests.py index c2377bf82e..a494df0340 100644 --- a/tests/contenttypes_tests/tests.py +++ b/tests/contenttypes_tests/tests.py @@ -12,11 +12,14 @@ from django.contrib.contenttypes.models import ContentType from django.contrib.sites.models import Site from django.core import checks from django.db import connections, models -from django.test import SimpleTestCase, TestCase, override_settings +from django.test import SimpleTestCase, TestCase, mock, override_settings from django.test.utils import captured_stdout, isolate_apps from django.utils.encoding import force_str, force_text -from .models import Article, Author, SchemeIncludedURL +from .models import ( + Article, Author, ModelWithNullFKToSite, SchemeIncludedURL, + Site as MockSite, +) @override_settings(ROOT_URLCONF='contenttypes_tests.urls') @@ -94,6 +97,21 @@ class ContentTypesViewsTests(TestCase): response = self.client.get(short_url) self.assertEqual(response.status_code, 404) + @mock.patch('django.apps.apps.get_model') + def test_shortcut_view_with_null_site_fk(self, get_model): + """ + The shortcut view works if a model's ForeignKey to site is None. + """ + get_model.side_effect = lambda *args, **kwargs: MockSite if args[0] == 'sites.Site' else ModelWithNullFKToSite + + obj = ModelWithNullFKToSite.objects.create(title='title') + url = '/shortcut/%s/%s/' % (ContentType.objects.get_for_model(ModelWithNullFKToSite).id, obj.pk) + response = self.client.get(url) + self.assertRedirects( + response, '%s' % obj.get_absolute_url(), + fetch_redirect_response=False, + ) + def test_create_contenttype_on_the_spot(self): """ Make sure ContentTypeManager.get_for_model creates the corresponding |
