diff options
| author | Baptiste Mispelon <bmispelon@gmail.com> | 2024-04-26 22:10:40 +0200 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2024-06-18 17:25:43 +0200 |
| commit | 62300b81cf7687d176af3b40aa6101942801292d (patch) | |
| tree | 12190b41d01e55f0799a439a6a690d7c644e0464 /tests/syndication_tests/tests.py | |
| parent | ce1ad98565d98b7939367b2bcab755c3555ceb42 (diff) | |
Fixed #12978 -- Added support for RSS feed stylesheets.
Diffstat (limited to 'tests/syndication_tests/tests.py')
| -rw-r--r-- | tests/syndication_tests/tests.py | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/tests/syndication_tests/tests.py b/tests/syndication_tests/tests.py index a68ed879db..6403f7461a 100644 --- a/tests/syndication_tests/tests.py +++ b/tests/syndication_tests/tests.py @@ -4,12 +4,16 @@ from xml.dom import minidom from django.contrib.sites.models import Site from django.contrib.syndication import views from django.core.exceptions import ImproperlyConfigured +from django.templatetags.static import static from django.test import TestCase, override_settings from django.test.utils import requires_tz_support +from django.urls import reverse, reverse_lazy from django.utils import timezone from django.utils.feedgenerator import ( Atom1Feed, Rss201rev2Feed, + Stylesheet, + SyndicationFeed, rfc2822_date, rfc3339_date, ) @@ -561,6 +565,125 @@ class SyndicationFeedTest(FeedTestCase): doc = feed.writeString("utf-8") self.assertIn(f'<{tag} href="https://feed.url.com" rel="self"/>', doc) + def test_stylesheets_none(self): + feed = Rss201rev2Feed( + title="test", + link="https://example.com", + description="test", + stylesheets=None, + ) + self.assertNotIn("xml-stylesheet", feed.writeString("utf-8")) + + def test_stylesheets(self): + testdata = [ + # Plain strings. + ("/test.xsl", 'href="/test.xsl" type="text/xsl" media="screen"'), + ("/test.xslt", 'href="/test.xslt" type="text/xsl" media="screen"'), + ("/test.css", 'href="/test.css" type="text/css" media="screen"'), + ("/test", 'href="/test" media="screen"'), + ( + "https://example.com/test.xsl", + 'href="https://example.com/test.xsl" type="text/xsl" media="screen"', + ), + ( + "https://example.com/test.css", + 'href="https://example.com/test.css" type="text/css" media="screen"', + ), + ( + "https://example.com/test", + 'href="https://example.com/test" media="screen"', + ), + ("/♥.xsl", 'href="/%E2%99%A5.xsl" type="text/xsl" media="screen"'), + ( + static("stylesheet.xsl"), + 'href="/static/stylesheet.xsl" type="text/xsl" media="screen"', + ), + ( + static("stylesheet.css"), + 'href="/static/stylesheet.css" type="text/css" media="screen"', + ), + (static("stylesheet"), 'href="/static/stylesheet" media="screen"'), + ( + reverse("syndication-xsl-stylesheet"), + 'href="/syndication/stylesheet.xsl" type="text/xsl" media="screen"', + ), + ( + reverse_lazy("syndication-xsl-stylesheet"), + 'href="/syndication/stylesheet.xsl" type="text/xsl" media="screen"', + ), + # Stylesheet objects. + ( + Stylesheet("/test.xsl"), + 'href="/test.xsl" type="text/xsl" media="screen"', + ), + (Stylesheet("/test.xsl", mimetype=None), 'href="/test.xsl" media="screen"'), + (Stylesheet("/test.xsl", media=None), 'href="/test.xsl" type="text/xsl"'), + (Stylesheet("/test.xsl", mimetype=None, media=None), 'href="/test.xsl"'), + ( + Stylesheet("/test.xsl", mimetype="text/xml"), + 'href="/test.xsl" type="text/xml" media="screen"', + ), + ] + for stylesheet, expected in testdata: + feed = Rss201rev2Feed( + title="test", + link="https://example.com", + description="test", + stylesheets=[stylesheet], + ) + doc = feed.writeString("utf-8") + with self.subTest(expected=expected): + self.assertIn(f"<?xml-stylesheet {expected}?>", doc) + + def test_stylesheets_instructions_are_at_the_top(self): + response = self.client.get("/syndication/stylesheet/") + doc = minidom.parseString(response.content) + self.assertEqual(doc.childNodes[0].nodeName, "xml-stylesheet") + self.assertEqual( + doc.childNodes[0].data, + 'href="/stylesheet1.xsl" type="text/xsl" media="screen"', + ) + self.assertEqual(doc.childNodes[1].nodeName, "xml-stylesheet") + self.assertEqual( + doc.childNodes[1].data, + 'href="/stylesheet2.xsl" type="text/xsl" media="screen"', + ) + + def test_stylesheets_typeerror_if_str_or_stylesheet(self): + for stylesheet, error_message in [ + ("/stylesheet.xsl", "stylesheets should be a list, not <class 'str'>"), + ( + Stylesheet("/stylesheet.xsl"), + "stylesheets should be a list, " + "not <class 'django.utils.feedgenerator.Stylesheet'>", + ), + ]: + args = ("title", "/link", "description") + with self.subTest(stylesheets=stylesheet): + self.assertRaisesMessage( + TypeError, + error_message, + SyndicationFeed, + *args, + stylesheets=stylesheet, + ) + + def test_stylesheets_repr(self): + testdata = [ + (Stylesheet("/test.xsl", mimetype=None), "('/test.xsl', None, 'screen')"), + (Stylesheet("/test.xsl", media=None), "('/test.xsl', 'text/xsl', None)"), + ( + Stylesheet("/test.xsl", mimetype=None, media=None), + "('/test.xsl', None, None)", + ), + ( + Stylesheet("/test.xsl", mimetype="text/xml"), + "('/test.xsl', 'text/xml', 'screen')", + ), + ] + for stylesheet, expected in testdata: + self.assertEqual(repr(stylesheet), expected) + @requires_tz_support def test_feed_last_modified_time_naive_date(self): """ |
