2013-05-21 03:22:38 +08:00
|
|
|
from django import http
|
2012-11-02 02:44:45 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.sites.models import Site
|
2013-02-08 23:32:09 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2012-11-02 02:44:45 +08:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.utils import override_settings
|
|
|
|
from django.utils import six
|
|
|
|
|
2013-02-08 23:32:09 +08:00
|
|
|
from .middleware import RedirectFallbackMiddleware
|
2012-11-02 02:44:45 +08:00
|
|
|
from .models import Redirect
|
|
|
|
|
|
|
|
|
|
|
|
@override_settings(
|
2013-02-08 05:35:23 +08:00
|
|
|
APPEND_SLASH=False,
|
2012-11-02 02:44:45 +08:00
|
|
|
MIDDLEWARE_CLASSES=list(settings.MIDDLEWARE_CLASSES) +
|
2013-10-20 07:33:10 +08:00
|
|
|
['django.contrib.redirects.middleware.RedirectFallbackMiddleware'],
|
2013-02-08 05:35:23 +08:00
|
|
|
SITE_ID=1,
|
2012-11-02 02:44:45 +08:00
|
|
|
)
|
|
|
|
class RedirectTests(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.site = Site.objects.get(pk=settings.SITE_ID)
|
|
|
|
|
|
|
|
def test_model(self):
|
|
|
|
r1 = Redirect.objects.create(
|
|
|
|
site=self.site, old_path='/initial', new_path='/new_target')
|
|
|
|
self.assertEqual(six.text_type(r1), "/initial ---> /new_target")
|
|
|
|
|
2013-02-08 05:35:23 +08:00
|
|
|
def test_redirect(self):
|
|
|
|
Redirect.objects.create(
|
2012-11-02 02:44:45 +08:00
|
|
|
site=self.site, old_path='/initial', new_path='/new_target')
|
|
|
|
response = self.client.get('/initial')
|
|
|
|
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):
|
|
|
|
Redirect.objects.create(
|
|
|
|
site=self.site, old_path='/initial/', new_path='/new_target/')
|
|
|
|
response = self.client.get('/initial')
|
2012-11-02 02:44:45 +08:00
|
|
|
self.assertRedirects(response,
|
2013-02-08 05:35:23 +08:00
|
|
|
'/new_target/', status_code=301, target_status_code=404)
|
|
|
|
|
|
|
|
@override_settings(APPEND_SLASH=True)
|
|
|
|
def test_redirect_with_append_slash_and_query_string(self):
|
|
|
|
Redirect.objects.create(
|
|
|
|
site=self.site, old_path='/initial/?foo', new_path='/new_target/')
|
|
|
|
response = self.client.get('/initial?foo')
|
|
|
|
self.assertRedirects(response,
|
|
|
|
'/new_target/', status_code=301, target_status_code=404)
|
2012-11-02 02:44:45 +08:00
|
|
|
|
|
|
|
def test_response_gone(self):
|
|
|
|
"""When the redirect target is '', return a 410"""
|
2013-02-08 05:35:23 +08:00
|
|
|
Redirect.objects.create(
|
2012-11-02 02:44:45 +08:00
|
|
|
site=self.site, old_path='/initial', new_path='')
|
|
|
|
response = self.client.get('/initial')
|
|
|
|
self.assertEqual(response.status_code, 410)
|
2013-02-08 23:32:09 +08:00
|
|
|
|
|
|
|
@override_settings(
|
|
|
|
INSTALLED_APPS=[app for app in settings.INSTALLED_APPS
|
|
|
|
if app != 'django.contrib.sites'])
|
|
|
|
def test_sites_not_installed(self):
|
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
RedirectFallbackMiddleware()
|
2013-05-21 03:22:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
class OverriddenRedirectFallbackMiddleware(RedirectFallbackMiddleware):
|
|
|
|
# Use HTTP responses different from the defaults
|
|
|
|
response_gone_class = http.HttpResponseForbidden
|
|
|
|
response_redirect_class = http.HttpResponseRedirect
|
|
|
|
|
|
|
|
|
|
|
|
@override_settings(
|
|
|
|
MIDDLEWARE_CLASSES=list(settings.MIDDLEWARE_CLASSES) +
|
2013-10-20 07:33:10 +08:00
|
|
|
['django.contrib.redirects.tests.OverriddenRedirectFallbackMiddleware'],
|
2013-05-21 03:22:38 +08:00
|
|
|
SITE_ID=1,
|
|
|
|
)
|
|
|
|
class OverriddenRedirectMiddlewareTests(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.site = Site.objects.get(pk=settings.SITE_ID)
|
|
|
|
|
|
|
|
def test_response_gone_class(self):
|
|
|
|
Redirect.objects.create(
|
|
|
|
site=self.site, old_path='/initial/', new_path='')
|
|
|
|
response = self.client.get('/initial/')
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
|
|
|
def test_response_redirect_class(self):
|
|
|
|
Redirect.objects.create(
|
|
|
|
site=self.site, old_path='/initial/', new_path='/new_target/')
|
|
|
|
response = self.client.get('/initial/')
|
|
|
|
self.assertEqual(response.status_code, 302)
|