1 |
1 |
"""
|
2 |
|
This file demonstrates writing tests using the unittest module. These will pass
|
3 |
|
when you run "manage.py test".
|
4 |
|
|
5 |
|
Replace this with more appropriate tests for your application.
|
|
2 |
Test multitenant framework
|
6 |
3 |
"""
|
7 |
4 |
|
8 |
|
from django.test import TestCase
|
|
5 |
import tempfile
|
|
6 |
import shutil
|
|
7 |
import os
|
|
8 |
import json
|
|
9 |
|
|
10 |
from django.conf.urls import patterns
|
|
11 |
from django.test import TestCase, Client
|
|
12 |
from django.http import HttpResponse
|
|
13 |
from django.template.response import TemplateResponse
|
|
14 |
|
|
15 |
try:
|
|
16 |
from django.test import override_settings
|
|
17 |
except ImportError: # django < 1.7
|
|
18 |
from django.test.utils import override_settings
|
|
19 |
|
9 |
20 |
|
|
21 |
def json_key(request, *args, **kwargs):
|
|
22 |
from django.conf import settings
|
|
23 |
return HttpResponse(settings.JSON_KEY + ' json')
|
10 |
24 |
|
|
25 |
def python_key(request, *args, **kwargs):
|
|
26 |
from django.conf import settings
|
|
27 |
return HttpResponse(settings.PYTHON_KEY + ' python')
|
|
28 |
|
|
29 |
def template(request, *args, **kwargs):
|
|
30 |
return TemplateResponse(request, 'tenant.html')
|
|
31 |
|
|
32 |
urlpatterns = patterns('',
|
|
33 |
('^json_key/$', json_key),
|
|
34 |
('^python_key/$', python_key),
|
|
35 |
('^template/$', template),
|
|
36 |
)
|
|
37 |
|
|
38 |
@override_settings(
|
|
39 |
ROOT_URLCONF=__name__,
|
|
40 |
MIDDLEWARE_CLASSES=(
|
|
41 |
'entrouvert.djommon.multitenant.middleware.TenantMiddleware',
|
|
42 |
'entrouvert.djommon.multitenant.middleware.JSONSettingsMiddleware',
|
|
43 |
'entrouvert.djommon.multitenant.middleware.PythonSettingsMiddleware',
|
|
44 |
),
|
|
45 |
TEMPLATE_LOADERS = (
|
|
46 |
'entrouvert.djommon.multitenant.template_loader.FilesystemLoader',
|
|
47 |
)
|
|
48 |
)
|
11 |
49 |
class SimpleTest(TestCase):
|
12 |
|
def test_basic_addition(self):
|
13 |
|
"""
|
14 |
|
Tests that 1 + 1 always equals 2.
|
15 |
|
"""
|
16 |
|
self.assertEqual(1 + 1, 2)
|
|
50 |
TENANTS = ['tenant1', 'tenant2']
|
|
51 |
|
|
52 |
def setUp(self):
|
|
53 |
self.tenant_base = tempfile.mkdtemp()
|
|
54 |
for tenant in self.TENANTS:
|
|
55 |
tenant_dir = os.path.join(self.tenant_base, tenant)
|
|
56 |
os.mkdir(tenant_dir)
|
|
57 |
settings_py = os.path.join(tenant_dir, 'settings.json')
|
|
58 |
with file(settings_py, 'w') as f:
|
|
59 |
json.dump({'JSON_KEY': tenant}, f)
|
|
60 |
settings_json = os.path.join(tenant_dir, 'settings.py')
|
|
61 |
with file(settings_json, 'w') as f:
|
|
62 |
print >>f, 'PYTHON_KEY = %r' % tenant
|
|
63 |
templates_dir = os.path.join(tenant_dir, 'templates')
|
|
64 |
os.mkdir(templates_dir)
|
|
65 |
tenant_html = os.path.join(templates_dir, 'tenant.html')
|
|
66 |
with file(tenant_html, 'w') as f:
|
|
67 |
print >>f, tenant + ' template',
|
|
68 |
|
|
69 |
def tearDown(self):
|
|
70 |
shutil.rmtree(self.tenant_base, ignore_errors=True)
|
|
71 |
|
|
72 |
def tenant_settings(self):
|
|
73 |
return self.settings(
|
|
74 |
TENANT_BASE=self.tenant_base,
|
|
75 |
TENANT_TEMPLATE_DIRS=(self.tenant_base,)
|
|
76 |
)
|
|
77 |
|
|
78 |
def test_tenants(self):
|
|
79 |
with self.tenant_settings():
|
|
80 |
for tenant in self.TENANTS:
|
|
81 |
c = Client(HTTP_HOST=tenant)
|
|
82 |
response = c.get('/json_key/')
|
|
83 |
self.assertEqual(response.content, tenant + ' json')
|
|
84 |
response = c.get('/python_key/')
|
|
85 |
self.assertEqual(response.content, tenant + ' python')
|
|
86 |
response = c.get('/template/')
|
|
87 |
self.assertEqual(response.content, tenant + ' template')
|
|
88 |
|
|
89 |
def test_list_tenants(self):
|
|
90 |
from entrouvert.djommon.multitenant.middleware import TenantMiddleware
|
|
91 |
from tenant_schemas.utils import get_tenant_model
|
|
92 |
|
|
93 |
with self.tenant_settings():
|
|
94 |
l1 = set(map(str, TenantMiddleware.get_tenants()))
|
|
95 |
l2 = set(str(get_tenant_model()(schema_name=tenant,
|
|
96 |
domain_url=tenant)) for tenant in self.TENANTS)
|
|
97 |
self.assertEquals(l1, l2)
|
|
98 |
|
|
99 |
|
|
100 |
|
17 |
|
-
|