tests.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. from __future__ import unicode_literals
  2. from django.apps import apps
  3. from django.apps.registry import Apps
  4. from django.conf import settings
  5. from django.contrib.sites import models
  6. from django.contrib.sites.management import create_default_site
  7. from django.contrib.sites.middleware import CurrentSiteMiddleware
  8. from django.contrib.sites.models import Site, clear_site_cache
  9. from django.contrib.sites.requests import RequestSite
  10. from django.contrib.sites.shortcuts import get_current_site
  11. from django.core.exceptions import ObjectDoesNotExist, ValidationError
  12. from django.db.models.signals import post_migrate
  13. from django.http import HttpRequest, HttpResponse
  14. from django.test import TestCase, modify_settings, override_settings
  15. from django.test.utils import captured_stdout
  16. @modify_settings(INSTALLED_APPS={'append': 'django.contrib.sites'})
  17. class SitesFrameworkTests(TestCase):
  18. multi_db = True
  19. def setUp(self):
  20. self.site = Site(
  21. id=settings.SITE_ID,
  22. domain="example.com",
  23. name="example.com",
  24. )
  25. self.site.save()
  26. def tearDown(self):
  27. Site.objects.clear_cache()
  28. def test_site_manager(self):
  29. # Make sure that get_current() does not return a deleted Site object.
  30. s = Site.objects.get_current()
  31. self.assertIsInstance(s, Site)
  32. s.delete()
  33. with self.assertRaises(ObjectDoesNotExist):
  34. Site.objects.get_current()
  35. def test_site_cache(self):
  36. # After updating a Site object (e.g. via the admin), we shouldn't return a
  37. # bogus value from the SITE_CACHE.
  38. site = Site.objects.get_current()
  39. self.assertEqual("example.com", site.name)
  40. s2 = Site.objects.get(id=settings.SITE_ID)
  41. s2.name = "Example site"
  42. s2.save()
  43. site = Site.objects.get_current()
  44. self.assertEqual("Example site", site.name)
  45. def test_delete_all_sites_clears_cache(self):
  46. # When all site objects are deleted the cache should also
  47. # be cleared and get_current() should raise a DoesNotExist.
  48. self.assertIsInstance(Site.objects.get_current(), Site)
  49. Site.objects.all().delete()
  50. with self.assertRaises(Site.DoesNotExist):
  51. Site.objects.get_current()
  52. @override_settings(ALLOWED_HOSTS=['example.com'])
  53. def test_get_current_site(self):
  54. # Test that the correct Site object is returned
  55. request = HttpRequest()
  56. request.META = {
  57. "SERVER_NAME": "example.com",
  58. "SERVER_PORT": "80",
  59. }
  60. site = get_current_site(request)
  61. self.assertIsInstance(site, Site)
  62. self.assertEqual(site.id, settings.SITE_ID)
  63. # Test that an exception is raised if the sites framework is installed
  64. # but there is no matching Site
  65. site.delete()
  66. with self.assertRaises(ObjectDoesNotExist):
  67. get_current_site(request)
  68. # A RequestSite is returned if the sites framework is not installed
  69. with self.modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'}):
  70. site = get_current_site(request)
  71. self.assertIsInstance(site, RequestSite)
  72. self.assertEqual(site.name, "example.com")
  73. @override_settings(SITE_ID='', ALLOWED_HOSTS=['example.com'])
  74. def test_get_current_site_no_site_id(self):
  75. request = HttpRequest()
  76. request.META = {
  77. "SERVER_NAME": "example.com",
  78. "SERVER_PORT": "80",
  79. }
  80. del settings.SITE_ID
  81. site = get_current_site(request)
  82. self.assertEqual(site.name, "example.com")
  83. @override_settings(SITE_ID='', ALLOWED_HOSTS=['example.com', 'example.net'])
  84. def test_get_current_site_no_site_id_and_handle_port_fallback(self):
  85. request = HttpRequest()
  86. s1 = self.site
  87. s2 = Site.objects.create(domain='example.com:80', name='example.com:80')
  88. # Host header without port
  89. request.META = {'HTTP_HOST': 'example.com'}
  90. site = get_current_site(request)
  91. self.assertEqual(site, s1)
  92. # Host header with port - match, no fallback without port
  93. request.META = {'HTTP_HOST': 'example.com:80'}
  94. site = get_current_site(request)
  95. self.assertEqual(site, s2)
  96. # Host header with port - no match, fallback without port
  97. request.META = {'HTTP_HOST': 'example.com:81'}
  98. site = get_current_site(request)
  99. self.assertEqual(site, s1)
  100. # Host header with non-matching domain
  101. request.META = {'HTTP_HOST': 'example.net'}
  102. with self.assertRaises(ObjectDoesNotExist):
  103. get_current_site(request)
  104. # Ensure domain for RequestSite always matches host header
  105. with self.modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'}):
  106. request.META = {'HTTP_HOST': 'example.com'}
  107. site = get_current_site(request)
  108. self.assertEqual(site.name, 'example.com')
  109. request.META = {'HTTP_HOST': 'example.com:80'}
  110. site = get_current_site(request)
  111. self.assertEqual(site.name, 'example.com:80')
  112. def test_domain_name_with_whitespaces(self):
  113. # Regression for #17320
  114. # Domain names are not allowed contain whitespace characters
  115. site = Site(name="test name", domain="test test")
  116. with self.assertRaises(ValidationError):
  117. site.full_clean()
  118. site.domain = "test\ttest"
  119. with self.assertRaises(ValidationError):
  120. site.full_clean()
  121. site.domain = "test\ntest"
  122. with self.assertRaises(ValidationError):
  123. site.full_clean()
  124. @override_settings(ALLOWED_HOSTS=['example.com'])
  125. def test_clear_site_cache(self):
  126. request = HttpRequest()
  127. request.META = {
  128. "SERVER_NAME": "example.com",
  129. "SERVER_PORT": "80",
  130. }
  131. self.assertEqual(models.SITE_CACHE, {})
  132. get_current_site(request)
  133. expected_cache = {self.site.id: self.site}
  134. self.assertEqual(models.SITE_CACHE, expected_cache)
  135. with self.settings(SITE_ID=''):
  136. get_current_site(request)
  137. expected_cache.update({self.site.domain: self.site})
  138. self.assertEqual(models.SITE_CACHE, expected_cache)
  139. clear_site_cache(Site, instance=self.site, using='default')
  140. self.assertEqual(models.SITE_CACHE, {})
  141. @override_settings(SITE_ID='', ALLOWED_HOSTS=['example2.com'])
  142. def test_clear_site_cache_domain(self):
  143. site = Site.objects.create(name='example2.com', domain='example2.com')
  144. request = HttpRequest()
  145. request.META = {
  146. "SERVER_NAME": "example2.com",
  147. "SERVER_PORT": "80",
  148. }
  149. get_current_site(request) # prime the models.SITE_CACHE
  150. expected_cache = {site.domain: site}
  151. self.assertEqual(models.SITE_CACHE, expected_cache)
  152. # Site exists in 'default' database so using='other' shouldn't clear.
  153. clear_site_cache(Site, instance=site, using='other')
  154. self.assertEqual(models.SITE_CACHE, expected_cache)
  155. # using='default' should clear.
  156. clear_site_cache(Site, instance=site, using='default')
  157. self.assertEqual(models.SITE_CACHE, {})
  158. def test_unique_domain(self):
  159. site = Site(domain=self.site.domain)
  160. msg = 'Site with this Domain name already exists.'
  161. with self.assertRaisesMessage(ValidationError, msg):
  162. site.validate_unique()
  163. def test_site_natural_key(self):
  164. self.assertEqual(Site.objects.get_by_natural_key(self.site.domain), self.site)
  165. self.assertEqual(self.site.natural_key(), (self.site.domain,))
  166. @override_settings(ALLOWED_HOSTS=['example.com'])
  167. def test_requestsite_save_notimplemented_msg(self):
  168. # Test response msg for RequestSite.save NotImplementedError
  169. request = HttpRequest()
  170. request.META = {
  171. "HTTP_HOST": "example.com",
  172. }
  173. msg = 'RequestSite cannot be saved.'
  174. with self.assertRaisesMessage(NotImplementedError, msg):
  175. RequestSite(request).save()
  176. @override_settings(ALLOWED_HOSTS=['example.com'])
  177. def test_requestsite_delete_notimplemented_msg(self):
  178. # Test response msg for RequestSite.delete NotImplementedError
  179. request = HttpRequest()
  180. request.META = {
  181. "HTTP_HOST": "example.com",
  182. }
  183. msg = 'RequestSite cannot be deleted.'
  184. with self.assertRaisesMessage(NotImplementedError, msg):
  185. RequestSite(request).delete()
  186. class JustOtherRouter(object):
  187. def allow_migrate(self, db, app_label, **hints):
  188. return db == 'other'
  189. @modify_settings(INSTALLED_APPS={'append': 'django.contrib.sites'})
  190. class CreateDefaultSiteTests(TestCase):
  191. multi_db = True
  192. def setUp(self):
  193. self.app_config = apps.get_app_config('sites')
  194. # Delete the site created as part of the default migration process.
  195. Site.objects.all().delete()
  196. def test_basic(self):
  197. """
  198. #15346, #15573 - create_default_site() creates an example site only if
  199. none exist.
  200. """
  201. with captured_stdout() as stdout:
  202. create_default_site(self.app_config)
  203. self.assertEqual(Site.objects.count(), 1)
  204. self.assertIn("Creating example.com", stdout.getvalue())
  205. with captured_stdout() as stdout:
  206. create_default_site(self.app_config)
  207. self.assertEqual(Site.objects.count(), 1)
  208. self.assertEqual("", stdout.getvalue())
  209. @override_settings(DATABASE_ROUTERS=[JustOtherRouter()])
  210. def test_multi_db_with_router(self):
  211. """
  212. #16353, #16828 - The default site creation should respect db routing.
  213. """
  214. create_default_site(self.app_config, using='default', verbosity=0)
  215. create_default_site(self.app_config, using='other', verbosity=0)
  216. self.assertFalse(Site.objects.using('default').exists())
  217. self.assertTrue(Site.objects.using('other').exists())
  218. def test_multi_db(self):
  219. create_default_site(self.app_config, using='default', verbosity=0)
  220. create_default_site(self.app_config, using='other', verbosity=0)
  221. self.assertTrue(Site.objects.using('default').exists())
  222. self.assertTrue(Site.objects.using('other').exists())
  223. def test_save_another(self):
  224. """
  225. #17415 - Another site can be created right after the default one.
  226. On some backends the sequence needs to be reset after saving with an
  227. explicit ID. Test that there isn't a sequence collisions by saving
  228. another site. This test is only meaningful with databases that use
  229. sequences for automatic primary keys such as PostgreSQL and Oracle.
  230. """
  231. create_default_site(self.app_config, verbosity=0)
  232. Site(domain='example2.com', name='example2.com').save()
  233. def test_signal(self):
  234. """
  235. #23641 - Sending the ``post_migrate`` signal triggers creation of the
  236. default site.
  237. """
  238. post_migrate.send(sender=self.app_config, app_config=self.app_config, verbosity=0)
  239. self.assertTrue(Site.objects.exists())
  240. @override_settings(SITE_ID=35696)
  241. def test_custom_site_id(self):
  242. """
  243. #23945 - The configured ``SITE_ID`` should be respected.
  244. """
  245. create_default_site(self.app_config, verbosity=0)
  246. self.assertEqual(Site.objects.get().pk, 35696)
  247. @override_settings() # Restore original ``SITE_ID`` afterwards.
  248. def test_no_site_id(self):
  249. """
  250. #24488 - The pk should default to 1 if no ``SITE_ID`` is configured.
  251. """
  252. del settings.SITE_ID
  253. create_default_site(self.app_config, verbosity=0)
  254. self.assertEqual(Site.objects.get().pk, 1)
  255. def test_unavailable_site_model(self):
  256. """
  257. #24075 - A Site shouldn't be created if the model isn't available.
  258. """
  259. apps = Apps()
  260. create_default_site(self.app_config, verbosity=0, apps=apps)
  261. self.assertFalse(Site.objects.exists())
  262. class MiddlewareTest(TestCase):
  263. def test_old_style_request(self):
  264. """ Makes sure that the request has correct `site` attribute. """
  265. middleware = CurrentSiteMiddleware()
  266. request = HttpRequest()
  267. middleware.process_request(request)
  268. self.assertEqual(request.site.id, settings.SITE_ID)
  269. def test_request(self):
  270. def get_response(request):
  271. return HttpResponse(str(request.site.id))
  272. response = CurrentSiteMiddleware(get_response)(HttpRequest())
  273. self.assertContains(response, settings.SITE_ID)