|
1 |
# hobo - portal to configure and deploy applications
|
|
2 |
# Copyright (C) 2015-2016 Entr'ouvert
|
|
3 |
#
|
|
4 |
# This program is free software: you can redistribute it and/or modify it
|
|
5 |
# under the terms of the GNU Affero General Public License as published
|
|
6 |
# by the Free Software Foundation, either version 3 of the License, or
|
|
7 |
# (at your option) any later version.
|
|
8 |
#
|
|
9 |
# This program is distributed in the hope that it will be useful,
|
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
# GNU Affero General Public License for more details.
|
|
13 |
#
|
|
14 |
# You should have received a copy of the GNU Affero General Public License
|
|
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
16 |
|
1 |
17 |
import os
|
2 |
18 |
import threading
|
3 |
19 |
import random
|
4 |
20 |
import pytest
|
5 |
21 |
|
6 |
|
from django.apps import apps
|
7 |
22 |
import django.conf
|
8 |
|
from django.core.handlers.base import BaseHandler
|
9 |
|
from django.core.wsgi import get_wsgi_application
|
10 |
23 |
import django.db
|
11 |
24 |
from django.core.cache import cache, caches
|
12 |
|
from django.test import override_settings
|
13 |
25 |
|
14 |
26 |
from tenant_schemas.utils import tenant_context
|
15 |
27 |
|
16 |
28 |
import utilities
|
17 |
|
from hobo.theme.utils import set_theme, get_themes
|
|
29 |
|
18 |
30 |
|
19 |
31 |
def test_tenant_middleware(tenants, client, settings):
|
20 |
32 |
settings.ALLOWED_HOSTS.append('invalid.example.net')
|
... | ... | |
27 |
39 |
assert res.status_code != 404
|
28 |
40 |
assert res.wsgi_request.tenant.schema_name == tenant.schema_name
|
29 |
41 |
|
|
42 |
|
30 |
43 |
def test_tenant_json_settings(tenants, settings):
|
31 |
44 |
settings.clear_tenants_settings()
|
32 |
45 |
|
... | ... | |
39 |
52 |
assert django.conf.settings.UPDATE_ME == {'x': 1}
|
40 |
53 |
assert django.conf.settings.EXTEND_ME == [1]
|
41 |
54 |
|
42 |
|
|
43 |
55 |
# check that for each tenant it contains the tenant domain
|
44 |
56 |
# it's set by the tenants fixture in conftest.py
|
45 |
57 |
for tenant in tenants:
|
... | ... | |
56 |
68 |
|
57 |
69 |
|
58 |
70 |
def test_tenant_template_vars(tenants, settings, client):
|
59 |
|
from hobo.multitenant.models import Tenant
|
60 |
|
|
61 |
71 |
django.conf.settings.clear_tenants_settings()
|
62 |
72 |
|
63 |
73 |
with utilities.patch_default_settings(settings,
|
... | ... | |
80 |
90 |
with pytest.raises(AttributeError):
|
81 |
91 |
django.conf.settings.TEMPLATE_VARS
|
82 |
92 |
|
83 |
|
def test_tenant_settings_vars(tenants, settings, client):
|
84 |
|
from hobo.multitenant.models import Tenant
|
85 |
93 |
|
|
94 |
def test_tenant_settings_vars(tenants, settings, client):
|
86 |
95 |
django.conf.settings.clear_tenants_settings()
|
87 |
96 |
|
88 |
97 |
with utilities.patch_default_settings(settings,
|
... | ... | |
108 |
117 |
assert django.conf.settings.LOCAL2 == [tenant.domain_url, 7, 8]
|
109 |
118 |
assert django.conf.settings.LOCAL3 == {'a': tenant.domain_url, 'b': 2}
|
110 |
119 |
|
|
120 |
|
111 |
121 |
def test_tenant_cors_settings(tenants, settings, client):
|
112 |
122 |
settings.clear_tenants_settings()
|
113 |
123 |
|
... | ... | |
127 |
137 |
with pytest.raises(AttributeError):
|
128 |
138 |
django.conf.settings.CORS_ORIGIN_WHITELIST
|
129 |
139 |
|
130 |
|
def test_tenant_theme_settings(tenants, settings, client):
|
131 |
|
from hobo.multitenant.models import Tenant
|
132 |
140 |
|
|
141 |
def test_tenant_theme_settings(tenants, settings, client):
|
133 |
142 |
django.conf.settings.clear_tenants_settings()
|
134 |
143 |
|
135 |
|
with utilities.patch_default_settings(settings,
|
136 |
|
TENANT_SETTINGS_LOADERS=('hobo.multitenant.settings_loaders.TemplateVars',
|
137 |
|
'hobo.multitenant.settings_loaders.ThemeSettings')):
|
|
144 |
with utilities.patch_default_settings(
|
|
145 |
settings, TENANT_SETTINGS_LOADERS=(
|
|
146 |
'hobo.multitenant.settings_loaders.TemplateVars',
|
|
147 |
'hobo.multitenant.settings_loaders.ThemeSettings')):
|
138 |
148 |
old_value = os.environ['DJANGO_SETTINGS_MODULE']
|
139 |
149 |
os.environ['DJANGO_SETTINGS_MODULE'] = 'fake.settings'
|
140 |
150 |
with tenant_context(tenants[0]):
|
... | ... | |
144 |
154 |
assert django.conf.settings.HELLO == 'world'
|
145 |
155 |
os.environ['DJANGO_SETTINGS_MODULE'] = old_value
|
146 |
156 |
|
|
157 |
|
147 |
158 |
def test_multithreading(tenants, settings, client):
|
148 |
159 |
|
149 |
160 |
settings.clear_tenants_settings()
|
... | ... | |
151 |
162 |
TENANT_SETTINGS_LOADERS=('hobo.multitenant.settings_loaders.TemplateVars',)):
|
152 |
163 |
|
153 |
164 |
def f(tenant=None):
|
154 |
|
if not tenant is None:
|
|
165 |
if tenant is not None:
|
155 |
166 |
assert hasattr(settings, 'TEMPLATE_VARS')
|
156 |
167 |
assert settings.TEMPLATE_VARS['test_url'] == tenant.get_base_url()
|
157 |
168 |
else:
|
... | ... | |
174 |
185 |
t3.start()
|
175 |
186 |
t3.join()
|
176 |
187 |
|
|
188 |
|
177 |
189 |
def test_cache(tenants, client):
|
178 |
190 |
# Clear caches
|
179 |
191 |
caches._caches.caches = {}
|
... | ... | |
227 |
239 |
assert hasattr(settings, 'KNOWN_SERVICES')
|
228 |
240 |
assert 'authentic' in settings.KNOWN_SERVICES
|
229 |
241 |
assert 'other' in settings.KNOWN_SERVICES['authentic']
|
230 |
|
assert (set(['url', 'backoffice-menu-url', 'title', 'orig', 'verif_orig', 'secret', 'template_name', 'variables', 'secondary'])
|
|
242 |
assert (set(['url', 'backoffice-menu-url', 'title', 'orig',
|
|
243 |
'verif_orig', 'secret', 'template_name', 'variables',
|
|
244 |
'secondary'])
|
231 |
245 |
== set(settings.KNOWN_SERVICES['authentic']['other'].keys()))
|
232 |
246 |
assert (settings.KNOWN_SERVICES['authentic']['other']['url']
|
233 |
247 |
== hobo_json['services'][2]['base_url'])
|
... | ... | |
236 |
250 |
assert (settings.KNOWN_SERVICES['authentic']['other']['title']
|
237 |
251 |
== hobo_json['services'][2]['title'])
|
238 |
252 |
assert settings.KNOWN_SERVICES['authentic']['other']['orig'] == tenant.domain_url
|
239 |
|
assert (settings.KNOWN_SERVICES['authentic']['other']['verif_orig'] ==
|
240 |
|
'other.example.net')
|
|
253 |
assert (settings.KNOWN_SERVICES['authentic']['other']['verif_orig']
|
|
254 |
== 'other.example.net')
|
241 |
255 |
key1 = hobo_json['services'][0]['secret_key']
|
242 |
256 |
key2 = hobo_json['services'][2]['secret_key']
|
243 |
|
assert (settings.KNOWN_SERVICES['authentic']['other']['secret'] ==
|
244 |
|
KnownServices.shared_secret(key1, key2))
|
|
257 |
assert (settings.KNOWN_SERVICES['authentic']['other']['secret']
|
|
258 |
== KnownServices.shared_secret(key1, key2))
|
245 |
259 |
|
246 |
260 |
|
247 |
261 |
def test_unique_cookies(tenants, settings):
|
248 |
|
from hobo.multitenant.settings_loaders import KnownServices
|
249 |
|
|
250 |
262 |
settings.clear_tenants_settings()
|
251 |
263 |
|
252 |
264 |
cookie_names = set()
|
253 |
|
-
|