2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-12-22 18:35:17 +08:00
|
|
|
from django.apps import app_cache
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.sites.models import Site, RequestSite, get_current_site
|
2013-02-24 22:54:09 +08:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist, ValidationError
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
from django.http import HttpRequest
|
|
|
|
from django.test import TestCase
|
2013-02-10 01:17:01 +08:00
|
|
|
from django.test.utils import override_settings
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SitesFrameworkTests(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
Site(id=settings.SITE_ID, domain="example.com", name="example.com").save()
|
2013-12-18 20:16:33 +08:00
|
|
|
self._with_sites = app_cache._begin_with_app('django.contrib.sites')
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
|
|
|
|
def tearDown(self):
|
2013-12-18 20:16:33 +08:00
|
|
|
app_cache._end_with_app(self._with_sites)
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
|
2012-01-07 16:53:36 +08:00
|
|
|
def test_save_another(self):
|
|
|
|
# Regression for #17415
|
|
|
|
# On some backends the sequence needs reset after save with explicit ID.
|
|
|
|
# Test that there is no sequence collisions by saving another site.
|
|
|
|
Site(domain="example2.com", name="example2.com").save()
|
|
|
|
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
def test_site_manager(self):
|
|
|
|
# Make sure that get_current() does not return a deleted Site object.
|
|
|
|
s = Site.objects.get_current()
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertTrue(isinstance(s, Site))
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
s.delete()
|
|
|
|
self.assertRaises(ObjectDoesNotExist, Site.objects.get_current)
|
|
|
|
|
|
|
|
def test_site_cache(self):
|
|
|
|
# After updating a Site object (e.g. via the admin), we shouldn't return a
|
|
|
|
# bogus value from the SITE_CACHE.
|
|
|
|
site = Site.objects.get_current()
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual("example.com", site.name)
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
s2 = Site.objects.get(id=settings.SITE_ID)
|
|
|
|
s2.name = "Example site"
|
|
|
|
s2.save()
|
|
|
|
site = Site.objects.get_current()
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual("Example site", site.name)
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
|
2013-02-24 02:28:17 +08:00
|
|
|
def test_delete_all_sites_clears_cache(self):
|
|
|
|
# When all site objects are deleted the cache should also
|
|
|
|
# be cleared and get_current() should raise a DoesNotExist.
|
|
|
|
self.assertIsInstance(Site.objects.get_current(), Site)
|
|
|
|
Site.objects.all().delete()
|
|
|
|
self.assertRaises(Site.DoesNotExist, Site.objects.get_current)
|
|
|
|
|
2013-02-10 01:17:01 +08:00
|
|
|
@override_settings(ALLOWED_HOSTS=['example.com'])
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
def test_get_current_site(self):
|
|
|
|
# Test that the correct Site object is returned
|
|
|
|
request = HttpRequest()
|
|
|
|
request.META = {
|
|
|
|
"SERVER_NAME": "example.com",
|
|
|
|
"SERVER_PORT": "80",
|
|
|
|
}
|
|
|
|
site = get_current_site(request)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertTrue(isinstance(site, Site))
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
self.assertEqual(site.id, settings.SITE_ID)
|
|
|
|
|
|
|
|
# Test that an exception is raised if the sites framework is installed
|
|
|
|
# but there is no matching Site
|
|
|
|
site.delete()
|
|
|
|
self.assertRaises(ObjectDoesNotExist, get_current_site, request)
|
|
|
|
|
|
|
|
# A RequestSite is returned if the sites framework is not installed
|
2013-12-18 20:16:33 +08:00
|
|
|
with app_cache._without_app('django.contrib.sites'):
|
|
|
|
site = get_current_site(request)
|
|
|
|
self.assertTrue(isinstance(site, RequestSite))
|
|
|
|
self.assertEqual(site.name, "example.com")
|
2013-02-24 22:54:09 +08:00
|
|
|
|
|
|
|
def test_domain_name_with_whitespaces(self):
|
|
|
|
# Regression for #17320
|
|
|
|
# Domain names are not allowed contain whitespace characters
|
|
|
|
site = Site(name="test name", domain="test test")
|
|
|
|
self.assertRaises(ValidationError, site.full_clean)
|
|
|
|
site.domain = "test\ttest"
|
|
|
|
self.assertRaises(ValidationError, site.full_clean)
|
|
|
|
site.domain = "test\ntest"
|
|
|
|
self.assertRaises(ValidationError, site.full_clean)
|