2014-11-25 20:35:39 +08:00
|
|
|
from django.apps import apps
|
2016-05-13 23:59:27 +08:00
|
|
|
from django.apps.registry import Apps
|
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
|
2015-02-10 20:51:02 +08:00
|
|
|
from django.contrib.sites import models
|
2020-07-23 14:18:58 +08:00
|
|
|
from django.contrib.sites.checks import check_site_id
|
2015-02-10 20:51:02 +08:00
|
|
|
from django.contrib.sites.management import create_default_site
|
|
|
|
from django.contrib.sites.middleware import CurrentSiteMiddleware
|
|
|
|
from django.contrib.sites.models import Site, clear_site_cache
|
|
|
|
from django.contrib.sites.requests import RequestSite
|
|
|
|
from django.contrib.sites.shortcuts import get_current_site
|
2020-07-23 14:18:58 +08:00
|
|
|
from django.core import checks
|
2013-02-24 22:54:09 +08:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist, ValidationError
|
2014-11-25 20:35:39 +08:00
|
|
|
from django.db.models.signals import post_migrate
|
2016-06-07 08:26:24 +08:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2017-09-28 19:58:01 +08:00
|
|
|
from django.test import SimpleTestCase, TestCase, modify_settings, override_settings
|
2014-11-29 16:36:39 +08:00
|
|
|
from django.test.utils import captured_stdout
|
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-12-23 17:37:34 +08:00
|
|
|
@modify_settings(INSTALLED_APPS={"append": "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
|
|
|
class SitesFrameworkTests(TestCase):
|
2018-07-12 12:12:20 +08:00
|
|
|
databases = {"default", "other"}
|
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
|
|
|
|
2018-11-24 09:59:38 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2018-11-24 19:28:28 +08:00
|
|
|
cls.site = Site(id=settings.SITE_ID, domain="example.com", name="example.com")
|
2018-11-24 09:59:38 +08:00
|
|
|
cls.site.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
|
|
|
|
2019-12-27 15:57:34 +08:00
|
|
|
def setUp(self):
|
|
|
|
Site.objects.clear_cache()
|
2023-12-31 17:01:31 +08:00
|
|
|
self.addCleanup(Site.objects.clear_cache)
|
2015-06-19 04:25:32 +08:00
|
|
|
|
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()
|
2014-04-10 04:20:22 +08:00
|
|
|
self.assertIsInstance(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()
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(ObjectDoesNotExist):
|
|
|
|
Site.objects.get_current()
|
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_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()
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(Site.DoesNotExist):
|
|
|
|
Site.objects.get_current()
|
2013-02-24 02:28:17 +08:00
|
|
|
|
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):
|
2016-10-27 15:53:39 +08:00
|
|
|
# The correct Site object is returned
|
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
|
|
|
request = HttpRequest()
|
|
|
|
request.META = {
|
|
|
|
"SERVER_NAME": "example.com",
|
|
|
|
"SERVER_PORT": "80",
|
|
|
|
}
|
|
|
|
site = get_current_site(request)
|
2014-04-10 04:20:22 +08:00
|
|
|
self.assertIsInstance(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)
|
|
|
|
|
2016-10-27 15:53:39 +08:00
|
|
|
# An exception is raised if the sites framework is installed
|
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
|
|
|
# but there is no matching Site
|
|
|
|
site.delete()
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(ObjectDoesNotExist):
|
|
|
|
get_current_site(request)
|
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
|
|
|
|
|
|
|
# A RequestSite is returned if the sites framework is not installed
|
2013-12-23 17:37:34 +08:00
|
|
|
with self.modify_settings(INSTALLED_APPS={"remove": "django.contrib.sites"}):
|
2013-12-18 20:16:33 +08:00
|
|
|
site = get_current_site(request)
|
2014-04-10 04:20:22 +08:00
|
|
|
self.assertIsInstance(site, RequestSite)
|
2013-12-18 20:16:33 +08:00
|
|
|
self.assertEqual(site.name, "example.com")
|
2013-02-24 22:54:09 +08:00
|
|
|
|
2020-07-23 14:18:58 +08:00
|
|
|
@override_settings(SITE_ID=None, ALLOWED_HOSTS=["example.com"])
|
2014-10-01 01:15:59 +08:00
|
|
|
def test_get_current_site_no_site_id(self):
|
|
|
|
request = HttpRequest()
|
|
|
|
request.META = {
|
|
|
|
"SERVER_NAME": "example.com",
|
|
|
|
"SERVER_PORT": "80",
|
|
|
|
}
|
|
|
|
del settings.SITE_ID
|
|
|
|
site = get_current_site(request)
|
|
|
|
self.assertEqual(site.name, "example.com")
|
|
|
|
|
2020-07-23 14:18:58 +08:00
|
|
|
@override_settings(SITE_ID=None, ALLOWED_HOSTS=["example.com"])
|
2016-11-30 07:17:10 +08:00
|
|
|
def test_get_current_site_host_with_trailing_dot(self):
|
|
|
|
"""
|
|
|
|
The site is matched if the name in the request has a trailing dot.
|
|
|
|
"""
|
|
|
|
request = HttpRequest()
|
|
|
|
request.META = {
|
|
|
|
"SERVER_NAME": "example.com.",
|
|
|
|
"SERVER_PORT": "80",
|
|
|
|
}
|
|
|
|
site = get_current_site(request)
|
|
|
|
self.assertEqual(site.name, "example.com")
|
|
|
|
|
2020-07-23 14:18:58 +08:00
|
|
|
@override_settings(SITE_ID=None, ALLOWED_HOSTS=["example.com", "example.net"])
|
2015-05-04 06:08:28 +08:00
|
|
|
def test_get_current_site_no_site_id_and_handle_port_fallback(self):
|
|
|
|
request = HttpRequest()
|
|
|
|
s1 = self.site
|
|
|
|
s2 = Site.objects.create(domain="example.com:80", name="example.com:80")
|
|
|
|
|
|
|
|
# Host header without port
|
|
|
|
request.META = {"HTTP_HOST": "example.com"}
|
|
|
|
site = get_current_site(request)
|
|
|
|
self.assertEqual(site, s1)
|
|
|
|
|
|
|
|
# Host header with port - match, no fallback without port
|
|
|
|
request.META = {"HTTP_HOST": "example.com:80"}
|
|
|
|
site = get_current_site(request)
|
|
|
|
self.assertEqual(site, s2)
|
|
|
|
|
|
|
|
# Host header with port - no match, fallback without port
|
|
|
|
request.META = {"HTTP_HOST": "example.com:81"}
|
|
|
|
site = get_current_site(request)
|
|
|
|
self.assertEqual(site, s1)
|
|
|
|
|
|
|
|
# Host header with non-matching domain
|
|
|
|
request.META = {"HTTP_HOST": "example.net"}
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(ObjectDoesNotExist):
|
|
|
|
get_current_site(request)
|
2015-05-04 06:08:28 +08:00
|
|
|
|
|
|
|
# Ensure domain for RequestSite always matches host header
|
|
|
|
with self.modify_settings(INSTALLED_APPS={"remove": "django.contrib.sites"}):
|
|
|
|
request.META = {"HTTP_HOST": "example.com"}
|
|
|
|
site = get_current_site(request)
|
|
|
|
self.assertEqual(site.name, "example.com")
|
|
|
|
|
|
|
|
request.META = {"HTTP_HOST": "example.com:80"}
|
|
|
|
site = get_current_site(request)
|
|
|
|
self.assertEqual(site.name, "example.com:80")
|
|
|
|
|
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")
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
site.full_clean()
|
2013-02-24 22:54:09 +08:00
|
|
|
site.domain = "test\ttest"
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
site.full_clean()
|
2013-02-24 22:54:09 +08:00
|
|
|
site.domain = "test\ntest"
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
site.full_clean()
|
2013-11-19 04:16:09 +08:00
|
|
|
|
2016-06-04 06:02:38 +08:00
|
|
|
@override_settings(ALLOWED_HOSTS=["example.com"])
|
2014-10-01 01:15:59 +08:00
|
|
|
def test_clear_site_cache(self):
|
|
|
|
request = HttpRequest()
|
|
|
|
request.META = {
|
|
|
|
"SERVER_NAME": "example.com",
|
|
|
|
"SERVER_PORT": "80",
|
|
|
|
}
|
|
|
|
self.assertEqual(models.SITE_CACHE, {})
|
|
|
|
get_current_site(request)
|
|
|
|
expected_cache = {self.site.id: self.site}
|
|
|
|
self.assertEqual(models.SITE_CACHE, expected_cache)
|
|
|
|
|
2020-07-23 14:18:58 +08:00
|
|
|
with self.settings(SITE_ID=None):
|
2014-10-01 01:15:59 +08:00
|
|
|
get_current_site(request)
|
|
|
|
|
|
|
|
expected_cache.update({self.site.domain: self.site})
|
|
|
|
self.assertEqual(models.SITE_CACHE, expected_cache)
|
|
|
|
|
2015-02-13 02:58:37 +08:00
|
|
|
clear_site_cache(Site, instance=self.site, using="default")
|
|
|
|
self.assertEqual(models.SITE_CACHE, {})
|
|
|
|
|
2020-07-23 14:18:58 +08:00
|
|
|
@override_settings(SITE_ID=None, ALLOWED_HOSTS=["example2.com"])
|
2015-02-13 02:58:37 +08:00
|
|
|
def test_clear_site_cache_domain(self):
|
|
|
|
site = Site.objects.create(name="example2.com", domain="example2.com")
|
|
|
|
request = HttpRequest()
|
|
|
|
request.META = {
|
|
|
|
"SERVER_NAME": "example2.com",
|
|
|
|
"SERVER_PORT": "80",
|
|
|
|
}
|
|
|
|
get_current_site(request) # prime the models.SITE_CACHE
|
|
|
|
expected_cache = {site.domain: site}
|
|
|
|
self.assertEqual(models.SITE_CACHE, expected_cache)
|
|
|
|
|
|
|
|
# Site exists in 'default' database so using='other' shouldn't clear.
|
|
|
|
clear_site_cache(Site, instance=site, using="other")
|
|
|
|
self.assertEqual(models.SITE_CACHE, expected_cache)
|
|
|
|
# using='default' should clear.
|
|
|
|
clear_site_cache(Site, instance=site, using="default")
|
2014-10-01 01:15:59 +08:00
|
|
|
self.assertEqual(models.SITE_CACHE, {})
|
|
|
|
|
2015-05-15 01:59:36 +08:00
|
|
|
def test_unique_domain(self):
|
|
|
|
site = Site(domain=self.site.domain)
|
|
|
|
msg = "Site with this Domain name already exists."
|
|
|
|
with self.assertRaisesMessage(ValidationError, msg):
|
|
|
|
site.validate_unique()
|
|
|
|
|
2015-11-07 06:55:44 +08:00
|
|
|
def test_site_natural_key(self):
|
|
|
|
self.assertEqual(Site.objects.get_by_natural_key(self.site.domain), self.site)
|
|
|
|
self.assertEqual(self.site.natural_key(), (self.site.domain,))
|
|
|
|
|
2020-07-23 14:18:58 +08:00
|
|
|
@override_settings(SITE_ID="1")
|
|
|
|
def test_check_site_id(self):
|
|
|
|
self.assertEqual(
|
|
|
|
check_site_id(None),
|
|
|
|
[
|
|
|
|
checks.Error(
|
|
|
|
msg="The SITE_ID setting must be an integer",
|
|
|
|
id="sites.E101",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_valid_site_id(self):
|
|
|
|
for site_id in [1, None]:
|
|
|
|
with self.subTest(site_id=site_id), self.settings(SITE_ID=site_id):
|
|
|
|
self.assertEqual(check_site_id(None), [])
|
|
|
|
|
2017-09-28 19:58:01 +08:00
|
|
|
|
|
|
|
@override_settings(ALLOWED_HOSTS=["example.com"])
|
|
|
|
class RequestSiteTests(SimpleTestCase):
|
|
|
|
def setUp(self):
|
2016-04-03 16:45:23 +08:00
|
|
|
request = HttpRequest()
|
2017-09-28 19:58:01 +08:00
|
|
|
request.META = {"HTTP_HOST": "example.com"}
|
|
|
|
self.site = RequestSite(request)
|
|
|
|
|
2017-09-27 23:22:50 +08:00
|
|
|
def test_init_attributes(self):
|
|
|
|
self.assertEqual(self.site.domain, "example.com")
|
|
|
|
self.assertEqual(self.site.name, "example.com")
|
|
|
|
|
|
|
|
def test_str(self):
|
|
|
|
self.assertEqual(str(self.site), "example.com")
|
|
|
|
|
2017-09-28 19:58:01 +08:00
|
|
|
def test_save(self):
|
2016-04-03 16:45:23 +08:00
|
|
|
msg = "RequestSite cannot be saved."
|
|
|
|
with self.assertRaisesMessage(NotImplementedError, msg):
|
2017-09-28 19:58:01 +08:00
|
|
|
self.site.save()
|
2016-04-03 16:45:23 +08:00
|
|
|
|
2017-09-28 19:58:01 +08:00
|
|
|
def test_delete(self):
|
2016-04-03 16:45:23 +08:00
|
|
|
msg = "RequestSite cannot be deleted."
|
|
|
|
with self.assertRaisesMessage(NotImplementedError, msg):
|
2017-09-28 19:58:01 +08:00
|
|
|
self.site.delete()
|
2016-04-03 16:45:23 +08:00
|
|
|
|
2014-11-29 16:36:39 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class JustOtherRouter:
|
2015-02-19 15:27:58 +08:00
|
|
|
def allow_migrate(self, db, app_label, **hints):
|
2014-11-29 16:36:39 +08:00
|
|
|
return db == "other"
|
|
|
|
|
|
|
|
|
|
|
|
@modify_settings(INSTALLED_APPS={"append": "django.contrib.sites"})
|
|
|
|
class CreateDefaultSiteTests(TestCase):
|
2018-07-12 12:12:20 +08:00
|
|
|
databases = {"default", "other"}
|
2014-11-29 16:36:39 +08:00
|
|
|
|
2018-11-24 09:59:38 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2014-11-29 16:36:39 +08:00
|
|
|
# Delete the site created as part of the default migration process.
|
|
|
|
Site.objects.all().delete()
|
|
|
|
|
2018-11-24 09:59:38 +08:00
|
|
|
def setUp(self):
|
|
|
|
self.app_config = apps.get_app_config("sites")
|
|
|
|
|
2014-11-29 16:36:39 +08:00
|
|
|
def test_basic(self):
|
2014-11-25 20:35:39 +08:00
|
|
|
"""
|
2014-11-29 16:36:39 +08:00
|
|
|
#15346, #15573 - create_default_site() creates an example site only if
|
|
|
|
none exist.
|
2014-11-25 20:35:39 +08:00
|
|
|
"""
|
2014-11-29 16:36:39 +08:00
|
|
|
with captured_stdout() as stdout:
|
|
|
|
create_default_site(self.app_config)
|
|
|
|
self.assertEqual(Site.objects.count(), 1)
|
|
|
|
self.assertIn("Creating example.com", stdout.getvalue())
|
|
|
|
|
|
|
|
with captured_stdout() as stdout:
|
|
|
|
create_default_site(self.app_config)
|
|
|
|
self.assertEqual(Site.objects.count(), 1)
|
|
|
|
self.assertEqual("", stdout.getvalue())
|
|
|
|
|
|
|
|
@override_settings(DATABASE_ROUTERS=[JustOtherRouter()])
|
2015-02-13 02:58:37 +08:00
|
|
|
def test_multi_db_with_router(self):
|
2014-11-29 16:36:39 +08:00
|
|
|
"""
|
|
|
|
#16353, #16828 - The default site creation should respect db routing.
|
|
|
|
"""
|
2014-12-27 22:06:38 +08:00
|
|
|
create_default_site(self.app_config, using="default", verbosity=0)
|
|
|
|
create_default_site(self.app_config, using="other", verbosity=0)
|
2014-11-29 16:36:39 +08:00
|
|
|
self.assertFalse(Site.objects.using("default").exists())
|
|
|
|
self.assertTrue(Site.objects.using("other").exists())
|
|
|
|
|
2015-02-13 02:58:37 +08:00
|
|
|
def test_multi_db(self):
|
|
|
|
create_default_site(self.app_config, using="default", verbosity=0)
|
|
|
|
create_default_site(self.app_config, using="other", verbosity=0)
|
|
|
|
self.assertTrue(Site.objects.using("default").exists())
|
|
|
|
self.assertTrue(Site.objects.using("other").exists())
|
|
|
|
|
2014-11-29 16:36:39 +08:00
|
|
|
def test_save_another(self):
|
|
|
|
"""
|
|
|
|
#17415 - Another site can be created right after the default one.
|
|
|
|
|
|
|
|
On some backends the sequence needs to be reset after saving with an
|
2016-10-27 15:53:39 +08:00
|
|
|
explicit ID. There shouldn't be a sequence collisions by saving another
|
|
|
|
site. This test is only meaningful with databases that use sequences
|
|
|
|
for automatic primary keys such as PostgreSQL and Oracle.
|
2014-11-29 16:36:39 +08:00
|
|
|
"""
|
|
|
|
create_default_site(self.app_config, verbosity=0)
|
|
|
|
Site(domain="example2.com", name="example2.com").save()
|
|
|
|
|
|
|
|
def test_signal(self):
|
|
|
|
"""
|
|
|
|
#23641 - Sending the ``post_migrate`` signal triggers creation of the
|
|
|
|
default site.
|
|
|
|
"""
|
|
|
|
post_migrate.send(
|
|
|
|
sender=self.app_config, app_config=self.app_config, verbosity=0
|
|
|
|
)
|
2014-11-25 20:35:39 +08:00
|
|
|
self.assertTrue(Site.objects.exists())
|
|
|
|
|
2014-12-01 01:03:40 +08:00
|
|
|
@override_settings(SITE_ID=35696)
|
|
|
|
def test_custom_site_id(self):
|
|
|
|
"""
|
|
|
|
#23945 - The configured ``SITE_ID`` should be respected.
|
|
|
|
"""
|
|
|
|
create_default_site(self.app_config, verbosity=0)
|
|
|
|
self.assertEqual(Site.objects.get().pk, 35696)
|
|
|
|
|
2021-07-27 16:41:51 +08:00
|
|
|
@override_settings() # Restore original ``SITE_ID`` afterward.
|
2015-03-16 06:54:20 +08:00
|
|
|
def test_no_site_id(self):
|
|
|
|
"""
|
|
|
|
#24488 - The pk should default to 1 if no ``SITE_ID`` is configured.
|
|
|
|
"""
|
|
|
|
del settings.SITE_ID
|
|
|
|
create_default_site(self.app_config, verbosity=0)
|
|
|
|
self.assertEqual(Site.objects.get().pk, 1)
|
|
|
|
|
2016-05-13 23:59:27 +08:00
|
|
|
def test_unavailable_site_model(self):
|
|
|
|
"""
|
|
|
|
#24075 - A Site shouldn't be created if the model isn't available.
|
|
|
|
"""
|
|
|
|
apps = Apps()
|
|
|
|
create_default_site(self.app_config, verbosity=0, apps=apps)
|
|
|
|
self.assertFalse(Site.objects.exists())
|
|
|
|
|
2013-11-19 04:16:09 +08:00
|
|
|
|
|
|
|
class MiddlewareTest(TestCase):
|
2016-06-07 08:26:24 +08:00
|
|
|
def test_request(self):
|
|
|
|
def get_response(request):
|
|
|
|
return HttpResponse(str(request.site.id))
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2016-06-07 08:26:24 +08:00
|
|
|
response = CurrentSiteMiddleware(get_response)(HttpRequest())
|
|
|
|
self.assertContains(response, settings.SITE_ID)
|