| 1 |
import posixpath |
| 2 |
from urllib.parse import quote |
| 3 |
|
| 4 |
from django.conf import settings |
| 5 |
from django.test import override_settings |
| 6 |
|
| 7 |
from .cases import StaticFilesTestCase, TestDefaults |
| 8 |
|
| 9 |
|
| 10 |
@override_settings(ROOT_URLCONF="staticfiles_tests.urls.default") |
| 11 |
class TestServeStatic(StaticFilesTestCase): |
| 12 |
""" |
| 13 |
Test static asset serving view. |
| 14 |
""" |
| 15 |
|
| 16 |
def _response(self, filepath): |
| 17 |
return self.client.get(quote(posixpath.join(settings.STATIC_URL, filepath))) |
| 18 |
|
| 19 |
def assertFileContains(self, filepath, text): |
| 20 |
self.assertContains(self._response(filepath), text) |
| 21 |
|
| 22 |
def assertFileNotFound(self, filepath): |
| 23 |
self.assertEqual(self._response(filepath).status_code, 404) |
| 24 |
|
| 25 |
|
| 26 |
@override_settings(DEBUG=False) |
| 27 |
class TestServeDisabled(TestServeStatic): |
| 28 |
""" |
| 29 |
Test serving static files disabled when DEBUG is False. |
| 30 |
""" |
| 31 |
|
| 32 |
def test_disabled_serving(self): |
| 33 |
self.assertFileNotFound("test.txt") |
| 34 |
|
| 35 |
|
| 36 |
@override_settings(DEBUG=True) |
| 37 |
class TestServeStaticWithDefaultURL(TestDefaults, TestServeStatic): |
| 38 |
""" |
| 39 |
Test static asset serving view with manually configured URLconf. |
| 40 |
""" |
| 41 |
|
| 42 |
|
| 43 |
@override_settings(DEBUG=True, ROOT_URLCONF="staticfiles_tests.urls.helper") |
| 44 |
class TestServeStaticWithURLHelper(TestDefaults, TestServeStatic): |
| 45 |
""" |
| 46 |
Test static asset serving view with staticfiles_urlpatterns helper. |
| 47 |
""" |