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