diff options
author | ttt | 2017-05-13 00:29:47 +0530 |
---|---|---|
committer | ttt | 2017-05-13 00:29:47 +0530 |
commit | abf599be33b383a6a5baf9493093b2126a622ac8 (patch) | |
tree | 4c5ab6e0d935d5e65fabcf0258e4a00dd20a5afa /lib/python2.7/site-packages/django/contrib/sitemaps/tests | |
download | SBHS-2018-Rpi-abf599be33b383a6a5baf9493093b2126a622ac8.tar.gz SBHS-2018-Rpi-abf599be33b383a6a5baf9493093b2126a622ac8.tar.bz2 SBHS-2018-Rpi-abf599be33b383a6a5baf9493093b2126a622ac8.zip |
added all server files
Diffstat (limited to 'lib/python2.7/site-packages/django/contrib/sitemaps/tests')
11 files changed, 384 insertions, 0 deletions
diff --git a/lib/python2.7/site-packages/django/contrib/sitemaps/tests/__init__.py b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/__init__.py diff --git a/lib/python2.7/site-packages/django/contrib/sitemaps/tests/base.py b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/base.py new file mode 100644 index 0000000..8e027d4 --- /dev/null +++ b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/base.py @@ -0,0 +1,34 @@ +from django.contrib.sites.models import Site +from django.core.cache import cache +from django.db import models +from django.test import TestCase + + +class TestModel(models.Model): + "A test model for " + name = models.CharField(max_length=100) + + class Meta: + app_label = 'sitemaps' + + def __unicode__(self): + return self.name + + def get_absolute_url(self): + return '/testmodel/%s/' % self.id + + +class SitemapTestsBase(TestCase): + protocol = 'http' + domain = 'example.com' if Site._meta.installed else 'testserver' + urls = 'django.contrib.sitemaps.tests.urls.http' + + def setUp(self): + self.base_url = '%s://%s' % (self.protocol, self.domain) + self.old_Site_meta_installed = Site._meta.installed + cache.clear() + # Create an object for sitemap content. + TestModel.objects.create(name='Test Object') + + def tearDown(self): + Site._meta.installed = self.old_Site_meta_installed diff --git a/lib/python2.7/site-packages/django/contrib/sitemaps/tests/templates/custom_sitemap.xml b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/templates/custom_sitemap.xml new file mode 100644 index 0000000..594aef1 --- /dev/null +++ b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/templates/custom_sitemap.xml @@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- This is a customised template --> +<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +{% spaceless %} +{% for url in urlset %} + <url> + <loc>{{ url.location }}</loc> + {% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}</lastmod>{% endif %} + {% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %} + {% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %} + </url> +{% endfor %} +{% endspaceless %} +</urlset> diff --git a/lib/python2.7/site-packages/django/contrib/sitemaps/tests/templates/custom_sitemap_index.xml b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/templates/custom_sitemap_index.xml new file mode 100644 index 0000000..406c6b7 --- /dev/null +++ b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/templates/custom_sitemap_index.xml @@ -0,0 +1,5 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- This is a customised template --> +<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +{% for location in sitemaps %}<sitemap><loc>{{ location }}</loc></sitemap>{% endfor %} +</sitemapindex> diff --git a/lib/python2.7/site-packages/django/contrib/sitemaps/tests/test_flatpages.py b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/test_flatpages.py new file mode 100644 index 0000000..930f24f --- /dev/null +++ b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/test_flatpages.py @@ -0,0 +1,39 @@ +from __future__ import unicode_literals + +from django.conf import settings +from django.utils.unittest import skipUnless + +from .base import SitemapTestsBase + +class FlatpagesSitemapTests(SitemapTestsBase): + + @skipUnless("django.contrib.flatpages" in settings.INSTALLED_APPS, + "django.contrib.flatpages app not installed.") + def test_flatpage_sitemap(self): + "Basic FlatPage sitemap test" + + # Import FlatPage inside the test so that when django.contrib.flatpages + # is not installed we don't get problems trying to delete Site + # objects (FlatPage has an M2M to Site, Site.delete() tries to + # delete related objects, but the M2M table doesn't exist. + from django.contrib.flatpages.models import FlatPage + + public = FlatPage.objects.create( + url='/public/', + title='Public Page', + enable_comments=True, + registration_required=False, + ) + public.sites.add(settings.SITE_ID) + private = FlatPage.objects.create( + url='/private/', + title='Private Page', + enable_comments=True, + registration_required=True + ) + private.sites.add(settings.SITE_ID) + response = self.client.get('/flatpages/sitemap.xml') + # Public flatpage should be in the sitemap + self.assertContains(response, '<loc>%s%s</loc>' % (self.base_url, public.url)) + # Private flatpage should not be in the sitemap + self.assertNotContains(response, '<loc>%s%s</loc>' % (self.base_url, private.url)) diff --git a/lib/python2.7/site-packages/django/contrib/sitemaps/tests/test_generic.py b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/test_generic.py new file mode 100644 index 0000000..5b26573 --- /dev/null +++ b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/test_generic.py @@ -0,0 +1,22 @@ +from __future__ import unicode_literals + +from django.test.utils import override_settings + +from .base import TestModel, SitemapTestsBase + + +@override_settings(ABSOLUTE_URL_OVERRIDES={}) +class GenericViewsSitemapTests(SitemapTestsBase): + + def test_generic_sitemap(self): + "A minimal generic sitemap can be rendered" + response = self.client.get('/generic/sitemap.xml') + expected = '' + for pk in TestModel.objects.values_list("id", flat=True): + expected += "<url><loc>%s/testmodel/%s/</loc></url>" % (self.base_url, pk) + expected_content = """<?xml version="1.0" encoding="UTF-8"?> +<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +%s +</urlset> +""" % expected + self.assertXMLEqual(response.content.decode('utf-8'), expected_content) diff --git a/lib/python2.7/site-packages/django/contrib/sitemaps/tests/test_http.py b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/test_http.py new file mode 100644 index 0000000..a99025e --- /dev/null +++ b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/test_http.py @@ -0,0 +1,153 @@ +from __future__ import unicode_literals + +import os +from datetime import date + +from django.conf import settings +from django.contrib.sitemaps import Sitemap, GenericSitemap +from django.contrib.sites.models import Site +from django.core.exceptions import ImproperlyConfigured +from django.test.utils import override_settings +from django.utils.unittest import skipUnless +from django.utils.formats import localize +from django.utils._os import upath +from django.utils.translation import activate, deactivate + +from .base import TestModel, SitemapTestsBase + + +class HTTPSitemapTests(SitemapTestsBase): + + def test_simple_sitemap_index(self): + "A simple sitemap index can be rendered" + response = self.client.get('/simple/index.xml') + expected_content = """<?xml version="1.0" encoding="UTF-8"?> +<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +<sitemap><loc>%s/simple/sitemap-simple.xml</loc></sitemap> +</sitemapindex> +""" % self.base_url + self.assertXMLEqual(response.content.decode('utf-8'), expected_content) + + @override_settings( + TEMPLATE_DIRS=(os.path.join(os.path.dirname(upath(__file__)), 'templates'),) + ) + def test_simple_sitemap_custom_index(self): + "A simple sitemap index can be rendered with a custom template" + response = self.client.get('/simple/custom-index.xml') + expected_content = """<?xml version="1.0" encoding="UTF-8"?> +<!-- This is a customised template --> +<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +<sitemap><loc>%s/simple/sitemap-simple.xml</loc></sitemap> +</sitemapindex> +""" % self.base_url + self.assertXMLEqual(response.content.decode('utf-8'), expected_content) + + + def test_simple_sitemap_section(self): + "A simple sitemap section can be rendered" + response = self.client.get('/simple/sitemap-simple.xml') + expected_content = """<?xml version="1.0" encoding="UTF-8"?> +<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> +</urlset> +""" % (self.base_url, date.today()) + self.assertXMLEqual(response.content.decode('utf-8'), expected_content) + + def test_simple_sitemap(self): + "A simple sitemap can be rendered" + response = self.client.get('/simple/sitemap.xml') + expected_content = """<?xml version="1.0" encoding="UTF-8"?> +<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> +</urlset> +""" % (self.base_url, date.today()) + self.assertXMLEqual(response.content.decode('utf-8'), expected_content) + + @override_settings( + TEMPLATE_DIRS=(os.path.join(os.path.dirname(upath(__file__)), 'templates'),) + ) + def test_simple_custom_sitemap(self): + "A simple sitemap can be rendered with a custom template" + response = self.client.get('/simple/custom-sitemap.xml') + expected_content = """<?xml version="1.0" encoding="UTF-8"?> +<!-- This is a customised template --> +<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> +</urlset> +""" % (self.base_url, date.today()) + self.assertXMLEqual(response.content.decode('utf-8'), expected_content) + + @skipUnless(settings.USE_I18N, "Internationalization is not enabled") + @override_settings(USE_L10N=True) + def test_localized_priority(self): + "The priority value should not be localized (Refs #14164)" + activate('fr') + self.assertEqual('0,3', localize(0.3)) + + # Retrieve the sitemap. Check that priorities + # haven't been rendered in localized format + response = self.client.get('/simple/sitemap.xml') + self.assertContains(response, '<priority>0.5</priority>') + self.assertContains(response, '<lastmod>%s</lastmod>' % date.today()) + deactivate() + + def test_requestsite_sitemap(self): + # Make sure hitting the flatpages sitemap without the sites framework + # installed doesn't raise an exception + Site._meta.installed = False + response = self.client.get('/simple/sitemap.xml') + expected_content = """<?xml version="1.0" encoding="UTF-8"?> +<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +<url><loc>http://testserver/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> +</urlset> +""" % date.today() + self.assertXMLEqual(response.content.decode('utf-8'), expected_content) + + @skipUnless("django.contrib.sites" in settings.INSTALLED_APPS, + "django.contrib.sites app not installed.") + def test_sitemap_get_urls_no_site_1(self): + """ + Check we get ImproperlyConfigured if we don't pass a site object to + Sitemap.get_urls and no Site objects exist + """ + Site.objects.all().delete() + self.assertRaises(ImproperlyConfigured, Sitemap().get_urls) + + def test_sitemap_get_urls_no_site_2(self): + """ + Check we get ImproperlyConfigured when we don't pass a site object to + Sitemap.get_urls if Site objects exists, but the sites framework is not + actually installed. + """ + Site._meta.installed = False + self.assertRaises(ImproperlyConfigured, Sitemap().get_urls) + + def test_sitemap_item(self): + """ + Check to make sure that the raw item is included with each + Sitemap.get_url() url result. + """ + test_sitemap = GenericSitemap({'queryset': TestModel.objects.all()}) + def is_testmodel(url): + return isinstance(url['item'], TestModel) + item_in_url_info = all(map(is_testmodel, test_sitemap.get_urls())) + self.assertTrue(item_in_url_info) + + def test_cached_sitemap_index(self): + """ + Check that a cached sitemap index can be rendered (#2713). + """ + response = self.client.get('/cached/index.xml') + expected_content = """<?xml version="1.0" encoding="UTF-8"?> +<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +<sitemap><loc>%s/cached/sitemap-simple.xml</loc></sitemap> +</sitemapindex> +""" % self.base_url + self.assertXMLEqual(response.content.decode('utf-8'), expected_content) + + def test_x_robots_sitemap(self): + response = self.client.get('/simple/index.xml') + self.assertEqual(response['X-Robots-Tag'], 'noindex, noodp, noarchive') + + response = self.client.get('/simple/sitemap.xml') + self.assertEqual(response['X-Robots-Tag'], 'noindex, noodp, noarchive') diff --git a/lib/python2.7/site-packages/django/contrib/sitemaps/tests/test_https.py b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/test_https.py new file mode 100644 index 0000000..baad02a --- /dev/null +++ b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/test_https.py @@ -0,0 +1,56 @@ +from __future__ import unicode_literals + +from datetime import date + +from django.test.utils import override_settings + +from .base import SitemapTestsBase + +class HTTPSSitemapTests(SitemapTestsBase): + protocol = 'https' + urls = 'django.contrib.sitemaps.tests.urls.https' + + def test_secure_sitemap_index(self): + "A secure sitemap index can be rendered" + response = self.client.get('/secure/index.xml') + expected_content = """<?xml version="1.0" encoding="UTF-8"?> +<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +<sitemap><loc>%s/secure/sitemap-simple.xml</loc></sitemap> +</sitemapindex> +""" % self.base_url + self.assertXMLEqual(response.content.decode('utf-8'), expected_content) + + def test_secure_sitemap_section(self): + "A secure sitemap section can be rendered" + response = self.client.get('/secure/sitemap-simple.xml') + expected_content = """<?xml version="1.0" encoding="UTF-8"?> +<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> +</urlset> +""" % (self.base_url, date.today()) + self.assertXMLEqual(response.content.decode('utf-8'), expected_content) + + +@override_settings(SECURE_PROXY_SSL_HEADER=False) +class HTTPSDetectionSitemapTests(SitemapTestsBase): + extra = {'wsgi.url_scheme': 'https'} + + def test_sitemap_index_with_https_request(self): + "A sitemap index requested in HTTPS is rendered with HTTPS links" + response = self.client.get('/simple/index.xml', **self.extra) + expected_content = """<?xml version="1.0" encoding="UTF-8"?> +<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +<sitemap><loc>%s/simple/sitemap-simple.xml</loc></sitemap> +</sitemapindex> +""" % self.base_url.replace('http://', 'https://') + self.assertXMLEqual(response.content.decode('utf-8'), expected_content) + + def test_sitemap_section_with_https_request(self): + "A sitemap section requested in HTTPS is rendered with HTTPS links" + response = self.client.get('/simple/sitemap-simple.xml', **self.extra) + expected_content = """<?xml version="1.0" encoding="UTF-8"?> +<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> +</urlset> +""" % (self.base_url.replace('http://', 'https://'), date.today()) + self.assertXMLEqual(response.content.decode('utf-8'), expected_content) diff --git a/lib/python2.7/site-packages/django/contrib/sitemaps/tests/urls/__init__.py b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/urls/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/urls/__init__.py diff --git a/lib/python2.7/site-packages/django/contrib/sitemaps/tests/urls/http.py b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/urls/http.py new file mode 100644 index 0000000..a8b804f --- /dev/null +++ b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/urls/http.py @@ -0,0 +1,45 @@ +from datetime import datetime +from django.conf.urls import patterns, url +from django.contrib.sitemaps import Sitemap, GenericSitemap, FlatPageSitemap, views +from django.views.decorators.cache import cache_page + +from django.contrib.sitemaps.tests.base import TestModel + + +class SimpleSitemap(Sitemap): + changefreq = "never" + priority = 0.5 + location = '/location/' + lastmod = datetime.now() + + def items(self): + return [object()] + +simple_sitemaps = { + 'simple': SimpleSitemap, +} + +generic_sitemaps = { + 'generic': GenericSitemap({'queryset': TestModel.objects.all()}), +} + +flatpage_sitemaps = { + 'flatpages': FlatPageSitemap, +} + +urlpatterns = patterns('django.contrib.sitemaps.views', + (r'^simple/index\.xml$', 'index', {'sitemaps': simple_sitemaps}), + (r'^simple/custom-index\.xml$', 'index', + {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap_index.xml'}), + (r'^simple/sitemap-(?P<section>.+)\.xml$', 'sitemap', + {'sitemaps': simple_sitemaps}), + (r'^simple/sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}), + (r'^simple/custom-sitemap\.xml$', 'sitemap', + {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap.xml'}), + (r'^generic/sitemap\.xml$', 'sitemap', {'sitemaps': generic_sitemaps}), + (r'^flatpages/sitemap\.xml$', 'sitemap', {'sitemaps': flatpage_sitemaps}), + url(r'^cached/index\.xml$', cache_page(1)(views.index), + {'sitemaps': simple_sitemaps, 'sitemap_url_name': 'cached_sitemap'}), + url(r'^cached/sitemap-(?P<section>.+)\.xml', cache_page(1)(views.sitemap), + {'sitemaps': simple_sitemaps}, name='cached_sitemap') +) diff --git a/lib/python2.7/site-packages/django/contrib/sitemaps/tests/urls/https.py b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/urls/https.py new file mode 100644 index 0000000..a1b4b93 --- /dev/null +++ b/lib/python2.7/site-packages/django/contrib/sitemaps/tests/urls/https.py @@ -0,0 +1,16 @@ +from django.conf.urls import patterns + +from .http import SimpleSitemap + +class HTTPSSitemap(SimpleSitemap): + protocol = 'https' + +secure_sitemaps = { + 'simple': HTTPSSitemap, +} + +urlpatterns = patterns('django.contrib.sitemaps.views', + (r'^secure/index\.xml$', 'index', {'sitemaps': secure_sitemaps}), + (r'^secure/sitemap-(?P<section>.+)\.xml$', 'sitemap', + {'sitemaps': secure_sitemaps}), +) |