PageRenderTime 138ms CodeModel.GetById 129ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/flatpages/tests/middleware.py

https://code.google.com/p/mango-py/
Python | 70 lines | 61 code | 9 blank | 0 comment | 3 complexity | d1e39fec5614f488d4336c3596cec3b5 MD5 | raw file
 1import os
 2from django.conf import settings
 3from django.contrib.auth.models import User
 4from django.test import TestCase
 5
 6class FlatpageMiddlewareTests(TestCase):
 7    fixtures = ['sample_flatpages']
 8    urls = 'django.contrib.flatpages.tests.urls'
 9
10    def setUp(self):
11        self.old_MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
12        flatpage_middleware_class = 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
13        if flatpage_middleware_class not in settings.MIDDLEWARE_CLASSES:
14            settings.MIDDLEWARE_CLASSES += (flatpage_middleware_class,)
15        self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
16        settings.TEMPLATE_DIRS = (
17            os.path.join(
18                os.path.dirname(__file__),
19                'templates'
20            ),
21        )
22        self.old_LOGIN_URL = settings.LOGIN_URL
23        settings.LOGIN_URL = '/accounts/login/'
24
25    def tearDown(self):
26        settings.MIDDLEWARE_CLASSES = self.old_MIDDLEWARE_CLASSES
27        settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
28        settings.LOGIN_URL = self.old_LOGIN_URL
29
30    def test_view_flatpage(self):
31        "A flatpage can be served through a view, even when the middleware is in use"
32        response = self.client.get('/flatpage_root/flatpage/')
33        self.assertEqual(response.status_code, 200)
34        self.assertContains(response, "<p>Isn't it flat!</p>")
35
36    def test_view_non_existent_flatpage(self):
37        "A non-existent flatpage raises 404 when served through a view, even when the middleware is in use"
38        response = self.client.get('/flatpage_root/no_such_flatpage/')
39        self.assertEqual(response.status_code, 404)
40
41    def test_view_authenticated_flatpage(self):
42        "A flatpage served through a view can require authentication"
43        response = self.client.get('/flatpage_root/sekrit/')
44        self.assertRedirects(response, '/accounts/login/?next=/flatpage_root/sekrit/')
45        User.objects.create_user('testuser', 'test@example.com', 's3krit')
46        self.client.login(username='testuser',password='s3krit')
47        response = self.client.get('/flatpage_root/sekrit/')
48        self.assertEqual(response.status_code, 200)
49        self.assertContains(response, "<p>Isn't it sekrit!</p>")
50
51    def test_fallback_flatpage(self):
52        "A flatpage can be served by the fallback middlware"
53        response = self.client.get('/flatpage/')
54        self.assertEqual(response.status_code, 200)
55        self.assertContains(response, "<p>Isn't it flat!</p>")
56
57    def test_fallback_non_existent_flatpage(self):
58        "A non-existent flatpage raises a 404 when served by the fallback middlware"
59        response = self.client.get('/no_such_flatpage/')
60        self.assertEqual(response.status_code, 404)
61
62    def test_fallback_authenticated_flatpage(self):
63        "A flatpage served by the middleware can require authentication"
64        response = self.client.get('/sekrit/')
65        self.assertRedirects(response, '/accounts/login/?next=/sekrit/')
66        User.objects.create_user('testuser', 'test@example.com', 's3krit')
67        self.client.login(username='testuser',password='s3krit')
68        response = self.client.get('/sekrit/')
69        self.assertEqual(response.status_code, 200)
70        self.assertContains(response, "<p>Isn't it sekrit!</p>")