def test_add_default(self): """Testing SiteConfiguration.add_global_default""" SiteConfiguration.add_global_default('valid_key_1', 'global_value') try: self.assertEqual(self.siteconfig.get('valid_key_1'), 'global_value') finally: SiteConfiguration.remove_global_default('valid_key_1')
def test_get_with_missing_key_and_registered_global_default(self): """Testing SiteConfiguration.get and passing a missing key with a registered global default """ SiteConfiguration.add_global_default('valid_key_1', 'global_default_1') try: self.assertEqual(self.siteconfig.get('valid_key_1'), 'global_default_1') finally: SiteConfiguration.remove_global_default('valid_key_1')
class SiteConfigTestCase(TestCase): """Base class for SiteConfiguration-related unit tests.""" def setUp(self): super(SiteConfigTestCase, self).setUp() self.siteconfig = SiteConfiguration(site=Site.objects.get_current()) self.siteconfig.save() def tearDown(self): super(SiteConfigTestCase, self).tearDown() self.siteconfig.delete() SiteConfiguration.objects.clear_cache()
def test_get_defaults_excludes_global_defaults(self): """Testing SiteConfiguration.get_defaults excludes global defaults""" SiteConfiguration.add_global_default('test_global_key', 123) try: self.siteconfig.add_defaults({ 'valid_key_1': 'valid_parameter_1', }) self.siteconfig.add_default('valid_key_1', 'valid_new_parameter_1') siteconfig_defaults = self.siteconfig.get_defaults() self.assertEqual(siteconfig_defaults['valid_key_1'], 'valid_new_parameter_1') self.assertNotIn('test_global_key', siteconfig_defaults) finally: SiteConfiguration.remove_global_default('test_global_key')
def test_clear_defaults(self): """Testing SiteConfiguration.clear_defaults""" SiteConfiguration.add_global_default('valid_key_1', 'global_default_1') SiteConfiguration.add_global_default('valid_key_2', 'global_default_2') SiteConfiguration.clear_global_defaults() self.assertIsNone(self.siteconfig.get('valid_key_1')) self.assertIsNone(self.siteconfig.get('valid_key_2'))
def load_site_config(): """Sets up the SiteConfiguration, provides defaults and syncs settings.""" try: siteconfig = SiteConfiguration.objects.get_current() except SiteConfiguration.DoesNotExist: # Either warn or just create the thing. Depends on your app siteconfig = SiteConfiguration(site=Site.objects.get_current(), version="1.0") siteconfig.save() if not siteconfig.get_defaults(): siteconfig.add_defaults(defaults) apply_django_settings(siteconfig, settings_map) return siteconfig
def test_add_global_defaults(self): """Testing SiteConfiguration.add_global_defaults""" SiteConfiguration.add_global_defaults({ 'valid_key_1': 'global_value_1', 'valid_key_2': 'global_value_2', 'valid_key_3': 'global_value_3', }) try: self.assertEqual(self.siteconfig.get('valid_key_1'), 'global_value_1') self.assertEqual(self.siteconfig.get('valid_key_2'), 'global_value_2') self.assertEqual(self.siteconfig.get('valid_key_3'), 'global_value_3') finally: SiteConfiguration.remove_global_default('valid_key_1') SiteConfiguration.remove_global_default('valid_key_2') SiteConfiguration.remove_global_default('valid_key_3')
def load_site_config(): """Set up the SiteConfiguration, provide defaults and sync settings.""" try: siteconfig = SiteConfiguration.objects.get_current() except SiteConfiguration.DoesNotExist: # Either warn or just create the thing. Depends on your app. siteconfig = SiteConfiguration(site=Site.objects.get_current(), version="1.0") siteconfig.save() # If TITLE and DESCRIPTION are not on the database then pick the defaults # from the settings and save them in the database. if not siteconfig.get_defaults(): SETTINGS_MAP = { # siteconfig key settings.py key 'DESCRIPTION': 'DESCRIPTION', 'TITLE': 'TITLE', } siteconfig.add_defaults(generate_defaults(SETTINGS_MAP)) return siteconfig
def load_site_config(): """Set up the SiteConfiguration, provide defaults and sync settings.""" try: siteconfig = SiteConfiguration.objects.get_current() except SiteConfiguration.DoesNotExist: siteconfig = SiteConfiguration(site=Site.objects.get_current(), version="1.0") siteconfig.save() # If TITLE and DESCRIPTION are not on the database then pick the defaults # from the settings and save them in the database. if not siteconfig.get_defaults(): from django.conf import settings defaults = {} for setting_name in ('DESCRIPTION', 'TITLE'): if hasattr(settings, setting_name): defaults[setting_name] = getattr(settings, setting_name) siteconfig.add_defaults(defaults) return siteconfig
def load_site_config(): """Set up the SiteConfiguration, provide defaults and sync settings.""" try: siteconfig = SiteConfiguration.objects.get_current() except SiteConfiguration.DoesNotExist: siteconfig = SiteConfiguration(site=Site.objects.get_current(), version="1.0") siteconfig.save() # If TITLE and DESCRIPTION are not on the database then pick the defaults # from the settings and save them in the database. if not siteconfig.get_defaults(): from django.conf import settings defaults = {} for setting_name in ("DESCRIPTION", "TITLE"): if hasattr(settings, setting_name): defaults[setting_name] = getattr(settings, setting_name) siteconfig.add_defaults(defaults) return siteconfig
class SiteConfigTest(TestCase): def setUp(self): self.siteconfig = SiteConfiguration(site=Site.objects.get_current()) self.siteconfig.save() def tearDown(self): self.siteconfig.delete() SiteConfiguration.objects.clear_cache() def testMailAuthDeserialize(self): """Testing mail authentication settings deserialization""" # This is bug 1476. We deserialized the e-mail settings to Unicode # strings automatically, but this broke mail sending on some setups. # The HMAC library is incompatible with Unicode strings in more recent # Python 2.6 versions. Now we deserialize as a string. This test # ensures that these settings never break again. username = '******' password = '******' self.assertEqual(type(username), six.text_type) self.assertEqual(type(password), six.text_type) self.siteconfig.set('mail_host_user', username) self.siteconfig.set('mail_host_password', password) apply_django_settings(self.siteconfig, mail_settings_map) self.assertEqual(settings.EMAIL_HOST_USER, username) self.assertEqual(settings.EMAIL_HOST_PASSWORD, password) self.assertEqual(type(settings.EMAIL_HOST_USER), bytes) self.assertEqual(type(settings.EMAIL_HOST_PASSWORD), bytes) # Simulate the failure point in HMAC import hmac settings.EMAIL_HOST_USER.translate(hmac.trans_5C) settings.EMAIL_HOST_PASSWORD.translate(hmac.trans_5C) def testSynchronization(self): """Testing synchronizing SiteConfigurations through cache""" siteconfig1 = SiteConfiguration.objects.get_current() self.assertFalse(siteconfig1.is_expired()) siteconfig2 = SiteConfiguration.objects.get(site=self.siteconfig.site) siteconfig2.set('foobar', 123) # Save, and prevent clearing of caches to simulate still having the # stale cache around for another thread. siteconfig2.save(clear_caches=False) self.assertTrue(siteconfig1.is_expired()) SiteConfiguration.objects.check_expired() # See if we fetch the same one again siteconfig1 = SiteConfiguration.objects.get_current() self.assertEqual(siteconfig1.get('foobar'), 123) def testSynchronizationExpiredCache(self): """Testing synchronizing SiteConfigurations with an expired cache""" siteconfig1 = SiteConfiguration.objects.get_current() self.assertFalse(siteconfig1.is_expired()) siteconfig2 = SiteConfiguration.objects.get(site=self.siteconfig.site) siteconfig2.set('foobar', 123) # Save, and prevent clearing of caches to simulate still having the # stale cache around for another thread. siteconfig2.save(clear_caches=False) cache.delete('%s:siteconfig:%s:generation' % (siteconfig2.site.domain, siteconfig2.id)) self.assertTrue(siteconfig1.is_expired()) SiteConfiguration.objects.check_expired() # See if we fetch the same one again siteconfig1 = SiteConfiguration.objects.get_current() self.assertEqual(siteconfig1.get('foobar'), 123) def test_cache_backend(self): """Testing cache backend setting with CACHES['default']""" settings.CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'foo', }, 'staticfiles': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'staticfiles-cache', } } self.siteconfig.set('cache_backend', 'memcached://localhost:12345/') apply_django_settings(self.siteconfig, cache_settings_map) self.assertEqual(settings.CACHES['default']['BACKEND'], 'django.core.cache.backends.memcached.CacheClass') self.assertEqual(settings.CACHES['default']['LOCATION'], 'localhost:12345') self.assertTrue('staticfiles' in settings.CACHES) def test_cache_backend_with_caches(self): """Testing cache backend setting with siteconfig-stored CACHES""" settings.CACHES['staticfiles'] = { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'staticfiles-cache', } self.siteconfig.set('cache_backend', { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.CacheClass', 'LOCATION': 'localhost:12345', }, }) apply_django_settings(self.siteconfig, cache_settings_map) self.assertEqual(settings.CACHES['default']['BACKEND'], 'django.core.cache.backends.memcached.CacheClass') self.assertEqual(settings.CACHES['default']['LOCATION'], 'localhost:12345') self.assertTrue('staticfiles' in settings.CACHES)
def setUp(self): super(SiteConfigTestCase, self).setUp() self.siteconfig = SiteConfiguration(site=Site.objects.get_current()) self.siteconfig.save()
class SiteConfigTest(TestCase): def setUp(self): self.siteconfig = SiteConfiguration(site=Site.objects.get_current()) self.siteconfig.save() def tearDown(self): self.siteconfig.delete() SiteConfiguration.objects.clear_cache() def testMailAuthDeserialize(self): """Testing mail authentication settings deserialization""" # This is bug 1476. We deserialized the e-mail settings to Unicode # strings automatically, but this broke mail sending on some setups. # The HMAC library is incompatible with Unicode strings in more recent # Python 2.6 versions. Now we deserialize as a string. This test # ensures that these settings never break again. username = "******" password = "******" self.assertEqual(type(username), six.text_type) self.assertEqual(type(password), six.text_type) self.siteconfig.set("mail_host_user", username) self.siteconfig.set("mail_host_password", password) apply_django_settings(self.siteconfig, mail_settings_map) self.assertEqual(settings.EMAIL_HOST_USER, username) self.assertEqual(settings.EMAIL_HOST_PASSWORD, password) self.assertEqual(type(settings.EMAIL_HOST_USER), bytes) self.assertEqual(type(settings.EMAIL_HOST_PASSWORD), bytes) # Simulate the failure point in HMAC import hmac settings.EMAIL_HOST_USER.translate(hmac.trans_5C) settings.EMAIL_HOST_PASSWORD.translate(hmac.trans_5C) def testSynchronization(self): """Testing synchronizing SiteConfigurations through cache""" siteconfig1 = SiteConfiguration.objects.get_current() self.assertFalse(siteconfig1.is_expired()) siteconfig2 = SiteConfiguration.objects.get(site=self.siteconfig.site) siteconfig2.set("foobar", 123) # Save, and prevent clearing of caches to simulate still having the # stale cache around for another thread. siteconfig2.save(clear_caches=False) self.assertTrue(siteconfig1.is_expired()) SiteConfiguration.objects.check_expired() # See if we fetch the same one again siteconfig1 = SiteConfiguration.objects.get_current() self.assertEqual(siteconfig1.get("foobar"), 123) def testSynchronizationExpiredCache(self): """Testing synchronizing SiteConfigurations with an expired cache""" siteconfig1 = SiteConfiguration.objects.get_current() self.assertFalse(siteconfig1.is_expired()) siteconfig2 = SiteConfiguration.objects.get(site=self.siteconfig.site) siteconfig2.set("foobar", 123) # Save, and prevent clearing of caches to simulate still having the # stale cache around for another thread. siteconfig2.save(clear_caches=False) cache.delete("%s:siteconfig:%s:generation" % (siteconfig2.site.domain, siteconfig2.id)) self.assertTrue(siteconfig1.is_expired()) SiteConfiguration.objects.check_expired() # See if we fetch the same one again siteconfig1 = SiteConfiguration.objects.get_current() self.assertEqual(siteconfig1.get("foobar"), 123) def test_cache_backend(self): """Testing cache backend setting with CACHES['default']""" settings.CACHES = { "default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache", "LOCATION": "foo"}, "staticfiles": { "BACKEND": "django.core.cache.backends.locmem.LocMemCache", "LOCATION": "staticfiles-cache", }, } self.siteconfig.set("cache_backend", "memcached://localhost:12345/") apply_django_settings(self.siteconfig, cache_settings_map) self.assertTrue("staticfiles" in settings.CACHES) self.assertTrue("default" in settings.CACHES) self.assertTrue("forwarded_backend" in settings.CACHES) self.assertEqual( settings.CACHES["default"]["BACKEND"], "djblets.cache.forwarding_backend.ForwardingCacheBackend" ) self.assertEqual(settings.CACHES["default"]["LOCATION"], "forwarded_backend") self.assertEqual( settings.CACHES["forwarded_backend"]["BACKEND"], "django.core.cache.backends.memcached.MemcachedCache" ) self.assertEqual(settings.CACHES["forwarded_backend"]["LOCATION"], "localhost:12345") def test_cache_backend_with_caches(self): """Testing cache backend setting with siteconfig-stored CACHES""" settings.CACHES["staticfiles"] = { "BACKEND": "django.core.cache.backends.locmem.LocMemCache", "LOCATION": "staticfiles-cache", } self.siteconfig.set( "cache_backend", { "default": { "BACKEND": "django.core.cache.backends.memcached.MemcachedCache", "LOCATION": "localhost:12345", } }, ) apply_django_settings(self.siteconfig, cache_settings_map) self.assertTrue("staticfiles" in settings.CACHES) self.assertTrue("default" in settings.CACHES) self.assertTrue("forwarded_backend" in settings.CACHES) self.assertEqual( settings.CACHES["default"]["BACKEND"], "djblets.cache.forwarding_backend.ForwardingCacheBackend" ) self.assertEqual(settings.CACHES["default"]["LOCATION"], "forwarded_backend") self.assertEqual( settings.CACHES["forwarded_backend"]["BACKEND"], "django.core.cache.backends.memcached.MemcachedCache" ) self.assertEqual(settings.CACHES["forwarded_backend"]["LOCATION"], "localhost:12345") def test_cache_backend_with_caches_legacy_memcached(self): """Testing cache backend setting with siteconfig-stored CACHES and legacy memcached.CacheClass """ settings.CACHES["staticfiles"] = { "BACKEND": "django.core.cache.backends.locmem.LocMemCache", "LOCATION": "staticfiles-cache", } self.siteconfig.set( "cache_backend", {"default": {"BACKEND": "django.core.cache.backends.memcached.CacheClass", "LOCATION": "localhost:12345"}}, ) apply_django_settings(self.siteconfig, cache_settings_map) self.assertTrue("staticfiles" in settings.CACHES) self.assertTrue("default" in settings.CACHES) self.assertTrue("forwarded_backend" in settings.CACHES) self.assertEqual( settings.CACHES["default"]["BACKEND"], "djblets.cache.forwarding_backend.ForwardingCacheBackend" ) self.assertEqual(settings.CACHES["default"]["LOCATION"], "forwarded_backend") self.assertEqual( settings.CACHES["forwarded_backend"]["BACKEND"], "django.core.cache.backends.memcached.MemcachedCache" ) self.assertEqual(settings.CACHES["forwarded_backend"]["LOCATION"], "localhost:12345") def test_siteconfig_get_invalid_key_no_default(self): """Testing SiteConfiguration.get with passing an invalid key without a registered default """ self.assertEqual(self.siteconfig.get("invalid_key", default="default"), "default") def test_siteconfig_get_valid_key_default_not_stored(self): """Testing SiteConfiguration.get with passing a valid key stored in the SiteConfiguration instance but not a registered default """ self.siteconfig.add_default("valid_key_1", "valid_parameter_1") self.assertEqual(self.siteconfig.get("valid_key_1"), "valid_parameter_1") def test_siteconfig_get_valid_key_default_and_stored(self): """Testing SiteConfiguration.get with passing a valid key stored in the SiteConfiguration instance """ self.siteconfig.set("valid_key_2", "valid_parameter_2") self.assertEqual(self.siteconfig.get("valid_key_2"), "valid_parameter_2") def test_siteconfig_set(self): """Testing SiteConfiguration.set with stored in the SiteConfiguration.settings """ self.siteconfig.set("valid_key_2", "valid_parameter_2") self.siteconfig.set("valid_key_3", "valid_parameter_3") self.assertEqual( self.siteconfig.settings, {"valid_key_2": "valid_parameter_2", "valid_key_3": "valid_parameter_3"} ) def test_siteconfig_add_defaults(self): """Testing SiteConfiguration.add_defaults with registering defaults """ defaults = { "valid_key_1": "valid_parameter_1", "valid_key_2": "valid_parameter_2", "valid_key_3": "valid_parameter_3", } self.siteconfig.add_defaults(defaults) self.assertEqual(defaults, self.siteconfig.get_defaults()) self.assertEqual(self.siteconfig.get("valid_key_1"), "valid_parameter_1") def test_siteconfig_add_default(self): """Testing SiteConfiguration.add_default with registering default """ self.siteconfig.add_default("valid_key_1", "valid_new_parameter_2") self.assertEqual(self.siteconfig.get("valid_key_1"), "valid_new_parameter_2") def test_siteconfig_get_defaults(self): """Testing SiteConfiguration.get_defaults with compare registering value """ defaults = { "valid_key_1": "valid_parameter_1", "valid_key_2": "valid_parameter_2", "valid_key_3": "valid_parameter_3", } self.siteconfig.add_defaults(defaults) self.siteconfig.add_default("valid_key_1", "valid_new_parameter_1") self.assertEqual( self.siteconfig.get_defaults(), { "valid_key_1": "valid_new_parameter_1", "valid_key_2": "valid_parameter_2", "valid_key_3": "valid_parameter_3", }, )
class SiteConfigTest(TestCase): def setUp(self): self.siteconfig = SiteConfiguration(site=Site.objects.get_current()) self.siteconfig.save() def tearDown(self): self.siteconfig.delete() SiteConfiguration.objects.clear_cache() def testMailAuthDeserialize(self): """Testing mail authentication settings deserialization""" # This is bug 1476. We deserialized the e-mail settings to Unicode # strings automatically, but this broke mail sending on some setups. # The HMAC library is incompatible with Unicode strings in more recent # Python 2.6 versions. Now we deserialize as a string. This test # ensures that these settings never break again. username = u'myuser' password = u'mypass' self.siteconfig.set('mail_host_user', username) self.siteconfig.set('mail_host_password', password) apply_django_settings(self.siteconfig, mail_settings_map) self.assertEqual(settings.EMAIL_HOST_USER, username) self.assertEqual(settings.EMAIL_HOST_PASSWORD, password) self.assertEqual(type(settings.EMAIL_HOST_USER), str) self.assertEqual(type(settings.EMAIL_HOST_PASSWORD), str) # Simulate the failure point in HMAC trans_5C = "".join ([chr (x ^ 0x5C) for x in xrange(256)]) settings.EMAIL_HOST_USER.translate(trans_5C) settings.EMAIL_HOST_PASSWORD.translate(trans_5C) def testSynchronization(self): """Testing synchronizing SiteConfigurations through cache""" siteconfig1 = SiteConfiguration.objects.get_current() self.assertFalse(siteconfig1.is_expired()) siteconfig2 = SiteConfiguration.objects.get(site=self.siteconfig.site) siteconfig2.set('foobar', 123) # Save, and prevent clearing of caches to simulate still having the # stale cache around for another thread. siteconfig2.save(clear_caches=False) self.assertTrue(siteconfig1.is_expired()) SiteConfiguration.objects.check_expired() # See if we fetch the same one again siteconfig1 = SiteConfiguration.objects.get_current() self.assertEqual(siteconfig1.get('foobar'), 123) def testSynchronizationExpiredCache(self): """Testing synchronizing SiteConfigurations with an expired cache""" siteconfig1 = SiteConfiguration.objects.get_current() self.assertFalse(siteconfig1.is_expired()) siteconfig2 = SiteConfiguration.objects.get(site=self.siteconfig.site) siteconfig2.set('foobar', 123) # Save, and prevent clearing of caches to simulate still having the # stale cache around for another thread. siteconfig2.save(clear_caches=False) cache.delete('%s:siteconfig:%s:generation' % (siteconfig2.site.domain, siteconfig2.id)) self.assertTrue(siteconfig1.is_expired()) SiteConfiguration.objects.check_expired() # See if we fetch the same one again siteconfig1 = SiteConfiguration.objects.get_current() self.assertEqual(siteconfig1.get('foobar'), 123)
def test_remove_default(self): """Testing SiteConfiguration.remove_default""" SiteConfiguration.add_global_default('valid_key_1', 'global_value') SiteConfiguration.remove_global_default('valid_key_1') self.assertIsNone(self.siteconfig.get('valid_key_1'))
class SiteConfigTest(TestCase): def setUp(self): self.siteconfig = SiteConfiguration(site=Site.objects.get_current()) self.siteconfig.save() def tearDown(self): self.siteconfig.delete() SiteConfiguration.objects.clear_cache() def testMailAuthDeserialize(self): """Testing mail authentication settings deserialization""" # This is bug 1476. We deserialized the e-mail settings to Unicode # strings automatically, but this broke mail sending on some setups. # The HMAC library is incompatible with Unicode strings in more recent # Python 2.6 versions. Now we deserialize as a string. This test # ensures that these settings never break again. username = '******' password = '******' self.assertEqual(type(username), six.text_type) self.assertEqual(type(password), six.text_type) self.siteconfig.set('mail_host_user', username) self.siteconfig.set('mail_host_password', password) apply_django_settings(self.siteconfig, mail_settings_map) self.assertEqual(settings.EMAIL_HOST_USER, username) self.assertEqual(settings.EMAIL_HOST_PASSWORD, password) self.assertEqual(type(settings.EMAIL_HOST_USER), bytes) self.assertEqual(type(settings.EMAIL_HOST_PASSWORD), bytes) # Simulate the failure point in HMAC import hmac settings.EMAIL_HOST_USER.translate(hmac.trans_5C) settings.EMAIL_HOST_PASSWORD.translate(hmac.trans_5C) def testSynchronization(self): """Testing synchronizing SiteConfigurations through cache""" siteconfig1 = SiteConfiguration.objects.get_current() self.assertFalse(siteconfig1.is_expired()) siteconfig2 = SiteConfiguration.objects.get(site=self.siteconfig.site) siteconfig2.set('foobar', 123) # Save, and prevent clearing of caches to simulate still having the # stale cache around for another thread. siteconfig2.save(clear_caches=False) self.assertTrue(siteconfig1.is_expired()) SiteConfiguration.objects.check_expired() # See if we fetch the same one again siteconfig1 = SiteConfiguration.objects.get_current() self.assertEqual(siteconfig1.get('foobar'), 123) def testSynchronizationExpiredCache(self): """Testing synchronizing SiteConfigurations with an expired cache""" siteconfig1 = SiteConfiguration.objects.get_current() self.assertFalse(siteconfig1.is_expired()) siteconfig2 = SiteConfiguration.objects.get(site=self.siteconfig.site) siteconfig2.set('foobar', 123) # Save, and prevent clearing of caches to simulate still having the # stale cache around for another thread. siteconfig2.save(clear_caches=False) cache.delete('%s:siteconfig:%s:generation' % (siteconfig2.site.domain, siteconfig2.id)) self.assertTrue(siteconfig1.is_expired()) SiteConfiguration.objects.check_expired() # See if we fetch the same one again siteconfig1 = SiteConfiguration.objects.get_current() self.assertEqual(siteconfig1.get('foobar'), 123) def test_cache_backend(self): """Testing cache backend setting with CACHES['default']""" settings.CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'foo', }, 'staticfiles': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'staticfiles-cache', } } self.siteconfig.set('cache_backend', 'memcached://localhost:12345/') apply_django_settings(self.siteconfig, cache_settings_map) self.assertTrue('staticfiles' in settings.CACHES) self.assertTrue('default' in settings.CACHES) self.assertTrue('forwarded_backend' in settings.CACHES) self.assertEqual( settings.CACHES['default']['BACKEND'], 'djblets.cache.forwarding_backend.ForwardingCacheBackend') self.assertEqual(settings.CACHES['default']['LOCATION'], 'forwarded_backend') self.assertEqual(settings.CACHES['forwarded_backend']['BACKEND'], 'django.core.cache.backends.memcached.MemcachedCache') self.assertEqual(settings.CACHES['forwarded_backend']['LOCATION'], 'localhost:12345') def test_cache_backend_with_caches(self): """Testing cache backend setting with siteconfig-stored CACHES""" settings.CACHES['staticfiles'] = { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'staticfiles-cache', } self.siteconfig.set('cache_backend', { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': 'localhost:12345', }, }) apply_django_settings(self.siteconfig, cache_settings_map) self.assertTrue('staticfiles' in settings.CACHES) self.assertTrue('default' in settings.CACHES) self.assertTrue('forwarded_backend' in settings.CACHES) self.assertEqual( settings.CACHES['default']['BACKEND'], 'djblets.cache.forwarding_backend.ForwardingCacheBackend') self.assertEqual(settings.CACHES['default']['LOCATION'], 'forwarded_backend') self.assertEqual(settings.CACHES['forwarded_backend']['BACKEND'], 'django.core.cache.backends.memcached.MemcachedCache') self.assertEqual(settings.CACHES['forwarded_backend']['LOCATION'], 'localhost:12345') def test_cache_backend_with_caches_legacy_memcached(self): """Testing cache backend setting with siteconfig-stored CACHES and legacy memcached.CacheClass """ settings.CACHES['staticfiles'] = { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'staticfiles-cache', } self.siteconfig.set('cache_backend', { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.CacheClass', 'LOCATION': 'localhost:12345', }, }) apply_django_settings(self.siteconfig, cache_settings_map) self.assertTrue('staticfiles' in settings.CACHES) self.assertTrue('default' in settings.CACHES) self.assertTrue('forwarded_backend' in settings.CACHES) self.assertEqual( settings.CACHES['default']['BACKEND'], 'djblets.cache.forwarding_backend.ForwardingCacheBackend') self.assertEqual(settings.CACHES['default']['LOCATION'], 'forwarded_backend') self.assertEqual(settings.CACHES['forwarded_backend']['BACKEND'], 'django.core.cache.backends.memcached.MemcachedCache') self.assertEqual(settings.CACHES['forwarded_backend']['LOCATION'], 'localhost:12345')
def setUp(self): self.siteconfig = SiteConfiguration(site=Site.objects.get_current()) self.siteconfig.save()
def test_get_defaults(self): """Testing SiteConfiguration.get_defaults""" SiteConfiguration.add_global_defaults({ 'valid_key_1': 'global_value_1', 'valid_key_2': 'global_value_2', 'valid_key_3': 'global_value_3', }) SiteConfiguration.add_global_default('valid_key_1', 'new_global_value') try: siteconfig_defaults = SiteConfiguration.get_global_defaults() self.assertEqual(siteconfig_defaults['valid_key_1'], 'new_global_value') self.assertEqual(siteconfig_defaults['valid_key_2'], 'global_value_2') self.assertEqual(siteconfig_defaults['valid_key_3'], 'global_value_3') finally: SiteConfiguration.remove_global_default('valid_key_1') SiteConfiguration.remove_global_default('valid_key_2') SiteConfiguration.remove_global_default('valid_key_3')
class SiteConfigTest(TestCase): def setUp(self): self.siteconfig = SiteConfiguration(site=Site.objects.get_current()) self.siteconfig.save() def tearDown(self): self.siteconfig.delete() SiteConfiguration.objects.clear_cache() def testMailAuthDeserialize(self): """Testing mail authentication settings deserialization""" # This is bug 1476. We deserialized the e-mail settings to Unicode # strings automatically, but this broke mail sending on some setups. # The HMAC library is incompatible with Unicode strings in more recent # Python 2.6 versions. Now we deserialize as a string. This test # ensures that these settings never break again. username = u'myuser' password = u'mypass' self.siteconfig.set('mail_host_user', username) self.siteconfig.set('mail_host_password', password) apply_django_settings(self.siteconfig, mail_settings_map) self.assertEqual(settings.EMAIL_HOST_USER, username) self.assertEqual(settings.EMAIL_HOST_PASSWORD, password) self.assertEqual(type(settings.EMAIL_HOST_USER), str) self.assertEqual(type(settings.EMAIL_HOST_PASSWORD), str) # Simulate the failure point in HMAC trans_5C = "".join([chr(x ^ 0x5C) for x in xrange(256)]) settings.EMAIL_HOST_USER.translate(trans_5C) settings.EMAIL_HOST_PASSWORD.translate(trans_5C) def testSynchronization(self): """Testing synchronizing SiteConfigurations through cache""" siteconfig1 = SiteConfiguration.objects.get_current() self.assertFalse(siteconfig1.is_expired()) siteconfig2 = SiteConfiguration.objects.get(site=self.siteconfig.site) siteconfig2.set('foobar', 123) # Save, and prevent clearing of caches to simulate still having the # stale cache around for another thread. siteconfig2.save(clear_caches=False) self.assertTrue(siteconfig1.is_expired()) SiteConfiguration.objects.check_expired() # See if we fetch the same one again siteconfig1 = SiteConfiguration.objects.get_current() self.assertEqual(siteconfig1.get('foobar'), 123) def testSynchronizationExpiredCache(self): """Testing synchronizing SiteConfigurations with an expired cache""" siteconfig1 = SiteConfiguration.objects.get_current() self.assertFalse(siteconfig1.is_expired()) siteconfig2 = SiteConfiguration.objects.get(site=self.siteconfig.site) siteconfig2.set('foobar', 123) # Save, and prevent clearing of caches to simulate still having the # stale cache around for another thread. siteconfig2.save(clear_caches=False) cache.delete('%s:siteconfig:%s:generation' % (siteconfig2.site.domain, siteconfig2.id)) self.assertTrue(siteconfig1.is_expired()) SiteConfiguration.objects.check_expired() # See if we fetch the same one again siteconfig1 = SiteConfiguration.objects.get_current() self.assertEqual(siteconfig1.get('foobar'), 123)
user (django.contrib.auth.models.User): The user to retrieve the avatar service for. service_id (unicode, optional): The unique identifier of the service that is to be retrieved. If this is ``None``, the default service will be used. Returns: djblets.avatars.services.base.AvatarService: An avatar service, or ``None`` if one could not be found. """ settings_manager = self.settings_manager_class(user) user_service_id = settings_manager.avatar_service_id for sid in (service_id, user_service_id): if sid is None or not self.has_service(sid): continue service = self.get('avatar_service_id', sid) if self.is_enabled(service): return self.get_avatar_service(sid) return self.default_service SiteConfiguration.add_global_defaults({ AvatarServiceRegistry.ENABLED_SERVICES_KEY: [], AvatarServiceRegistry.DEFAULT_SERVICE_KEY: None, })