test_sitemaps.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from __future__ import unicode_literals
  2. import zipfile
  3. from io import BytesIO
  4. from xml.dom import minidom
  5. from django.conf import settings
  6. from django.contrib.sites.models import Site
  7. from django.test import (
  8. TestCase, ignore_warnings, modify_settings, override_settings,
  9. skipUnlessDBFeature,
  10. )
  11. from django.utils.deprecation import RemovedInDjango110Warning
  12. from .models import City, Country
  13. @modify_settings(INSTALLED_APPS={'append': ['django.contrib.sites', 'django.contrib.sitemaps']})
  14. @override_settings(ROOT_URLCONF='gis_tests.geoapp.urls')
  15. @skipUnlessDBFeature("gis_enabled")
  16. class GeoSitemapTest(TestCase):
  17. def setUp(self):
  18. super(GeoSitemapTest, self).setUp()
  19. Site(id=settings.SITE_ID, domain="example.com", name="example.com").save()
  20. def assertChildNodes(self, elem, expected):
  21. "Taken from syndication/tests.py."
  22. actual = set(n.nodeName for n in elem.childNodes)
  23. expected = set(expected)
  24. self.assertEqual(actual, expected)
  25. @ignore_warnings(category=RemovedInDjango110Warning)
  26. def test_geositemap_kml(self):
  27. "Tests KML/KMZ geographic sitemaps."
  28. for kml_type in ('kml', 'kmz'):
  29. # The URL for the sitemaps in urls.py have been updated
  30. # with a name but since reversing by Python path is tried first
  31. # before reversing by name and works since we're giving
  32. # name='django.contrib.gis.sitemaps.views.(kml|kmz)', we need
  33. # to silence the erroneous warning until reversing by dotted
  34. # path is removed. The test will work without modification when
  35. # it's removed.
  36. doc = minidom.parseString(self.client.get('/sitemaps/%s.xml' % kml_type).content)
  37. # Ensuring the right sitemaps namespace is present.
  38. urlset = doc.firstChild
  39. self.assertEqual(urlset.getAttribute('xmlns'), 'http://www.sitemaps.org/schemas/sitemap/0.9')
  40. urls = urlset.getElementsByTagName('url')
  41. self.assertEqual(2, len(urls)) # Should only be 2 sitemaps.
  42. for url in urls:
  43. self.assertChildNodes(url, ['loc'])
  44. # Getting the relative URL since we don't have a real site.
  45. kml_url = url.getElementsByTagName('loc')[0].childNodes[0].data.split('http://example.com')[1]
  46. if kml_type == 'kml':
  47. kml_doc = minidom.parseString(self.client.get(kml_url).content)
  48. elif kml_type == 'kmz':
  49. # Have to decompress KMZ before parsing.
  50. buf = BytesIO(self.client.get(kml_url).content)
  51. zf = zipfile.ZipFile(buf)
  52. self.assertEqual(1, len(zf.filelist))
  53. self.assertEqual('doc.kml', zf.filelist[0].filename)
  54. kml_doc = minidom.parseString(zf.read('doc.kml'))
  55. # Ensuring the correct number of placemarks are in the KML doc.
  56. if 'city' in kml_url:
  57. model = City
  58. elif 'country' in kml_url:
  59. model = Country
  60. self.assertEqual(model.objects.count(), len(kml_doc.getElementsByTagName('Placemark')))