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