2013-06-01 19:24:46 +02:00
|
|
|
"""
|
2015-12-03 00:55:50 +01:00
|
|
|
A subset of the tests in tests/servers/tests exercising
|
2014-08-28 08:44:26 +02:00
|
|
|
django.contrib.staticfiles.testing.StaticLiveServerTestCase instead of
|
2013-06-01 19:24:46 +02:00
|
|
|
django.test.LiveServerTestCase.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
2017-01-07 12:11:46 +01:00
|
|
|
from urllib.request import urlopen
|
2013-06-01 19:24:46 +02:00
|
|
|
|
2015-01-28 13:35:27 +01:00
|
|
|
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
|
2013-06-01 19:24:46 +02:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2014-06-21 18:27:46 +02:00
|
|
|
from django.test import modify_settings, override_settings
|
2013-06-01 19:24:46 +02:00
|
|
|
|
2017-01-20 14:01:02 +01:00
|
|
|
TEST_ROOT = os.path.dirname(__file__)
|
2013-06-01 19:24:46 +02:00
|
|
|
TEST_SETTINGS = {
|
2021-02-05 18:41:49 +01:00
|
|
|
'MEDIA_URL': 'media/',
|
|
|
|
'STATIC_URL': 'static/',
|
2013-06-01 19:24:46 +02:00
|
|
|
'MEDIA_ROOT': os.path.join(TEST_ROOT, 'project', 'site_media', 'media'),
|
|
|
|
'STATIC_ROOT': os.path.join(TEST_ROOT, 'project', 'site_media', 'static'),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-28 08:44:26 +02:00
|
|
|
class LiveServerBase(StaticLiveServerTestCase):
|
2013-06-01 19:24:46 +02:00
|
|
|
|
|
|
|
available_apps = []
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
# Override settings
|
|
|
|
cls.settings_override = override_settings(**TEST_SETTINGS)
|
|
|
|
cls.settings_override.enable()
|
2021-02-15 10:15:06 +01:00
|
|
|
try:
|
|
|
|
super().setUpClass()
|
|
|
|
except Exception:
|
|
|
|
# Clean up since tearDownClass() isn't called on errors.
|
|
|
|
cls._tearDownLiveServerBase()
|
|
|
|
raise
|
2013-06-01 19:24:46 +02:00
|
|
|
|
|
|
|
@classmethod
|
2021-02-15 10:15:06 +01:00
|
|
|
def _tearDownLiveServerBase(cls):
|
2013-06-01 19:24:46 +02:00
|
|
|
# Restore original settings
|
|
|
|
cls.settings_override.disable()
|
|
|
|
|
2021-02-15 10:15:06 +01:00
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
super().tearDownClass()
|
|
|
|
cls._tearDownLiveServerBase()
|
|
|
|
|
2013-06-01 19:24:46 +02:00
|
|
|
|
|
|
|
class StaticLiveServerChecks(LiveServerBase):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
# If contrib.staticfiles isn't configured properly, the exception
|
|
|
|
# should bubble up to the main thread.
|
|
|
|
old_STATIC_URL = TEST_SETTINGS['STATIC_URL']
|
|
|
|
TEST_SETTINGS['STATIC_URL'] = None
|
2021-02-15 10:15:06 +01:00
|
|
|
try:
|
|
|
|
cls.raises_exception()
|
|
|
|
finally:
|
|
|
|
TEST_SETTINGS['STATIC_URL'] = old_STATIC_URL
|
2013-06-01 19:24:46 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
# skip it, as setUpClass doesn't call its parent either
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
2016-06-23 18:04:05 +02:00
|
|
|
def raises_exception(cls):
|
2013-06-01 19:24:46 +02:00
|
|
|
try:
|
2017-01-21 14:13:44 +01:00
|
|
|
super().setUpClass()
|
2016-06-23 18:04:05 +02:00
|
|
|
except ImproperlyConfigured:
|
|
|
|
# This raises ImproperlyConfigured("You're using the staticfiles
|
|
|
|
# app without having set the required STATIC_URL setting.")
|
2013-06-01 19:24:46 +02:00
|
|
|
pass
|
2021-02-15 10:15:06 +01:00
|
|
|
else:
|
|
|
|
# super().setUpClass() cleans up after itself on a failure.
|
2017-01-21 14:13:44 +01:00
|
|
|
super().tearDownClass()
|
2021-02-15 10:15:06 +01:00
|
|
|
raise Exception('setUpClass() should have raised an exception.')
|
2013-06-01 19:24:46 +02:00
|
|
|
|
|
|
|
def test_test_test(self):
|
|
|
|
# Intentionally empty method so that the test is picked up by the
|
|
|
|
# test runner and the overridden setUpClass() method is executed.
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class StaticLiveServerView(LiveServerBase):
|
|
|
|
|
|
|
|
def urlopen(self, url):
|
|
|
|
return urlopen(self.live_server_url + url)
|
|
|
|
|
2014-06-21 18:27:46 +02:00
|
|
|
# The test is going to access a static file stored in this application.
|
|
|
|
@modify_settings(INSTALLED_APPS={'append': 'staticfiles_tests.apps.test'})
|
2013-06-01 19:24:46 +02:00
|
|
|
def test_collectstatic_emulation(self):
|
|
|
|
"""
|
2016-10-27 09:53:39 +02:00
|
|
|
StaticLiveServerTestCase use of staticfiles' serve() allows it
|
2014-08-28 08:44:26 +02:00
|
|
|
to discover app's static assets without having to collectstatic first.
|
2013-06-01 19:24:46 +02:00
|
|
|
"""
|
2017-01-19 14:50:28 +01:00
|
|
|
with self.urlopen('/static/test/file.txt') as f:
|
2015-07-01 23:37:10 +02:00
|
|
|
self.assertEqual(f.read().rstrip(b'\r\n'), b'In static directory.')
|