diff options
Diffstat (limited to 'tests/regressiontests/syndication')
7 files changed, 404 insertions, 94 deletions
diff --git a/tests/regressiontests/syndication/feeds.py b/tests/regressiontests/syndication/feeds.py index 79837f9459..5563170d6f 100644 --- a/tests/regressiontests/syndication/feeds.py +++ b/tests/regressiontests/syndication/feeds.py @@ -1,66 +1,142 @@ +from django.contrib.syndication import feeds, views from django.core.exceptions import ObjectDoesNotExist -from django.contrib.syndication import feeds -from django.utils.feedgenerator import Atom1Feed -from django.utils import tzinfo +from django.utils import feedgenerator, tzinfo +from models import Article, Entry -class ComplexFeed(feeds.Feed): - def get_object(self, bits): - if len(bits) != 1: + +class ComplexFeed(views.Feed): + def get_object(self, request, foo=None): + if foo is not None: raise ObjectDoesNotExist return None -class TestRssFeed(feeds.Feed): - link = "/blog/" + +class TestRss2Feed(views.Feed): title = 'My blog' - + description = 'A more thorough description of my blog.' + link = '/blog/' + feed_guid = '/foo/bar/1234' + author_name = 'Sally Smith' + author_email = 'test@example.com' + author_link = 'http://www.example.com/' + categories = ('python', 'django') + feed_copyright = 'Copyright (c) 2007, Sally Smith' + ttl = 600 + def items(self): - from models import Entry return Entry.objects.all() - - def item_link(self, item): - return "/blog/%s/" % item.pk -class TestAtomFeed(TestRssFeed): - feed_type = Atom1Feed + def item_description(self, item): + return "Overridden description: %s" % item + + def item_pubdate(self, item): + return item.date + + item_author_name = 'Sally Smith' + item_author_email = 'test@example.com' + item_author_link = 'http://www.example.com/' + item_categories = ('python', 'testing') + item_copyright = 'Copyright (c) 2007, Sally Smith' + + +class TestRss091Feed(TestRss2Feed): + feed_type = feedgenerator.RssUserland091Feed + + +class TestAtomFeed(TestRss2Feed): + feed_type = feedgenerator.Atom1Feed + subtitle = TestRss2Feed.description + + +class ArticlesFeed(TestRss2Feed): + """ + A feed to test no link being defined. Articles have no get_absolute_url() + method, and item_link() is not defined. + """ + def items(self): + return Article.objects.all() + + +class TestEnclosureFeed(TestRss2Feed): + pass + + +class TemplateFeed(TestRss2Feed): + """ + A feed to test defining item titles and descriptions with templates. + """ + title_template = 'syndication/title.html' + description_template = 'syndication/description.html' + + # Defining a template overrides any item_title definition + def item_title(self): + return "Not in a template" + + +class NaiveDatesFeed(TestAtomFeed): + """ + A feed with naive (non-timezone-aware) dates. + """ + def item_pubdate(self, item): + return item.date + + +class TZAwareDatesFeed(TestAtomFeed): + """ + A feed with timezone-aware dates. + """ + def item_pubdate(self, item): + # Provide a weird offset so that the test can know it's getting this + # specific offset and not accidentally getting on from + # settings.TIME_ZONE. + return item.date.replace(tzinfo=tzinfo.FixedOffset(42)) + + +class TestFeedUrlFeed(TestAtomFeed): + feed_url = 'http://example.com/customfeedurl/' + -class MyCustomAtom1Feed(Atom1Feed): +class MyCustomAtom1Feed(feedgenerator.Atom1Feed): """ Test of a custom feed generator class. - """ + """ def root_attributes(self): attrs = super(MyCustomAtom1Feed, self).root_attributes() attrs[u'django'] = u'rocks' return attrs - + def add_root_elements(self, handler): super(MyCustomAtom1Feed, self).add_root_elements(handler) handler.addQuickElement(u'spam', u'eggs') - + def item_attributes(self, item): attrs = super(MyCustomAtom1Feed, self).item_attributes(item) attrs[u'bacon'] = u'yum' return attrs - + def add_item_elements(self, handler, item): super(MyCustomAtom1Feed, self).add_item_elements(handler, item) handler.addQuickElement(u'ministry', u'silly walks') - + + class TestCustomFeed(TestAtomFeed): feed_type = MyCustomAtom1Feed - -class NaiveDatesFeed(TestAtomFeed): - """ - A feed with naive (non-timezone-aware) dates. - """ - def item_pubdate(self, item): - return item.date - -class TZAwareDatesFeed(TestAtomFeed): - """ - A feed with timezone-aware dates. - """ - def item_pubdate(self, item): - # Provide a weird offset so that the test can know it's getting this - # specific offset and not accidentally getting on from - # settings.TIME_ZONE. - return item.date.replace(tzinfo=tzinfo.FixedOffset(42))
\ No newline at end of file + + +class DeprecatedComplexFeed(feeds.Feed): + def get_object(self, bits): + if len(bits) != 1: + raise ObjectDoesNotExist + return None + + +class DeprecatedRssFeed(feeds.Feed): + link = "/blog/" + title = 'My blog' + + def items(self): + return Entry.objects.all() + + def item_link(self, item): + return "/blog/%s/" % item.pk + diff --git a/tests/regressiontests/syndication/fixtures/feeddata.json b/tests/regressiontests/syndication/fixtures/feeddata.json index 375ee16c41..4a5c02297a 100644 --- a/tests/regressiontests/syndication/fixtures/feeddata.json +++ b/tests/regressiontests/syndication/fixtures/feeddata.json @@ -30,5 +30,13 @@ "title": "A & B < C > D", "date": "2008-01-03 13:30:00" } + }, + { + "model": "syndication.article", + "pk": 1, + "fields": { + "title": "My first article", + "entry": "1" + } } -]
\ No newline at end of file +] diff --git a/tests/regressiontests/syndication/models.py b/tests/regressiontests/syndication/models.py index 99e14ade8a..54230b9623 100644 --- a/tests/regressiontests/syndication/models.py +++ b/tests/regressiontests/syndication/models.py @@ -3,6 +3,21 @@ from django.db import models class Entry(models.Model): title = models.CharField(max_length=200) date = models.DateTimeField() - + + class Meta: + ordering = ('date',) + def __unicode__(self): - return self.title
\ No newline at end of file + return self.title + + def get_absolute_url(self): + return "/blog/%s/" % self.pk + + +class Article(models.Model): + title = models.CharField(max_length=200) + entry = models.ForeignKey(Entry) + + def __unicode__(self): + return self.title + diff --git a/tests/regressiontests/syndication/templates/syndication/description.html b/tests/regressiontests/syndication/templates/syndication/description.html new file mode 100644 index 0000000000..85ec82c8df --- /dev/null +++ b/tests/regressiontests/syndication/templates/syndication/description.html @@ -0,0 +1 @@ +Description in your templates: {{ obj }}
\ No newline at end of file diff --git a/tests/regressiontests/syndication/templates/syndication/title.html b/tests/regressiontests/syndication/templates/syndication/title.html new file mode 100644 index 0000000000..eb17969714 --- /dev/null +++ b/tests/regressiontests/syndication/templates/syndication/title.html @@ -0,0 +1 @@ +Title in your templates: {{ obj }}
\ No newline at end of file diff --git a/tests/regressiontests/syndication/tests.py b/tests/regressiontests/syndication/tests.py index 816cb44675..8949bc9dc9 100644 --- a/tests/regressiontests/syndication/tests.py +++ b/tests/regressiontests/syndication/tests.py @@ -1,17 +1,17 @@ -# -*- coding: utf-8 -*- - import datetime -from xml.dom import minidom +from django.contrib.syndication import feeds, views +from django.core.exceptions import ImproperlyConfigured from django.test import TestCase -from django.test.client import Client from django.utils import tzinfo from models import Entry +from xml.dom import minidom + try: set except NameError: from sets import Set as set -class SyndicationFeedTest(TestCase): +class FeedTestCase(TestCase): fixtures = ['feeddata.json'] def assertChildNodes(self, elem, expected): @@ -19,101 +19,300 @@ class SyndicationFeedTest(TestCase): expected = set(expected) self.assertEqual(actual, expected) - def test_rss_feed(self): - response = self.client.get('/syndication/feeds/rss/') + 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): + self.assertEqual(set(i.firstChild.wholeText for i in elem.childNodes if i.nodeName == 'category'), set(expected)); + +###################################### +# Feed view +###################################### + +class SyndicationFeedTest(FeedTestCase): + """ + Tests for the high-level syndication feed framework. + """ + + def test_rss2_feed(self): + """ + Test the structure and content of feeds generated by Rss201rev2Feed. + """ + response = self.client.get('/syndication/rss2/') doc = minidom.parseString(response.content) - + # 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') - + # 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']) - + 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', + 'lastBuildDate': 'Thu, 03 Jan 2008 13:30:00 -0600', + #'atom:link': '', + 'ttl': '600', + 'copyright': 'Copyright (c) 2007, Sally Smith', + }) + self.assertCategories(chan, ['python', 'django']); + + # 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/' + ) + 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/', + 'guid': 'http://example.com/blog/1/', + 'pubDate': 'Tue, 01 Jan 2008 12:30:00 -0600', + 'author': 'test@example.com (Sally Smith)', + }) + self.assertCategories(items[0], ['python', 'testing']); + for item in items: - self.assertChildNodes(item, ['title', 'link', 'description', 'guid']) - - def test_atom_feed(self): - response = self.client.get('/syndication/feeds/atom/') + 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/') doc = minidom.parseString(response.content) - - feed = doc.firstChild + + # 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 + self.assertEqual(feed.nodeName, 'feed') - self.assertEqual(feed.getAttribute('xmlns'), 'http://www.w3.org/2005/Atom') - self.assertChildNodes(feed, ['title', 'link', 'id', 'updated', 'entry']) - + 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/') + entries = feed.getElementsByTagName('entry') self.assertEqual(len(entries), Entry.objects.count()) for entry in entries: - self.assertChildNodes(entry, ['title', 'link', 'id', 'summary']) + self.assertChildNodes(entry, ['title', 'link', 'id', 'summary', 'category', 'updated', 'rights', 'author']) summary = entry.getElementsByTagName('summary')[0] self.assertEqual(summary.getAttribute('type'), 'html') - + def test_custom_feed_generator(self): - response = self.client.get('/syndication/feeds/custom/') - doc = minidom.parseString(response.content) - - feed = doc.firstChild + response = self.client.get('/syndication/custom/') + feed = minidom.parseString(response.content).firstChild + self.assertEqual(feed.nodeName, 'feed') self.assertEqual(feed.getAttribute('django'), 'rocks') - self.assertChildNodes(feed, ['title', 'link', 'id', 'updated', 'entry', 'spam']) - + self.assertChildNodes(feed, ['title', 'subtitle', 'link', 'id', 'updated', 'entry', 'spam', 'rights', 'category', 'author']) + entries = feed.getElementsByTagName('entry') self.assertEqual(len(entries), Entry.objects.count()) for entry in entries: self.assertEqual(entry.getAttribute('bacon'), 'yum') - self.assertChildNodes(entry, ['title', 'link', 'id', 'summary', 'ministry']) + self.assertChildNodes(entry, ['title', 'link', 'id', 'summary', 'ministry', 'rights', 'author', 'updated', 'category']) summary = entry.getElementsByTagName('summary')[0] self.assertEqual(summary.getAttribute('type'), 'html') - - def test_complex_base_url(self): - """ - Tests that that the base url for a complex feed doesn't raise a 500 - exception. - """ - response = self.client.get('/syndication/feeds/complex/') - self.assertEquals(response.status_code, 404) def test_title_escaping(self): """ Tests that titles are escaped correctly in RSS feeds. """ - response = self.client.get('/syndication/feeds/rss/') + response = self.client.get('/syndication/rss2/') 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] self.assertEquals(title.firstChild.wholeText, u'A & B < C > D') - + 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. - response = self.client.get('/syndication/feeds/naive-dates/') + response = self.client.get('/syndication/naive-dates/') doc = minidom.parseString(response.content) - updated = doc.getElementsByTagName('updated')[0].firstChild.wholeText + updated = doc.getElementsByTagName('updated')[0].firstChild.wholeText tz = tzinfo.LocalTimezone(datetime.datetime.now()) now = datetime.datetime.now(tz) self.assertEqual(updated[-6:], str(now)[-6:]) - + def test_aware_datetime_conversion(self): """ Test that datetimes with timezones don't get trodden on. """ - response = self.client.get('/syndication/feeds/aware-dates/') + response = self.client.get('/syndication/aware-dates/') doc = minidom.parseString(response.content) updated = doc.getElementsByTagName('updated')[0].firstChild.wholeText self.assertEqual(updated[-6:], '+00:42') -
\ No newline at end of file + + 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/') + + 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' + ) + 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' + ) + + +###################################### +# Deprecated feeds +###################################### + +class DeprecatedSyndicationFeedTest(FeedTestCase): + """ + Tests for the deprecated API (feed() view and the feed_dict etc). + """ + + def test_empty_feed_dict(self): + """ + Test that an empty feed_dict raises a 404. + """ + response = self.client.get('/syndication/depr-feeds-empty/aware-dates/') + self.assertEquals(response.status_code, 404) + + def test_nonexistent_slug(self): + """ + Test that a non-existent slug raises a 404. + """ + response = self.client.get('/syndication/depr-feeds/foobar/') + self.assertEquals(response.status_code, 404) + + def test_rss_feed(self): + """ + A simple test for Rss201rev2Feed feeds generated by the deprecated + system. + """ + response = self.client.get('/syndication/depr-feeds/rss/') + doc = minidom.parseString(response.content) + feed = doc.getElementsByTagName('rss')[0] + self.assertEqual(feed.getAttribute('version'), '2.0') + + chan = feed.getElementsByTagName('channel')[0] + self.assertChildNodes(chan, ['title', 'link', 'description', 'language', 'lastBuildDate', 'item', 'atom:link']) + + items = chan.getElementsByTagName('item') + self.assertEqual(len(items), Entry.objects.count()) + + def test_complex_base_url(self): + """ + Tests that the base url for a complex feed doesn't raise a 500 + exception. + """ + response = self.client.get('/syndication/depr-feeds/complex/') + self.assertEquals(response.status_code, 404) + diff --git a/tests/regressiontests/syndication/urls.py b/tests/regressiontests/syndication/urls.py index ec45026fc4..881fa48006 100644 --- a/tests/regressiontests/syndication/urls.py +++ b/tests/regressiontests/syndication/urls.py @@ -1,14 +1,24 @@ +from django.conf.urls.defaults import * + import feeds -from django.conf.urls.defaults import patterns feed_dict = { - 'complex': feeds.ComplexFeed, - 'rss': feeds.TestRssFeed, - 'atom': feeds.TestAtomFeed, - 'custom': feeds.TestCustomFeed, - 'naive-dates': feeds.NaiveDatesFeed, - 'aware-dates': feeds.TZAwareDatesFeed, + 'complex': feeds.DeprecatedComplexFeed, + 'rss': feeds.DeprecatedRssFeed, } -urlpatterns = patterns('', - (r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': feed_dict}) + +urlpatterns = patterns('django.contrib.syndication.views', + (r'^complex/(?P<foo>.*)/$', feeds.ComplexFeed()), + (r'^rss2/$', feeds.TestRss2Feed()), + (r'^rss091/$', feeds.TestRss091Feed()), + (r'^atom/$', feeds.TestAtomFeed()), + (r'^custom/$', feeds.TestCustomFeed()), + (r'^naive-dates/$', feeds.NaiveDatesFeed()), + (r'^aware-dates/$', feeds.TZAwareDatesFeed()), + (r'^feedurl/$', feeds.TestFeedUrlFeed()), + (r'^articles/$', feeds.ArticlesFeed()), + (r'^template/$', feeds.TemplateFeed()), + + (r'^depr-feeds/(?P<url>.*)/$', 'feed', {'feed_dict': feed_dict}), + (r'^depr-feeds-empty/(?P<url>.*)/$', 'feed', {'feed_dict': None}), ) |
