test_liveserver.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """
  2. A subset of the tests in tests/servers/tests exercising
  3. django.contrib.staticfiles.testing.StaticLiveServerTestCase instead of
  4. django.test.LiveServerTestCase.
  5. """
  6. import contextlib
  7. import os
  8. from django.contrib.staticfiles.testing import StaticLiveServerTestCase
  9. from django.core.exceptions import ImproperlyConfigured
  10. from django.test import modify_settings, override_settings
  11. from django.utils._os import upath
  12. from django.utils.six.moves.urllib.request import urlopen
  13. TEST_ROOT = os.path.dirname(upath(__file__))
  14. TEST_SETTINGS = {
  15. 'MEDIA_URL': '/media/',
  16. 'STATIC_URL': '/static/',
  17. 'MEDIA_ROOT': os.path.join(TEST_ROOT, 'project', 'site_media', 'media'),
  18. 'STATIC_ROOT': os.path.join(TEST_ROOT, 'project', 'site_media', 'static'),
  19. }
  20. class LiveServerBase(StaticLiveServerTestCase):
  21. available_apps = []
  22. @classmethod
  23. def setUpClass(cls):
  24. # Override settings
  25. cls.settings_override = override_settings(**TEST_SETTINGS)
  26. cls.settings_override.enable()
  27. super(LiveServerBase, cls).setUpClass()
  28. @classmethod
  29. def tearDownClass(cls):
  30. super(LiveServerBase, cls).tearDownClass()
  31. # Restore original settings
  32. cls.settings_override.disable()
  33. class StaticLiveServerChecks(LiveServerBase):
  34. @classmethod
  35. def setUpClass(cls):
  36. # Backup original environment variable
  37. address_predefined = 'DJANGO_LIVE_TEST_SERVER_ADDRESS' in os.environ
  38. old_address = os.environ.get('DJANGO_LIVE_TEST_SERVER_ADDRESS')
  39. # If contrib.staticfiles isn't configured properly, the exception
  40. # should bubble up to the main thread.
  41. old_STATIC_URL = TEST_SETTINGS['STATIC_URL']
  42. TEST_SETTINGS['STATIC_URL'] = None
  43. cls.raises_exception('localhost:8081', ImproperlyConfigured)
  44. TEST_SETTINGS['STATIC_URL'] = old_STATIC_URL
  45. # Restore original environment variable
  46. if address_predefined:
  47. os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = old_address
  48. else:
  49. del os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS']
  50. @classmethod
  51. def tearDownClass(cls):
  52. # skip it, as setUpClass doesn't call its parent either
  53. pass
  54. @classmethod
  55. def raises_exception(cls, address, exception):
  56. os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = address
  57. try:
  58. super(StaticLiveServerChecks, cls).setUpClass()
  59. raise Exception("The line above should have raised an exception")
  60. except exception:
  61. pass
  62. finally:
  63. super(StaticLiveServerChecks, cls).tearDownClass()
  64. def test_test_test(self):
  65. # Intentionally empty method so that the test is picked up by the
  66. # test runner and the overridden setUpClass() method is executed.
  67. pass
  68. class StaticLiveServerView(LiveServerBase):
  69. def urlopen(self, url):
  70. return urlopen(self.live_server_url + url)
  71. # The test is going to access a static file stored in this application.
  72. @modify_settings(INSTALLED_APPS={'append': 'staticfiles_tests.apps.test'})
  73. def test_collectstatic_emulation(self):
  74. """
  75. Test that StaticLiveServerTestCase use of staticfiles' serve() allows it
  76. to discover app's static assets without having to collectstatic first.
  77. """
  78. with contextlib.closing(self.urlopen('/static/test/file.txt')) as f:
  79. self.assertEqual(f.read().rstrip(b'\r\n'), b'In static directory.')