tests_multitenant: restore default settings after modifying them (#8580)
This commit is contained in:
parent
7939ca70a1
commit
4359db0c35
|
@ -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', )
|
||||
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
|
||||
|
||||
# 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'
|
||||
|
||||
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
|
||||
# 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', )
|
||||
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')
|
||||
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()
|
||||
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()
|
||||
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()
|
||||
assert not hasattr(django.conf.settings, 'TEMPLATE_VARS')
|
||||
t3 = threading.Thread(target=f)
|
||||
t3.start()
|
||||
t3.join()
|
||||
|
||||
def test_cache(tenants, client):
|
||||
# Clear caches
|
||||
|
|
|
@ -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)
|
Loading…
Reference in New Issue