tests.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from django import http
  2. from django.conf import settings
  3. from django.contrib.redirects.middleware import RedirectFallbackMiddleware
  4. from django.contrib.redirects.models import Redirect
  5. from django.contrib.sites.models import Site
  6. from django.core.exceptions import ImproperlyConfigured
  7. from django.test import TestCase, modify_settings, override_settings
  8. from django.test.utils import ignore_warnings
  9. from django.utils import six
  10. from django.utils.deprecation import RemovedInDjango20Warning
  11. @modify_settings(MIDDLEWARE={'append': 'django.contrib.redirects.middleware.RedirectFallbackMiddleware'})
  12. @override_settings(APPEND_SLASH=False, SITE_ID=1)
  13. class RedirectTests(TestCase):
  14. def setUp(self):
  15. self.site = Site.objects.get(pk=settings.SITE_ID)
  16. def test_model(self):
  17. r1 = Redirect.objects.create(site=self.site, old_path='/initial', new_path='/new_target')
  18. self.assertEqual(six.text_type(r1), "/initial ---> /new_target")
  19. def test_redirect(self):
  20. Redirect.objects.create(site=self.site, old_path='/initial', new_path='/new_target')
  21. response = self.client.get('/initial')
  22. self.assertRedirects(response, '/new_target', status_code=301, target_status_code=404)
  23. @override_settings(APPEND_SLASH=True)
  24. def test_redirect_with_append_slash(self):
  25. Redirect.objects.create(site=self.site, old_path='/initial/', new_path='/new_target/')
  26. response = self.client.get('/initial')
  27. self.assertRedirects(response, '/new_target/', status_code=301, target_status_code=404)
  28. @override_settings(APPEND_SLASH=True)
  29. def test_redirect_with_append_slash_and_query_string(self):
  30. Redirect.objects.create(site=self.site, old_path='/initial/?foo', new_path='/new_target/')
  31. response = self.client.get('/initial?foo')
  32. self.assertRedirects(response, '/new_target/', status_code=301, target_status_code=404)
  33. def test_response_gone(self):
  34. """When the redirect target is '', return a 410"""
  35. Redirect.objects.create(site=self.site, old_path='/initial', new_path='')
  36. response = self.client.get('/initial')
  37. self.assertEqual(response.status_code, 410)
  38. @ignore_warnings(category=RemovedInDjango20Warning)
  39. @override_settings(MIDDLEWARE=None)
  40. @modify_settings(MIDDLEWARE_CLASSES={'append': 'django.contrib.redirects.middleware.RedirectFallbackMiddleware'})
  41. def test_redirect_middleware_classes(self):
  42. self.test_redirect()
  43. @ignore_warnings(category=RemovedInDjango20Warning)
  44. @override_settings(MIDDLEWARE=None)
  45. @modify_settings(MIDDLEWARE_CLASSES={'append': 'django.contrib.redirects.middleware.RedirectFallbackMiddleware'})
  46. def test_more_redirects_middleware_classes(self):
  47. self.test_redirect_with_append_slash()
  48. self.test_redirect_with_append_slash_and_query_string()
  49. self.test_response_gone()
  50. @modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'})
  51. def test_sites_not_installed(self):
  52. with self.assertRaises(ImproperlyConfigured):
  53. RedirectFallbackMiddleware()
  54. class OverriddenRedirectFallbackMiddleware(RedirectFallbackMiddleware):
  55. # Use HTTP responses different from the defaults
  56. response_gone_class = http.HttpResponseForbidden
  57. response_redirect_class = http.HttpResponseRedirect
  58. @modify_settings(MIDDLEWARE={'append': 'redirects_tests.tests.OverriddenRedirectFallbackMiddleware'})
  59. @override_settings(SITE_ID=1)
  60. class OverriddenRedirectMiddlewareTests(TestCase):
  61. def setUp(self):
  62. self.site = Site.objects.get(pk=settings.SITE_ID)
  63. def test_response_gone_class(self):
  64. Redirect.objects.create(site=self.site, old_path='/initial/', new_path='')
  65. response = self.client.get('/initial/')
  66. self.assertEqual(response.status_code, 403)
  67. def test_response_redirect_class(self):
  68. Redirect.objects.create(site=self.site, old_path='/initial/', new_path='/new_target/')
  69. response = self.client.get('/initial/')
  70. self.assertEqual(response.status_code, 302)