2010-08-28 19:59:14 +08:00
|
|
|
from django.contrib.auth.models import User
|
2015-02-23 08:53:57 +08:00
|
|
|
from django.contrib.flatpages.models import FlatPage
|
|
|
|
from django.contrib.sites.models import Site
|
2015-02-10 08:20:39 +08:00
|
|
|
from django.test import Client, TestCase, modify_settings, override_settings
|
2010-08-27 21:55:11 +08:00
|
|
|
|
2014-12-18 05:51:42 +08:00
|
|
|
from .settings import FLATPAGES_TEMPLATES
|
|
|
|
|
2012-04-01 00:03:09 +08:00
|
|
|
|
2015-02-10 08:20:39 +08:00
|
|
|
@modify_settings(INSTALLED_APPS={"append": "django.contrib.flatpages"})
|
2012-04-01 00:03:09 +08:00
|
|
|
@override_settings(
|
|
|
|
LOGIN_URL="/accounts/login/",
|
2015-11-07 23:12:37 +08:00
|
|
|
MIDDLEWARE=[
|
2012-04-01 00:03:09 +08:00
|
|
|
"django.middleware.common.CommonMiddleware",
|
|
|
|
"django.contrib.sessions.middleware.SessionMiddleware",
|
|
|
|
"django.middleware.csrf.CsrfViewMiddleware",
|
|
|
|
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
|
|
|
"django.contrib.messages.middleware.MessageMiddleware",
|
|
|
|
"django.contrib.flatpages.middleware.FlatpageFallbackMiddleware",
|
2015-01-22 00:55:57 +08:00
|
|
|
],
|
2015-02-10 08:20:39 +08:00
|
|
|
ROOT_URLCONF="flatpages_tests.urls",
|
2012-11-29 00:48:04 +08:00
|
|
|
CSRF_FAILURE_VIEW="django.views.csrf.csrf_failure",
|
2014-12-18 05:51:42 +08:00
|
|
|
TEMPLATES=FLATPAGES_TEMPLATES,
|
2012-05-11 05:09:29 +08:00
|
|
|
SITE_ID=1,
|
2012-04-01 00:03:09 +08:00
|
|
|
)
|
2010-08-27 21:55:11 +08:00
|
|
|
class FlatpageCSRFTests(TestCase):
|
2015-02-23 08:53:57 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
# don't use the manager because we want to ensure the site exists
|
|
|
|
# with pk=1, regardless of whether or not it already exists.
|
|
|
|
cls.site1 = Site(pk=1, domain="example.com", name="example.com")
|
|
|
|
cls.site1.save()
|
|
|
|
cls.fp1 = FlatPage.objects.create(
|
|
|
|
url="/flatpage/",
|
|
|
|
title="A Flatpage",
|
|
|
|
content="Isn't it flat!",
|
|
|
|
enable_comments=False,
|
|
|
|
template_name="",
|
|
|
|
registration_required=False,
|
|
|
|
)
|
|
|
|
cls.fp2 = FlatPage.objects.create(
|
|
|
|
url="/location/flatpage/",
|
|
|
|
title="A Nested Flatpage",
|
|
|
|
content="Isn't it flat and deep!",
|
|
|
|
enable_comments=False,
|
|
|
|
template_name="",
|
|
|
|
registration_required=False,
|
|
|
|
)
|
|
|
|
cls.fp3 = FlatPage.objects.create(
|
|
|
|
url="/sekrit/",
|
|
|
|
title="Sekrit Flatpage",
|
|
|
|
content="Isn't it sekrit!",
|
|
|
|
enable_comments=False,
|
|
|
|
template_name="",
|
|
|
|
registration_required=True,
|
|
|
|
)
|
|
|
|
cls.fp4 = FlatPage.objects.create(
|
|
|
|
url="/location/sekrit/",
|
|
|
|
title="Sekrit Nested Flatpage",
|
|
|
|
content="Isn't it sekrit and deep!",
|
|
|
|
enable_comments=False,
|
|
|
|
template_name="",
|
|
|
|
registration_required=True,
|
|
|
|
)
|
|
|
|
cls.fp1.sites.add(cls.site1)
|
|
|
|
cls.fp2.sites.add(cls.site1)
|
|
|
|
cls.fp3.sites.add(cls.site1)
|
|
|
|
cls.fp4.sites.add(cls.site1)
|
2010-08-27 21:55:11 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client = Client(enforce_csrf_checks=True)
|
|
|
|
|
|
|
|
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/")
|
|
|
|
self.assertContains(response, "<p>Isn't it flat!</p>")
|
|
|
|
|
|
|
|
def test_view_non_existent_flatpage(self):
|
2017-02-03 09:43:21 +08:00
|
|
|
"""
|
|
|
|
A nonexistent flatpage raises 404 when served through a view, even when
|
|
|
|
the middleware is in use.
|
|
|
|
"""
|
2010-08-27 21:55:11 +08:00
|
|
|
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/")
|
2016-02-06 04:56:52 +08:00
|
|
|
user = User.objects.create_user("testuser", "test@example.com", "s3krit")
|
|
|
|
self.client.force_login(user)
|
2010-08-28 19:59:14 +08:00
|
|
|
response = self.client.get("/flatpage_root/sekrit/")
|
|
|
|
self.assertContains(response, "<p>Isn't it sekrit!</p>")
|
2010-08-27 21:55:11 +08:00
|
|
|
|
|
|
|
def test_fallback_flatpage(self):
|
2014-03-02 22:25:53 +08:00
|
|
|
"A flatpage can be served by the fallback middleware"
|
2010-08-27 21:55:11 +08:00
|
|
|
response = self.client.get("/flatpage/")
|
|
|
|
self.assertContains(response, "<p>Isn't it flat!</p>")
|
|
|
|
|
|
|
|
def test_fallback_non_existent_flatpage(self):
|
2017-02-03 09:43:21 +08:00
|
|
|
"""
|
|
|
|
A nonexistent flatpage raises a 404 when served by the fallback
|
|
|
|
middleware.
|
|
|
|
"""
|
2010-08-27 21:55:11 +08:00
|
|
|
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_post_view_flatpage(self):
|
|
|
|
"""
|
|
|
|
POSTing to a flatpage served through a view will raise a CSRF error if
|
|
|
|
no token is provided.
|
2022-02-04 15:08:27 +08:00
|
|
|
"""
|
2010-08-27 21:55:11 +08:00
|
|
|
response = self.client.post("/flatpage_root/flatpage/")
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
2010-08-27 21:55:11 +08:00
|
|
|
|
|
|
|
def test_post_fallback_flatpage(self):
|
|
|
|
"""
|
|
|
|
POSTing to a flatpage served by the middleware will raise a CSRF error
|
|
|
|
if no token is provided.
|
2022-02-04 15:08:27 +08:00
|
|
|
"""
|
2010-08-27 21:55:11 +08:00
|
|
|
response = self.client.post("/flatpage/")
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
2010-08-27 21:55:11 +08:00
|
|
|
|
|
|
|
def test_post_unknown_page(self):
|
|
|
|
"POSTing to an unknown page isn't caught as a 403 CSRF error"
|
|
|
|
response = self.client.post("/no_such_page/")
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 404)
|