Projet

Général

Profil

0001-tests_multitenant-restore-default-settings-after-mod.patch

Benjamin Dauvergne, 12 novembre 2015 13:12

Télécharger (9,44 ko)

Voir les différences:

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
tests_multitenant/test_settings.py
11 11

  
12 12
from tenant_schemas.utils import tenant_context
13 13

  
14
import utilities
15

  
14 16
def test_tenant_middleware(tenants, client):
15 17
    res = client.get('/', SERVER_NAME='invalid.example.net')
16 18
    assert res.status_code == 404
......
22 24
def test_tenant_json_settings(tenants, settings):
23 25
    settings.clear_tenants_settings()
24 26

  
25
    settings.default_settings.TENANT_SETTINGS_LOADERS = ('hobo.multitenant.settings_loaders.SettingsJSON', )
27
    with utilities.patch_default_settings(settings,
28
                                          TENANT_SETTINGS_LOADERS=('hobo.multitenant.settings_loaders.SettingsJSON',)):
26 29

  
27
    # check the setting is not defined
28
    with pytest.raises(AttributeError):
29
        settings.HOBO_TEST_VARIABLE
30
        # check the setting is not defined
31
        with pytest.raises(AttributeError):
32
            settings.HOBO_TEST_VARIABLE
30 33

  
31
    # check that for each tenant it contains the tenant domain
32
    # it's set by the tenants fixture in conftest.py
33
    for tenant in tenants:
34
        with tenant_context(tenant):
35
            assert django.conf.settings.HOBO_TEST_VARIABLE == tenant.domain_url
34
        # check that for each tenant it contains the tenant domain
35
        # it's set by the tenants fixture in conftest.py
36
        for tenant in tenants:
37
            with tenant_context(tenant):
38
                assert django.conf.settings.HOBO_TEST_VARIABLE == tenant.domain_url
36 39

  
37
    # check it's no longer defined after going back to the public schema
38
    with pytest.raises(AttributeError):
39
        settings.HOBO_TEST_VARIABLE
40
        # check it's no longer defined after going back to the public schema
41
        with pytest.raises(AttributeError):
42
            settings.HOBO_TEST_VARIABLE
40 43

  
41 44
def test_tenant_template_vars(tenants, settings, client):
42 45
    from hobo.multitenant.models import Tenant
43 46

  
44 47
    django.conf.settings.clear_tenants_settings()
45 48

  
46
    settings.default_settings.TENANT_SETTINGS_LOADERS = ('hobo.multitenant.settings_loaders.TemplateVars', )
47

  
48
    # check the setting is not defined
49
    with pytest.raises(AttributeError):
50
        django.conf.settings.TEMPLATE_VARS
51

  
52
    for tenant in tenants:
53
        with tenant_context(tenant):
54
            # check it's defined when moving into the schema
55
            assert django.conf.settings.TEMPLATE_VARS
56
            assert django.conf.settings.TEMPLATE_VARS['hobo_test_variable'] is True
57
            assert django.conf.settings.TEMPLATE_VARS['test_url'] == tenant.get_base_url()
58
            assert django.conf.settings.TEMPLATE_VARS['other_url'] == 'http://other.example.net'
59
            assert django.conf.settings.TEMPLATE_VARS['slug_with_hyphen_url'] == 'http://slug-with-hyphen.example.net'
60
            assert django.conf.settings.TEMPLATE_VARS['site_title'] == 'Test'
61
            assert django.conf.settings.TEMPLATE_VARS['other_variable'] == 'bar'
62

  
63
    # check it's no longer defined after going back to the public schema
64
    with pytest.raises(AttributeError):
65
        django.conf.settings.TEMPLATE_VARS
49
    with utilities.patch_default_settings(settings,
50
                                          TENANT_SETTINGS_LOADERS=('hobo.multitenant.settings_loaders.TemplateVars',)):
51
        # check the setting is not defined
52
        with pytest.raises(AttributeError):
53
            django.conf.settings.TEMPLATE_VARS
54

  
55
        for tenant in tenants:
56
            with tenant_context(tenant):
57
                # check it's defined when moving into the schema
58
                assert django.conf.settings.TEMPLATE_VARS
59
                assert django.conf.settings.TEMPLATE_VARS['hobo_test_variable'] is True
60
                assert django.conf.settings.TEMPLATE_VARS['test_url'] == tenant.get_base_url()
61
                assert django.conf.settings.TEMPLATE_VARS['other_url'] == 'http://other.example.net'
62
                assert django.conf.settings.TEMPLATE_VARS['site_title'] == 'Test'
63
                assert django.conf.settings.TEMPLATE_VARS['other_variable'] == 'bar'
64

  
65
        # check it's no longer defined after going back to the public schema
66
        with pytest.raises(AttributeError):
67
            django.conf.settings.TEMPLATE_VARS
66 68

  
67 69
def test_tenant_cors_settings(tenants, settings, client):
68 70
    settings.clear_tenants_settings()
69 71

  
70
    settings.default_settings.TENANT_SETTINGS_LOADERS = ('hobo.multitenant.settings_loaders.CORSSettings', )
72
    with utilities.patch_default_settings(settings,
73
                                          TENANT_SETTINGS_LOADERS=('hobo.multitenant.settings_loaders.CORSSettings',)):
74
        # check the setting is not defined
75
        with pytest.raises(AttributeError):
76
            settings.CORS_ORIGIN_WHITELIST
71 77

  
72
    # check the setting is not defined
73
    with pytest.raises(AttributeError):
74
        settings.CORS_ORIGIN_WHITELIST
78
        for tenant in tenants:
79
            with tenant_context(tenant):
80
                # check it's defined when moving into the schema
81
                assert django.conf.settings.CORS_ORIGIN_WHITELIST
82
                assert tenant.get_base_url() in django.conf.settings.CORS_ORIGIN_WHITELIST
83
                assert 'http://other.example.net' in django.conf.settings.CORS_ORIGIN_WHITELIST
75 84

  
76
    for tenant in tenants:
77
        with tenant_context(tenant):
78
            # check it's defined when moving into the schema
79
            assert django.conf.settings.CORS_ORIGIN_WHITELIST
80
            assert tenant.get_base_url() in django.conf.settings.CORS_ORIGIN_WHITELIST
81
            assert 'http://other.example.net' in django.conf.settings.CORS_ORIGIN_WHITELIST
82

  
83
    with pytest.raises(AttributeError):
84
        django.conf.settings.CORS_ORIGIN_WHITELIST
85
        with pytest.raises(AttributeError):
86
            django.conf.settings.CORS_ORIGIN_WHITELIST
85 87

  
86 88
def test_multithreading(tenants, settings, client):
87 89

  
88 90
    settings.clear_tenants_settings()
89
    settings.default_settings.TENANT_SETTINGS_LOADERS = ('hobo.multitenant.settings_loaders.TemplateVars', )
90

  
91
    def f(tenant=None):
92
        if not tenant is None:
93
            assert hasattr(settings, 'TEMPLATE_VARS')
94
            assert settings.TEMPLATE_VARS['test_url'] == tenant.get_base_url()
95
        else:
96
            assert not hasattr(django.conf.settings, 'TEMPLATE_VARS')
97

  
98
    assert not hasattr(django.conf.settings, 'TEMPLATE_VARS')
99
    t1 = threading.Thread(target=f)
100
    t1.start()
101
    t1.join()
102

  
103
    for tenant in tenants:
104
        with tenant_context(tenant):
105
            assert hasattr(settings, 'TEMPLATE_VARS')
106
            t2 = threading.Thread(target=f, args=(tenant,))
107
            t2.start()
108
            t2.join()
109

  
110
    assert not hasattr(django.conf.settings, 'TEMPLATE_VARS')
111
    t3 = threading.Thread(target=f)
112
    t3.start()
113
    t3.join()
91
    with utilities.patch_default_settings(settings,
92
                                          TENANT_SETTINGS_LOADERS=('hobo.multitenant.settings_loaders.TemplateVars',)):
93

  
94
        def f(tenant=None):
95
            if not tenant is None:
96
                assert hasattr(settings, 'TEMPLATE_VARS')
97
                assert settings.TEMPLATE_VARS['test_url'] == tenant.get_base_url()
98
            else:
99
                assert not hasattr(django.conf.settings, 'TEMPLATE_VARS')
100

  
101
        assert not hasattr(django.conf.settings, 'TEMPLATE_VARS')
102
        t1 = threading.Thread(target=f)
103
        t1.start()
104
        t1.join()
105

  
106
        for tenant in tenants:
107
            with tenant_context(tenant):
108
                assert hasattr(settings, 'TEMPLATE_VARS')
109
                t2 = threading.Thread(target=f, args=(tenant,))
110
                t2.start()
111
                t2.join()
112

  
113
        assert not hasattr(django.conf.settings, 'TEMPLATE_VARS')
114
        t3 = threading.Thread(target=f)
115
        t3.start()
116
        t3.join()
114 117

  
115 118
def test_cache(tenants, client):
116 119
    # Clear caches
tests_multitenant/utilities.py
1

  
2
class PatchDefaultSettings(object):
3
    empty = object()
4

  
5
    def __init__(self, settings, **kwargs):
6
        self.settings = settings
7
        self.patch = kwargs
8
        self.old = {}
9

  
10
    def __enter__(self):
11
        for key, value in self.patch.iteritems():
12
            self.old[key] = getattr(self.settings.default_settings, key, self.empty)
13
            setattr(self.settings.default_settings, key, value)
14

  
15
    def __exit__(self, *args, **kwargs):
16
        for key, value in self.old.iteritems():
17
            if value is self.empty:
18
                delattr(self.settings.default_settings, key)
19
            else:
20
                setattr(self.settings.default_settings, key, value)
21

  
22

  
23
def patch_default_settings(settings, **kwargs):
24
    return PatchDefaultSettings(settings, **kwargs)
0
-