test_http.py 10 KB

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