2015-07-01 19:43:25 +08:00
|
|
|
import posixpath
|
2017-03-17 19:55:00 +08:00
|
|
|
from urllib.parse import quote
|
2015-07-01 19:43:25 +08:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.test import override_settings
|
|
|
|
|
|
|
|
from .cases import StaticFilesTestCase, TestDefaults
|
|
|
|
|
|
|
|
|
|
|
|
@override_settings(ROOT_URLCONF="staticfiles_tests.urls.default")
|
|
|
|
class TestServeStatic(StaticFilesTestCase):
|
|
|
|
"""
|
|
|
|
Test static asset serving view.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-07-01 19:43:25 +08:00
|
|
|
def _response(self, filepath):
|
2017-03-17 19:55:00 +08:00
|
|
|
return self.client.get(quote(posixpath.join(settings.STATIC_URL, filepath)))
|
2015-07-01 19:43:25 +08:00
|
|
|
|
|
|
|
def assertFileContains(self, filepath, text):
|
|
|
|
self.assertContains(self._response(filepath), text)
|
|
|
|
|
|
|
|
def assertFileNotFound(self, filepath):
|
|
|
|
self.assertEqual(self._response(filepath).status_code, 404)
|
|
|
|
|
|
|
|
|
|
|
|
@override_settings(DEBUG=False)
|
|
|
|
class TestServeDisabled(TestServeStatic):
|
|
|
|
"""
|
|
|
|
Test serving static files disabled when DEBUG is False.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-07-01 19:43:25 +08:00
|
|
|
def test_disabled_serving(self):
|
|
|
|
self.assertFileNotFound("test.txt")
|
|
|
|
|
|
|
|
|
2016-03-26 23:17:06 +08:00
|
|
|
@override_settings(DEBUG=True)
|
|
|
|
class TestServeStaticWithDefaultURL(TestDefaults, TestServeStatic):
|
2015-07-01 19:43:25 +08:00
|
|
|
"""
|
|
|
|
Test static asset serving view with manually configured URLconf.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2016-03-26 23:17:06 +08:00
|
|
|
@override_settings(DEBUG=True, ROOT_URLCONF="staticfiles_tests.urls.helper")
|
|
|
|
class TestServeStaticWithURLHelper(TestDefaults, TestServeStatic):
|
2015-07-01 19:43:25 +08:00
|
|
|
"""
|
|
|
|
Test static asset serving view with staticfiles_urlpatterns helper.
|
|
|
|
"""
|