From e70bd151792ff9a4e3f4d4914574621bdbc0f6f2 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Thu, 28 Apr 2016 15:48:13 +0100 Subject: [PATCH] Tests for Page.dummy_request, including failing test for #2499 --- wagtail/wagtailcore/tests/test_page_model.py | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/wagtail/wagtailcore/tests/test_page_model.py b/wagtail/wagtailcore/tests/test_page_model.py index 639e82f63..20cbecd69 100644 --- a/wagtail/wagtailcore/tests/test_page_model.py +++ b/wagtail/wagtailcore/tests/test_page_model.py @@ -1169,3 +1169,33 @@ class TestIssue2024(TestCase): # Check that the content_type changed to Page self.assertEqual(event_index.content_type, ContentType.objects.get_for_model(Page)) + + +class TestDummyRequest(TestCase): + fixtures = ['test.json'] + + def test_dummy_request_for_accessible_page(self): + event_index = Page.objects.get(url_path='/home/events/') + request = event_index.dummy_request() + + # request should have the correct path and hostname for this page + self.assertEqual(request.path, '/events/') + self.assertEqual(request.META['HTTP_HOST'], 'localhost') + + @override_settings(ALLOWED_HOSTS=['production.example.com']) + def test_dummy_request_for_inaccessible_page_should_use_valid_host(self): + root_page = Page.objects.get(url_path='/') + request = root_page.dummy_request() + + # in the absence of an actual Site record where we can access this page, + # dummy_request should still provide a hostname that Django's host header + # validation won't reject + self.assertEqual(request.META['HTTP_HOST'], 'production.example.com') + + @override_settings(ALLOWED_HOSTS=['*']) + def test_dummy_request_for_inaccessible_page_with_wildcard_allowed_hosts(self): + root_page = Page.objects.get(url_path='/') + request = root_page.dummy_request() + + # '*' is not a valid hostname, so ensure that we replace it with something sensible + self.assertNotEqual(request.META['HTTP_HOST'], '*')