6 |
6 |
import shutil
|
7 |
7 |
import os
|
8 |
8 |
import json
|
|
9 |
import StringIO
|
9 |
10 |
|
10 |
11 |
from django.conf.urls import patterns
|
11 |
12 |
from django.test import TestCase, Client
|
... | ... | |
29 |
30 |
def template(request, *args, **kwargs):
|
30 |
31 |
return TemplateResponse(request, 'tenant.html')
|
31 |
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 |
|
32 |
42 |
urlpatterns = patterns('',
|
33 |
43 |
('^json_key/$', json_key),
|
34 |
44 |
('^python_key/$', python_key),
|
35 |
45 |
('^template/$', template),
|
|
46 |
('^upload/$', upload),
|
|
47 |
('^download/$', download),
|
36 |
48 |
)
|
37 |
49 |
|
38 |
50 |
@override_settings(
|
... | ... | |
44 |
56 |
),
|
45 |
57 |
TEMPLATE_LOADERS = (
|
46 |
58 |
'entrouvert.djommon.multitenant.template_loader.FilesystemLoader',
|
47 |
|
)
|
|
59 |
),
|
|
60 |
DEFAULT_FILE_STORAGE = 'entrouvert.djommon.multitenant.storage.TenantFileSystemStorage',
|
48 |
61 |
)
|
49 |
62 |
class SimpleTest(TestCase):
|
50 |
63 |
TENANTS = ['tenant1', 'tenant2']
|
... | ... | |
65 |
78 |
tenant_html = os.path.join(templates_dir, 'tenant.html')
|
66 |
79 |
with file(tenant_html, 'w') as f:
|
67 |
80 |
print >>f, tenant + ' template',
|
|
81 |
media_dir = os.path.join(tenant_dir, 'media')
|
|
82 |
os.mkdir(media_dir)
|
68 |
83 |
|
69 |
84 |
def tearDown(self):
|
70 |
85 |
shutil.rmtree(self.tenant_base, ignore_errors=True)
|
... | ... | |
96 |
111 |
domain_url=tenant)) for tenant in self.TENANTS)
|
97 |
112 |
self.assertEquals(l1, l2)
|
98 |
113 |
|
99 |
|
|
100 |
|
|
|
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')
|
Add tenant based storage handler (fixes #5501)
To use it, add this to your settings.py: