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 = {
|
2021-02-06 01:41:49 +08:00
|
|
|
"MEDIA_URL": "media/",
|
|
|
|
"STATIC_URL": "static/",
|
2013-06-02 01:24:46 +08: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 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()
|
2021-10-17 05:15:50 +08:00
|
|
|
cls.addClassCleanup(cls.settings_override.disable)
|
|
|
|
super().setUpClass()
|
2021-02-15 17:15:06 +08:00
|
|
|
|
2013-06-02 01:24:46 +08: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 17:15:06 +08:00
|
|
|
try:
|
|
|
|
cls.raises_exception()
|
|
|
|
finally:
|
|
|
|
TEST_SETTINGS["STATIC_URL"] = old_STATIC_URL
|
2013-06-02 01:24:46 +08:00
|
|
|
|
|
|
|
@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()
|
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
|
2021-02-15 17:15:06 +08:00
|
|
|
else:
|
|
|
|
raise Exception("setUpClass() should have raised an exception.")
|
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.")
|