2010-08-27 13:55:11 +00:00
|
|
|
from django.conf import settings
|
2010-08-28 11:59:14 +00:00
|
|
|
from django.contrib.auth.models import User
|
2011-04-20 14:41:47 +00:00
|
|
|
from django.contrib.flatpages.models import FlatPage
|
2015-02-23 00:53:57 +00:00
|
|
|
from django.contrib.sites.models import Site
|
2015-02-10 00:20:39 +00:00
|
|
|
from django.test import TestCase, modify_settings, override_settings
|
2016-05-09 17:13:55 +00:00
|
|
|
from django.test.utils import ignore_warnings
|
|
|
|
from django.utils.deprecation import RemovedInDjango20Warning
|
2012-03-31 16:03:09 +00:00
|
|
|
|
2014-12-17 21:51:42 +00:00
|
|
|
from .settings import FLATPAGES_TEMPLATES
|
|
|
|
|
2012-03-31 16:03:09 +00:00
|
|
|
|
2015-02-23 00:53:57 +00:00
|
|
|
class TestDataMixin(object):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
# don't use the manager because we want to ensure the site exists
|
|
|
|
# with pk=1, regardless of whether or not it already exists.
|
|
|
|
cls.site1 = Site(pk=1, domain='example.com', name='example.com')
|
|
|
|
cls.site1.save()
|
|
|
|
cls.fp1 = FlatPage.objects.create(
|
|
|
|
url='/flatpage/', title='A Flatpage', content="Isn't it flat!",
|
|
|
|
enable_comments=False, template_name='', registration_required=False
|
|
|
|
)
|
|
|
|
cls.fp2 = FlatPage.objects.create(
|
|
|
|
url='/location/flatpage/', title='A Nested Flatpage', content="Isn't it flat and deep!",
|
|
|
|
enable_comments=False, template_name='', registration_required=False
|
|
|
|
)
|
|
|
|
cls.fp3 = FlatPage.objects.create(
|
|
|
|
url='/sekrit/', title='Sekrit Flatpage', content="Isn't it sekrit!",
|
|
|
|
enable_comments=False, template_name='', registration_required=True
|
|
|
|
)
|
|
|
|
cls.fp4 = FlatPage.objects.create(
|
|
|
|
url='/location/sekrit/', title='Sekrit Nested Flatpage', content="Isn't it sekrit and deep!",
|
|
|
|
enable_comments=False, template_name='', registration_required=True
|
|
|
|
)
|
|
|
|
cls.fp1.sites.add(cls.site1)
|
|
|
|
cls.fp2.sites.add(cls.site1)
|
|
|
|
cls.fp3.sites.add(cls.site1)
|
|
|
|
cls.fp4.sites.add(cls.site1)
|
|
|
|
|
|
|
|
|
2015-02-10 00:20:39 +00:00
|
|
|
@modify_settings(INSTALLED_APPS={'append': 'django.contrib.flatpages'})
|
2012-03-31 16:03:09 +00:00
|
|
|
@override_settings(
|
|
|
|
LOGIN_URL='/accounts/login/',
|
2015-11-07 15:12:37 +00:00
|
|
|
MIDDLEWARE=[
|
2012-03-31 16:03:09 +00:00
|
|
|
'django.middleware.common.CommonMiddleware',
|
|
|
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
|
|
|
'django.middleware.csrf.CsrfViewMiddleware',
|
|
|
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
|
|
'django.contrib.messages.middleware.MessageMiddleware',
|
|
|
|
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
|
2015-01-21 16:55:57 +00:00
|
|
|
],
|
2015-02-10 00:20:39 +00:00
|
|
|
ROOT_URLCONF='flatpages_tests.urls',
|
2014-12-17 21:51:42 +00:00
|
|
|
TEMPLATES=FLATPAGES_TEMPLATES,
|
2012-05-10 21:09:29 +00:00
|
|
|
SITE_ID=1,
|
2012-03-31 16:03:09 +00:00
|
|
|
)
|
2015-02-23 00:53:57 +00:00
|
|
|
class FlatpageMiddlewareTests(TestDataMixin, TestCase):
|
2010-08-27 13:55:11 +00:00
|
|
|
|
|
|
|
def test_view_flatpage(self):
|
|
|
|
"A flatpage can be served through a view, even when the middleware is in use"
|
|
|
|
response = self.client.get('/flatpage_root/flatpage/')
|
|
|
|
self.assertContains(response, "<p>Isn't it flat!</p>")
|
|
|
|
|
|
|
|
def test_view_non_existent_flatpage(self):
|
|
|
|
"A non-existent flatpage raises 404 when served through a view, even when the middleware is in use"
|
|
|
|
response = self.client.get('/flatpage_root/no_such_flatpage/')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(response.status_code, 404)
|
2010-08-27 13:55:11 +00:00
|
|
|
|
|
|
|
def test_view_authenticated_flatpage(self):
|
|
|
|
"A flatpage served through a view can require authentication"
|
|
|
|
response = self.client.get('/flatpage_root/sekrit/')
|
|
|
|
self.assertRedirects(response, '/accounts/login/?next=/flatpage_root/sekrit/')
|
2016-02-05 20:56:52 +00:00
|
|
|
user = User.objects.create_user('testuser', 'test@example.com', 's3krit')
|
|
|
|
self.client.force_login(user)
|
2010-08-28 11:59:14 +00:00
|
|
|
response = self.client.get('/flatpage_root/sekrit/')
|
|
|
|
self.assertContains(response, "<p>Isn't it sekrit!</p>")
|
2010-08-27 13:55:11 +00:00
|
|
|
|
|
|
|
def test_fallback_flatpage(self):
|
2014-03-02 14:25:53 +00:00
|
|
|
"A flatpage can be served by the fallback middleware"
|
2010-08-27 13:55:11 +00:00
|
|
|
response = self.client.get('/flatpage/')
|
|
|
|
self.assertContains(response, "<p>Isn't it flat!</p>")
|
|
|
|
|
|
|
|
def test_fallback_non_existent_flatpage(self):
|
2014-03-02 14:25:53 +00:00
|
|
|
"A non-existent flatpage raises a 404 when served by the fallback middleware"
|
2010-08-27 13:55:11 +00:00
|
|
|
response = self.client.get('/no_such_flatpage/')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(response.status_code, 404)
|
2010-08-27 13:55:11 +00:00
|
|
|
|
|
|
|
def test_fallback_authenticated_flatpage(self):
|
|
|
|
"A flatpage served by the middleware can require authentication"
|
|
|
|
response = self.client.get('/sekrit/')
|
|
|
|
self.assertRedirects(response, '/accounts/login/?next=/sekrit/')
|
2016-02-05 20:56:52 +00:00
|
|
|
user = User.objects.create_user('testuser', 'test@example.com', 's3krit')
|
|
|
|
self.client.force_login(user)
|
2010-08-28 11:59:14 +00:00
|
|
|
response = self.client.get('/sekrit/')
|
|
|
|
self.assertContains(response, "<p>Isn't it sekrit!</p>")
|
2011-04-20 14:41:47 +00:00
|
|
|
|
|
|
|
def test_fallback_flatpage_special_chars(self):
|
|
|
|
"A flatpage with special chars in the URL can be served by the fallback middleware"
|
|
|
|
fp = FlatPage.objects.create(
|
|
|
|
url="/some.very_special~chars-here/",
|
|
|
|
title="A very special page",
|
|
|
|
content="Isn't it special!",
|
|
|
|
enable_comments=False,
|
|
|
|
registration_required=False,
|
|
|
|
)
|
2012-05-10 21:09:29 +00:00
|
|
|
fp.sites.add(settings.SITE_ID)
|
2011-04-20 14:41:47 +00:00
|
|
|
|
|
|
|
response = self.client.get('/some.very_special~chars-here/')
|
|
|
|
self.assertContains(response, "<p>Isn't it special!</p>")
|
|
|
|
|
|
|
|
|
2016-05-09 17:13:55 +00:00
|
|
|
@ignore_warnings(category=RemovedInDjango20Warning)
|
2015-11-07 15:12:37 +00:00
|
|
|
@override_settings(
|
|
|
|
MIDDLEWARE=None,
|
|
|
|
MIDDLEWARE_CLASSES=[
|
|
|
|
'django.middleware.common.CommonMiddleware',
|
|
|
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
|
|
|
'django.middleware.csrf.CsrfViewMiddleware',
|
|
|
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
|
|
'django.contrib.messages.middleware.MessageMiddleware',
|
|
|
|
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
|
|
|
|
],
|
|
|
|
)
|
|
|
|
class FlatpageMiddlewareClassesTests(FlatpageMiddlewareTests):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-02-10 00:20:39 +00:00
|
|
|
@modify_settings(INSTALLED_APPS={'append': 'django.contrib.flatpages'})
|
2012-03-31 16:03:09 +00:00
|
|
|
@override_settings(
|
2013-11-03 09:22:11 +00:00
|
|
|
APPEND_SLASH=True,
|
2012-03-31 16:03:09 +00:00
|
|
|
LOGIN_URL='/accounts/login/',
|
2015-11-07 15:12:37 +00:00
|
|
|
MIDDLEWARE=[
|
2012-03-31 16:03:09 +00:00
|
|
|
'django.middleware.common.CommonMiddleware',
|
|
|
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
|
|
|
'django.middleware.csrf.CsrfViewMiddleware',
|
|
|
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
|
|
'django.contrib.messages.middleware.MessageMiddleware',
|
|
|
|
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
|
2015-01-21 16:55:57 +00:00
|
|
|
],
|
2015-02-10 00:20:39 +00:00
|
|
|
ROOT_URLCONF='flatpages_tests.urls',
|
2014-12-17 21:51:42 +00:00
|
|
|
TEMPLATES=FLATPAGES_TEMPLATES,
|
2012-05-10 21:09:29 +00:00
|
|
|
SITE_ID=1,
|
2012-03-31 16:03:09 +00:00
|
|
|
)
|
2015-02-23 00:53:57 +00:00
|
|
|
class FlatpageMiddlewareAppendSlashTests(TestDataMixin, TestCase):
|
2011-04-20 14:41:47 +00:00
|
|
|
|
|
|
|
def test_redirect_view_flatpage(self):
|
|
|
|
"A flatpage can be served through a view and should add a slash"
|
|
|
|
response = self.client.get('/flatpage_root/flatpage')
|
|
|
|
self.assertRedirects(response, '/flatpage_root/flatpage/', status_code=301)
|
|
|
|
|
|
|
|
def test_redirect_view_non_existent_flatpage(self):
|
|
|
|
"A non-existent flatpage raises 404 when served through a view and should not add a slash"
|
|
|
|
response = self.client.get('/flatpage_root/no_such_flatpage')
|
2011-05-07 17:05:06 +00:00
|
|
|
self.assertEqual(response.status_code, 404)
|
2011-04-20 14:41:47 +00:00
|
|
|
|
|
|
|
def test_redirect_fallback_flatpage(self):
|
2014-03-02 14:25:53 +00:00
|
|
|
"A flatpage can be served by the fallback middleware and should add a slash"
|
2011-04-20 14:41:47 +00:00
|
|
|
response = self.client.get('/flatpage')
|
|
|
|
self.assertRedirects(response, '/flatpage/', status_code=301)
|
|
|
|
|
|
|
|
def test_redirect_fallback_non_existent_flatpage(self):
|
2014-03-02 14:25:53 +00:00
|
|
|
"A non-existent flatpage raises a 404 when served by the fallback middleware and should not add a slash"
|
2011-04-20 14:41:47 +00:00
|
|
|
response = self.client.get('/no_such_flatpage')
|
2011-05-07 17:05:06 +00:00
|
|
|
self.assertEqual(response.status_code, 404)
|
2011-04-20 14:41:47 +00:00
|
|
|
|
|
|
|
def test_redirect_fallback_flatpage_special_chars(self):
|
|
|
|
"A flatpage with special chars in the URL can be served by the fallback middleware and should add a slash"
|
|
|
|
fp = FlatPage.objects.create(
|
|
|
|
url="/some.very_special~chars-here/",
|
|
|
|
title="A very special page",
|
|
|
|
content="Isn't it special!",
|
|
|
|
enable_comments=False,
|
|
|
|
registration_required=False,
|
|
|
|
)
|
2012-05-10 21:09:29 +00:00
|
|
|
fp.sites.add(settings.SITE_ID)
|
2011-04-20 14:41:47 +00:00
|
|
|
|
|
|
|
response = self.client.get('/some.very_special~chars-here')
|
|
|
|
self.assertRedirects(response, '/some.very_special~chars-here/', status_code=301)
|
|
|
|
|
|
|
|
def test_redirect_fallback_flatpage_root(self):
|
|
|
|
"A flatpage at / should not cause a redirect loop when APPEND_SLASH is set"
|
|
|
|
fp = FlatPage.objects.create(
|
|
|
|
url="/",
|
|
|
|
title="Root",
|
|
|
|
content="Root",
|
|
|
|
enable_comments=False,
|
|
|
|
registration_required=False,
|
|
|
|
)
|
2012-05-10 21:09:29 +00:00
|
|
|
fp.sites.add(settings.SITE_ID)
|
2011-04-20 14:41:47 +00:00
|
|
|
|
|
|
|
response = self.client.get('/')
|
|
|
|
self.assertContains(response, "<p>Root</p>")
|
2015-11-07 15:12:37 +00:00
|
|
|
|
|
|
|
|
2016-05-09 17:13:55 +00:00
|
|
|
@ignore_warnings(category=RemovedInDjango20Warning)
|
2015-11-07 15:12:37 +00:00
|
|
|
@override_settings(
|
|
|
|
MIDDLEWARE=None,
|
|
|
|
MIDDLEWARE_CLASSES=[
|
|
|
|
'django.middleware.common.CommonMiddleware',
|
|
|
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
|
|
|
'django.middleware.csrf.CsrfViewMiddleware',
|
|
|
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
|
|
'django.contrib.messages.middleware.MessageMiddleware',
|
|
|
|
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
|
|
|
|
],
|
|
|
|
)
|
|
|
|
class FlatpageAppendSlashMiddlewareClassesTests(FlatpageMiddlewareAppendSlashTests):
|
|
|
|
pass
|