2010-08-27 21:55:11 +08:00
|
|
|
import os
|
|
|
|
from django.conf import settings
|
2010-08-28 19:59:14 +08:00
|
|
|
from django.contrib.auth.models import User
|
2013-03-23 09:57:48 +08:00
|
|
|
from django.contrib.auth.tests.utils import skipIfCustomUser
|
2010-08-28 20:25:54 +08:00
|
|
|
from django.contrib.flatpages.models import FlatPage
|
2010-08-27 21:55:11 +08:00
|
|
|
from django.test import TestCase
|
2012-04-01 00:03:09 +08:00
|
|
|
from django.test.utils import override_settings
|
|
|
|
|
|
|
|
|
|
|
|
@override_settings(
|
|
|
|
LOGIN_URL='/accounts/login/',
|
|
|
|
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',
|
|
|
|
# no 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
|
|
|
|
),
|
|
|
|
TEMPLATE_DIRS=(
|
|
|
|
os.path.join(os.path.dirname(__file__), 'templates'),
|
|
|
|
),
|
2012-05-11 05:09:29 +08:00
|
|
|
SITE_ID=1,
|
2012-04-01 00:03:09 +08:00
|
|
|
)
|
2010-08-27 21:55:11 +08:00
|
|
|
class FlatpageViewTests(TestCase):
|
2012-05-11 05:09:29 +08:00
|
|
|
fixtures = ['sample_flatpages', 'example_site']
|
2010-08-27 21:55:11 +08:00
|
|
|
urls = 'django.contrib.flatpages.tests.urls'
|
|
|
|
|
|
|
|
def test_view_flatpage(self):
|
|
|
|
"A flatpage can be served through a view"
|
|
|
|
response = self.client.get('/flatpage_root/flatpage/')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2010-08-27 21:55:11 +08:00
|
|
|
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"
|
|
|
|
response = self.client.get('/flatpage_root/no_such_flatpage/')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 404)
|
2010-08-27 21:55:11 +08:00
|
|
|
|
2013-03-23 09:57:48 +08:00
|
|
|
@skipIfCustomUser
|
2010-08-27 21:55:11 +08: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/')
|
2010-08-28 19:59:14 +08:00
|
|
|
User.objects.create_user('testuser', 'test@example.com', 's3krit')
|
2013-10-25 01:30:03 +08:00
|
|
|
self.client.login(username='testuser', password='s3krit')
|
2010-08-28 19:59:14 +08:00
|
|
|
response = self.client.get('/flatpage_root/sekrit/')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2010-08-28 19:59:14 +08:00
|
|
|
self.assertContains(response, "<p>Isn't it sekrit!</p>")
|
2010-08-27 21:55:11 +08:00
|
|
|
|
|
|
|
def test_fallback_flatpage(self):
|
|
|
|
"A fallback flatpage won't be served if the middleware is disabled"
|
|
|
|
response = self.client.get('/flatpage/')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 404)
|
2010-08-27 21:55:11 +08:00
|
|
|
|
|
|
|
def test_fallback_non_existent_flatpage(self):
|
|
|
|
"A non-existent flatpage won't be served if the fallback middlware is disabled"
|
|
|
|
response = self.client.get('/no_such_flatpage/')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 404)
|
2010-08-28 20:25:54 +08:00
|
|
|
|
|
|
|
def test_view_flatpage_special_chars(self):
|
|
|
|
"A flatpage with special chars in the URL can be served through a view"
|
|
|
|
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,
|
|
|
|
)
|
2011-03-10 07:46:28 +08:00
|
|
|
fp.sites.add(settings.SITE_ID)
|
2010-08-28 20:25:54 +08:00
|
|
|
|
|
|
|
response = self.client.get('/flatpage_root/some.very_special~chars-here/')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2010-08-28 20:25:54 +08:00
|
|
|
self.assertContains(response, "<p>Isn't it special!</p>")
|
2011-04-20 22:41:47 +08:00
|
|
|
|
|
|
|
|
2012-04-01 00:03:09 +08:00
|
|
|
@override_settings(
|
|
|
|
APPEND_SLASH = True,
|
|
|
|
LOGIN_URL='/accounts/login/',
|
|
|
|
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',
|
|
|
|
# no 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
|
|
|
|
),
|
|
|
|
TEMPLATE_DIRS=(
|
|
|
|
os.path.join(os.path.dirname(__file__), 'templates'),
|
|
|
|
),
|
2012-05-11 05:09:29 +08:00
|
|
|
SITE_ID=1,
|
2012-04-01 00:03:09 +08:00
|
|
|
)
|
2011-04-20 22:41:47 +08:00
|
|
|
class FlatpageViewAppendSlashTests(TestCase):
|
2012-05-11 05:09:29 +08:00
|
|
|
fixtures = ['sample_flatpages', 'example_site']
|
2011-04-20 22:41:47 +08:00
|
|
|
urls = 'django.contrib.flatpages.tests.urls'
|
|
|
|
|
|
|
|
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-08 01:05:06 +08:00
|
|
|
self.assertEqual(response.status_code, 404)
|
2011-04-20 22:41:47 +08:00
|
|
|
|
|
|
|
def test_redirect_fallback_flatpage(self):
|
|
|
|
"A fallback flatpage won't be served if the middleware is disabled and should not add a slash"
|
|
|
|
response = self.client.get('/flatpage')
|
2011-05-08 01:05:06 +08:00
|
|
|
self.assertEqual(response.status_code, 404)
|
2011-04-20 22:41:47 +08:00
|
|
|
|
|
|
|
def test_redirect_fallback_non_existent_flatpage(self):
|
|
|
|
"A non-existent flatpage won't be served if the fallback middlware is disabled and should not add a slash"
|
|
|
|
response = self.client.get('/no_such_flatpage')
|
2011-05-08 01:05:06 +08:00
|
|
|
self.assertEqual(response.status_code, 404)
|
2011-04-20 22:41:47 +08:00
|
|
|
|
|
|
|
def test_redirect_view_flatpage_special_chars(self):
|
|
|
|
"A flatpage with special chars in the URL can be served through a view 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-11 05:09:29 +08:00
|
|
|
fp.sites.add(settings.SITE_ID)
|
2011-04-20 22:41:47 +08:00
|
|
|
|
|
|
|
response = self.client.get('/flatpage_root/some.very_special~chars-here')
|
|
|
|
self.assertRedirects(response, '/flatpage_root/some.very_special~chars-here/', status_code=301)
|