2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import absolute_import, unicode_literals
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2010-10-11 20:20:07 +08:00
|
|
|
from xml.dom import minidom
|
|
|
|
|
2011-04-02 16:44:47 +08:00
|
|
|
from django.contrib.syndication import views
|
2010-01-28 21:46:18 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2008-03-20 14:35:53 +08:00
|
|
|
from django.test import TestCase
|
2009-04-08 05:20:14 +08:00
|
|
|
from django.utils import tzinfo
|
2010-03-15 23:25:39 +08:00
|
|
|
from django.utils.feedgenerator import rfc2822_date, rfc3339_date
|
2010-10-11 20:20:07 +08:00
|
|
|
|
2011-10-14 05:34:56 +08:00
|
|
|
from .models import Entry
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2008-03-20 14:35:53 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
class FeedTestCase(TestCase):
|
2008-08-12 04:49:19 +08:00
|
|
|
fixtures = ['feeddata.json']
|
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
def assertChildNodes(self, elem, expected):
|
|
|
|
actual = set([n.nodeName for n in elem.childNodes])
|
|
|
|
expected = set(expected)
|
|
|
|
self.assertEqual(actual, expected)
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
def assertChildNodeContent(self, elem, expected):
|
|
|
|
for k, v in expected.items():
|
|
|
|
self.assertEqual(
|
|
|
|
elem.getElementsByTagName(k)[0].firstChild.wholeText, v)
|
|
|
|
|
|
|
|
def assertCategories(self, elem, expected):
|
2011-06-10 18:18:06 +08:00
|
|
|
self.assertEqual(set(i.firstChild.wholeText for i in elem.childNodes if i.nodeName == 'category'), set(expected))
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
######################################
|
|
|
|
# Feed view
|
|
|
|
######################################
|
|
|
|
|
|
|
|
class SyndicationFeedTest(FeedTestCase):
|
|
|
|
"""
|
|
|
|
Tests for the high-level syndication feed framework.
|
|
|
|
"""
|
2011-04-02 21:27:17 +08:00
|
|
|
urls = 'regressiontests.syndication.urls'
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
def test_rss2_feed(self):
|
|
|
|
"""
|
|
|
|
Test the structure and content of feeds generated by Rss201rev2Feed.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/syndication/rss2/')
|
2008-08-12 04:49:19 +08:00
|
|
|
doc = minidom.parseString(response.content)
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2008-08-13 06:12:14 +08:00
|
|
|
# Making sure there's only 1 `rss` element and that the correct
|
|
|
|
# RSS version was specified.
|
|
|
|
feed_elem = doc.getElementsByTagName('rss')
|
|
|
|
self.assertEqual(len(feed_elem), 1)
|
|
|
|
feed = feed_elem[0]
|
|
|
|
self.assertEqual(feed.getAttribute('version'), '2.0')
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2008-08-13 06:12:14 +08:00
|
|
|
# Making sure there's only one `channel` element w/in the
|
|
|
|
# `rss` element.
|
|
|
|
chan_elem = feed.getElementsByTagName('channel')
|
|
|
|
self.assertEqual(len(chan_elem), 1)
|
|
|
|
chan = chan_elem[0]
|
2010-03-15 23:25:39 +08:00
|
|
|
|
|
|
|
# Find the last build date
|
|
|
|
d = Entry.objects.latest('date').date
|
|
|
|
ltz = tzinfo.LocalTimezone(d)
|
|
|
|
last_build_date = rfc2822_date(d.replace(tzinfo=ltz))
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
self.assertChildNodes(chan, ['title', 'link', 'description', 'language', 'lastBuildDate', 'item', 'atom:link', 'ttl', 'copyright', 'category'])
|
|
|
|
self.assertChildNodeContent(chan, {
|
|
|
|
'title': 'My blog',
|
|
|
|
'description': 'A more thorough description of my blog.',
|
|
|
|
'link': 'http://example.com/blog/',
|
|
|
|
'language': 'en',
|
2010-03-15 23:25:39 +08:00
|
|
|
'lastBuildDate': last_build_date,
|
2010-01-28 21:46:18 +08:00
|
|
|
#'atom:link': '',
|
|
|
|
'ttl': '600',
|
|
|
|
'copyright': 'Copyright (c) 2007, Sally Smith',
|
|
|
|
})
|
2011-06-10 18:18:06 +08:00
|
|
|
self.assertCategories(chan, ['python', 'django'])
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
# Ensure the content of the channel is correct
|
|
|
|
self.assertChildNodeContent(chan, {
|
|
|
|
'title': 'My blog',
|
|
|
|
'link': 'http://example.com/blog/',
|
|
|
|
})
|
|
|
|
|
|
|
|
# Check feed_url is passed
|
|
|
|
self.assertEqual(
|
|
|
|
chan.getElementsByTagName('atom:link')[0].getAttribute('href'),
|
|
|
|
'http://example.com/syndication/rss2/'
|
|
|
|
)
|
|
|
|
|
2010-03-15 23:25:39 +08:00
|
|
|
# Find the pubdate of the first feed item
|
|
|
|
d = Entry.objects.get(pk=1).date
|
|
|
|
ltz = tzinfo.LocalTimezone(d)
|
|
|
|
pub_date = rfc2822_date(d.replace(tzinfo=ltz))
|
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
items = chan.getElementsByTagName('item')
|
|
|
|
self.assertEqual(len(items), Entry.objects.count())
|
2010-01-28 21:46:18 +08:00
|
|
|
self.assertChildNodeContent(items[0], {
|
|
|
|
'title': 'My first entry',
|
|
|
|
'description': 'Overridden description: My first entry',
|
|
|
|
'link': 'http://example.com/blog/1/',
|
|
|
|
'guid': 'http://example.com/blog/1/',
|
2010-03-15 23:25:39 +08:00
|
|
|
'pubDate': pub_date,
|
2010-01-28 21:46:18 +08:00
|
|
|
'author': 'test@example.com (Sally Smith)',
|
|
|
|
})
|
2011-06-10 18:18:06 +08:00
|
|
|
self.assertCategories(items[0], ['python', 'testing'])
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
for item in items:
|
2010-01-28 21:46:18 +08:00
|
|
|
self.assertChildNodes(item, ['title', 'link', 'description', 'guid', 'category', 'pubDate', 'author'])
|
|
|
|
|
|
|
|
def test_rss091_feed(self):
|
|
|
|
"""
|
|
|
|
Test the structure and content of feeds generated by RssUserland091Feed.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/syndication/rss091/')
|
2008-08-12 04:49:19 +08:00
|
|
|
doc = minidom.parseString(response.content)
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
# Making sure there's only 1 `rss` element and that the correct
|
|
|
|
# RSS version was specified.
|
|
|
|
feed_elem = doc.getElementsByTagName('rss')
|
|
|
|
self.assertEqual(len(feed_elem), 1)
|
|
|
|
feed = feed_elem[0]
|
|
|
|
self.assertEqual(feed.getAttribute('version'), '0.91')
|
|
|
|
|
|
|
|
# Making sure there's only one `channel` element w/in the
|
|
|
|
# `rss` element.
|
|
|
|
chan_elem = feed.getElementsByTagName('channel')
|
|
|
|
self.assertEqual(len(chan_elem), 1)
|
|
|
|
chan = chan_elem[0]
|
|
|
|
self.assertChildNodes(chan, ['title', 'link', 'description', 'language', 'lastBuildDate', 'item', 'atom:link', 'ttl', 'copyright', 'category'])
|
|
|
|
|
|
|
|
# Ensure the content of the channel is correct
|
|
|
|
self.assertChildNodeContent(chan, {
|
|
|
|
'title': 'My blog',
|
|
|
|
'link': 'http://example.com/blog/',
|
|
|
|
})
|
|
|
|
self.assertCategories(chan, ['python', 'django'])
|
|
|
|
|
|
|
|
# Check feed_url is passed
|
|
|
|
self.assertEqual(
|
|
|
|
chan.getElementsByTagName('atom:link')[0].getAttribute('href'),
|
|
|
|
'http://example.com/syndication/rss091/'
|
|
|
|
)
|
|
|
|
|
|
|
|
items = chan.getElementsByTagName('item')
|
|
|
|
self.assertEqual(len(items), Entry.objects.count())
|
|
|
|
self.assertChildNodeContent(items[0], {
|
|
|
|
'title': 'My first entry',
|
|
|
|
'description': 'Overridden description: My first entry',
|
|
|
|
'link': 'http://example.com/blog/1/',
|
|
|
|
})
|
|
|
|
for item in items:
|
|
|
|
self.assertChildNodes(item, ['title', 'link', 'description'])
|
|
|
|
self.assertCategories(item, [])
|
|
|
|
|
|
|
|
def test_atom_feed(self):
|
|
|
|
"""
|
|
|
|
Test the structure and content of feeds generated by Atom1Feed.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/syndication/atom/')
|
|
|
|
feed = minidom.parseString(response.content).firstChild
|
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
self.assertEqual(feed.nodeName, 'feed')
|
2010-01-28 21:46:18 +08:00
|
|
|
self.assertEqual(feed.getAttribute('xmlns'), 'http://www.w3.org/2005/Atom')
|
|
|
|
self.assertChildNodes(feed, ['title', 'subtitle', 'link', 'id', 'updated', 'entry', 'rights', 'category', 'author'])
|
|
|
|
for link in feed.getElementsByTagName('link'):
|
|
|
|
if link.getAttribute('rel') == 'self':
|
|
|
|
self.assertEqual(link.getAttribute('href'), 'http://example.com/syndication/atom/')
|
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
entries = feed.getElementsByTagName('entry')
|
|
|
|
self.assertEqual(len(entries), Entry.objects.count())
|
|
|
|
for entry in entries:
|
2010-01-28 21:46:18 +08:00
|
|
|
self.assertChildNodes(entry, ['title', 'link', 'id', 'summary', 'category', 'updated', 'rights', 'author'])
|
2008-08-12 06:22:26 +08:00
|
|
|
summary = entry.getElementsByTagName('summary')[0]
|
|
|
|
self.assertEqual(summary.getAttribute('type'), 'html')
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
def test_custom_feed_generator(self):
|
2010-01-28 21:46:18 +08:00
|
|
|
response = self.client.get('/syndication/custom/')
|
|
|
|
feed = minidom.parseString(response.content).firstChild
|
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
self.assertEqual(feed.nodeName, 'feed')
|
|
|
|
self.assertEqual(feed.getAttribute('django'), 'rocks')
|
2010-01-28 21:46:18 +08:00
|
|
|
self.assertChildNodes(feed, ['title', 'subtitle', 'link', 'id', 'updated', 'entry', 'spam', 'rights', 'category', 'author'])
|
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
entries = feed.getElementsByTagName('entry')
|
|
|
|
self.assertEqual(len(entries), Entry.objects.count())
|
|
|
|
for entry in entries:
|
|
|
|
self.assertEqual(entry.getAttribute('bacon'), 'yum')
|
2010-01-28 21:46:18 +08:00
|
|
|
self.assertChildNodes(entry, ['title', 'link', 'id', 'summary', 'ministry', 'rights', 'author', 'updated', 'category'])
|
2008-08-12 06:22:26 +08:00
|
|
|
summary = entry.getElementsByTagName('summary')[0]
|
|
|
|
self.assertEqual(summary.getAttribute('type'), 'html')
|
2008-08-12 04:49:19 +08:00
|
|
|
|
2008-08-28 03:52:27 +08:00
|
|
|
def test_title_escaping(self):
|
|
|
|
"""
|
|
|
|
Tests that titles are escaped correctly in RSS feeds.
|
|
|
|
"""
|
2010-01-28 21:46:18 +08:00
|
|
|
response = self.client.get('/syndication/rss2/')
|
2008-08-28 03:52:27 +08:00
|
|
|
doc = minidom.parseString(response.content)
|
|
|
|
for item in doc.getElementsByTagName('item'):
|
|
|
|
link = item.getElementsByTagName('link')[0]
|
|
|
|
if link.firstChild.wholeText == 'http://example.com/blog/4/':
|
|
|
|
title = item.getElementsByTagName('title')[0]
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(title.firstChild.wholeText, 'A & B < C > D')
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2009-04-08 05:20:14 +08:00
|
|
|
def test_naive_datetime_conversion(self):
|
|
|
|
"""
|
|
|
|
Test that datetimes are correctly converted to the local time zone.
|
|
|
|
"""
|
|
|
|
# Naive date times passed in get converted to the local time zone, so
|
|
|
|
# check the recived zone offset against the local offset.
|
2010-01-28 21:46:18 +08:00
|
|
|
response = self.client.get('/syndication/naive-dates/')
|
2009-04-08 05:20:14 +08:00
|
|
|
doc = minidom.parseString(response.content)
|
2010-01-28 21:46:18 +08:00
|
|
|
updated = doc.getElementsByTagName('updated')[0].firstChild.wholeText
|
2010-03-15 23:25:39 +08:00
|
|
|
|
|
|
|
d = Entry.objects.latest('date').date
|
|
|
|
ltz = tzinfo.LocalTimezone(d)
|
|
|
|
latest = rfc3339_date(d.replace(tzinfo=ltz))
|
|
|
|
|
|
|
|
self.assertEqual(updated, latest)
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2009-04-08 05:20:14 +08:00
|
|
|
def test_aware_datetime_conversion(self):
|
|
|
|
"""
|
|
|
|
Test that datetimes with timezones don't get trodden on.
|
|
|
|
"""
|
2010-01-28 21:46:18 +08:00
|
|
|
response = self.client.get('/syndication/aware-dates/')
|
2009-04-08 05:20:14 +08:00
|
|
|
doc = minidom.parseString(response.content)
|
|
|
|
updated = doc.getElementsByTagName('updated')[0].firstChild.wholeText
|
|
|
|
self.assertEqual(updated[-6:], '+00:42')
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2012-10-01 02:44:27 +08:00
|
|
|
def test_feed_last_modified_time(self):
|
|
|
|
response = self.client.get('/syndication/naive-dates/')
|
|
|
|
self.assertEqual(response['Last-Modified'], 'Thu, 03 Jan 2008 19:30:00 GMT')
|
|
|
|
|
|
|
|
# No last-modified when feed has no item_pubdate
|
|
|
|
response = self.client.get('/syndication/no_pubdate/')
|
|
|
|
self.assertFalse(response.has_header('Last-Modified'))
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
def test_feed_url(self):
|
|
|
|
"""
|
|
|
|
Test that the feed_url can be overridden.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/syndication/feedurl/')
|
|
|
|
doc = minidom.parseString(response.content)
|
|
|
|
for link in doc.getElementsByTagName('link'):
|
|
|
|
if link.getAttribute('rel') == 'self':
|
|
|
|
self.assertEqual(link.getAttribute('href'), 'http://example.com/customfeedurl/')
|
|
|
|
|
2010-10-08 22:14:05 +08:00
|
|
|
def test_secure_urls(self):
|
|
|
|
"""
|
|
|
|
Test URLs are prefixed with https:// when feed is requested over HTTPS.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/syndication/rss2/', **{
|
|
|
|
'wsgi.url_scheme': 'https',
|
|
|
|
})
|
|
|
|
doc = minidom.parseString(response.content)
|
|
|
|
chan = doc.getElementsByTagName('channel')[0]
|
|
|
|
self.assertEqual(
|
|
|
|
chan.getElementsByTagName('link')[0].firstChild.wholeText[0:5],
|
|
|
|
'https'
|
|
|
|
)
|
|
|
|
atom_link = chan.getElementsByTagName('atom:link')[0]
|
|
|
|
self.assertEqual(atom_link.getAttribute('href')[0:5], 'https')
|
|
|
|
for link in doc.getElementsByTagName('link'):
|
|
|
|
if link.getAttribute('rel') == 'self':
|
|
|
|
self.assertEqual(link.getAttribute('href')[0:5], 'https')
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
def test_item_link_error(self):
|
|
|
|
"""
|
|
|
|
Test that a ImproperlyConfigured is raised if no link could be found
|
|
|
|
for the item(s).
|
|
|
|
"""
|
|
|
|
self.assertRaises(ImproperlyConfigured,
|
|
|
|
self.client.get,
|
|
|
|
'/syndication/articles/')
|
|
|
|
|
|
|
|
def test_template_feed(self):
|
|
|
|
"""
|
|
|
|
Test that the item title and description can be overridden with
|
|
|
|
templates.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/syndication/template/')
|
|
|
|
doc = minidom.parseString(response.content)
|
|
|
|
feed = doc.getElementsByTagName('rss')[0]
|
|
|
|
chan = feed.getElementsByTagName('channel')[0]
|
|
|
|
items = chan.getElementsByTagName('item')
|
|
|
|
|
|
|
|
self.assertChildNodeContent(items[0], {
|
|
|
|
'title': 'Title in your templates: My first entry',
|
|
|
|
'description': 'Description in your templates: My first entry',
|
|
|
|
'link': 'http://example.com/blog/1/',
|
|
|
|
})
|
|
|
|
|
|
|
|
def test_add_domain(self):
|
|
|
|
"""
|
|
|
|
Test add_domain() prefixes domains onto the correct URLs.
|
|
|
|
"""
|
|
|
|
self.assertEqual(
|
|
|
|
views.add_domain('example.com', '/foo/?arg=value'),
|
|
|
|
'http://example.com/foo/?arg=value'
|
|
|
|
)
|
2010-10-08 22:14:05 +08:00
|
|
|
self.assertEqual(
|
|
|
|
views.add_domain('example.com', '/foo/?arg=value', True),
|
|
|
|
'https://example.com/foo/?arg=value'
|
|
|
|
)
|
2010-01-28 21:46:18 +08:00
|
|
|
self.assertEqual(
|
|
|
|
views.add_domain('example.com', 'http://djangoproject.com/doc/'),
|
|
|
|
'http://djangoproject.com/doc/'
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
views.add_domain('example.com', 'https://djangoproject.com/doc/'),
|
|
|
|
'https://djangoproject.com/doc/'
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
views.add_domain('example.com', 'mailto:uhoh@djangoproject.com'),
|
|
|
|
'mailto:uhoh@djangoproject.com'
|
|
|
|
)
|
2011-11-19 06:54:24 +08:00
|
|
|
self.assertEqual(
|
|
|
|
views.add_domain('example.com', '//example.com/foo/?arg=value'),
|
|
|
|
'http://example.com/foo/?arg=value'
|
|
|
|
)
|