test_liveserver.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 os
  7. from urllib.request import urlopen
  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. TEST_ROOT = os.path.dirname(__file__)
  12. TEST_SETTINGS = {
  13. 'MEDIA_URL': '/media/',
  14. 'STATIC_URL': '/static/',
  15. 'MEDIA_ROOT': os.path.join(TEST_ROOT, 'project', 'site_media', 'media'),
  16. 'STATIC_ROOT': os.path.join(TEST_ROOT, 'project', 'site_media', 'static'),
  17. }
  18. class LiveServerBase(StaticLiveServerTestCase):
  19. available_apps = []
  20. @classmethod
  21. def setUpClass(cls):
  22. # Override settings
  23. cls.settings_override = override_settings(**TEST_SETTINGS)
  24. cls.settings_override.enable()
  25. super().setUpClass()
  26. @classmethod
  27. def tearDownClass(cls):
  28. super().tearDownClass()
  29. # Restore original settings
  30. cls.settings_override.disable()
  31. class StaticLiveServerChecks(LiveServerBase):
  32. @classmethod
  33. def setUpClass(cls):
  34. # If contrib.staticfiles isn't configured properly, the exception
  35. # should bubble up to the main thread.
  36. old_STATIC_URL = TEST_SETTINGS['STATIC_URL']
  37. TEST_SETTINGS['STATIC_URL'] = None
  38. cls.raises_exception()
  39. TEST_SETTINGS['STATIC_URL'] = old_STATIC_URL
  40. @classmethod
  41. def tearDownClass(cls):
  42. # skip it, as setUpClass doesn't call its parent either
  43. pass
  44. @classmethod
  45. def raises_exception(cls):
  46. try:
  47. super().setUpClass()
  48. raise Exception("The line above should have raised an exception")
  49. except ImproperlyConfigured:
  50. # This raises ImproperlyConfigured("You're using the staticfiles
  51. # app without having set the required STATIC_URL setting.")
  52. pass
  53. finally:
  54. super().tearDownClass()
  55. def test_test_test(self):
  56. # Intentionally empty method so that the test is picked up by the
  57. # test runner and the overridden setUpClass() method is executed.
  58. pass
  59. class StaticLiveServerView(LiveServerBase):
  60. def urlopen(self, url):
  61. return urlopen(self.live_server_url + url)
  62. # The test is going to access a static file stored in this application.
  63. @modify_settings(INSTALLED_APPS={'append': 'staticfiles_tests.apps.test'})
  64. def test_collectstatic_emulation(self):
  65. """
  66. StaticLiveServerTestCase use of staticfiles' serve() allows it
  67. to discover app's static assets without having to collectstatic first.
  68. """
  69. with self.urlopen('/static/test/file.txt') as f:
  70. self.assertEqual(f.read().rstrip(b'\r\n'), b'In static directory.')