test_http.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. from __future__ import unicode_literals
  2. import os
  3. from datetime import date
  4. from unittest import skipUnless
  5. from django.apps import apps
  6. from django.conf import settings
  7. from django.contrib.sitemaps import Sitemap, GenericSitemap
  8. from django.contrib.sites.models import Site
  9. from django.core.exceptions import ImproperlyConfigured
  10. from django.test import modify_settings, override_settings
  11. from django.utils.formats import localize
  12. from django.utils._os import upath
  13. from django.utils.translation import activate, deactivate
  14. from .base import TestModel, SitemapTestsBase
  15. class HTTPSitemapTests(SitemapTestsBase):
  16. def test_simple_sitemap_index(self):
  17. "A simple sitemap index can be rendered"
  18. response = self.client.get('/simple/index.xml')
  19. expected_content = """<?xml version="1.0" encoding="UTF-8"?>
  20. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  21. <sitemap><loc>%s/simple/sitemap-simple.xml</loc></sitemap>
  22. </sitemapindex>
  23. """ % self.base_url
  24. self.assertXMLEqual(response.content.decode('utf-8'), expected_content)
  25. @override_settings(
  26. TEMPLATE_DIRS=(os.path.join(os.path.dirname(upath(__file__)), 'templates'),)
  27. )
  28. def test_simple_sitemap_custom_index(self):
  29. "A simple sitemap index can be rendered with a custom template"
  30. response = self.client.get('/simple/custom-index.xml')
  31. expected_content = """<?xml version="1.0" encoding="UTF-8"?>
  32. <!-- This is a customised template -->
  33. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  34. <sitemap><loc>%s/simple/sitemap-simple.xml</loc></sitemap>
  35. </sitemapindex>
  36. """ % self.base_url
  37. self.assertXMLEqual(response.content.decode('utf-8'), expected_content)
  38. def test_simple_sitemap_section(self):
  39. "A simple sitemap section can be rendered"
  40. response = self.client.get('/simple/sitemap-simple.xml')
  41. expected_content = """<?xml version="1.0" encoding="UTF-8"?>
  42. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  43. <url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
  44. </urlset>
  45. """ % (self.base_url, date.today())
  46. self.assertXMLEqual(response.content.decode('utf-8'), expected_content)
  47. def test_simple_sitemap(self):
  48. "A simple sitemap can be rendered"
  49. response = self.client.get('/simple/sitemap.xml')
  50. expected_content = """<?xml version="1.0" encoding="UTF-8"?>
  51. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  52. <url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
  53. </urlset>
  54. """ % (self.base_url, date.today())
  55. self.assertXMLEqual(response.content.decode('utf-8'), expected_content)
  56. @override_settings(
  57. TEMPLATE_DIRS=(os.path.join(os.path.dirname(upath(__file__)), 'templates'),)
  58. )
  59. def test_simple_custom_sitemap(self):
  60. "A simple sitemap can be rendered with a custom template"
  61. response = self.client.get('/simple/custom-sitemap.xml')
  62. expected_content = """<?xml version="1.0" encoding="UTF-8"?>
  63. <!-- This is a customised template -->
  64. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  65. <url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
  66. </urlset>
  67. """ % (self.base_url, date.today())
  68. self.assertXMLEqual(response.content.decode('utf-8'), expected_content)
  69. def test_sitemap_last_modified(self):
  70. "Tests that Last-Modified header is set correctly"
  71. response = self.client.get('/lastmod/sitemap.xml')
  72. self.assertEqual(response['Last-Modified'], 'Wed, 13 Mar 2013 10:00:00 GMT')
  73. def test_sitemap_last_modified_missing(self):
  74. "Tests that Last-Modified header is missing when sitemap has no lastmod"
  75. response = self.client.get('/generic/sitemap.xml')
  76. self.assertFalse(response.has_header('Last-Modified'))
  77. def test_sitemap_last_modified_mixed(self):
  78. "Tests that Last-Modified header is omitted when lastmod not on all items"
  79. response = self.client.get('/lastmod-mixed/sitemap.xml')
  80. self.assertFalse(response.has_header('Last-Modified'))
  81. @skipUnless(settings.USE_I18N, "Internationalization is not enabled")
  82. @override_settings(USE_L10N=True)
  83. def test_localized_priority(self):
  84. "The priority value should not be localized (Refs #14164)"
  85. activate('fr')
  86. self.assertEqual('0,3', localize(0.3))
  87. # Retrieve the sitemap. Check that priorities
  88. # haven't been rendered in localized format
  89. response = self.client.get('/simple/sitemap.xml')
  90. self.assertContains(response, '<priority>0.5</priority>')
  91. self.assertContains(response, '<lastmod>%s</lastmod>' % date.today())
  92. deactivate()
  93. @modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'})
  94. def test_requestsite_sitemap(self):
  95. # Make sure hitting the flatpages sitemap without the sites framework
  96. # installed doesn't raise an exception.
  97. response = self.client.get('/simple/sitemap.xml')
  98. expected_content = """<?xml version="1.0" encoding="UTF-8"?>
  99. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  100. <url><loc>http://testserver/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
  101. </urlset>
  102. """ % date.today()
  103. self.assertXMLEqual(response.content.decode('utf-8'), expected_content)
  104. @skipUnless(apps.is_installed('django.contrib.sites'),
  105. "django.contrib.sites app not installed.")
  106. def test_sitemap_get_urls_no_site_1(self):
  107. """
  108. Check we get ImproperlyConfigured if we don't pass a site object to
  109. Sitemap.get_urls and no Site objects exist
  110. """
  111. Site.objects.all().delete()
  112. self.assertRaises(ImproperlyConfigured, Sitemap().get_urls)
  113. @modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'})
  114. def test_sitemap_get_urls_no_site_2(self):
  115. """
  116. Check we get ImproperlyConfigured when we don't pass a site object to
  117. Sitemap.get_urls if Site objects exists, but the sites framework is not
  118. actually installed.
  119. """
  120. self.assertRaises(ImproperlyConfigured, Sitemap().get_urls)
  121. def test_sitemap_item(self):
  122. """
  123. Check to make sure that the raw item is included with each
  124. Sitemap.get_url() url result.
  125. """
  126. test_sitemap = GenericSitemap({'queryset': TestModel.objects.all()})
  127. def is_testmodel(url):
  128. return isinstance(url['item'], TestModel)
  129. item_in_url_info = all(map(is_testmodel, test_sitemap.get_urls()))
  130. self.assertTrue(item_in_url_info)
  131. def test_cached_sitemap_index(self):
  132. """
  133. Check that a cached sitemap index can be rendered (#2713).
  134. """
  135. response = self.client.get('/cached/index.xml')
  136. expected_content = """<?xml version="1.0" encoding="UTF-8"?>
  137. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  138. <sitemap><loc>%s/cached/sitemap-simple.xml</loc></sitemap>
  139. </sitemapindex>
  140. """ % self.base_url
  141. self.assertXMLEqual(response.content.decode('utf-8'), expected_content)
  142. def test_x_robots_sitemap(self):
  143. response = self.client.get('/simple/index.xml')
  144. self.assertEqual(response['X-Robots-Tag'], 'noindex, noodp, noarchive')
  145. response = self.client.get('/simple/sitemap.xml')
  146. self.assertEqual(response['X-Robots-Tag'], 'noindex, noodp, noarchive')
  147. def test_empty_sitemap(self):
  148. response = self.client.get('/empty/sitemap.xml')
  149. self.assertEqual(response.status_code, 200)