Project

General

Profile

Download (4.84 KB) Statistics
| Branch: | Tag: | Revision:

root / entrouvert / djommon / multitenant / tests.py @ 1db4b242

1
"""
2
Test multitenant framework
3
"""
4

    
5
import tempfile
6
import shutil
7
import os
8
import json
9
import StringIO
10

    
11
from django.conf.urls import patterns
12
from django.test import TestCase, Client
13
from django.http import HttpResponse
14
from django.template.response import TemplateResponse
15

    
16
try:
17
    from django.test import override_settings
18
except ImportError: # django < 1.7
19
    from django.test.utils import override_settings
20

    
21

    
22
def json_key(request, *args, **kwargs):
23
    from django.conf import settings
24
    return HttpResponse(settings.JSON_KEY + ' json')
25

    
26
def python_key(request, *args, **kwargs):
27
    from django.conf import settings
28
    return HttpResponse(settings.PYTHON_KEY + ' python')
29

    
30
def template(request, *args, **kwargs):
31
    return TemplateResponse(request, 'tenant.html')
32

    
33
def upload(request):
34
    from django.core.files.storage import default_storage
35
    default_storage.save('upload', request.FILES['upload'])
36
    return HttpResponse('')
37

    
38
def download(request):
39
    from django.core.files.storage import default_storage
40
    return HttpResponse(default_storage.open('upload').read())
41

    
42
urlpatterns = patterns('',
43
        ('^json_key/$', json_key),
44
        ('^python_key/$', python_key),
45
        ('^template/$', template),
46
        ('^upload/$', upload),
47
        ('^download/$', download),
48
)
49

    
50
@override_settings(
51
        ROOT_URLCONF=__name__,
52
        MIDDLEWARE_CLASSES=(
53
            'entrouvert.djommon.multitenant.middleware.TenantMiddleware',
54
            'entrouvert.djommon.multitenant.middleware.JSONSettingsMiddleware',
55
            'entrouvert.djommon.multitenant.middleware.PythonSettingsMiddleware',
56
        ),
57
        TEMPLATE_LOADERS = (
58
           'entrouvert.djommon.multitenant.template_loader.FilesystemLoader',
59
        ),
60
        DEFAULT_FILE_STORAGE = 'entrouvert.djommon.multitenant.storage.TenantFileSystemStorage',
61
)
62
class SimpleTest(TestCase):
63
    TENANTS = ['tenant1', 'tenant2']
64

    
65
    def setUp(self):
66
        self.tenant_base = tempfile.mkdtemp()
67
        for tenant in self.TENANTS:
68
            tenant_dir = os.path.join(self.tenant_base, tenant)
69
            os.mkdir(tenant_dir)
70
            settings_py = os.path.join(tenant_dir, 'settings.json')
71
            with file(settings_py, 'w') as f:
72
                json.dump({'JSON_KEY': tenant}, f)
73
            settings_json = os.path.join(tenant_dir, 'settings.py')
74
            with file(settings_json, 'w') as f:
75
                print >>f, 'PYTHON_KEY = %r' % tenant
76
            templates_dir = os.path.join(tenant_dir, 'templates')
77
            os.mkdir(templates_dir)
78
            tenant_html = os.path.join(templates_dir, 'tenant.html')
79
            with file(tenant_html, 'w') as f:
80
                print >>f, tenant + ' template',
81
            media_dir = os.path.join(tenant_dir, 'media')
82
            os.mkdir(media_dir)
83

    
84
    def tearDown(self):
85
        shutil.rmtree(self.tenant_base, ignore_errors=True) 
86

    
87
    def tenant_settings(self):
88
        return self.settings(
89
                TENANT_BASE=self.tenant_base,
90
                TENANT_TEMPLATE_DIRS=(self.tenant_base,)
91
        )
92

    
93
    def test_tenants(self):
94
        with self.tenant_settings():
95
            for tenant in self.TENANTS:
96
                c = Client(HTTP_HOST=tenant)
97
                response = c.get('/json_key/')
98
                self.assertEqual(response.content, tenant + ' json')
99
                response = c.get('/python_key/')
100
                self.assertEqual(response.content, tenant + ' python')
101
                response = c.get('/template/')
102
                self.assertEqual(response.content, tenant + ' template')
103

    
104
    def test_list_tenants(self):
105
        from entrouvert.djommon.multitenant.middleware import TenantMiddleware
106
        from tenant_schemas.utils import get_tenant_model
107

    
108
        with self.tenant_settings():
109
            l1 = set(map(str, TenantMiddleware.get_tenants()))
110
            l2 = set(str(get_tenant_model()(schema_name=tenant,
111
                        domain_url=tenant)) for tenant in self.TENANTS)
112
            self.assertEquals(l1, l2)
113

    
114
    def test_storage(self):
115
        from django.core.files.base import ContentFile
116
        with self.tenant_settings():
117
            for tenant in self.TENANTS:
118
                c = Client(HTTP_HOST=tenant)
119
                uploaded_file_path = os.path.join(self.tenant_base, tenant, 'media', 'upload')
120
                self.assertFalse(os.path.exists(uploaded_file_path), uploaded_file_path)
121
                response = c.post('/upload/', {'upload': ContentFile(tenant + ' upload', name='upload.txt')})
122
                self.assertEqual(response.status_code, 200)
123
                self.assertEqual(response.content, '')
124
                self.assertTrue(os.path.exists(uploaded_file_path))
125
                self.assertEqual(file(uploaded_file_path).read(), tenant + ' upload')
126
                response = c.get('/download/')
127
                self.assertEqual(response.status_code, 200)
128
                self.assertEqual(response.content, tenant + ' upload')
(7-7/8)