tests.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from __future__ import unicode_literals
  2. from django.conf import settings
  3. from django.core.exceptions import ObjectDoesNotExist, ValidationError
  4. from django.http import HttpRequest
  5. from django.test import TestCase, modify_settings, override_settings
  6. from .middleware import CurrentSiteMiddleware
  7. from .models import Site
  8. from .requests import RequestSite
  9. from .shortcuts import get_current_site
  10. @modify_settings(INSTALLED_APPS={'append': 'django.contrib.sites'})
  11. class SitesFrameworkTests(TestCase):
  12. def setUp(self):
  13. Site(id=settings.SITE_ID, domain="example.com", name="example.com").save()
  14. def test_save_another(self):
  15. # Regression for #17415
  16. # On some backends the sequence needs reset after save with explicit ID.
  17. # Test that there is no sequence collisions by saving another site.
  18. Site(domain="example2.com", name="example2.com").save()
  19. def test_site_manager(self):
  20. # Make sure that get_current() does not return a deleted Site object.
  21. s = Site.objects.get_current()
  22. self.assertTrue(isinstance(s, Site))
  23. s.delete()
  24. self.assertRaises(ObjectDoesNotExist, Site.objects.get_current)
  25. def test_site_cache(self):
  26. # After updating a Site object (e.g. via the admin), we shouldn't return a
  27. # bogus value from the SITE_CACHE.
  28. site = Site.objects.get_current()
  29. self.assertEqual("example.com", site.name)
  30. s2 = Site.objects.get(id=settings.SITE_ID)
  31. s2.name = "Example site"
  32. s2.save()
  33. site = Site.objects.get_current()
  34. self.assertEqual("Example site", site.name)
  35. def test_delete_all_sites_clears_cache(self):
  36. # When all site objects are deleted the cache should also
  37. # be cleared and get_current() should raise a DoesNotExist.
  38. self.assertIsInstance(Site.objects.get_current(), Site)
  39. Site.objects.all().delete()
  40. self.assertRaises(Site.DoesNotExist, Site.objects.get_current)
  41. @override_settings(ALLOWED_HOSTS=['example.com'])
  42. def test_get_current_site(self):
  43. # Test that the correct Site object is returned
  44. request = HttpRequest()
  45. request.META = {
  46. "SERVER_NAME": "example.com",
  47. "SERVER_PORT": "80",
  48. }
  49. site = get_current_site(request)
  50. self.assertTrue(isinstance(site, Site))
  51. self.assertEqual(site.id, settings.SITE_ID)
  52. # Test that an exception is raised if the sites framework is installed
  53. # but there is no matching Site
  54. site.delete()
  55. self.assertRaises(ObjectDoesNotExist, get_current_site, request)
  56. # A RequestSite is returned if the sites framework is not installed
  57. with self.modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'}):
  58. site = get_current_site(request)
  59. self.assertTrue(isinstance(site, RequestSite))
  60. self.assertEqual(site.name, "example.com")
  61. def test_domain_name_with_whitespaces(self):
  62. # Regression for #17320
  63. # Domain names are not allowed contain whitespace characters
  64. site = Site(name="test name", domain="test test")
  65. self.assertRaises(ValidationError, site.full_clean)
  66. site.domain = "test\ttest"
  67. self.assertRaises(ValidationError, site.full_clean)
  68. site.domain = "test\ntest"
  69. self.assertRaises(ValidationError, site.full_clean)
  70. class MiddlewareTest(TestCase):
  71. def test_request(self):
  72. """ Makes sure that the request has correct `site` attribute. """
  73. middleware = CurrentSiteMiddleware()
  74. request = HttpRequest()
  75. middleware.process_request(request)
  76. self.assertEqual(request.site.id, settings.SITE_ID)