| 1 |
import datetime |
| 2 |
from unittest import mock |
| 3 |
|
| 4 |
from django.contrib.contenttypes.models import ContentType |
| 5 |
from django.contrib.contenttypes.views import shortcut |
| 6 |
from django.contrib.sites.models import Site |
| 7 |
from django.contrib.sites.shortcuts import get_current_site |
| 8 |
from django.http import Http404, HttpRequest |
| 9 |
from django.test import TestCase, override_settings |
| 10 |
|
| 11 |
from .models import ( # isort:skip |
| 12 |
Article, |
| 13 |
Author, |
| 14 |
FooWithBrokenAbsoluteUrl, |
| 15 |
FooWithoutUrl, |
| 16 |
FooWithUrl, |
| 17 |
ModelWithM2MToSite, |
| 18 |
ModelWithNullFKToSite, |
| 19 |
SchemeIncludedURL, |
| 20 |
Site as MockSite, |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
@override_settings(ROOT_URLCONF="contenttypes_tests.urls") |
| 25 |
class ContentTypesViewsTests(TestCase): |
| 26 |
@classmethod |
| 27 |
def setUpTestData(cls): |
| 28 |
# Don't use the manager to ensure the site exists with pk=1, regardless |
| 29 |
# of whether or not it already exists. |
| 30 |
cls.site1 = Site(pk=1, domain="testserver", name="testserver") |
| 31 |
cls.site1.save() |
| 32 |
cls.author1 = Author.objects.create(name="Boris") |
| 33 |
cls.article1 = Article.objects.create( |
| 34 |
title="Old Article", |
| 35 |
slug="old_article", |
| 36 |
author=cls.author1, |
| 37 |
date_created=datetime.datetime(2001, 1, 1, 21, 22, 23), |
| 38 |
) |
| 39 |
cls.article2 = Article.objects.create( |
| 40 |
title="Current Article", |
| 41 |
slug="current_article", |
| 42 |
author=cls.author1, |
| 43 |
date_created=datetime.datetime(2007, 9, 17, 21, 22, 23), |
| 44 |
) |
| 45 |
cls.article3 = Article.objects.create( |
| 46 |
title="Future Article", |
| 47 |
slug="future_article", |
| 48 |
author=cls.author1, |
| 49 |
date_created=datetime.datetime(3000, 1, 1, 21, 22, 23), |
| 50 |
) |
| 51 |
cls.scheme1 = SchemeIncludedURL.objects.create( |
| 52 |
url="http://test_scheme_included_http/" |
| 53 |
) |
| 54 |
cls.scheme2 = SchemeIncludedURL.objects.create( |
| 55 |
url="https://test_scheme_included_https/" |
| 56 |
) |
| 57 |
cls.scheme3 = SchemeIncludedURL.objects.create( |
| 58 |
url="//test_default_scheme_kept/" |
| 59 |
) |
| 60 |
|
| 61 |
def setUp(self): |
| 62 |
Site.objects.clear_cache() |
| 63 |
|
| 64 |
def test_shortcut_with_absolute_url(self): |
| 65 |
"Can view a shortcut for an Author object that has a get_absolute_url method" |
| 66 |
for obj in Author.objects.all(): |
| 67 |
with self.subTest(obj=obj): |
| 68 |
short_url = "/shortcut/%s/%s/" % ( |
| 69 |
ContentType.objects.get_for_model(Author).id, |
| 70 |
obj.pk, |
| 71 |
) |
| 72 |
response = self.client.get(short_url) |
| 73 |
self.assertRedirects( |
| 74 |
response, |
| 75 |
"http://testserver%s" % obj.get_absolute_url(), |
| 76 |
target_status_code=404, |
| 77 |
) |
| 78 |
|
| 79 |
def test_shortcut_with_absolute_url_including_scheme(self): |
| 80 |
""" |
| 81 |
Can view a shortcut when object's get_absolute_url returns a full URL |
| 82 |
the tested URLs are: "http://...", "https://..." and "//..." |
| 83 |
""" |
| 84 |
for obj in SchemeIncludedURL.objects.all(): |
| 85 |
with self.subTest(obj=obj): |
| 86 |
short_url = "/shortcut/%s/%s/" % ( |
| 87 |
ContentType.objects.get_for_model(SchemeIncludedURL).id, |
| 88 |
obj.pk, |
| 89 |
) |
| 90 |
response = self.client.get(short_url) |
| 91 |
self.assertRedirects( |
| 92 |
response, obj.get_absolute_url(), fetch_redirect_response=False |
| 93 |
) |
| 94 |
|
| 95 |
def test_shortcut_no_absolute_url(self): |
| 96 |
""" |
| 97 |
Shortcuts for an object that has no get_absolute_url() method raise |
| 98 |
404. |
| 99 |
""" |
| 100 |
for obj in Article.objects.all(): |
| 101 |
with self.subTest(obj=obj): |
| 102 |
short_url = "/shortcut/%s/%s/" % ( |
| 103 |
ContentType.objects.get_for_model(Article).id, |
| 104 |
obj.pk, |
| 105 |
) |
| 106 |
response = self.client.get(short_url) |
| 107 |
self.assertEqual(response.status_code, 404) |
| 108 |
|
| 109 |
def test_wrong_type_pk(self): |
| 110 |
short_url = "/shortcut/%s/%s/" % ( |
| 111 |
ContentType.objects.get_for_model(Author).id, |
| 112 |
"nobody/expects", |
| 113 |
) |
| 114 |
response = self.client.get(short_url) |
| 115 |
self.assertEqual(response.status_code, 404) |
| 116 |
|
| 117 |
def test_shortcut_bad_pk(self): |
| 118 |
short_url = "/shortcut/%s/%s/" % ( |
| 119 |
ContentType.objects.get_for_model(Author).id, |
| 120 |
"42424242", |
| 121 |
) |
| 122 |
response = self.client.get(short_url) |
| 123 |
self.assertEqual(response.status_code, 404) |
| 124 |
|
| 125 |
def test_nonint_content_type(self): |
| 126 |
an_author = Author.objects.all()[0] |
| 127 |
short_url = "/shortcut/%s/%s/" % ("spam", an_author.pk) |
| 128 |
response = self.client.get(short_url) |
| 129 |
self.assertEqual(response.status_code, 404) |
| 130 |
|
| 131 |
def test_bad_content_type(self): |
| 132 |
an_author = Author.objects.all()[0] |
| 133 |
short_url = "/shortcut/%s/%s/" % (42424242, an_author.pk) |
| 134 |
response = self.client.get(short_url) |
| 135 |
self.assertEqual(response.status_code, 404) |
| 136 |
|
| 137 |
|
| 138 |
@override_settings(ROOT_URLCONF="contenttypes_tests.urls") |
| 139 |
class ContentTypesViewsSiteRelTests(TestCase): |
| 140 |
def setUp(self): |
| 141 |
Site.objects.clear_cache() |
| 142 |
|
| 143 |
@classmethod |
| 144 |
def setUpTestData(cls): |
| 145 |
cls.site_2 = Site.objects.create(domain="example2.com", name="example2.com") |
| 146 |
cls.site_3 = Site.objects.create(domain="example3.com", name="example3.com") |
| 147 |
|
| 148 |
@mock.patch("django.apps.apps.get_model") |
| 149 |
def test_shortcut_view_with_null_site_fk(self, get_model): |
| 150 |
""" |
| 151 |
The shortcut view works if a model's ForeignKey to site is None. |
| 152 |
""" |
| 153 |
get_model.side_effect = lambda *args, **kwargs: ( |
| 154 |
MockSite if args[0] == "sites.Site" else ModelWithNullFKToSite |
| 155 |
) |
| 156 |
|
| 157 |
obj = ModelWithNullFKToSite.objects.create(title="title") |
| 158 |
url = "/shortcut/%s/%s/" % ( |
| 159 |
ContentType.objects.get_for_model(ModelWithNullFKToSite).id, |
| 160 |
obj.pk, |
| 161 |
) |
| 162 |
response = self.client.get(url) |
| 163 |
expected_url = "http://example.com%s" % obj.get_absolute_url() |
| 164 |
self.assertRedirects(response, expected_url, fetch_redirect_response=False) |
| 165 |
|
| 166 |
@mock.patch("django.apps.apps.get_model") |
| 167 |
def test_shortcut_view_with_site_m2m(self, get_model): |
| 168 |
""" |
| 169 |
When the object has a ManyToManyField to Site, redirect to the current |
| 170 |
site if it's attached to the object or to the domain of the first site |
| 171 |
found in the m2m relationship. |
| 172 |
""" |
| 173 |
get_model.side_effect = lambda *args, **kwargs: ( |
| 174 |
MockSite if args[0] == "sites.Site" else ModelWithM2MToSite |
| 175 |
) |
| 176 |
|
| 177 |
# get_current_site() will lookup a Site object, so these must match the |
| 178 |
# domains in the MockSite model. |
| 179 |
MockSite.objects.bulk_create( |
| 180 |
[ |
| 181 |
MockSite(pk=1, domain="example.com"), |
| 182 |
MockSite(pk=self.site_2.pk, domain=self.site_2.domain), |
| 183 |
MockSite(pk=self.site_3.pk, domain=self.site_3.domain), |
| 184 |
] |
| 185 |
) |
| 186 |
ct = ContentType.objects.get_for_model(ModelWithM2MToSite) |
| 187 |
site_3_obj = ModelWithM2MToSite.objects.create( |
| 188 |
title="Not Linked to Current Site" |
| 189 |
) |
| 190 |
site_3_obj.sites.add(MockSite.objects.get(pk=self.site_3.pk)) |
| 191 |
expected_url = "http://%s%s" % ( |
| 192 |
self.site_3.domain, |
| 193 |
site_3_obj.get_absolute_url(), |
| 194 |
) |
| 195 |
|
| 196 |
with self.settings(SITE_ID=self.site_2.pk): |
| 197 |
# Redirects to the domain of the first Site found in the m2m |
| 198 |
# relationship (ordering is arbitrary). |
| 199 |
response = self.client.get("/shortcut/%s/%s/" % (ct.pk, site_3_obj.pk)) |
| 200 |
self.assertRedirects(response, expected_url, fetch_redirect_response=False) |
| 201 |
|
| 202 |
obj_with_sites = ModelWithM2MToSite.objects.create( |
| 203 |
title="Linked to Current Site" |
| 204 |
) |
| 205 |
obj_with_sites.sites.set(MockSite.objects.all()) |
| 206 |
shortcut_url = "/shortcut/%s/%s/" % (ct.pk, obj_with_sites.pk) |
| 207 |
expected_url = "http://%s%s" % ( |
| 208 |
self.site_2.domain, |
| 209 |
obj_with_sites.get_absolute_url(), |
| 210 |
) |
| 211 |
|
| 212 |
with self.settings(SITE_ID=self.site_2.pk): |
| 213 |
# Redirects to the domain of the Site matching the current site's |
| 214 |
# domain. |
| 215 |
response = self.client.get(shortcut_url) |
| 216 |
self.assertRedirects(response, expected_url, fetch_redirect_response=False) |
| 217 |
|
| 218 |
with self.settings(SITE_ID=None, ALLOWED_HOSTS=[self.site_2.domain]): |
| 219 |
# Redirects to the domain of the Site matching the request's host |
| 220 |
# header. |
| 221 |
response = self.client.get(shortcut_url, SERVER_NAME=self.site_2.domain) |
| 222 |
self.assertRedirects(response, expected_url, fetch_redirect_response=False) |
| 223 |
|
| 224 |
|
| 225 |
class ShortcutViewTests(TestCase): |
| 226 |
def setUp(self): |
| 227 |
self.request = HttpRequest() |
| 228 |
self.request.META = {"SERVER_NAME": "Example.com", "SERVER_PORT": "80"} |
| 229 |
|
| 230 |
@override_settings(ALLOWED_HOSTS=["example.com"]) |
| 231 |
def test_not_dependent_on_sites_app(self): |
| 232 |
""" |
| 233 |
The view returns a complete URL regardless of whether the sites |
| 234 |
framework is installed. |
| 235 |
""" |
| 236 |
user_ct = ContentType.objects.get_for_model(FooWithUrl) |
| 237 |
obj = FooWithUrl.objects.create(name="john") |
| 238 |
with self.modify_settings(INSTALLED_APPS={"append": "django.contrib.sites"}): |
| 239 |
response = shortcut(self.request, user_ct.id, obj.id) |
| 240 |
self.assertEqual( |
| 241 |
"http://%s/users/john/" % get_current_site(self.request).domain, |
| 242 |
response.headers.get("location"), |
| 243 |
) |
| 244 |
with self.modify_settings(INSTALLED_APPS={"remove": "django.contrib.sites"}): |
| 245 |
response = shortcut(self.request, user_ct.id, obj.id) |
| 246 |
self.assertEqual( |
| 247 |
"http://Example.com/users/john/", response.headers.get("location") |
| 248 |
) |
| 249 |
|
| 250 |
def test_model_without_get_absolute_url(self): |
| 251 |
"""The view returns 404 when Model.get_absolute_url() isn't defined.""" |
| 252 |
user_ct = ContentType.objects.get_for_model(FooWithoutUrl) |
| 253 |
obj = FooWithoutUrl.objects.create(name="john") |
| 254 |
with self.assertRaises(Http404): |
| 255 |
shortcut(self.request, user_ct.id, obj.id) |
| 256 |
|
| 257 |
def test_model_with_broken_get_absolute_url(self): |
| 258 |
""" |
| 259 |
The view doesn't catch an AttributeError raised by |
| 260 |
Model.get_absolute_url() (#8997). |
| 261 |
""" |
| 262 |
user_ct = ContentType.objects.get_for_model(FooWithBrokenAbsoluteUrl) |
| 263 |
obj = FooWithBrokenAbsoluteUrl.objects.create(name="john") |
| 264 |
with self.assertRaises(AttributeError): |
| 265 |
shortcut(self.request, user_ct.id, obj.id) |