Fixed #30234 -- Disallowed non-upper settings in settings.configure().

This commit is contained in:
orlnub123 2019-03-03 03:23:18 +03:00 committed by Tim Graham
parent 9681e968eb
commit 163236ea0e
2 changed files with 15 additions and 2 deletions

View File

@ -104,6 +104,8 @@ class LazySettings(LazyObject):
raise RuntimeError('Settings already configured.') raise RuntimeError('Settings already configured.')
holder = UserSettingsHolder(default_settings) holder = UserSettingsHolder(default_settings)
for name, value in options.items(): for name, value in options.items():
if not name.isupper():
raise TypeError('Setting %r must be uppercase.' % name)
setattr(holder, name, value) setattr(holder, name, value)
self._wrapped = holder self._wrapped = holder
@ -198,7 +200,7 @@ class UserSettingsHolder:
self.default_settings = default_settings self.default_settings = default_settings
def __getattr__(self, name): def __getattr__(self, name):
if name in self._deleted: if not name.isupper() or name in self._deleted:
raise AttributeError raise AttributeError
return getattr(self.default_settings, name) return getattr(self.default_settings, name)

View File

@ -1,7 +1,7 @@
import os import os
import sys import sys
import unittest import unittest
from types import ModuleType from types import ModuleType, SimpleNamespace
from unittest import mock from unittest import mock
from django.conf import ENVIRONMENT_VARIABLE, LazySettings, Settings, settings from django.conf import ENVIRONMENT_VARIABLE, LazySettings, Settings, settings
@ -318,6 +318,17 @@ class SettingsTests(SimpleTestCase):
with self.assertRaisesMessage(RuntimeError, 'Settings already configured.'): with self.assertRaisesMessage(RuntimeError, 'Settings already configured.'):
settings.configure() settings.configure()
def test_nonupper_settings_prohibited_in_configure(self):
s = LazySettings()
with self.assertRaisesMessage(TypeError, "Setting 'foo' must be uppercase."):
s.configure(foo='bar')
def test_nonupper_settings_ignored_in_default_settings(self):
s = LazySettings()
s.configure(SimpleNamespace(foo='bar'))
with self.assertRaises(AttributeError):
getattr(s, 'foo')
@requires_tz_support @requires_tz_support
@mock.patch('django.conf.global_settings.TIME_ZONE', 'test') @mock.patch('django.conf.global_settings.TIME_ZONE', 'test')
def test_incorrect_timezone(self): def test_incorrect_timezone(self):