summaryrefslogtreecommitdiff
path: root/tests/syndication_tests
diff options
context:
space:
mode:
authorUnai Zalakain <unai@gisa-elkartea.org>2015-08-21 11:50:43 +0200
committerTim Graham <timograham@gmail.com>2015-09-18 18:31:58 -0400
commitaac2a2d2ae2486342058db0c72ed7ba2c7c8eb1e (patch)
tree9d36cf0b889eceb832aee1ffbeea0a826a557b22 /tests/syndication_tests
parent71ebcb85b931f43865df5b322b2cf06d3da23f69 (diff)
downloaddjango-aac2a2d2ae2486342058db0c72ed7ba2c7c8eb1e.tar.gz
Fixed #13110 -- Added support for multiple enclosures in Atom feeds.
The ``item_enclosures`` hook returns a list of ``Enclosure`` objects which is then used by the feed builder. If the feed is a RSS feed, an exception is raised as RSS feeds don't allow multiple enclosures per feed item. The ``item_enclosures`` hook defaults to an empty list or, if the ``item_enclosure_url`` hook is defined, to a list with a single ``Enclosure`` built from the ``item_enclosure_url``, ``item_enclosure_length``, and ``item_enclosure_mime_type`` hooks.
Diffstat (limited to 'tests/syndication_tests')
-rw-r--r--tests/syndication_tests/feeds.py50
-rw-r--r--tests/syndication_tests/tests.py57
-rw-r--r--tests/syndication_tests/urls.py4
3 files changed, 104 insertions, 7 deletions
diff --git a/tests/syndication_tests/feeds.py b/tests/syndication_tests/feeds.py
index 6408838b68..ce938b8356 100644
--- a/tests/syndication_tests/feeds.py
+++ b/tests/syndication_tests/feeds.py
@@ -88,8 +88,29 @@ class ArticlesFeed(TestRss2Feed):
return Article.objects.all()
-class TestEnclosureFeed(TestRss2Feed):
- pass
+class TestSingleEnclosureRSSFeed(TestRss2Feed):
+ """
+ A feed to test that RSS feeds work with a single enclosure.
+ """
+ def item_enclosure_url(self, item):
+ return 'http://example.com'
+
+ def item_enclosure_size(self, item):
+ return 0
+
+ def item_mime_type(self, item):
+ return 'image/png'
+
+
+class TestMultipleEnclosureRSSFeed(TestRss2Feed):
+ """
+ A feed to test that RSS feeds raise an exception with multiple enclosures.
+ """
+ def item_enclosures(self, item):
+ return [
+ feedgenerator.Enclosure('http://example.com/hello.png', 0, 'image/png'),
+ feedgenerator.Enclosure('http://example.com/goodbye.png', 0, 'image/png'),
+ ]
class TemplateFeed(TestRss2Feed):
@@ -165,3 +186,28 @@ class MyCustomAtom1Feed(feedgenerator.Atom1Feed):
class TestCustomFeed(TestAtomFeed):
feed_type = MyCustomAtom1Feed
+
+
+class TestSingleEnclosureAtomFeed(TestAtomFeed):
+ """
+ A feed to test that Atom feeds work with a single enclosure.
+ """
+ def item_enclosure_url(self, item):
+ return 'http://example.com'
+
+ def item_enclosure_size(self, item):
+ return 0
+
+ def item_mime_type(self, item):
+ return 'image/png'
+
+
+class TestMultipleEnclosureAtomFeed(TestAtomFeed):
+ """
+ A feed to test that Atom feeds work with multiple enclosures.
+ """
+ def item_enclosures(self, item):
+ return [
+ feedgenerator.Enclosure('http://example.com/hello.png', 0, 'image/png'),
+ feedgenerator.Enclosure('http://example.com/goodbye.png', 0, 'image/png'),
+ ]
diff --git a/tests/syndication_tests/tests.py b/tests/syndication_tests/tests.py
index 78c8708f9e..5a7cfbb057 100644
--- a/tests/syndication_tests/tests.py
+++ b/tests/syndication_tests/tests.py
@@ -9,7 +9,10 @@ from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase, override_settings
from django.test.utils import requires_tz_support
from django.utils import timezone
-from django.utils.feedgenerator import rfc2822_date, rfc3339_date
+from django.utils.deprecation import RemovedInDjango20Warning
+from django.utils.feedgenerator import (
+ Enclosure, SyndicationFeed, rfc2822_date, rfc3339_date,
+)
from .models import Article, Entry
@@ -63,10 +66,6 @@ class FeedTestCase(TestCase):
set(expected)
)
-######################################
-# Feed view
-######################################
-
@override_settings(ROOT_URLCONF='syndication_tests.urls')
class SyndicationFeedTest(FeedTestCase):
@@ -186,6 +185,22 @@ class SyndicationFeedTest(FeedTestCase):
item.getElementsByTagName('guid')[0].attributes.get(
'isPermaLink').value, "true")
+ def test_rss2_single_enclosure(self):
+ response = self.client.get('/syndication/rss2/single-enclosure/')
+ doc = minidom.parseString(response.content)
+ chan = doc.getElementsByTagName('rss')[0].getElementsByTagName('channel')[0]
+ items = chan.getElementsByTagName('item')
+ for item in items:
+ enclosures = item.getElementsByTagName('enclosure')
+ self.assertEqual(len(enclosures), 1)
+
+ def test_rss2_multiple_enclosures(self):
+ with self.assertRaisesMessage(ValueError, (
+ "RSS feed items may only have one enclosure, see "
+ "http://www.rssboard.org/rss-profile#element-channel-item-enclosure"
+ )):
+ self.client.get('/syndication/rss2/multiple-enclosure/')
+
def test_rss091_feed(self):
"""
Test the structure and content of feeds generated by RssUserland091Feed.
@@ -284,6 +299,24 @@ class SyndicationFeedTest(FeedTestCase):
self.assertNotEqual(published, updated)
+ def test_atom_single_enclosure(self):
+ response = self.client.get('/syndication/rss2/single-enclosure/')
+ feed = minidom.parseString(response.content).firstChild
+ items = feed.getElementsByTagName('entry')
+ for item in items:
+ links = item.getElementsByTagName('link')
+ links = [link for link in links if link.getAttribute('rel') == 'enclosure']
+ self.assertEqual(len(links), 1)
+
+ def test_atom_multiple_enclosures(self):
+ response = self.client.get('/syndication/rss2/single-enclosure/')
+ feed = minidom.parseString(response.content).firstChild
+ items = feed.getElementsByTagName('entry')
+ for item in items:
+ links = item.getElementsByTagName('link')
+ links = [link for link in links if link.getAttribute('rel') == 'enclosure']
+ self.assertEqual(len(links), 2)
+
def test_latest_post_date(self):
"""
Test that both the published and updated dates are
@@ -493,3 +526,17 @@ class SyndicationFeedTest(FeedTestCase):
views.add_domain('example.com', '//example.com/foo/?arg=value'),
'http://example.com/foo/?arg=value'
)
+
+
+class FeedgeneratorTestCase(TestCase):
+ def test_add_item_warns_when_enclosure_kwarg_is_used(self):
+ feed = SyndicationFeed(title='Example', link='http://example.com', description='Foo')
+ with self.assertRaisesMessage(RemovedInDjango20Warning, (
+ 'The enclosure keyword argument is deprecated, use enclosures instead.'
+ )):
+ feed.add_item(
+ title='Example Item',
+ link='https://example.com/item',
+ description='bar',
+ enclosure=Enclosure('http://example.com/favicon.ico', 0, 'image/png'),
+ )
diff --git a/tests/syndication_tests/urls.py b/tests/syndication_tests/urls.py
index d3caeae888..09f7e789cd 100644
--- a/tests/syndication_tests/urls.py
+++ b/tests/syndication_tests/urls.py
@@ -19,4 +19,8 @@ urlpatterns = [
url(r'^syndication/articles/$', feeds.ArticlesFeed()),
url(r'^syndication/template/$', feeds.TemplateFeed()),
url(r'^syndication/template_context/$', feeds.TemplateContextFeed()),
+ url(r'^syndication/rss2/single-enclosure/$', feeds.TestSingleEnclosureRSSFeed()),
+ url(r'^syndication/rss2/multiple-enclosure/$', feeds.TestMultipleEnclosureRSSFeed()),
+ url(r'^syndication/atom/single-enclosure/$', feeds.TestSingleEnclosureAtomFeed()),
+ url(r'^syndication/atom/multiple-enclosure/$', feeds.TestMultipleEnclosureAtomFeed()),
]