Unit tests for new page serve methods introduced in #144

This commit is contained in:
Matt Westcott 2014-03-26 17:04:01 +00:00
parent 0157d4fb94
commit 341cacea54
5 changed files with 61 additions and 3 deletions

View file

@ -43,7 +43,7 @@
"show_in_menus": true,
"live": true,
"depth": 3,
"content_type": ["tests", "simplepage"],
"content_type": ["tests", "eventindex"],
"path": "000100010001",
"url_path": "/home/events/",
"slug": "events"
@ -51,9 +51,9 @@
},
{
"pk": 3,
"model": "tests.simplepage",
"model": "tests.eventindex",
"fields": {
"content": "Look at our lovely events."
"intro": "Look at our lovely events."
}
},

View file

@ -180,3 +180,19 @@ EventPage.promote_panels = [
MultiFieldPanel(COMMON_PANELS, "Common page configuration"),
ImageChooserPanel('feed_image'),
]
# Event index (has a separate AJAX template, and a custom template context)
class EventIndex(Page):
intro = RichTextField(blank=True)
ajax_template = 'tests/includes/event_listing.html'
def get_context(self, request):
context = super(EventIndex, self).get_context(request)
context['events'] = EventPage.objects.filter(live=True)
return context
EventIndex.content_panels = [
FieldPanel('title', classname="full title"),
FieldPanel('intro', classname="full"),
]

View file

@ -0,0 +1,10 @@
<!DOCTYPE HTML>
<html>
<head>
<title>{{ self.title }}</title>
</head>
<body>
<h1>{{ self.title }}</h1>
{% include "tests/includes/event_listing.html" %}
</body>
</html>

View file

@ -0,0 +1,7 @@
{% load pageurl %}
<ul>
{% for event in events %}
<li><a href="{% pageurl event %}">{{ event.title }}</a></li>
{% endfor %}
</ul>

View file

@ -99,6 +99,13 @@ class TestRouting(TestCase):
class TestServeView(TestCase):
fixtures = ['test.json']
def setUp(self):
# Explicitly clear the cache of site root paths. Normally this would be kept
# in sync by the Site.save logic, but this is bypassed when the database is
# rolled back between tests using transactions.
from django.core.cache import cache
cache.delete('wagtail_site_root_paths')
def test_serve(self):
response = self.client.get('/events/christmas/')
@ -136,6 +143,24 @@ class TestServeView(TestCase):
response = c.get('/christmas/', HTTP_HOST='localhost')
self.assertEqual(response.status_code, 404)
def test_serve_with_custom_context(self):
response = self.client.get('/events/')
self.assertEqual(response.status_code, 200)
# should render the whole page
self.assertContains(response, '<h1>Events</h1>')
# response should contain data from the custom 'events' context variable
self.assertContains(response, '<a href="/events/christmas/">Christmas</a>')
def test_ajax_response(self):
response = self.client.get('/events/', HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.status_code, 200)
# should only render the content of includes/event_listing.html, not the whole page
self.assertNotContains(response, '<h1>Events</h1>')
self.assertContains(response, '<a href="/events/christmas/">Christmas</a>')
class TestPagePermission(TestCase):
fixtures = ['test.json']