test_liveserver.py 3.2 KB

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