test_http.py 9.0 KB

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