test_sitemaps.py 3.2 KB

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