1
# -*- coding: utf-8 -*-
3
from xml.dom import minidom
4
from django.test import TestCase
5
from django.test.client import Client
6
from models import Entry
10
from sets import Set as set
12
class SyndicationFeedTest(TestCase):
13
fixtures = ['feeddata.json']
15
def assertChildNodes(self, elem, expected):
16
actual = set([n.nodeName for n in elem.childNodes])
17
expected = set(expected)
18
self.assertEqual(actual, expected)
20
def test_rss_feed(self):
21
response = self.client.get('/syndication/feeds/rss/')
22
doc = minidom.parseString(response.content)
24
# Making sure there's only 1 `rss` element and that the correct
25
# RSS version was specified.
26
feed_elem = doc.getElementsByTagName('rss')
27
self.assertEqual(len(feed_elem), 1)
29
self.assertEqual(feed.getAttribute('version'), '2.0')
31
# Making sure there's only one `channel` element w/in the
33
chan_elem = feed.getElementsByTagName('channel')
34
self.assertEqual(len(chan_elem), 1)
36
self.assertChildNodes(chan, ['title', 'link', 'description', 'language', 'lastBuildDate', 'item'])
38
items = chan.getElementsByTagName('item')
39
self.assertEqual(len(items), Entry.objects.count())
41
self.assertChildNodes(item, ['title', 'link', 'description', 'guid'])
43
def test_atom_feed(self):
44
response = self.client.get('/syndication/feeds/atom/')
45
doc = minidom.parseString(response.content)
48
self.assertEqual(feed.nodeName, 'feed')
49
self.assertEqual(feed.getAttribute('xmlns'), 'http://www.w3.org/2005/Atom')
50
self.assertChildNodes(feed, ['title', 'link', 'id', 'updated', 'entry'])
52
entries = feed.getElementsByTagName('entry')
53
self.assertEqual(len(entries), Entry.objects.count())
55
self.assertChildNodes(entry, ['title', 'link', 'id', 'summary'])
56
summary = entry.getElementsByTagName('summary')[0]
57
self.assertEqual(summary.getAttribute('type'), 'html')
59
def test_custom_feed_generator(self):
60
response = self.client.get('/syndication/feeds/custom/')
61
doc = minidom.parseString(response.content)
64
self.assertEqual(feed.nodeName, 'feed')
65
self.assertEqual(feed.getAttribute('django'), 'rocks')
66
self.assertChildNodes(feed, ['title', 'link', 'id', 'updated', 'entry', 'spam'])
68
entries = feed.getElementsByTagName('entry')
69
self.assertEqual(len(entries), Entry.objects.count())
71
self.assertEqual(entry.getAttribute('bacon'), 'yum')
72
self.assertChildNodes(entry, ['title', 'link', 'id', 'summary', 'ministry'])
73
summary = entry.getElementsByTagName('summary')[0]
74
self.assertEqual(summary.getAttribute('type'), 'html')
76
def test_complex_base_url(self):
78
Tests that that the base url for a complex feed doesn't raise a 500
81
response = self.client.get('/syndication/feeds/complex/')
82
self.assertEquals(response.status_code, 404)
84
def test_title_escaping(self):
86
Tests that titles are escaped correctly in RSS feeds.
88
response = self.client.get('/syndication/feeds/rss/')
89
doc = minidom.parseString(response.content)
90
for item in doc.getElementsByTagName('item'):
91
link = item.getElementsByTagName('link')[0]
92
if link.firstChild.wholeText == 'http://example.com/blog/4/':
93
title = item.getElementsByTagName('title')[0]
94
self.assertEquals(title.firstChild.wholeText, u'A & B < C > D')
b'\\ No newline at end of file'