summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorOleg Kainov <kainov.oleg@gmail.com>2018-12-05 16:15:33 -0800
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-09-25 19:47:03 +0200
commitc574bec0929cd2527268c96a492d25223a9fd576 (patch)
tree80c642eca0ca64d63d1596bc237465685c92ce5c /tests
parent580e644f24f1c5ae5b94784fb73a9953a178fd26 (diff)
Fixed #25598 -- Added SCRIPT_NAME prefix to STATIC_URL and MEDIA_URL set to relative paths.
Thanks Florian Apolloner for reviews. Co-authored-by: Joel Dunham <Joel.Dunham@technicalsafetybc.ca>
Diffstat (limited to 'tests')
-rw-r--r--tests/file_storage/tests.py2
-rw-r--r--tests/settings_tests/tests.py49
2 files changed, 50 insertions, 1 deletions
diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py
index 1c4176014c..1f7fd94b5a 100644
--- a/tests/file_storage/tests.py
+++ b/tests/file_storage/tests.py
@@ -521,7 +521,7 @@ class FileStorageTests(SimpleTestCase):
defaults_storage = self.storage_class()
settings = {
'MEDIA_ROOT': 'overridden_media_root',
- 'MEDIA_URL': 'overridden_media_url/',
+ 'MEDIA_URL': '/overridden_media_url/',
'FILE_UPLOAD_PERMISSIONS': 0o333,
'FILE_UPLOAD_DIRECTORY_PERMISSIONS': 0o333,
}
diff --git a/tests/settings_tests/tests.py b/tests/settings_tests/tests.py
index d663f6e819..55ca0de524 100644
--- a/tests/settings_tests/tests.py
+++ b/tests/settings_tests/tests.py
@@ -12,6 +12,7 @@ from django.test import (
override_settings, signals,
)
from django.test.utils import requires_tz_support
+from django.urls import clear_script_prefix, set_script_prefix
@modify_settings(ITEMS={
@@ -567,3 +568,51 @@ class OverrideSettingsIsolationOnExceptionTests(SimpleTestCase):
signals.setting_changed.disconnect(self.receiver)
# This call shouldn't raise any errors.
decorated_function()
+
+
+class MediaURLStaticURLPrefixTest(SimpleTestCase):
+ def set_script_name(self, val):
+ clear_script_prefix()
+ if val is not None:
+ set_script_prefix(val)
+
+ def test_not_prefixed(self):
+ # Don't add SCRIPT_NAME prefix to valid URLs, absolute paths or None.
+ tests = (
+ '/path/',
+ 'http://myhost.com/path/',
+ None,
+ )
+ for setting in ('MEDIA_URL', 'STATIC_URL'):
+ for path in tests:
+ new_settings = {setting: path}
+ with self.settings(**new_settings):
+ for script_name in ['/somesubpath', '/somesubpath/', '/', '', None]:
+ with self.subTest(script_name=script_name, **new_settings):
+ try:
+ self.set_script_name(script_name)
+ self.assertEqual(getattr(settings, setting), path)
+ finally:
+ clear_script_prefix()
+
+ def test_add_script_name_prefix(self):
+ tests = (
+ # Relative paths.
+ ('/somesubpath', 'path/', '/somesubpath/path/'),
+ ('/somesubpath/', 'path/', '/somesubpath/path/'),
+ ('/', 'path/', '/path/'),
+ # Invalid URLs.
+ ('/somesubpath/', 'htp://myhost.com/path/', '/somesubpath/htp://myhost.com/path/'),
+ # Blank settings.
+ ('/somesubpath/', '', '/somesubpath/'),
+ )
+ for setting in ('MEDIA_URL', 'STATIC_URL'):
+ for script_name, path, expected_path in tests:
+ new_settings = {setting: path}
+ with self.settings(**new_settings):
+ with self.subTest(script_name=script_name, **new_settings):
+ try:
+ self.set_script_name(script_name)
+ self.assertEqual(getattr(settings, setting), expected_path)
+ finally:
+ clear_script_prefix()