test_flatpages.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from __future__ import unicode_literals
  2. from unittest import skipUnless
  3. import warnings
  4. from django.apps import apps
  5. from django.conf import settings
  6. from django.contrib.sitemaps import FlatPageSitemap
  7. from django.test import SimpleTestCase, ignore_warnings
  8. from django.utils.deprecation import RemovedInDjango19Warning
  9. from .base import SitemapTestsBase
  10. class FlatpagesSitemapTests(SitemapTestsBase):
  11. @ignore_warnings(category=RemovedInDjango19Warning)
  12. @skipUnless(apps.is_installed('django.contrib.flatpages'),
  13. "django.contrib.flatpages app not installed.")
  14. def test_flatpage_sitemap(self):
  15. "Basic FlatPage sitemap test"
  16. # Import FlatPage inside the test so that when django.contrib.flatpages
  17. # is not installed we don't get problems trying to delete Site
  18. # objects (FlatPage has an M2M to Site, Site.delete() tries to
  19. # delete related objects, but the M2M table doesn't exist.
  20. from django.contrib.flatpages.models import FlatPage
  21. public = FlatPage.objects.create(
  22. url='/public/',
  23. title='Public Page',
  24. enable_comments=True,
  25. registration_required=False,
  26. )
  27. public.sites.add(settings.SITE_ID)
  28. private = FlatPage.objects.create(
  29. url='/private/',
  30. title='Private Page',
  31. enable_comments=True,
  32. registration_required=True
  33. )
  34. private.sites.add(settings.SITE_ID)
  35. response = self.client.get('/flatpages/sitemap.xml')
  36. # Public flatpage should be in the sitemap
  37. self.assertContains(response, '<loc>%s%s</loc>' % (self.base_url, public.url))
  38. # Private flatpage should not be in the sitemap
  39. self.assertNotContains(response, '<loc>%s%s</loc>' % (self.base_url, private.url))
  40. class FlatpagesSitemapDeprecationTests(SimpleTestCase):
  41. def test_deprecation(self):
  42. with warnings.catch_warnings(record=True) as warns:
  43. warnings.simplefilter('always')
  44. FlatPageSitemap()
  45. self.assertEqual(len(warns), 1)
  46. self.assertEqual(
  47. str(warns[0].message),
  48. "'django.contrib.sitemaps.FlatPageSitemap' is deprecated. "
  49. "Use 'django.contrib.flatpages.sitemaps.FlatPageSitemap' instead.",
  50. )