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
|
2010-08-27 21:55:11 +08:00
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
class FlatpageMiddlewareTests(TestCase):
|
|
|
|
fixtures = ['sample_flatpages']
|
|
|
|
urls = 'django.contrib.flatpages.tests.urls'
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.old_MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
|
|
|
|
flatpage_middleware_class = 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
|
|
|
|
if flatpage_middleware_class not in settings.MIDDLEWARE_CLASSES:
|
|
|
|
settings.MIDDLEWARE_CLASSES += (flatpage_middleware_class,)
|
|
|
|
self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
|
|
|
|
settings.TEMPLATE_DIRS = (
|
|
|
|
os.path.join(
|
|
|
|
os.path.dirname(__file__),
|
|
|
|
'templates'
|
|
|
|
),
|
|
|
|
)
|
2010-09-14 07:09:56 +08:00
|
|
|
self.old_LOGIN_URL = settings.LOGIN_URL
|
|
|
|
settings.LOGIN_URL = '/accounts/login/'
|
2010-08-27 21:55:11 +08:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
settings.MIDDLEWARE_CLASSES = self.old_MIDDLEWARE_CLASSES
|
|
|
|
settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
|
2010-09-14 07:09:56 +08:00
|
|
|
settings.LOGIN_URL = self.old_LOGIN_URL
|
2010-08-27 21:55:11 +08: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/')
|
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, even when the middleware is in use"
|
|
|
|
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
|
|
|
|
|
|
|
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')
|
|
|
|
self.client.login(username='testuser',password='s3krit')
|
|
|
|
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 flatpage can be served by the fallback middlware"
|
|
|
|
response = self.client.get('/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_fallback_non_existent_flatpage(self):
|
|
|
|
"A non-existent flatpage raises a 404 when served by the fallback middlware"
|
|
|
|
response = self.client.get('/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
|
|
|
|
|
|
|
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/')
|
2010-08-28 19:59:14 +08:00
|
|
|
User.objects.create_user('testuser', 'test@example.com', 's3krit')
|
|
|
|
self.client.login(username='testuser',password='s3krit')
|
|
|
|
response = self.client.get('/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>")
|