PageRenderTime 350ms CodeModel.GetById 198ms app.highlight 8ms RepoModel.GetById 143ms app.codeStats 0ms

/django/contrib/flatpages/tests/views.py

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