summaryrefslogtreecommitdiff
path: root/tests/sites_tests
diff options
context:
space:
mode:
authorParth Verma <v.parth98@gmail.com>2020-07-23 11:48:58 +0530
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-07-24 10:41:55 +0200
commit41065cfed56d5408dd8f267b9e70089471a7f1be (patch)
treed16d1947f5c2419e199c23cdf16133c5135670cd /tests/sites_tests
parent248d03fbe932b0844c628e56dafba334f9e028e4 (diff)
Fixed #31802 -- Added system check for non-integer SITE_ID.
Diffstat (limited to 'tests/sites_tests')
-rw-r--r--tests/sites_tests/tests.py26
1 files changed, 21 insertions, 5 deletions
diff --git a/tests/sites_tests/tests.py b/tests/sites_tests/tests.py
index 2971a5b853..23a3b5a13f 100644
--- a/tests/sites_tests/tests.py
+++ b/tests/sites_tests/tests.py
@@ -2,11 +2,13 @@ from django.apps import apps
from django.apps.registry import Apps
from django.conf import settings
from django.contrib.sites import models
+from django.contrib.sites.checks import check_site_id
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
+from django.core import checks
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db.models.signals import post_migrate
from django.http import HttpRequest, HttpResponse
@@ -82,7 +84,7 @@ class SitesFrameworkTests(TestCase):
self.assertIsInstance(site, RequestSite)
self.assertEqual(site.name, "example.com")
- @override_settings(SITE_ID='', ALLOWED_HOSTS=['example.com'])
+ @override_settings(SITE_ID=None, ALLOWED_HOSTS=['example.com'])
def test_get_current_site_no_site_id(self):
request = HttpRequest()
request.META = {
@@ -93,7 +95,7 @@ class SitesFrameworkTests(TestCase):
site = get_current_site(request)
self.assertEqual(site.name, "example.com")
- @override_settings(SITE_ID='', ALLOWED_HOSTS=['example.com'])
+ @override_settings(SITE_ID=None, ALLOWED_HOSTS=['example.com'])
def test_get_current_site_host_with_trailing_dot(self):
"""
The site is matched if the name in the request has a trailing dot.
@@ -106,7 +108,7 @@ class SitesFrameworkTests(TestCase):
site = get_current_site(request)
self.assertEqual(site.name, 'example.com')
- @override_settings(SITE_ID='', ALLOWED_HOSTS=['example.com', 'example.net'])
+ @override_settings(SITE_ID=None, ALLOWED_HOSTS=['example.com', 'example.net'])
def test_get_current_site_no_site_id_and_handle_port_fallback(self):
request = HttpRequest()
s1 = self.site
@@ -167,7 +169,7 @@ class SitesFrameworkTests(TestCase):
expected_cache = {self.site.id: self.site}
self.assertEqual(models.SITE_CACHE, expected_cache)
- with self.settings(SITE_ID=''):
+ with self.settings(SITE_ID=None):
get_current_site(request)
expected_cache.update({self.site.domain: self.site})
@@ -176,7 +178,7 @@ class SitesFrameworkTests(TestCase):
clear_site_cache(Site, instance=self.site, using='default')
self.assertEqual(models.SITE_CACHE, {})
- @override_settings(SITE_ID='', ALLOWED_HOSTS=['example2.com'])
+ @override_settings(SITE_ID=None, ALLOWED_HOSTS=['example2.com'])
def test_clear_site_cache_domain(self):
site = Site.objects.create(name='example2.com', domain='example2.com')
request = HttpRequest()
@@ -205,6 +207,20 @@ class SitesFrameworkTests(TestCase):
self.assertEqual(Site.objects.get_by_natural_key(self.site.domain), self.site)
self.assertEqual(self.site.natural_key(), (self.site.domain,))
+ @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), [])
+
@override_settings(ALLOWED_HOSTS=['example.com'])
class RequestSiteTests(SimpleTestCase):