Corrected location of some contenttypes_tests.

This commit is contained in:
Tim Graham 2018-06-21 11:50:44 -04:00
parent 238ed313c5
commit b49ed4be47
2 changed files with 61 additions and 83 deletions

View File

@ -1,13 +1,9 @@
from django.contrib.contenttypes.models import ContentType, ContentTypeManager from django.contrib.contenttypes.models import ContentType, ContentTypeManager
from django.contrib.contenttypes.views import shortcut from django.db import models
from django.contrib.sites.shortcuts import get_current_site
from django.http import Http404, HttpRequest
from django.test import TestCase, override_settings from django.test import TestCase, override_settings
from django.test.utils import isolate_apps
from .models import ( from .models import Author, ConcreteModel, FooWithUrl, ProxyModel
Author, ConcreteModel, FooWithBrokenAbsoluteUrl, FooWithoutUrl, FooWithUrl,
ProxyModel,
)
class ContentTypesTests(TestCase): class ContentTypesTests(TestCase):
@ -93,6 +89,20 @@ class ContentTypesTests(TestCase):
FooWithUrl: ContentType.objects.get_for_model(FooWithUrl), FooWithUrl: ContentType.objects.get_for_model(FooWithUrl),
}) })
@isolate_apps('contenttypes_tests')
def test_get_for_model_create_contenttype(self):
"""
ContentTypeManager.get_for_model() creates the corresponding content
type if it doesn't exist in the database.
"""
class ModelCreatedOnTheFly(models.Model):
name = models.CharField()
ct = ContentType.objects.get_for_model(ModelCreatedOnTheFly)
self.assertEqual(ct.app_label, 'contenttypes_tests')
self.assertEqual(ct.model, 'modelcreatedonthefly')
self.assertEqual(str(ct), 'modelcreatedonthefly')
def test_get_for_concrete_model(self): def test_get_for_concrete_model(self):
""" """
Make sure the `for_concrete_model` kwarg correctly works Make sure the `for_concrete_model` kwarg correctly works
@ -172,64 +182,6 @@ class ContentTypesTests(TestCase):
with self.assertNumQueries(0): with self.assertNumQueries(0):
other_manager.get_for_model(ContentType) other_manager.get_for_model(ContentType)
@override_settings(ALLOWED_HOSTS=['example.com'])
def test_shortcut_view(self):
"""
The shortcut view (used for the admin "view on site" functionality)
returns a complete URL regardless of whether the sites framework is
installed.
"""
request = HttpRequest()
request.META = {
"SERVER_NAME": "Example.com",
"SERVER_PORT": "80",
}
user_ct = ContentType.objects.get_for_model(FooWithUrl)
obj = FooWithUrl.objects.create(name="john")
with self.modify_settings(INSTALLED_APPS={'append': 'django.contrib.sites'}):
response = shortcut(request, user_ct.id, obj.id)
self.assertEqual(
"http://%s/users/john/" % get_current_site(request).domain,
response._headers.get("location")[1]
)
with self.modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'}):
response = shortcut(request, user_ct.id, obj.id)
self.assertEqual("http://Example.com/users/john/", response._headers.get("location")[1])
def test_shortcut_view_without_get_absolute_url(self):
"""
The shortcut view (used for the admin "view on site" functionality)
returns 404 when get_absolute_url is not defined.
"""
request = HttpRequest()
request.META = {
"SERVER_NAME": "Example.com",
"SERVER_PORT": "80",
}
user_ct = ContentType.objects.get_for_model(FooWithoutUrl)
obj = FooWithoutUrl.objects.create(name="john")
with self.assertRaises(Http404):
shortcut(request, user_ct.id, obj.id)
def test_shortcut_view_with_broken_get_absolute_url(self):
"""
The shortcut view does not catch an AttributeError raised by
the model's get_absolute_url() method (#8997).
"""
request = HttpRequest()
request.META = {
"SERVER_NAME": "Example.com",
"SERVER_PORT": "80",
}
user_ct = ContentType.objects.get_for_model(FooWithBrokenAbsoluteUrl)
obj = FooWithBrokenAbsoluteUrl.objects.create(name="john")
with self.assertRaises(AttributeError):
shortcut(request, user_ct.id, obj.id)
def test_missing_model(self): def test_missing_model(self):
""" """
Displaying content types in admin (or anywhere) doesn't break on Displaying content types in admin (or anywhere) doesn't break on

View File

@ -2,14 +2,15 @@ import datetime
from unittest import mock from unittest import mock
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.views import shortcut
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.db import models from django.contrib.sites.shortcuts import get_current_site
from django.http import Http404, HttpRequest
from django.test import TestCase, override_settings from django.test import TestCase, override_settings
from django.test.utils import isolate_apps
from .models import ( from .models import (
Article, Author, ModelWithNullFKToSite, SchemeIncludedURL, Article, Author, FooWithBrokenAbsoluteUrl, FooWithoutUrl, FooWithUrl,
Site as MockSite, ModelWithNullFKToSite, SchemeIncludedURL, Site as MockSite,
) )
@ -106,19 +107,44 @@ class ContentTypesViewsTests(TestCase):
response = self.client.get(url) response = self.client.get(url)
self.assertRedirects(response, '%s' % obj.get_absolute_url(), fetch_redirect_response=False) self.assertRedirects(response, '%s' % obj.get_absolute_url(), fetch_redirect_response=False)
@isolate_apps('contenttypes_tests')
def test_create_contenttype_on_the_spot(self):
"""
ContentTypeManager.get_for_model() creates the corresponding content
type if it doesn't exist in the database.
"""
class ModelCreatedOnTheFly(models.Model):
name = models.CharField()
class Meta: class ShortcutViewTests(TestCase):
verbose_name = 'a model created on the fly'
ct = ContentType.objects.get_for_model(ModelCreatedOnTheFly) def setUp(self):
self.assertEqual(ct.app_label, 'contenttypes_tests') self.request = HttpRequest()
self.assertEqual(ct.model, 'modelcreatedonthefly') self.request.META = {'SERVER_NAME': 'Example.com', 'SERVER_PORT': '80'}
self.assertEqual(str(ct), 'modelcreatedonthefly')
@override_settings(ALLOWED_HOSTS=['example.com'])
def test_not_dependent_on_sites_app(self):
"""
The view returns a complete URL regardless of whether the sites
framework is installed.
"""
user_ct = ContentType.objects.get_for_model(FooWithUrl)
obj = FooWithUrl.objects.create(name='john')
with self.modify_settings(INSTALLED_APPS={'append': 'django.contrib.sites'}):
response = shortcut(self.request, user_ct.id, obj.id)
self.assertEqual(
'http://%s/users/john/' % get_current_site(self.request).domain,
response._headers.get('location')[1]
)
with self.modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'}):
response = shortcut(self.request, user_ct.id, obj.id)
self.assertEqual('http://Example.com/users/john/', response._headers.get('location')[1])
def test_model_without_get_absolute_url(self):
"""The view returns 404 when Model.get_absolute_url() isn't defined."""
user_ct = ContentType.objects.get_for_model(FooWithoutUrl)
obj = FooWithoutUrl.objects.create(name='john')
with self.assertRaises(Http404):
shortcut(self.request, user_ct.id, obj.id)
def test_model_with_broken_get_absolute_url(self):
"""
The view doesn't catch an AttributeError raised by
Model.get_absolute_url() (#8997).
"""
user_ct = ContentType.objects.get_for_model(FooWithBrokenAbsoluteUrl)
obj = FooWithBrokenAbsoluteUrl.objects.create(name='john')
with self.assertRaises(AttributeError):
shortcut(self.request, user_ct.id, obj.id)