test_http.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. import os
  2. from datetime import date
  3. from django.contrib.sitemaps import Sitemap
  4. from django.contrib.sites.models import Site
  5. from django.core.exceptions import ImproperlyConfigured
  6. from django.test import ignore_warnings, modify_settings, override_settings
  7. from django.utils import translation
  8. from django.utils.deprecation import RemovedInDjango50Warning
  9. from django.utils.formats import localize
  10. from .base import SitemapTestsBase
  11. from .models import I18nTestModel, TestModel
  12. class HTTPSitemapTests(SitemapTestsBase):
  13. use_sitemap_err_msg = (
  14. "To use sitemaps, either enable the sites framework or pass a "
  15. "Site/RequestSite object in your view."
  16. )
  17. def test_simple_sitemap_index(self):
  18. "A simple sitemap index can be rendered"
  19. response = self.client.get("/simple/index.xml")
  20. expected_content = """<?xml version="1.0" encoding="UTF-8"?>
  21. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  22. <sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod></sitemap>
  23. </sitemapindex>
  24. """ % (
  25. self.base_url,
  26. date.today(),
  27. )
  28. self.assertXMLEqual(response.content.decode(), expected_content)
  29. def test_sitemap_not_callable(self):
  30. """A sitemap may not be callable."""
  31. response = self.client.get("/simple-not-callable/index.xml")
  32. expected_content = """<?xml version="1.0" encoding="UTF-8"?>
  33. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  34. <sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod></sitemap>
  35. </sitemapindex>
  36. """ % (
  37. self.base_url,
  38. date.today(),
  39. )
  40. self.assertXMLEqual(response.content.decode(), expected_content)
  41. def test_paged_sitemap(self):
  42. """A sitemap may have multiple pages."""
  43. response = self.client.get("/simple-paged/index.xml")
  44. expected_content = """<?xml version="1.0" encoding="UTF-8"?>
  45. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  46. <sitemap><loc>{0}/simple/sitemap-simple.xml</loc><lastmod>{1}</lastmod></sitemap><sitemap><loc>{0}/simple/sitemap-simple.xml?p=2</loc><lastmod>{1}</lastmod></sitemap>
  47. </sitemapindex>
  48. """.format(
  49. self.base_url, date.today()
  50. )
  51. self.assertXMLEqual(response.content.decode(), expected_content)
  52. @override_settings(
  53. TEMPLATES=[
  54. {
  55. "BACKEND": "django.template.backends.django.DjangoTemplates",
  56. "DIRS": [os.path.join(os.path.dirname(__file__), "templates")],
  57. }
  58. ]
  59. )
  60. def test_simple_sitemap_custom_lastmod_index(self):
  61. "A simple sitemap index can be rendered with a custom template"
  62. response = self.client.get("/simple/custom-lastmod-index.xml")
  63. expected_content = """<?xml version="1.0" encoding="UTF-8"?>
  64. <!-- This is a customised template -->
  65. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  66. <sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod></sitemap>
  67. </sitemapindex>
  68. """ % (
  69. self.base_url,
  70. date.today(),
  71. )
  72. self.assertXMLEqual(response.content.decode(), expected_content)
  73. def test_simple_sitemap_section(self):
  74. "A simple sitemap section can be rendered"
  75. response = self.client.get("/simple/sitemap-simple.xml")
  76. expected_content = (
  77. '<?xml version="1.0" encoding="UTF-8"?>\n'
  78. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
  79. 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
  80. "<url><loc>%s/location/</loc><lastmod>%s</lastmod>"
  81. "<changefreq>never</changefreq><priority>0.5</priority></url>\n"
  82. "</urlset>"
  83. ) % (
  84. self.base_url,
  85. date.today(),
  86. )
  87. self.assertXMLEqual(response.content.decode(), expected_content)
  88. def test_no_section(self):
  89. response = self.client.get("/simple/sitemap-simple2.xml")
  90. self.assertEqual(
  91. str(response.context["exception"]),
  92. "No sitemap available for section: 'simple2'",
  93. )
  94. self.assertEqual(response.status_code, 404)
  95. def test_empty_page(self):
  96. response = self.client.get("/simple/sitemap-simple.xml?p=0")
  97. self.assertEqual(str(response.context["exception"]), "Page 0 empty")
  98. self.assertEqual(response.status_code, 404)
  99. def test_page_not_int(self):
  100. response = self.client.get("/simple/sitemap-simple.xml?p=test")
  101. self.assertEqual(str(response.context["exception"]), "No page 'test'")
  102. self.assertEqual(response.status_code, 404)
  103. def test_simple_sitemap(self):
  104. "A simple sitemap can be rendered"
  105. response = self.client.get("/simple/sitemap.xml")
  106. expected_content = (
  107. '<?xml version="1.0" encoding="UTF-8"?>\n'
  108. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
  109. 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
  110. "<url><loc>%s/location/</loc><lastmod>%s</lastmod>"
  111. "<changefreq>never</changefreq><priority>0.5</priority></url>\n"
  112. "</urlset>"
  113. ) % (
  114. self.base_url,
  115. date.today(),
  116. )
  117. self.assertXMLEqual(response.content.decode(), expected_content)
  118. @override_settings(
  119. TEMPLATES=[
  120. {
  121. "BACKEND": "django.template.backends.django.DjangoTemplates",
  122. "DIRS": [os.path.join(os.path.dirname(__file__), "templates")],
  123. }
  124. ]
  125. )
  126. def test_simple_custom_sitemap(self):
  127. "A simple sitemap can be rendered with a custom template"
  128. response = self.client.get("/simple/custom-sitemap.xml")
  129. expected_content = """<?xml version="1.0" encoding="UTF-8"?>
  130. <!-- This is a customised template -->
  131. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  132. <url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
  133. </urlset>
  134. """ % (
  135. self.base_url,
  136. date.today(),
  137. )
  138. self.assertXMLEqual(response.content.decode(), expected_content)
  139. def test_sitemap_last_modified(self):
  140. "Last-Modified header is set correctly"
  141. response = self.client.get("/lastmod/sitemap.xml")
  142. self.assertEqual(
  143. response.headers["Last-Modified"], "Wed, 13 Mar 2013 10:00:00 GMT"
  144. )
  145. def test_sitemap_last_modified_date(self):
  146. """
  147. The Last-Modified header should be support dates (without time).
  148. """
  149. response = self.client.get("/lastmod/date-sitemap.xml")
  150. self.assertEqual(
  151. response.headers["Last-Modified"], "Wed, 13 Mar 2013 00:00:00 GMT"
  152. )
  153. def test_sitemap_last_modified_tz(self):
  154. """
  155. The Last-Modified header should be converted from timezone aware dates
  156. to GMT.
  157. """
  158. response = self.client.get("/lastmod/tz-sitemap.xml")
  159. self.assertEqual(
  160. response.headers["Last-Modified"], "Wed, 13 Mar 2013 15:00:00 GMT"
  161. )
  162. def test_sitemap_last_modified_missing(self):
  163. "Last-Modified header is missing when sitemap has no lastmod"
  164. response = self.client.get("/generic/sitemap.xml")
  165. self.assertFalse(response.has_header("Last-Modified"))
  166. def test_sitemap_last_modified_mixed(self):
  167. "Last-Modified header is omitted when lastmod not on all items"
  168. response = self.client.get("/lastmod-mixed/sitemap.xml")
  169. self.assertFalse(response.has_header("Last-Modified"))
  170. def test_sitemaps_lastmod_mixed_ascending_last_modified_missing(self):
  171. """
  172. The Last-Modified header is omitted when lastmod isn't found in all
  173. sitemaps. Test sitemaps are sorted by lastmod in ascending order.
  174. """
  175. response = self.client.get("/lastmod-sitemaps/mixed-ascending.xml")
  176. self.assertFalse(response.has_header("Last-Modified"))
  177. def test_sitemaps_lastmod_mixed_descending_last_modified_missing(self):
  178. """
  179. The Last-Modified header is omitted when lastmod isn't found in all
  180. sitemaps. Test sitemaps are sorted by lastmod in descending order.
  181. """
  182. response = self.client.get("/lastmod-sitemaps/mixed-descending.xml")
  183. self.assertFalse(response.has_header("Last-Modified"))
  184. def test_sitemaps_lastmod_ascending(self):
  185. """
  186. The Last-Modified header is set to the most recent sitemap lastmod.
  187. Test sitemaps are sorted by lastmod in ascending order.
  188. """
  189. response = self.client.get("/lastmod-sitemaps/ascending.xml")
  190. self.assertEqual(
  191. response.headers["Last-Modified"], "Sat, 20 Apr 2013 05:00:00 GMT"
  192. )
  193. def test_sitemaps_lastmod_descending(self):
  194. """
  195. The Last-Modified header is set to the most recent sitemap lastmod.
  196. Test sitemaps are sorted by lastmod in descending order.
  197. """
  198. response = self.client.get("/lastmod-sitemaps/descending.xml")
  199. self.assertEqual(
  200. response.headers["Last-Modified"], "Sat, 20 Apr 2013 05:00:00 GMT"
  201. )
  202. def test_sitemap_get_latest_lastmod_none(self):
  203. """
  204. sitemapindex.lastmod is omitted when Sitemap.lastmod is
  205. callable and Sitemap.get_latest_lastmod is not implemented
  206. """
  207. response = self.client.get("/lastmod/get-latest-lastmod-none-sitemap.xml")
  208. self.assertNotContains(response, "<lastmod>")
  209. def test_sitemap_get_latest_lastmod(self):
  210. """
  211. sitemapindex.lastmod is included when Sitemap.lastmod is
  212. attribute and Sitemap.get_latest_lastmod is implemented
  213. """
  214. response = self.client.get("/lastmod/get-latest-lastmod-sitemap.xml")
  215. self.assertContains(response, "<lastmod>2013-03-13T10:00:00</lastmod>")
  216. def test_sitemap_latest_lastmod_timezone(self):
  217. """
  218. lastmod datestamp shows timezones if Sitemap.get_latest_lastmod
  219. returns an aware datetime.
  220. """
  221. response = self.client.get("/lastmod/latest-lastmod-timezone-sitemap.xml")
  222. self.assertContains(response, "<lastmod>2013-03-13T10:00:00-05:00</lastmod>")
  223. def test_localized_priority(self):
  224. """The priority value should not be localized."""
  225. with translation.override("fr"):
  226. self.assertEqual("0,3", localize(0.3))
  227. # Priorities aren't rendered in localized format.
  228. response = self.client.get("/simple/sitemap.xml")
  229. self.assertContains(response, "<priority>0.5</priority>")
  230. self.assertContains(response, "<lastmod>%s</lastmod>" % date.today())
  231. @modify_settings(INSTALLED_APPS={"remove": "django.contrib.sites"})
  232. def test_requestsite_sitemap(self):
  233. # Hitting the flatpages sitemap without the sites framework installed
  234. # doesn't raise an exception.
  235. response = self.client.get("/simple/sitemap.xml")
  236. expected_content = (
  237. '<?xml version="1.0" encoding="UTF-8"?>\n'
  238. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
  239. 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
  240. "<url><loc>http://testserver/location/</loc><lastmod>%s</lastmod>"
  241. "<changefreq>never</changefreq><priority>0.5</priority></url>\n"
  242. "</urlset>"
  243. ) % date.today()
  244. self.assertXMLEqual(response.content.decode(), expected_content)
  245. def test_sitemap_get_urls_no_site_1(self):
  246. """
  247. Check we get ImproperlyConfigured if we don't pass a site object to
  248. Sitemap.get_urls and no Site objects exist
  249. """
  250. Site.objects.all().delete()
  251. with self.assertRaisesMessage(ImproperlyConfigured, self.use_sitemap_err_msg):
  252. Sitemap().get_urls()
  253. @modify_settings(INSTALLED_APPS={"remove": "django.contrib.sites"})
  254. def test_sitemap_get_urls_no_site_2(self):
  255. """
  256. Check we get ImproperlyConfigured when we don't pass a site object to
  257. Sitemap.get_urls if Site objects exists, but the sites framework is not
  258. actually installed.
  259. """
  260. with self.assertRaisesMessage(ImproperlyConfigured, self.use_sitemap_err_msg):
  261. Sitemap().get_urls()
  262. def test_sitemap_item(self):
  263. """
  264. Check to make sure that the raw item is included with each
  265. Sitemap.get_url() url result.
  266. """
  267. test_sitemap = Sitemap()
  268. test_sitemap.items = TestModel.objects.order_by("pk").all
  269. def is_testmodel(url):
  270. return isinstance(url["item"], TestModel)
  271. item_in_url_info = all(map(is_testmodel, test_sitemap.get_urls()))
  272. self.assertTrue(item_in_url_info)
  273. def test_cached_sitemap_index(self):
  274. """
  275. A cached sitemap index can be rendered (#2713).
  276. """
  277. response = self.client.get("/cached/index.xml")
  278. expected_content = """<?xml version="1.0" encoding="UTF-8"?>
  279. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  280. <sitemap><loc>%s/cached/sitemap-simple.xml</loc><lastmod>%s</lastmod></sitemap>
  281. </sitemapindex>
  282. """ % (
  283. self.base_url,
  284. date.today(),
  285. )
  286. self.assertXMLEqual(response.content.decode(), expected_content)
  287. def test_x_robots_sitemap(self):
  288. response = self.client.get("/simple/index.xml")
  289. self.assertEqual(response.headers["X-Robots-Tag"], "noindex, noodp, noarchive")
  290. response = self.client.get("/simple/sitemap.xml")
  291. self.assertEqual(response.headers["X-Robots-Tag"], "noindex, noodp, noarchive")
  292. def test_empty_sitemap(self):
  293. response = self.client.get("/empty/sitemap.xml")
  294. self.assertEqual(response.status_code, 200)
  295. @override_settings(LANGUAGES=(("en", "English"), ("pt", "Portuguese")))
  296. def test_simple_i18n_sitemap_index(self):
  297. """
  298. A simple i18n sitemap index can be rendered, without logging variable
  299. lookup errors.
  300. """
  301. with self.assertNoLogs("django.template", "DEBUG"):
  302. response = self.client.get("/simple/i18n.xml")
  303. expected_content = (
  304. '<?xml version="1.0" encoding="UTF-8"?>\n'
  305. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
  306. 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
  307. "<url><loc>{0}/en/i18n/testmodel/{1}/</loc><changefreq>never</changefreq>"
  308. "<priority>0.5</priority></url><url><loc>{0}/pt/i18n/testmodel/{1}/</loc>"
  309. "<changefreq>never</changefreq><priority>0.5</priority></url>\n"
  310. "</urlset>"
  311. ).format(self.base_url, self.i18n_model.pk)
  312. self.assertXMLEqual(response.content.decode(), expected_content)
  313. @override_settings(LANGUAGES=(("en", "English"), ("pt", "Portuguese")))
  314. def test_alternate_i18n_sitemap_index(self):
  315. """
  316. A i18n sitemap with alternate/hreflang links can be rendered.
  317. """
  318. response = self.client.get("/alternates/i18n.xml")
  319. url, pk = self.base_url, self.i18n_model.pk
  320. expected_urls = f"""
  321. <url><loc>{url}/en/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority>
  322. <xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>
  323. <xhtml:link rel="alternate" hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/>
  324. </url>
  325. <url><loc>{url}/pt/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority>
  326. <xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>
  327. <xhtml:link rel="alternate" hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/>
  328. </url>
  329. """.replace(
  330. "\n", ""
  331. )
  332. expected_content = (
  333. f'<?xml version="1.0" encoding="UTF-8"?>\n'
  334. f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
  335. f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
  336. f"{expected_urls}\n"
  337. f"</urlset>"
  338. )
  339. self.assertXMLEqual(response.content.decode(), expected_content)
  340. @override_settings(
  341. LANGUAGES=(("en", "English"), ("pt", "Portuguese"), ("es", "Spanish"))
  342. )
  343. def test_alternate_i18n_sitemap_limited(self):
  344. """
  345. A i18n sitemap index with limited languages can be rendered.
  346. """
  347. response = self.client.get("/limited/i18n.xml")
  348. url, pk = self.base_url, self.i18n_model.pk
  349. expected_urls = f"""
  350. <url><loc>{url}/en/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority>
  351. <xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>
  352. <xhtml:link rel="alternate" hreflang="es" href="{url}/es/i18n/testmodel/{pk}/"/>
  353. </url>
  354. <url><loc>{url}/es/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority>
  355. <xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>
  356. <xhtml:link rel="alternate" hreflang="es" href="{url}/es/i18n/testmodel/{pk}/"/>
  357. </url>
  358. """.replace(
  359. "\n", ""
  360. )
  361. expected_content = (
  362. f'<?xml version="1.0" encoding="UTF-8"?>\n'
  363. f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
  364. f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
  365. f"{expected_urls}\n"
  366. f"</urlset>"
  367. )
  368. self.assertXMLEqual(response.content.decode(), expected_content)
  369. @override_settings(LANGUAGES=(("en", "English"), ("pt", "Portuguese")))
  370. def test_alternate_i18n_sitemap_xdefault(self):
  371. """
  372. A i18n sitemap index with x-default can be rendered.
  373. """
  374. response = self.client.get("/x-default/i18n.xml")
  375. url, pk = self.base_url, self.i18n_model.pk
  376. expected_urls = f"""
  377. <url><loc>{url}/en/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority>
  378. <xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>
  379. <xhtml:link rel="alternate" hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/>
  380. <xhtml:link rel="alternate" hreflang="x-default" href="{url}/i18n/testmodel/{pk}/"/>
  381. </url>
  382. <url><loc>{url}/pt/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority>
  383. <xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>
  384. <xhtml:link rel="alternate" hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/>
  385. <xhtml:link rel="alternate" hreflang="x-default" href="{url}/i18n/testmodel/{pk}/"/>
  386. </url>
  387. """.replace(
  388. "\n", ""
  389. )
  390. expected_content = (
  391. f'<?xml version="1.0" encoding="UTF-8"?>\n'
  392. f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
  393. f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
  394. f"{expected_urls}\n"
  395. f"</urlset>"
  396. )
  397. self.assertXMLEqual(response.content.decode(), expected_content)
  398. @override_settings(LANGUAGES=(("en", "English"), ("pt", "Portuguese")))
  399. def test_language_for_item_i18n_sitemap(self):
  400. """
  401. A i18n sitemap index in which item can be chosen to be displayed for a
  402. lang or not.
  403. """
  404. only_pt = I18nTestModel.objects.create(name="Only for PT")
  405. response = self.client.get("/item-by-lang/i18n.xml")
  406. url, pk, only_pt_pk = self.base_url, self.i18n_model.pk, only_pt.pk
  407. expected_urls = (
  408. f"<url><loc>{url}/en/i18n/testmodel/{pk}/</loc>"
  409. f"<changefreq>never</changefreq><priority>0.5</priority></url>"
  410. f"<url><loc>{url}/pt/i18n/testmodel/{pk}/</loc>"
  411. f"<changefreq>never</changefreq><priority>0.5</priority></url>"
  412. f"<url><loc>{url}/pt/i18n/testmodel/{only_pt_pk}/</loc>"
  413. f"<changefreq>never</changefreq><priority>0.5</priority></url>"
  414. )
  415. expected_content = (
  416. f'<?xml version="1.0" encoding="UTF-8"?>\n'
  417. f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
  418. f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
  419. f"{expected_urls}\n"
  420. f"</urlset>"
  421. )
  422. self.assertXMLEqual(response.content.decode(), expected_content)
  423. @override_settings(LANGUAGES=(("en", "English"), ("pt", "Portuguese")))
  424. def test_alternate_language_for_item_i18n_sitemap(self):
  425. """
  426. A i18n sitemap index in which item can be chosen to be displayed for a
  427. lang or not.
  428. """
  429. only_pt = I18nTestModel.objects.create(name="Only for PT")
  430. response = self.client.get("/item-by-lang-alternates/i18n.xml")
  431. url, pk, only_pt_pk = self.base_url, self.i18n_model.pk, only_pt.pk
  432. expected_urls = (
  433. f"<url><loc>{url}/en/i18n/testmodel/{pk}/</loc>"
  434. f"<changefreq>never</changefreq><priority>0.5</priority>"
  435. f'<xhtml:link rel="alternate" '
  436. f'hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>'
  437. f'<xhtml:link rel="alternate" '
  438. f'hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/>'
  439. f'<xhtml:link rel="alternate" '
  440. f'hreflang="x-default" href="{url}/i18n/testmodel/{pk}/"/></url>'
  441. f"<url><loc>{url}/pt/i18n/testmodel/{pk}/</loc>"
  442. f"<changefreq>never</changefreq><priority>0.5</priority>"
  443. f'<xhtml:link rel="alternate" '
  444. f'hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>'
  445. f'<xhtml:link rel="alternate" '
  446. f'hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/>'
  447. f'<xhtml:link rel="alternate" '
  448. f'hreflang="x-default" href="{url}/i18n/testmodel/{pk}/"/></url>'
  449. f"<url><loc>{url}/pt/i18n/testmodel/{only_pt_pk}/</loc>"
  450. f"<changefreq>never</changefreq><priority>0.5</priority>"
  451. f'<xhtml:link rel="alternate" '
  452. f'hreflang="pt" href="{url}/pt/i18n/testmodel/{only_pt_pk}/"/></url>'
  453. )
  454. expected_content = (
  455. f'<?xml version="1.0" encoding="UTF-8"?>\n'
  456. f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
  457. f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
  458. f"{expected_urls}\n"
  459. f"</urlset>"
  460. )
  461. self.assertXMLEqual(response.content.decode(), expected_content)
  462. def test_sitemap_without_entries(self):
  463. response = self.client.get("/sitemap-without-entries/sitemap.xml")
  464. expected_content = (
  465. '<?xml version="1.0" encoding="UTF-8"?>\n'
  466. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
  467. 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n\n'
  468. "</urlset>"
  469. )
  470. self.assertXMLEqual(response.content.decode(), expected_content)
  471. def test_callable_sitemod_partial(self):
  472. """
  473. Not all items have `lastmod`. Therefore the `Last-Modified` header
  474. is not set by the detail or index sitemap view.
  475. """
  476. index_response = self.client.get("/callable-lastmod-partial/index.xml")
  477. sitemap_response = self.client.get("/callable-lastmod-partial/sitemap.xml")
  478. self.assertNotIn("Last-Modified", index_response)
  479. self.assertNotIn("Last-Modified", sitemap_response)
  480. expected_content_index = """<?xml version="1.0" encoding="UTF-8"?>
  481. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  482. <sitemap><loc>http://example.com/simple/sitemap-callable-lastmod.xml</loc></sitemap>
  483. </sitemapindex>
  484. """
  485. expected_content_sitemap = (
  486. '<?xml version="1.0" encoding="UTF-8"?>\n'
  487. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
  488. 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
  489. "<url><loc>http://example.com/location/</loc>"
  490. "<lastmod>2013-03-13</lastmod></url><url>"
  491. "<loc>http://example.com/location/</loc></url>\n"
  492. "</urlset>"
  493. )
  494. self.assertXMLEqual(index_response.content.decode(), expected_content_index)
  495. self.assertXMLEqual(sitemap_response.content.decode(), expected_content_sitemap)
  496. def test_callable_sitemod_full(self):
  497. """
  498. All items in the sitemap have `lastmod`. The `Last-Modified` header
  499. is set for the detail and index sitemap view.
  500. """
  501. index_response = self.client.get("/callable-lastmod-full/index.xml")
  502. sitemap_response = self.client.get("/callable-lastmod-full/sitemap.xml")
  503. self.assertEqual(
  504. index_response.headers["Last-Modified"], "Thu, 13 Mar 2014 10:00:00 GMT"
  505. )
  506. self.assertEqual(
  507. sitemap_response.headers["Last-Modified"], "Thu, 13 Mar 2014 10:00:00 GMT"
  508. )
  509. expected_content_index = """<?xml version="1.0" encoding="UTF-8"?>
  510. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  511. <sitemap><loc>http://example.com/simple/sitemap-callable-lastmod.xml</loc><lastmod>2014-03-13T10:00:00</lastmod></sitemap>
  512. </sitemapindex>
  513. """
  514. expected_content_sitemap = (
  515. '<?xml version="1.0" encoding="UTF-8"?>\n'
  516. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
  517. 'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
  518. "<url><loc>http://example.com/location/</loc>"
  519. "<lastmod>2013-03-13</lastmod></url>"
  520. "<url><loc>http://example.com/location/</loc>"
  521. "<lastmod>2014-03-13</lastmod></url>\n"
  522. "</urlset>"
  523. )
  524. self.assertXMLEqual(index_response.content.decode(), expected_content_index)
  525. self.assertXMLEqual(sitemap_response.content.decode(), expected_content_sitemap)
  526. def test_callable_sitemod_no_items(self):
  527. index_response = self.client.get("/callable-lastmod-no-items/index.xml")
  528. self.assertNotIn("Last-Modified", index_response)
  529. expected_content_index = """<?xml version="1.0" encoding="UTF-8"?>
  530. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  531. <sitemap><loc>http://example.com/simple/sitemap-callable-lastmod.xml</loc></sitemap>
  532. </sitemapindex>
  533. """
  534. self.assertXMLEqual(index_response.content.decode(), expected_content_index)
  535. # RemovedInDjango50Warning
  536. class DeprecatedTests(SitemapTestsBase):
  537. @override_settings(
  538. TEMPLATES=[
  539. {
  540. "BACKEND": "django.template.backends.django.DjangoTemplates",
  541. "DIRS": [os.path.join(os.path.dirname(__file__), "templates")],
  542. }
  543. ]
  544. )
  545. def test_simple_sitemap_custom_index_warning(self):
  546. msg = (
  547. "Calling `__str__` on SitemapIndexItem is deprecated, use the `location` "
  548. "attribute instead."
  549. )
  550. with self.assertRaisesMessage(RemovedInDjango50Warning, msg):
  551. self.client.get("/simple/custom-index.xml")
  552. @ignore_warnings(category=RemovedInDjango50Warning)
  553. @override_settings(
  554. TEMPLATES=[
  555. {
  556. "BACKEND": "django.template.backends.django.DjangoTemplates",
  557. "DIRS": [os.path.join(os.path.dirname(__file__), "templates")],
  558. }
  559. ]
  560. )
  561. def test_simple_sitemap_custom_index(self):
  562. "A simple sitemap index can be rendered with a custom template"
  563. response = self.client.get("/simple/custom-index.xml")
  564. expected_content = """<?xml version="1.0" encoding="UTF-8"?>
  565. <!-- This is a customised template -->
  566. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  567. <sitemap><loc>%s/simple/sitemap-simple.xml</loc></sitemap>
  568. </sitemapindex>
  569. """ % (
  570. self.base_url
  571. )
  572. self.assertXMLEqual(response.content.decode(), expected_content)