2013-06-02 01:24:46 +08:00
|
|
|
"""
|
2015-12-03 07:55:50 +08:00
|
|
|
A subset of the tests in tests/servers/tests exercising
|
2014-08-28 14:44:26 +08:00
|
|
|
django.contrib.staticfiles.testing.StaticLiveServerTestCase instead of
|
2013-06-02 01:24:46 +08:00
|
|
|
django.test.LiveServerTestCase.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
2017-01-07 19:11:46 +08:00
|
|
|
from urllib.request import urlopen
|
2013-06-02 01:24:46 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
|
2013-06-02 01:24:46 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2014-06-22 00:27:46 +08:00
|
|
|
from django.test import modify_settings, override_settings
|
2013-06-02 01:24:46 +08:00
|
|
|
|
2017-01-20 21:01:02 +08:00
|
|
|
TEST_ROOT = os.path.dirname(__file__)
|
2013-06-02 01:24:46 +08:00
|
|
|
TEST_SETTINGS = {
|
|
|
|
'MEDIA_URL': '/media/',
|
|
|
|
'STATIC_URL': '/static/',
|
|
|
|
'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 14:44:26 +08:00
|
|
|
class LiveServerBase(StaticLiveServerTestCase):
|
2013-06-02 01:24:46 +08:00
|
|
|
|
|
|
|
available_apps = []
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
# Override settings
|
|
|
|
cls.settings_override = override_settings(**TEST_SETTINGS)
|
|
|
|
cls.settings_override.enable()
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUpClass()
|
2013-06-02 01:24:46 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().tearDownClass()
|
2013-06-02 01:24:46 +08:00
|
|
|
# Restore original settings
|
|
|
|
cls.settings_override.disable()
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2016-06-24 00:04:05 +08:00
|
|
|
cls.raises_exception()
|
2013-06-02 01:24:46 +08:00
|
|
|
TEST_SETTINGS['STATIC_URL'] = old_STATIC_URL
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
# skip it, as setUpClass doesn't call its parent either
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
2016-06-24 00:04:05 +08:00
|
|
|
def raises_exception(cls):
|
2013-06-02 01:24:46 +08:00
|
|
|
try:
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUpClass()
|
2013-06-02 01:24:46 +08:00
|
|
|
raise Exception("The line above should have raised an exception")
|
2016-06-24 00:04:05 +08:00
|
|
|
except ImproperlyConfigured:
|
|
|
|
# This raises ImproperlyConfigured("You're using the staticfiles
|
|
|
|
# app without having set the required STATIC_URL setting.")
|
2013-06-02 01:24:46 +08:00
|
|
|
pass
|
|
|
|
finally:
|
2019-02-14 23:04:55 +08:00
|
|
|
# Use del to avoid decrementing the database thread sharing count a
|
|
|
|
# second time.
|
|
|
|
del cls.server_thread
|
2017-01-21 21:13:44 +08:00
|
|
|
super().tearDownClass()
|
2013-06-02 01:24:46 +08: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-22 00:27:46 +08: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-02 01:24:46 +08:00
|
|
|
def test_collectstatic_emulation(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
StaticLiveServerTestCase use of staticfiles' serve() allows it
|
2014-08-28 14:44:26 +08:00
|
|
|
to discover app's static assets without having to collectstatic first.
|
2013-06-02 01:24:46 +08:00
|
|
|
"""
|
2017-01-19 21:50:28 +08:00
|
|
|
with self.urlopen('/static/test/file.txt') as f:
|
2015-07-02 05:37:10 +08:00
|
|
|
self.assertEqual(f.read().rstrip(b'\r\n'), b'In static directory.')
|