| 1 |
import datetime |
| 2 |
from xml.dom import minidom |
| 3 |
|
| 4 |
from django.contrib.sites.models import Site |
| 5 |
from django.contrib.syndication import views |
| 6 |
from django.core.exceptions import ImproperlyConfigured |
| 7 |
from django.templatetags.static import static |
| 8 |
from django.test import TestCase, override_settings |
| 9 |
from django.test.utils import requires_tz_support |
| 10 |
from django.urls import reverse, reverse_lazy |
| 11 |
from django.utils import timezone |
| 12 |
from django.utils.feedgenerator import ( |
| 13 |
Atom1Feed, |
| 14 |
Rss201rev2Feed, |
| 15 |
Stylesheet, |
| 16 |
SyndicationFeed, |
| 17 |
rfc2822_date, |
| 18 |
rfc3339_date, |
| 19 |
) |
| 20 |
|
| 21 |
from .models import Article, Entry |
| 22 |
|
| 23 |
TZ = timezone.get_default_timezone() |
| 24 |
|
| 25 |
|
| 26 |
class FeedTestCase(TestCase): |
| 27 |
@classmethod |
| 28 |
def setUpTestData(cls): |
| 29 |
cls.e1 = Entry.objects.create( |
| 30 |
title="My first entry", |
| 31 |
updated=datetime.datetime(1980, 1, 1, 12, 30), |
| 32 |
published=datetime.datetime(1986, 9, 25, 20, 15, 00), |
| 33 |
) |
| 34 |
cls.e2 = Entry.objects.create( |
| 35 |
title="My second entry", |
| 36 |
updated=datetime.datetime(2008, 1, 2, 12, 30), |
| 37 |
published=datetime.datetime(2006, 3, 17, 18, 0), |
| 38 |
) |
| 39 |
cls.e3 = Entry.objects.create( |
| 40 |
title="My third entry", |
| 41 |
updated=datetime.datetime(2008, 1, 2, 13, 30), |
| 42 |
published=datetime.datetime(2005, 6, 14, 10, 45), |
| 43 |
) |
| 44 |
cls.e4 = Entry.objects.create( |
| 45 |
title="A & B < C > D", |
| 46 |
updated=datetime.datetime(2008, 1, 3, 13, 30), |
| 47 |
published=datetime.datetime(2005, 11, 25, 12, 11, 23), |
| 48 |
) |
| 49 |
cls.e5 = Entry.objects.create( |
| 50 |
title="My last entry", |
| 51 |
updated=datetime.datetime(2013, 1, 20, 0, 0), |
| 52 |
published=datetime.datetime(2013, 3, 25, 20, 0), |
| 53 |
) |
| 54 |
cls.a1 = Article.objects.create( |
| 55 |
title="My first article", |
| 56 |
entry=cls.e1, |
| 57 |
updated=datetime.datetime(1986, 11, 21, 9, 12, 18), |
| 58 |
published=datetime.datetime(1986, 10, 21, 9, 12, 18), |
| 59 |
) |
| 60 |
|
| 61 |
def assertChildNodes(self, elem, expected): |
| 62 |
actual = {n.nodeName for n in elem.childNodes} |
| 63 |
expected = set(expected) |
| 64 |
self.assertEqual(actual, expected) |
| 65 |
|
| 66 |
def assertChildNodeContent(self, elem, expected): |
| 67 |
for k, v in expected.items(): |
| 68 |
self.assertEqual(elem.getElementsByTagName(k)[0].firstChild.wholeText, v) |
| 69 |
|
| 70 |
def assertCategories(self, elem, expected): |
| 71 |
self.assertEqual( |
| 72 |
{ |
| 73 |
i.firstChild.wholeText |
| 74 |
for i in elem.childNodes |
| 75 |
if i.nodeName == "category" |
| 76 |
}, |
| 77 |
set(expected), |
| 78 |
) |
| 79 |
|
| 80 |
|
| 81 |
@override_settings(ROOT_URLCONF="syndication_tests.urls") |
| 82 |
class SyndicationFeedTest(FeedTestCase): |
| 83 |
""" |
| 84 |
Tests for the high-level syndication feed framework. |
| 85 |
""" |
| 86 |
|
| 87 |
@classmethod |
| 88 |
def setUpClass(cls): |
| 89 |
super().setUpClass() |
| 90 |
# This cleanup is necessary because contrib.sites cache |
| 91 |
# makes tests interfere with each other, see #11505 |
| 92 |
Site.objects.clear_cache() |
| 93 |
|
| 94 |
def test_rss2_feed(self): |
| 95 |
""" |
| 96 |
Test the structure and content of feeds generated by Rss201rev2Feed. |
| 97 |
""" |
| 98 |
response = self.client.get("/syndication/rss2/") |
| 99 |
doc = minidom.parseString(response.content) |
| 100 |
|
| 101 |
# Making sure there's only 1 `rss` element and that the correct |
| 102 |
# RSS version was specified. |
| 103 |
feed_elem = doc.getElementsByTagName("rss") |
| 104 |
self.assertEqual(len(feed_elem), 1) |
| 105 |
feed = feed_elem[0] |
| 106 |
self.assertEqual(feed.getAttribute("version"), "2.0") |
| 107 |
self.assertEqual( |
| 108 |
feed.getElementsByTagName("language")[0].firstChild.nodeValue, "en" |
| 109 |
) |
| 110 |
|
| 111 |
# Making sure there's only one `channel` element w/in the |
| 112 |
# `rss` element. |
| 113 |
chan_elem = feed.getElementsByTagName("channel") |
| 114 |
self.assertEqual(len(chan_elem), 1) |
| 115 |
chan = chan_elem[0] |
| 116 |
|
| 117 |
# Find the last build date |
| 118 |
d = Entry.objects.latest("published").published |
| 119 |
last_build_date = rfc2822_date(timezone.make_aware(d, TZ)) |
| 120 |
|
| 121 |
self.assertChildNodes( |
| 122 |
chan, |
| 123 |
[ |
| 124 |
"title", |
| 125 |
"link", |
| 126 |
"description", |
| 127 |
"language", |
| 128 |
"lastBuildDate", |
| 129 |
"item", |
| 130 |
"atom:link", |
| 131 |
"ttl", |
| 132 |
"copyright", |
| 133 |
"category", |
| 134 |
], |
| 135 |
) |
| 136 |
self.assertChildNodeContent( |
| 137 |
chan, |
| 138 |
{ |
| 139 |
"title": "My blog", |
| 140 |
"description": "A more thorough description of my blog.", |
| 141 |
"link": "http://example.com/blog/", |
| 142 |
"language": "en", |
| 143 |
"lastBuildDate": last_build_date, |
| 144 |
"ttl": "600", |
| 145 |
"copyright": "Copyright (c) 2007, Sally Smith", |
| 146 |
}, |
| 147 |
) |
| 148 |
self.assertCategories(chan, ["python", "django"]) |
| 149 |
|
| 150 |
# Ensure the content of the channel is correct |
| 151 |
self.assertChildNodeContent( |
| 152 |
chan, |
| 153 |
{ |
| 154 |
"title": "My blog", |
| 155 |
"link": "http://example.com/blog/", |
| 156 |
}, |
| 157 |
) |
| 158 |
|
| 159 |
# Check feed_url is passed |
| 160 |
self.assertEqual( |
| 161 |
chan.getElementsByTagName("atom:link")[0].getAttribute("href"), |
| 162 |
"http://example.com/syndication/rss2/", |
| 163 |
) |
| 164 |
|
| 165 |
# Find the pubdate of the first feed item |
| 166 |
d = Entry.objects.get(pk=self.e1.pk).published |
| 167 |
pub_date = rfc2822_date(timezone.make_aware(d, TZ)) |
| 168 |
|
| 169 |
items = chan.getElementsByTagName("item") |
| 170 |
self.assertEqual(len(items), Entry.objects.count()) |
| 171 |
self.assertChildNodeContent( |
| 172 |
items[0], |
| 173 |
{ |
| 174 |
"title": "My first entry", |
| 175 |
"description": "Overridden description: My first entry", |
| 176 |
"link": "http://example.com/blog/%s/" % self.e1.pk, |
| 177 |
"guid": "http://example.com/blog/%s/" % self.e1.pk, |
| 178 |
"pubDate": pub_date, |
| 179 |
"author": "[email protected] (Sally Smith)", |
| 180 |
"comments": "/blog/%s/comments" % self.e1.pk, |
| 181 |
}, |
| 182 |
) |
| 183 |
self.assertCategories(items[0], ["python", "testing"]) |
| 184 |
for item in items: |
| 185 |
self.assertChildNodes( |
| 186 |
item, |
| 187 |
[ |
| 188 |
"title", |
| 189 |
"link", |
| 190 |
"description", |
| 191 |
"guid", |
| 192 |
"category", |
| 193 |
"pubDate", |
| 194 |
"author", |
| 195 |
"comments", |
| 196 |
], |
| 197 |
) |
| 198 |
# Assert that <guid> does not have any 'isPermaLink' attribute |
| 199 |
self.assertIsNone( |
| 200 |
item.getElementsByTagName("guid")[0].attributes.get("isPermaLink") |
| 201 |
) |
| 202 |
|
| 203 |
def test_rss2_feed_with_callable_object(self): |
| 204 |
response = self.client.get("/syndication/rss2/with-callable-object/") |
| 205 |
doc = minidom.parseString(response.content) |
| 206 |
chan = doc.getElementsByTagName("rss")[0].getElementsByTagName("channel")[0] |
| 207 |
self.assertChildNodeContent(chan, {"ttl": "700"}) |
| 208 |
|
| 209 |
def test_rss2_feed_with_decorated_methods(self): |
| 210 |
response = self.client.get("/syndication/rss2/with-decorated-methods/") |
| 211 |
doc = minidom.parseString(response.content) |
| 212 |
chan = doc.getElementsByTagName("rss")[0].getElementsByTagName("channel")[0] |
| 213 |
self.assertCategories(chan, ["javascript", "vue"]) |
| 214 |
self.assertChildNodeContent( |
| 215 |
chan, |
| 216 |
{ |
| 217 |
"title": "Overridden title -- decorated by @wraps.", |
| 218 |
"description": "Overridden description -- decorated by @wraps.", |
| 219 |
"ttl": "800 -- decorated by @wraps.", |
| 220 |
"copyright": "Copyright (c) 2022, John Doe -- decorated by @wraps.", |
| 221 |
}, |
| 222 |
) |
| 223 |
items = chan.getElementsByTagName("item") |
| 224 |
self.assertChildNodeContent( |
| 225 |
items[0], |
| 226 |
{ |
| 227 |
"title": ( |
| 228 |
f"Overridden item title: {self.e1.title} -- decorated by @wraps." |
| 229 |
), |
| 230 |
"description": "Overridden item description -- decorated by @wraps.", |
| 231 |
}, |
| 232 |
) |
| 233 |
|
| 234 |
def test_rss2_feed_with_wrong_decorated_methods(self): |
| 235 |
msg = ( |
| 236 |
"Feed method 'item_description' decorated by 'wrapper' needs to use " |
| 237 |
"@functools.wraps." |
| 238 |
) |
| 239 |
with self.assertRaisesMessage(ImproperlyConfigured, msg): |
| 240 |
self.client.get("/syndication/rss2/with-wrong-decorated-methods/") |
| 241 |
|
| 242 |
def test_rss2_feed_guid_permalink_false(self): |
| 243 |
""" |
| 244 |
Test if the 'isPermaLink' attribute of <guid> element of an item |
| 245 |
in the RSS feed is 'false'. |
| 246 |
""" |
| 247 |
response = self.client.get("/syndication/rss2/guid_ispermalink_false/") |
| 248 |
doc = minidom.parseString(response.content) |
| 249 |
chan = doc.getElementsByTagName("rss")[0].getElementsByTagName("channel")[0] |
| 250 |
items = chan.getElementsByTagName("item") |
| 251 |
for item in items: |
| 252 |
self.assertEqual( |
| 253 |
item.getElementsByTagName("guid")[0] |
| 254 |
.attributes.get("isPermaLink") |
| 255 |
.value, |
| 256 |
"false", |
| 257 |
) |
| 258 |
|
| 259 |
def test_rss2_feed_guid_permalink_true(self): |
| 260 |
""" |
| 261 |
Test if the 'isPermaLink' attribute of <guid> element of an item |
| 262 |
in the RSS feed is 'true'. |
| 263 |
""" |
| 264 |
response = self.client.get("/syndication/rss2/guid_ispermalink_true/") |
| 265 |
doc = minidom.parseString(response.content) |
| 266 |
chan = doc.getElementsByTagName("rss")[0].getElementsByTagName("channel")[0] |
| 267 |
items = chan.getElementsByTagName("item") |
| 268 |
for item in items: |
| 269 |
self.assertEqual( |
| 270 |
item.getElementsByTagName("guid")[0] |
| 271 |
.attributes.get("isPermaLink") |
| 272 |
.value, |
| 273 |
"true", |
| 274 |
) |
| 275 |
|
| 276 |
def test_rss2_single_enclosure(self): |
| 277 |
response = self.client.get("/syndication/rss2/single-enclosure/") |
| 278 |
doc = minidom.parseString(response.content) |
| 279 |
chan = doc.getElementsByTagName("rss")[0].getElementsByTagName("channel")[0] |
| 280 |
items = chan.getElementsByTagName("item") |
| 281 |
for item in items: |
| 282 |
enclosures = item.getElementsByTagName("enclosure") |
| 283 |
self.assertEqual(len(enclosures), 1) |
| 284 |
|
| 285 |
def test_rss2_multiple_enclosures(self): |
| 286 |
with self.assertRaisesMessage( |
| 287 |
ValueError, |
| 288 |
"RSS feed items may only have one enclosure, see " |
| 289 |
"http://www.rssboard.org/rss-profile#element-channel-item-enclosure", |
| 290 |
): |
| 291 |
self.client.get("/syndication/rss2/multiple-enclosure/") |
| 292 |
|
| 293 |
def test_rss091_feed(self): |
| 294 |
""" |
| 295 |
Test the structure and content of feeds generated by RssUserland091Feed. |
| 296 |
""" |
| 297 |
response = self.client.get("/syndication/rss091/") |
| 298 |
doc = minidom.parseString(response.content) |
| 299 |
|
| 300 |
# Making sure there's only 1 `rss` element and that the correct |
| 301 |
# RSS version was specified. |
| 302 |
feed_elem = doc.getElementsByTagName("rss") |
| 303 |
self.assertEqual(len(feed_elem), 1) |
| 304 |
feed = feed_elem[0] |
| 305 |
self.assertEqual(feed.getAttribute("version"), "0.91") |
| 306 |
|
| 307 |
# Making sure there's only one `channel` element w/in the |
| 308 |
# `rss` element. |
| 309 |
chan_elem = feed.getElementsByTagName("channel") |
| 310 |
self.assertEqual(len(chan_elem), 1) |
| 311 |
chan = chan_elem[0] |
| 312 |
self.assertChildNodes( |
| 313 |
chan, |
| 314 |
[ |
| 315 |
"title", |
| 316 |
"link", |
| 317 |
"description", |
| 318 |
"language", |
| 319 |
"lastBuildDate", |
| 320 |
"item", |
| 321 |
"atom:link", |
| 322 |
"ttl", |
| 323 |
"copyright", |
| 324 |
"category", |
| 325 |
], |
| 326 |
) |
| 327 |
|
| 328 |
# Ensure the content of the channel is correct |
| 329 |
self.assertChildNodeContent( |
| 330 |
chan, |
| 331 |
{ |
| 332 |
"title": "My blog", |
| 333 |
"link": "http://example.com/blog/", |
| 334 |
}, |
| 335 |
) |
| 336 |
self.assertCategories(chan, ["python", "django"]) |
| 337 |
|
| 338 |
# Check feed_url is passed |
| 339 |
self.assertEqual( |
| 340 |
chan.getElementsByTagName("atom:link")[0].getAttribute("href"), |
| 341 |
"http://example.com/syndication/rss091/", |
| 342 |
) |
| 343 |
|
| 344 |
items = chan.getElementsByTagName("item") |
| 345 |
self.assertEqual(len(items), Entry.objects.count()) |
| 346 |
self.assertChildNodeContent( |
| 347 |
items[0], |
| 348 |
{ |
| 349 |
"title": "My first entry", |
| 350 |
"description": "Overridden description: My first entry", |
| 351 |
"link": "http://example.com/blog/%s/" % self.e1.pk, |
| 352 |
}, |
| 353 |
) |
| 354 |
for item in items: |
| 355 |
self.assertChildNodes(item, ["title", "link", "description"]) |
| 356 |
self.assertCategories(item, []) |
| 357 |
|
| 358 |
def test_atom_feed(self): |
| 359 |
""" |
| 360 |
Test the structure and content of feeds generated by Atom1Feed. |
| 361 |
""" |
| 362 |
response = self.client.get("/syndication/atom/") |
| 363 |
feed = minidom.parseString(response.content).firstChild |
| 364 |
|
| 365 |
self.assertEqual(feed.nodeName, "feed") |
| 366 |
self.assertEqual(feed.getAttribute("xmlns"), "http://www.w3.org/2005/Atom") |
| 367 |
self.assertChildNodes( |
| 368 |
feed, |
| 369 |
[ |
| 370 |
"title", |
| 371 |
"subtitle", |
| 372 |
"link", |
| 373 |
"id", |
| 374 |
"updated", |
| 375 |
"entry", |
| 376 |
"rights", |
| 377 |
"category", |
| 378 |
"author", |
| 379 |
], |
| 380 |
) |
| 381 |
for link in feed.getElementsByTagName("link"): |
| 382 |
if link.getAttribute("rel") == "self": |
| 383 |
self.assertEqual( |
| 384 |
link.getAttribute("href"), "http://example.com/syndication/atom/" |
| 385 |
) |
| 386 |
|
| 387 |
entries = feed.getElementsByTagName("entry") |
| 388 |
self.assertEqual(len(entries), Entry.objects.count()) |
| 389 |
for entry in entries: |
| 390 |
self.assertChildNodes( |
| 391 |
entry, |
| 392 |
[ |
| 393 |
"title", |
| 394 |
"link", |
| 395 |
"id", |
| 396 |
"summary", |
| 397 |
"category", |
| 398 |
"updated", |
| 399 |
"published", |
| 400 |
"rights", |
| 401 |
"author", |
| 402 |
], |
| 403 |
) |
| 404 |
summary = entry.getElementsByTagName("summary")[0] |
| 405 |
self.assertEqual(summary.getAttribute("type"), "html") |
| 406 |
|
| 407 |
def test_atom_feed_published_and_updated_elements(self): |
| 408 |
""" |
| 409 |
The published and updated elements are not |
| 410 |
the same and now adhere to RFC 4287. |
| 411 |
""" |
| 412 |
response = self.client.get("/syndication/atom/") |
| 413 |
feed = minidom.parseString(response.content).firstChild |
| 414 |
entries = feed.getElementsByTagName("entry") |
| 415 |
|
| 416 |
published = entries[0].getElementsByTagName("published")[0].firstChild.wholeText |
| 417 |
updated = entries[0].getElementsByTagName("updated")[0].firstChild.wholeText |
| 418 |
|
| 419 |
self.assertNotEqual(published, updated) |
| 420 |
|
| 421 |
def test_atom_single_enclosure(self): |
| 422 |
response = self.client.get("/syndication/atom/single-enclosure/") |
| 423 |
feed = minidom.parseString(response.content).firstChild |
| 424 |
items = feed.getElementsByTagName("entry") |
| 425 |
for item in items: |
| 426 |
links = item.getElementsByTagName("link") |
| 427 |
links = [link for link in links if link.getAttribute("rel") == "enclosure"] |
| 428 |
self.assertEqual(len(links), 1) |
| 429 |
|
| 430 |
def test_atom_multiple_enclosures(self): |
| 431 |
response = self.client.get("/syndication/atom/multiple-enclosure/") |
| 432 |
feed = minidom.parseString(response.content).firstChild |
| 433 |
items = feed.getElementsByTagName("entry") |
| 434 |
for item in items: |
| 435 |
links = item.getElementsByTagName("link") |
| 436 |
links = [link for link in links if link.getAttribute("rel") == "enclosure"] |
| 437 |
self.assertEqual(len(links), 2) |
| 438 |
|
| 439 |
def test_latest_post_date(self): |
| 440 |
""" |
| 441 |
Both the published and updated dates are |
| 442 |
considered when determining the latest post date. |
| 443 |
""" |
| 444 |
# this feed has a `published` element with the latest date |
| 445 |
response = self.client.get("/syndication/atom/") |
| 446 |
feed = minidom.parseString(response.content).firstChild |
| 447 |
updated = feed.getElementsByTagName("updated")[0].firstChild.wholeText |
| 448 |
|
| 449 |
d = Entry.objects.latest("published").published |
| 450 |
latest_published = rfc3339_date(timezone.make_aware(d, TZ)) |
| 451 |
|
| 452 |
self.assertEqual(updated, latest_published) |
| 453 |
|
| 454 |
# this feed has an `updated` element with the latest date |
| 455 |
response = self.client.get("/syndication/latest/") |
| 456 |
feed = minidom.parseString(response.content).firstChild |
| 457 |
updated = feed.getElementsByTagName("updated")[0].firstChild.wholeText |
| 458 |
|
| 459 |
d = Entry.objects.exclude(title="My last entry").latest("updated").updated |
| 460 |
latest_updated = rfc3339_date(timezone.make_aware(d, TZ)) |
| 461 |
|
| 462 |
self.assertEqual(updated, latest_updated) |
| 463 |
|
| 464 |
def test_custom_feed_generator(self): |
| 465 |
response = self.client.get("/syndication/custom/") |
| 466 |
feed = minidom.parseString(response.content).firstChild |
| 467 |
|
| 468 |
self.assertEqual(feed.nodeName, "feed") |
| 469 |
self.assertEqual(feed.getAttribute("django"), "rocks") |
| 470 |
self.assertChildNodes( |
| 471 |
feed, |
| 472 |
[ |
| 473 |
"title", |
| 474 |
"subtitle", |
| 475 |
"link", |
| 476 |
"id", |
| 477 |
"updated", |
| 478 |
"entry", |
| 479 |
"spam", |
| 480 |
"rights", |
| 481 |
"category", |
| 482 |
"author", |
| 483 |
], |
| 484 |
) |
| 485 |
|
| 486 |
entries = feed.getElementsByTagName("entry") |
| 487 |
self.assertEqual(len(entries), Entry.objects.count()) |
| 488 |
for entry in entries: |
| 489 |
self.assertEqual(entry.getAttribute("bacon"), "yum") |
| 490 |
self.assertChildNodes( |
| 491 |
entry, |
| 492 |
[ |
| 493 |
"title", |
| 494 |
"link", |
| 495 |
"id", |
| 496 |
"summary", |
| 497 |
"ministry", |
| 498 |
"rights", |
| 499 |
"author", |
| 500 |
"updated", |
| 501 |
"published", |
| 502 |
"category", |
| 503 |
], |
| 504 |
) |
| 505 |
summary = entry.getElementsByTagName("summary")[0] |
| 506 |
self.assertEqual(summary.getAttribute("type"), "html") |
| 507 |
|
| 508 |
def test_feed_generator_language_attribute(self): |
| 509 |
response = self.client.get("/syndication/language/") |
| 510 |
feed = minidom.parseString(response.content).firstChild |
| 511 |
self.assertEqual( |
| 512 |
feed.firstChild.getElementsByTagName("language")[0].firstChild.nodeValue, |
| 513 |
"de", |
| 514 |
) |
| 515 |
|
| 516 |
def test_title_escaping(self): |
| 517 |
""" |
| 518 |
Titles are escaped correctly in RSS feeds. |
| 519 |
""" |
| 520 |
response = self.client.get("/syndication/rss2/") |
| 521 |
doc = minidom.parseString(response.content) |
| 522 |
for item in doc.getElementsByTagName("item"): |
| 523 |
link = item.getElementsByTagName("link")[0] |
| 524 |
if link.firstChild.wholeText == "http://example.com/blog/4/": |
| 525 |
title = item.getElementsByTagName("title")[0] |
| 526 |
self.assertEqual(title.firstChild.wholeText, "A & B < C > D") |
| 527 |
|
| 528 |
def test_naive_datetime_conversion(self): |
| 529 |
""" |
| 530 |
Datetimes are correctly converted to the local time zone. |
| 531 |
""" |
| 532 |
# Naive date times passed in get converted to the local time zone, so |
| 533 |
# check the received zone offset against the local offset. |
| 534 |
response = self.client.get("/syndication/naive-dates/") |
| 535 |
doc = minidom.parseString(response.content) |
| 536 |
updated = doc.getElementsByTagName("updated")[0].firstChild.wholeText |
| 537 |
|
| 538 |
d = Entry.objects.latest("published").published |
| 539 |
latest = rfc3339_date(timezone.make_aware(d, TZ)) |
| 540 |
|
| 541 |
self.assertEqual(updated, latest) |
| 542 |
|
| 543 |
def test_aware_datetime_conversion(self): |
| 544 |
""" |
| 545 |
Datetimes with timezones don't get trodden on. |
| 546 |
""" |
| 547 |
response = self.client.get("/syndication/aware-dates/") |
| 548 |
doc = minidom.parseString(response.content) |
| 549 |
published = doc.getElementsByTagName("published")[0].firstChild.wholeText |
| 550 |
self.assertEqual(published[-6:], "+00:42") |
| 551 |
|
| 552 |
def test_feed_no_content_self_closing_tag(self): |
| 553 |
tests = [ |
| 554 |
(Atom1Feed, "link"), |
| 555 |
(Rss201rev2Feed, "atom:link"), |
| 556 |
] |
| 557 |
for feedgenerator, tag in tests: |
| 558 |
with self.subTest(feedgenerator=feedgenerator.__name__): |
| 559 |
feed = feedgenerator( |
| 560 |
title="title", |
| 561 |
link="https://example.com", |
| 562 |
description="self closing tags test", |
| 563 |
feed_url="https://feed.url.com", |
| 564 |
) |
| 565 |
doc = feed.writeString("utf-8") |
| 566 |
self.assertIn(f'<{tag} href="https://feed.url.com" rel="self"/>', doc) |
| 567 |
|
| 568 |
def test_stylesheets_none(self): |
| 569 |
feed = Rss201rev2Feed( |
| 570 |
title="test", |
| 571 |
link="https://example.com", |
| 572 |
description="test", |
| 573 |
stylesheets=None, |
| 574 |
) |
| 575 |
self.assertNotIn("xml-stylesheet", feed.writeString("utf-8")) |
| 576 |
|
| 577 |
def test_stylesheets(self): |
| 578 |
testdata = [ |
| 579 |
# Plain strings. |
| 580 |
("/test.xsl", 'href="/test.xsl" media="screen" type="text/xsl"'), |
| 581 |
("/test.xslt", 'href="/test.xslt" media="screen" type="text/xsl"'), |
| 582 |
("/test.css", 'href="/test.css" media="screen" type="text/css"'), |
| 583 |
("/test", 'href="/test" media="screen"'), |
| 584 |
( |
| 585 |
"https://example.com/test.xsl", |
| 586 |
'href="https://example.com/test.xsl" media="screen" type="text/xsl"', |
| 587 |
), |
| 588 |
( |
| 589 |
"https://example.com/test.css", |
| 590 |
'href="https://example.com/test.css" media="screen" type="text/css"', |
| 591 |
), |
| 592 |
( |
| 593 |
"https://example.com/test", |
| 594 |
'href="https://example.com/test" media="screen"', |
| 595 |
), |
| 596 |
("/♥.xsl", 'href="/%E2%99%A5.xsl" media="screen" type="text/xsl"'), |
| 597 |
( |
| 598 |
static("stylesheet.xsl"), |
| 599 |
'href="/static/stylesheet.xsl" media="screen" type="text/xsl"', |
| 600 |
), |
| 601 |
( |
| 602 |
static("stylesheet.css"), |
| 603 |
'href="/static/stylesheet.css" media="screen" type="text/css"', |
| 604 |
), |
| 605 |
(static("stylesheet"), 'href="/static/stylesheet" media="screen"'), |
| 606 |
( |
| 607 |
reverse("syndication-xsl-stylesheet"), |
| 608 |
'href="/syndication/stylesheet.xsl" media="screen" type="text/xsl"', |
| 609 |
), |
| 610 |
( |
| 611 |
reverse_lazy("syndication-xsl-stylesheet"), |
| 612 |
'href="/syndication/stylesheet.xsl" media="screen" type="text/xsl"', |
| 613 |
), |
| 614 |
# Stylesheet objects. |
| 615 |
( |
| 616 |
Stylesheet("/test.xsl"), |
| 617 |
'href="/test.xsl" media="screen" type="text/xsl"', |
| 618 |
), |
| 619 |
(Stylesheet("/test.xsl", mimetype=None), 'href="/test.xsl" media="screen"'), |
| 620 |
(Stylesheet("/test.xsl", media=None), 'href="/test.xsl" type="text/xsl"'), |
| 621 |
(Stylesheet("/test.xsl", mimetype=None, media=None), 'href="/test.xsl"'), |
| 622 |
( |
| 623 |
Stylesheet("/test.xsl", mimetype="text/xml"), |
| 624 |
'href="/test.xsl" media="screen" type="text/xml"', |
| 625 |
), |
| 626 |
] |
| 627 |
for stylesheet, expected in testdata: |
| 628 |
feed = Rss201rev2Feed( |
| 629 |
title="test", |
| 630 |
link="https://example.com", |
| 631 |
description="test", |
| 632 |
stylesheets=[stylesheet], |
| 633 |
) |
| 634 |
doc = feed.writeString("utf-8") |
| 635 |
with self.subTest(expected=expected): |
| 636 |
self.assertIn(f"<?xml-stylesheet {expected}?>", doc) |
| 637 |
|
| 638 |
def test_stylesheets_instructions_are_at_the_top(self): |
| 639 |
response = self.client.get("/syndication/stylesheet/") |
| 640 |
doc = minidom.parseString(response.content) |
| 641 |
self.assertEqual(doc.childNodes[0].nodeName, "xml-stylesheet") |
| 642 |
self.assertEqual( |
| 643 |
doc.childNodes[0].data, |
| 644 |
'href="/stylesheet1.xsl" media="screen" type="text/xsl"', |
| 645 |
) |
| 646 |
self.assertEqual(doc.childNodes[1].nodeName, "xml-stylesheet") |
| 647 |
self.assertEqual( |
| 648 |
doc.childNodes[1].data, |
| 649 |
'href="/stylesheet2.xsl" media="screen" type="text/xsl"', |
| 650 |
) |
| 651 |
|
| 652 |
def test_stylesheets_typeerror_if_str_or_stylesheet(self): |
| 653 |
for stylesheet, error_message in [ |
| 654 |
("/stylesheet.xsl", "stylesheets should be a list, not <class 'str'>"), |
| 655 |
( |
| 656 |
Stylesheet("/stylesheet.xsl"), |
| 657 |
"stylesheets should be a list, " |
| 658 |
"not <class 'django.utils.feedgenerator.Stylesheet'>", |
| 659 |
), |
| 660 |
]: |
| 661 |
args = ("title", "/link", "description") |
| 662 |
with self.subTest(stylesheets=stylesheet): |
| 663 |
self.assertRaisesMessage( |
| 664 |
TypeError, |
| 665 |
error_message, |
| 666 |
SyndicationFeed, |
| 667 |
*args, |
| 668 |
stylesheets=stylesheet, |
| 669 |
) |
| 670 |
|
| 671 |
def test_stylesheets_repr(self): |
| 672 |
testdata = [ |
| 673 |
(Stylesheet("/test.xsl", mimetype=None), "('/test.xsl', None, 'screen')"), |
| 674 |
(Stylesheet("/test.xsl", media=None), "('/test.xsl', 'text/xsl', None)"), |
| 675 |
( |
| 676 |
Stylesheet("/test.xsl", mimetype=None, media=None), |
| 677 |
"('/test.xsl', None, None)", |
| 678 |
), |
| 679 |
( |
| 680 |
Stylesheet("/test.xsl", mimetype="text/xml"), |
| 681 |
"('/test.xsl', 'text/xml', 'screen')", |
| 682 |
), |
| 683 |
] |
| 684 |
for stylesheet, expected in testdata: |
| 685 |
self.assertEqual(repr(stylesheet), expected) |
| 686 |
|
| 687 |
@requires_tz_support |
| 688 |
def test_feed_last_modified_time_naive_date(self): |
| 689 |
""" |
| 690 |
Tests the Last-Modified header with naive publication dates. |
| 691 |
""" |
| 692 |
response = self.client.get("/syndication/naive-dates/") |
| 693 |
self.assertEqual( |
| 694 |
response.headers["Last-Modified"], "Tue, 26 Mar 2013 01:00:00 GMT" |
| 695 |
) |
| 696 |
|
| 697 |
def test_feed_last_modified_time(self): |
| 698 |
""" |
| 699 |
Tests the Last-Modified header with aware publication dates. |
| 700 |
""" |
| 701 |
response = self.client.get("/syndication/aware-dates/") |
| 702 |
self.assertEqual( |
| 703 |
response.headers["Last-Modified"], "Mon, 25 Mar 2013 19:18:00 GMT" |
| 704 |
) |
| 705 |
|
| 706 |
# No last-modified when feed has no item_pubdate |
| 707 |
response = self.client.get("/syndication/no_pubdate/") |
| 708 |
self.assertFalse(response.has_header("Last-Modified")) |
| 709 |
|
| 710 |
def test_feed_url(self): |
| 711 |
""" |
| 712 |
The feed_url can be overridden. |
| 713 |
""" |
| 714 |
response = self.client.get("/syndication/feedurl/") |
| 715 |
doc = minidom.parseString(response.content) |
| 716 |
for link in doc.getElementsByTagName("link"): |
| 717 |
if link.getAttribute("rel") == "self": |
| 718 |
self.assertEqual( |
| 719 |
link.getAttribute("href"), "http://example.com/customfeedurl/" |
| 720 |
) |
| 721 |
|
| 722 |
def test_secure_urls(self): |
| 723 |
""" |
| 724 |
Test URLs are prefixed with https:// when feed is requested over HTTPS. |
| 725 |
""" |
| 726 |
response = self.client.get( |
| 727 |
"/syndication/rss2/", |
| 728 |
**{ |
| 729 |
"wsgi.url_scheme": "https", |
| 730 |
}, |
| 731 |
) |
| 732 |
doc = minidom.parseString(response.content) |
| 733 |
chan = doc.getElementsByTagName("channel")[0] |
| 734 |
self.assertEqual( |
| 735 |
chan.getElementsByTagName("link")[0].firstChild.wholeText[0:5], "https" |
| 736 |
) |
| 737 |
atom_link = chan.getElementsByTagName("atom:link")[0] |
| 738 |
self.assertEqual(atom_link.getAttribute("href")[0:5], "https") |
| 739 |
for link in doc.getElementsByTagName("link"): |
| 740 |
if link.getAttribute("rel") == "self": |
| 741 |
self.assertEqual(link.getAttribute("href")[0:5], "https") |
| 742 |
|
| 743 |
def test_item_link_error(self): |
| 744 |
""" |
| 745 |
An ImproperlyConfigured is raised if no link could be found for the |
| 746 |
item(s). |
| 747 |
""" |
| 748 |
msg = ( |
| 749 |
"Give your Article class a get_absolute_url() method, or define " |
| 750 |
"an item_link() method in your Feed class." |
| 751 |
) |
| 752 |
with self.assertRaisesMessage(ImproperlyConfigured, msg): |
| 753 |
self.client.get("/syndication/articles/") |
| 754 |
|
| 755 |
def test_template_feed(self): |
| 756 |
""" |
| 757 |
The item title and description can be overridden with templates. |
| 758 |
""" |
| 759 |
response = self.client.get("/syndication/template/") |
| 760 |
doc = minidom.parseString(response.content) |
| 761 |
feed = doc.getElementsByTagName("rss")[0] |
| 762 |
chan = feed.getElementsByTagName("channel")[0] |
| 763 |
items = chan.getElementsByTagName("item") |
| 764 |
|
| 765 |
self.assertChildNodeContent( |
| 766 |
items[0], |
| 767 |
{ |
| 768 |
"title": "Title in your templates: My first entry\n", |
| 769 |
"description": "Description in your templates: My first entry\n", |
| 770 |
"link": "http://example.com/blog/%s/" % self.e1.pk, |
| 771 |
}, |
| 772 |
) |
| 773 |
|
| 774 |
def test_template_context_feed(self): |
| 775 |
""" |
| 776 |
Custom context data can be passed to templates for title |
| 777 |
and description. |
| 778 |
""" |
| 779 |
response = self.client.get("/syndication/template_context/") |
| 780 |
doc = minidom.parseString(response.content) |
| 781 |
feed = doc.getElementsByTagName("rss")[0] |
| 782 |
chan = feed.getElementsByTagName("channel")[0] |
| 783 |
items = chan.getElementsByTagName("item") |
| 784 |
|
| 785 |
self.assertChildNodeContent( |
| 786 |
items[0], |
| 787 |
{ |
| 788 |
"title": "My first entry (foo is bar)\n", |
| 789 |
"description": "My first entry (foo is bar)\n", |
| 790 |
}, |
| 791 |
) |
| 792 |
|
| 793 |
def test_add_domain(self): |
| 794 |
""" |
| 795 |
add_domain() prefixes domains onto the correct URLs. |
| 796 |
""" |
| 797 |
prefix_domain_mapping = ( |
| 798 |
(("example.com", "/foo/?arg=value"), "http://example.com/foo/?arg=value"), |
| 799 |
( |
| 800 |
("example.com", "/foo/?arg=value", True), |
| 801 |
"https://example.com/foo/?arg=value", |
| 802 |
), |
| 803 |
( |
| 804 |
("example.com", "http://djangoproject.com/doc/"), |
| 805 |
"http://djangoproject.com/doc/", |
| 806 |
), |
| 807 |
( |
| 808 |
("example.com", "https://djangoproject.com/doc/"), |
| 809 |
"https://djangoproject.com/doc/", |
| 810 |
), |
| 811 |
( |
| 812 |
("example.com", "mailto:[email protected]"), |
| 813 |
"mailto:[email protected]", |
| 814 |
), |
| 815 |
( |
| 816 |
("example.com", "//example.com/foo/?arg=value"), |
| 817 |
"http://example.com/foo/?arg=value", |
| 818 |
), |
| 819 |
) |
| 820 |
for prefix in prefix_domain_mapping: |
| 821 |
with self.subTest(prefix=prefix): |
| 822 |
self.assertEqual(views.add_domain(*prefix[0]), prefix[1]) |
| 823 |
|
| 824 |
def test_get_object(self): |
| 825 |
response = self.client.get("/syndication/rss2/articles/%s/" % self.e1.pk) |
| 826 |
doc = minidom.parseString(response.content) |
| 827 |
feed = doc.getElementsByTagName("rss")[0] |
| 828 |
chan = feed.getElementsByTagName("channel")[0] |
| 829 |
items = chan.getElementsByTagName("item") |
| 830 |
|
| 831 |
self.assertChildNodeContent( |
| 832 |
items[0], |
| 833 |
{ |
| 834 |
"comments": "/blog/%s/article/%s/comments" % (self.e1.pk, self.a1.pk), |
| 835 |
"description": "Article description: My first article", |
| 836 |
"link": "http://example.com/blog/%s/article/%s/" |
| 837 |
% (self.e1.pk, self.a1.pk), |
| 838 |
"title": "Title: My first article", |
| 839 |
"pubDate": rfc2822_date(timezone.make_aware(self.a1.published, TZ)), |
| 840 |
}, |
| 841 |
) |
| 842 |
|
| 843 |
def test_get_non_existent_object(self): |
| 844 |
response = self.client.get("/syndication/rss2/articles/0/") |
| 845 |
self.assertEqual(response.status_code, 404) |