test_views.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. from django.conf import settings
  2. from django.contrib.auth.models import User
  3. from django.contrib.flatpages.models import FlatPage
  4. from django.contrib.sites.models import Site
  5. from django.test import TestCase, modify_settings, override_settings
  6. from .settings import FLATPAGES_TEMPLATES
  7. class TestDataMixin:
  8. @classmethod
  9. def setUpTestData(cls):
  10. # don't use the manager because we want to ensure the site exists
  11. # with pk=1, regardless of whether or not it already exists.
  12. cls.site1 = Site(pk=1, domain='example.com', name='example.com')
  13. cls.site1.save()
  14. cls.fp1 = FlatPage.objects.create(
  15. url='/flatpage/', title='A Flatpage', content="Isn't it flat!",
  16. enable_comments=False, template_name='', registration_required=False
  17. )
  18. cls.fp2 = FlatPage.objects.create(
  19. url='/location/flatpage/', title='A Nested Flatpage', content="Isn't it flat and deep!",
  20. enable_comments=False, template_name='', registration_required=False
  21. )
  22. cls.fp3 = FlatPage.objects.create(
  23. url='/sekrit/', title='Sekrit Flatpage', content="Isn't it sekrit!",
  24. enable_comments=False, template_name='', registration_required=True
  25. )
  26. cls.fp4 = FlatPage.objects.create(
  27. url='/location/sekrit/', title='Sekrit Nested Flatpage', content="Isn't it sekrit and deep!",
  28. enable_comments=False, template_name='', registration_required=True
  29. )
  30. cls.fp1.sites.add(cls.site1)
  31. cls.fp2.sites.add(cls.site1)
  32. cls.fp3.sites.add(cls.site1)
  33. cls.fp4.sites.add(cls.site1)
  34. @modify_settings(INSTALLED_APPS={'append': 'django.contrib.flatpages'})
  35. @override_settings(
  36. LOGIN_URL='/accounts/login/',
  37. MIDDLEWARE=[
  38. 'django.middleware.common.CommonMiddleware',
  39. 'django.contrib.sessions.middleware.SessionMiddleware',
  40. 'django.middleware.csrf.CsrfViewMiddleware',
  41. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  42. 'django.contrib.messages.middleware.MessageMiddleware',
  43. # no 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
  44. ],
  45. ROOT_URLCONF='flatpages_tests.urls',
  46. TEMPLATES=FLATPAGES_TEMPLATES,
  47. SITE_ID=1,
  48. )
  49. class FlatpageViewTests(TestDataMixin, TestCase):
  50. def test_view_flatpage(self):
  51. "A flatpage can be served through a view"
  52. response = self.client.get('/flatpage_root/flatpage/')
  53. self.assertContains(response, "<p>Isn't it flat!</p>")
  54. def test_view_non_existent_flatpage(self):
  55. """A nonexistent flatpage raises 404 when served through a view."""
  56. response = self.client.get('/flatpage_root/no_such_flatpage/')
  57. self.assertEqual(response.status_code, 404)
  58. def test_view_authenticated_flatpage(self):
  59. "A flatpage served through a view can require authentication"
  60. response = self.client.get('/flatpage_root/sekrit/')
  61. self.assertRedirects(response, '/accounts/login/?next=/flatpage_root/sekrit/')
  62. user = User.objects.create_user('testuser', 'test@example.com', 's3krit')
  63. self.client.force_login(user)
  64. response = self.client.get('/flatpage_root/sekrit/')
  65. self.assertContains(response, "<p>Isn't it sekrit!</p>")
  66. def test_fallback_flatpage(self):
  67. "A fallback flatpage won't be served if the middleware is disabled"
  68. response = self.client.get('/flatpage/')
  69. self.assertEqual(response.status_code, 404)
  70. def test_fallback_non_existent_flatpage(self):
  71. """
  72. A nonexistent flatpage won't be served if the fallback middleware is
  73. disabled.
  74. """
  75. response = self.client.get('/no_such_flatpage/')
  76. self.assertEqual(response.status_code, 404)
  77. def test_view_flatpage_special_chars(self):
  78. "A flatpage with special chars in the URL can be served through a view"
  79. fp = FlatPage.objects.create(
  80. url="/some.very_special~chars-here/",
  81. title="A very special page",
  82. content="Isn't it special!",
  83. enable_comments=False,
  84. registration_required=False,
  85. )
  86. fp.sites.add(settings.SITE_ID)
  87. response = self.client.get('/flatpage_root/some.very_special~chars-here/')
  88. self.assertContains(response, "<p>Isn't it special!</p>")
  89. @modify_settings(INSTALLED_APPS={'append': 'django.contrib.flatpages'})
  90. @override_settings(
  91. APPEND_SLASH=True,
  92. LOGIN_URL='/accounts/login/',
  93. MIDDLEWARE=[
  94. 'django.middleware.common.CommonMiddleware',
  95. 'django.contrib.sessions.middleware.SessionMiddleware',
  96. 'django.middleware.csrf.CsrfViewMiddleware',
  97. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  98. 'django.contrib.messages.middleware.MessageMiddleware',
  99. # no 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
  100. ],
  101. ROOT_URLCONF='flatpages_tests.urls',
  102. TEMPLATES=FLATPAGES_TEMPLATES,
  103. SITE_ID=1,
  104. )
  105. class FlatpageViewAppendSlashTests(TestDataMixin, TestCase):
  106. def test_redirect_view_flatpage(self):
  107. "A flatpage can be served through a view and should add a slash"
  108. response = self.client.get('/flatpage_root/flatpage')
  109. self.assertRedirects(response, '/flatpage_root/flatpage/', status_code=301)
  110. def test_redirect_view_non_existent_flatpage(self):
  111. """
  112. A nonexistent flatpage raises 404 when served through a view and
  113. should not add a slash.
  114. """
  115. response = self.client.get('/flatpage_root/no_such_flatpage')
  116. self.assertEqual(response.status_code, 404)
  117. def test_redirect_fallback_flatpage(self):
  118. "A fallback flatpage won't be served if the middleware is disabled and should not add a slash"
  119. response = self.client.get('/flatpage')
  120. self.assertEqual(response.status_code, 404)
  121. def test_redirect_fallback_non_existent_flatpage(self):
  122. """
  123. A nonexistent flatpage won't be served if the fallback middleware is
  124. disabled and should not add a slash.
  125. """
  126. response = self.client.get('/no_such_flatpage')
  127. self.assertEqual(response.status_code, 404)
  128. def test_redirect_view_flatpage_special_chars(self):
  129. "A flatpage with special chars in the URL can be served through a view and should add a slash"
  130. fp = FlatPage.objects.create(
  131. url="/some.very_special~chars-here/",
  132. title="A very special page",
  133. content="Isn't it special!",
  134. enable_comments=False,
  135. registration_required=False,
  136. )
  137. fp.sites.add(settings.SITE_ID)
  138. response = self.client.get('/flatpage_root/some.very_special~chars-here')
  139. self.assertRedirects(response, '/flatpage_root/some.very_special~chars-here/', status_code=301)