2012-11-02 02:44:45 +08:00
|
|
|
from django.conf import settings
|
2015-02-10 21:41:45 +08:00
|
|
|
from django.contrib.redirects.middleware import RedirectFallbackMiddleware
|
|
|
|
from django.contrib.redirects.models import Redirect
|
2012-11-02 02:44:45 +08:00
|
|
|
from django.contrib.sites.models import Site
|
2013-02-08 23:32:09 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2021-01-13 03:55:32 +08:00
|
|
|
from django.http import HttpResponse, HttpResponseForbidden, HttpResponseRedirect
|
2013-12-23 23:01:13 +08:00
|
|
|
from django.test import TestCase, modify_settings, override_settings
|
2012-11-02 02:44:45 +08:00
|
|
|
|
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
@modify_settings(
|
|
|
|
MIDDLEWARE={
|
|
|
|
"append": "django.contrib.redirects.middleware.RedirectFallbackMiddleware"
|
2022-02-04 03:24:19 +08:00
|
|
|
}
|
2015-11-07 23:12:37 +08:00
|
|
|
)
|
2017-03-18 03:34:04 +08:00
|
|
|
@override_settings(APPEND_SLASH=False, ROOT_URLCONF="redirects_tests.urls", SITE_ID=1)
|
2012-11-02 02:44:45 +08:00
|
|
|
class RedirectTests(TestCase):
|
2018-11-24 09:59:38 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.site = Site.objects.get(pk=settings.SITE_ID)
|
2012-11-02 02:44:45 +08:00
|
|
|
|
|
|
|
def test_model(self):
|
2016-04-08 10:04:45 +08:00
|
|
|
r1 = Redirect.objects.create(
|
|
|
|
site=self.site, old_path="/initial", new_path="/new_target"
|
|
|
|
)
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertEqual(str(r1), "/initial ---> /new_target")
|
2012-11-02 02:44:45 +08:00
|
|
|
|
2013-02-08 05:35:23 +08:00
|
|
|
def test_redirect(self):
|
2016-04-08 10:04:45 +08:00
|
|
|
Redirect.objects.create(
|
|
|
|
site=self.site, old_path="/initial", new_path="/new_target"
|
|
|
|
)
|
2012-11-02 02:44:45 +08:00
|
|
|
response = self.client.get("/initial")
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertRedirects(
|
|
|
|
response, "/new_target", status_code=301, target_status_code=404
|
|
|
|
)
|
2013-02-08 05:35:23 +08:00
|
|
|
|
|
|
|
@override_settings(APPEND_SLASH=True)
|
|
|
|
def test_redirect_with_append_slash(self):
|
2016-04-08 10:04:45 +08:00
|
|
|
Redirect.objects.create(
|
|
|
|
site=self.site, old_path="/initial/", new_path="/new_target/"
|
|
|
|
)
|
2013-02-08 05:35:23 +08:00
|
|
|
response = self.client.get("/initial")
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertRedirects(
|
|
|
|
response, "/new_target/", status_code=301, target_status_code=404
|
|
|
|
)
|
2013-02-08 05:35:23 +08:00
|
|
|
|
|
|
|
@override_settings(APPEND_SLASH=True)
|
|
|
|
def test_redirect_with_append_slash_and_query_string(self):
|
2016-04-08 10:04:45 +08:00
|
|
|
Redirect.objects.create(
|
|
|
|
site=self.site, old_path="/initial/?foo", new_path="/new_target/"
|
|
|
|
)
|
2013-02-08 05:35:23 +08:00
|
|
|
response = self.client.get("/initial?foo")
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertRedirects(
|
|
|
|
response, "/new_target/", status_code=301, target_status_code=404
|
|
|
|
)
|
2012-11-02 02:44:45 +08:00
|
|
|
|
2017-03-18 03:34:04 +08:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
|
|
|
def test_redirect_not_found_with_append_slash(self):
|
|
|
|
"""
|
|
|
|
Exercise the second Redirect.DoesNotExist branch in
|
|
|
|
RedirectFallbackMiddleware.
|
|
|
|
"""
|
|
|
|
response = self.client.get("/test")
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
|
|
def test_redirect_shortcircuits_non_404_response(self):
|
|
|
|
"""RedirectFallbackMiddleware short-circuits on non-404 requests."""
|
|
|
|
response = self.client.get("/")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2012-11-02 02:44:45 +08:00
|
|
|
def test_response_gone(self):
|
|
|
|
"""When the redirect target is '', return a 410"""
|
2016-04-08 10:04:45 +08:00
|
|
|
Redirect.objects.create(site=self.site, old_path="/initial", new_path="")
|
2012-11-02 02:44:45 +08:00
|
|
|
response = self.client.get("/initial")
|
|
|
|
self.assertEqual(response.status_code, 410)
|
2013-02-08 23:32:09 +08:00
|
|
|
|
2013-12-23 17:37:34 +08:00
|
|
|
@modify_settings(INSTALLED_APPS={"remove": "django.contrib.sites"})
|
2013-02-08 23:32:09 +08:00
|
|
|
def test_sites_not_installed(self):
|
2021-01-13 03:55:32 +08:00
|
|
|
def get_response(request):
|
|
|
|
return HttpResponse()
|
|
|
|
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = (
|
|
|
|
"You cannot use RedirectFallbackMiddleware when "
|
|
|
|
"django.contrib.sites is not installed."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2021-01-13 03:55:32 +08:00
|
|
|
RedirectFallbackMiddleware(get_response)
|
2013-05-21 03:22:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
class OverriddenRedirectFallbackMiddleware(RedirectFallbackMiddleware):
|
|
|
|
# Use HTTP responses different from the defaults
|
2017-02-25 14:48:20 +08:00
|
|
|
response_gone_class = HttpResponseForbidden
|
|
|
|
response_redirect_class = HttpResponseRedirect
|
2013-05-21 03:22:38 +08:00
|
|
|
|
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
@modify_settings(
|
|
|
|
MIDDLEWARE={"append": "redirects_tests.tests.OverriddenRedirectFallbackMiddleware"}
|
|
|
|
)
|
2013-12-23 17:37:34 +08:00
|
|
|
@override_settings(SITE_ID=1)
|
2013-05-21 03:22:38 +08:00
|
|
|
class OverriddenRedirectMiddlewareTests(TestCase):
|
2018-11-24 09:59:38 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.site = Site.objects.get(pk=settings.SITE_ID)
|
2013-05-21 03:22:38 +08:00
|
|
|
|
|
|
|
def test_response_gone_class(self):
|
2016-04-08 10:04:45 +08:00
|
|
|
Redirect.objects.create(site=self.site, old_path="/initial/", new_path="")
|
2013-05-21 03:22:38 +08:00
|
|
|
response = self.client.get("/initial/")
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
|
|
|
def test_response_redirect_class(self):
|
2016-04-08 10:04:45 +08:00
|
|
|
Redirect.objects.create(
|
|
|
|
site=self.site, old_path="/initial/", new_path="/new_target/"
|
|
|
|
)
|
2013-05-21 03:22:38 +08:00
|
|
|
response = self.client.get("/initial/")
|
|
|
|
self.assertEqual(response.status_code, 302)
|