From 729c3524643ee5b75c4836b71fdb716c2ec3b2ec Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 12 Nov 2015 12:59:00 +0100 Subject: [PATCH 1/3] tests_multitenant: restore default settings after modifying them (#8580) --- tests_multitenant/test_settings.py | 143 +++++++++++++++++++------------------ tests_multitenant/utilities.py | 24 +++++++ 2 files changed, 97 insertions(+), 70 deletions(-) create mode 100644 tests_multitenant/utilities.py diff --git a/tests_multitenant/test_settings.py b/tests_multitenant/test_settings.py index a59e5b8..cb62e90 100644 --- a/tests_multitenant/test_settings.py +++ b/tests_multitenant/test_settings.py @@ -11,6 +11,8 @@ from django.core.cache import cache, caches from tenant_schemas.utils import tenant_context +import utilities + def test_tenant_middleware(tenants, client): res = client.get('/', SERVER_NAME='invalid.example.net') assert res.status_code == 404 @@ -22,95 +24,96 @@ def test_tenant_middleware(tenants, client): def test_tenant_json_settings(tenants, settings): settings.clear_tenants_settings() - settings.default_settings.TENANT_SETTINGS_LOADERS = ('hobo.multitenant.settings_loaders.SettingsJSON', ) + with utilities.patch_default_settings(settings, + TENANT_SETTINGS_LOADERS=('hobo.multitenant.settings_loaders.SettingsJSON',)): - # check the setting is not defined - with pytest.raises(AttributeError): - settings.HOBO_TEST_VARIABLE + # check the setting is not defined + with pytest.raises(AttributeError): + settings.HOBO_TEST_VARIABLE - # check that for each tenant it contains the tenant domain - # it's set by the tenants fixture in conftest.py - for tenant in tenants: - with tenant_context(tenant): - assert django.conf.settings.HOBO_TEST_VARIABLE == tenant.domain_url + # check that for each tenant it contains the tenant domain + # it's set by the tenants fixture in conftest.py + for tenant in tenants: + with tenant_context(tenant): + assert django.conf.settings.HOBO_TEST_VARIABLE == tenant.domain_url - # check it's no longer defined after going back to the public schema - with pytest.raises(AttributeError): - settings.HOBO_TEST_VARIABLE + # check it's no longer defined after going back to the public schema + with pytest.raises(AttributeError): + settings.HOBO_TEST_VARIABLE def test_tenant_template_vars(tenants, settings, client): from hobo.multitenant.models import Tenant django.conf.settings.clear_tenants_settings() - settings.default_settings.TENANT_SETTINGS_LOADERS = ('hobo.multitenant.settings_loaders.TemplateVars', ) - - # check the setting is not defined - with pytest.raises(AttributeError): - django.conf.settings.TEMPLATE_VARS - - for tenant in tenants: - with tenant_context(tenant): - # check it's defined when moving into the schema - assert django.conf.settings.TEMPLATE_VARS - assert django.conf.settings.TEMPLATE_VARS['hobo_test_variable'] is True - assert django.conf.settings.TEMPLATE_VARS['test_url'] == tenant.get_base_url() - assert django.conf.settings.TEMPLATE_VARS['other_url'] == 'http://other.example.net' - assert django.conf.settings.TEMPLATE_VARS['slug_with_hyphen_url'] == 'http://slug-with-hyphen.example.net' - assert django.conf.settings.TEMPLATE_VARS['site_title'] == 'Test' - assert django.conf.settings.TEMPLATE_VARS['other_variable'] == 'bar' - - # check it's no longer defined after going back to the public schema - with pytest.raises(AttributeError): - django.conf.settings.TEMPLATE_VARS + with utilities.patch_default_settings(settings, + TENANT_SETTINGS_LOADERS=('hobo.multitenant.settings_loaders.TemplateVars',)): + # check the setting is not defined + with pytest.raises(AttributeError): + django.conf.settings.TEMPLATE_VARS + + for tenant in tenants: + with tenant_context(tenant): + # check it's defined when moving into the schema + assert django.conf.settings.TEMPLATE_VARS + assert django.conf.settings.TEMPLATE_VARS['hobo_test_variable'] is True + assert django.conf.settings.TEMPLATE_VARS['test_url'] == tenant.get_base_url() + assert django.conf.settings.TEMPLATE_VARS['other_url'] == 'http://other.example.net' + assert django.conf.settings.TEMPLATE_VARS['site_title'] == 'Test' + assert django.conf.settings.TEMPLATE_VARS['other_variable'] == 'bar' + + # check it's no longer defined after going back to the public schema + with pytest.raises(AttributeError): + django.conf.settings.TEMPLATE_VARS def test_tenant_cors_settings(tenants, settings, client): settings.clear_tenants_settings() - settings.default_settings.TENANT_SETTINGS_LOADERS = ('hobo.multitenant.settings_loaders.CORSSettings', ) + with utilities.patch_default_settings(settings, + TENANT_SETTINGS_LOADERS=('hobo.multitenant.settings_loaders.CORSSettings',)): + # check the setting is not defined + with pytest.raises(AttributeError): + settings.CORS_ORIGIN_WHITELIST - # check the setting is not defined - with pytest.raises(AttributeError): - settings.CORS_ORIGIN_WHITELIST + for tenant in tenants: + with tenant_context(tenant): + # check it's defined when moving into the schema + assert django.conf.settings.CORS_ORIGIN_WHITELIST + assert tenant.get_base_url() in django.conf.settings.CORS_ORIGIN_WHITELIST + assert 'http://other.example.net' in django.conf.settings.CORS_ORIGIN_WHITELIST - for tenant in tenants: - with tenant_context(tenant): - # check it's defined when moving into the schema - assert django.conf.settings.CORS_ORIGIN_WHITELIST - assert tenant.get_base_url() in django.conf.settings.CORS_ORIGIN_WHITELIST - assert 'http://other.example.net' in django.conf.settings.CORS_ORIGIN_WHITELIST - - with pytest.raises(AttributeError): - django.conf.settings.CORS_ORIGIN_WHITELIST + with pytest.raises(AttributeError): + django.conf.settings.CORS_ORIGIN_WHITELIST def test_multithreading(tenants, settings, client): settings.clear_tenants_settings() - settings.default_settings.TENANT_SETTINGS_LOADERS = ('hobo.multitenant.settings_loaders.TemplateVars', ) - - def f(tenant=None): - if not tenant is None: - assert hasattr(settings, 'TEMPLATE_VARS') - assert settings.TEMPLATE_VARS['test_url'] == tenant.get_base_url() - else: - assert not hasattr(django.conf.settings, 'TEMPLATE_VARS') - - assert not hasattr(django.conf.settings, 'TEMPLATE_VARS') - t1 = threading.Thread(target=f) - t1.start() - t1.join() - - for tenant in tenants: - with tenant_context(tenant): - assert hasattr(settings, 'TEMPLATE_VARS') - t2 = threading.Thread(target=f, args=(tenant,)) - t2.start() - t2.join() - - assert not hasattr(django.conf.settings, 'TEMPLATE_VARS') - t3 = threading.Thread(target=f) - t3.start() - t3.join() + with utilities.patch_default_settings(settings, + TENANT_SETTINGS_LOADERS=('hobo.multitenant.settings_loaders.TemplateVars',)): + + def f(tenant=None): + if not tenant is None: + assert hasattr(settings, 'TEMPLATE_VARS') + assert settings.TEMPLATE_VARS['test_url'] == tenant.get_base_url() + else: + assert not hasattr(django.conf.settings, 'TEMPLATE_VARS') + + assert not hasattr(django.conf.settings, 'TEMPLATE_VARS') + t1 = threading.Thread(target=f) + t1.start() + t1.join() + + for tenant in tenants: + with tenant_context(tenant): + assert hasattr(settings, 'TEMPLATE_VARS') + t2 = threading.Thread(target=f, args=(tenant,)) + t2.start() + t2.join() + + assert not hasattr(django.conf.settings, 'TEMPLATE_VARS') + t3 = threading.Thread(target=f) + t3.start() + t3.join() def test_cache(tenants, client): # Clear caches diff --git a/tests_multitenant/utilities.py b/tests_multitenant/utilities.py new file mode 100644 index 0000000..98d9086 --- /dev/null +++ b/tests_multitenant/utilities.py @@ -0,0 +1,24 @@ + +class PatchDefaultSettings(object): + empty = object() + + def __init__(self, settings, **kwargs): + self.settings = settings + self.patch = kwargs + self.old = {} + + def __enter__(self): + for key, value in self.patch.iteritems(): + self.old[key] = getattr(self.settings.default_settings, key, self.empty) + setattr(self.settings.default_settings, key, value) + + def __exit__(self, *args, **kwargs): + for key, value in self.old.iteritems(): + if value is self.empty: + delattr(self.settings.default_settings, key) + else: + setattr(self.settings.default_settings, key, value) + + +def patch_default_settings(settings, **kwargs): + return PatchDefaultSettings(settings, **kwargs) -- 2.1.4