Skip to content

Commit

Permalink
Add test for TenantFileSystemStorage(), Refs django-tenants#249
Browse files Browse the repository at this point in the history
according to documentation TenantFileSystemStorage() will create
a separate directory for each schema_name under MEDIA_ROOT.

This is not currently the case! My speculation is that this is
broken because the subdir prefix is configured only once in the
constructor of TenantFileSystemStorage() and then cached. Thus we
end-up using the schema_name of the first tenant which happens to
save files.

The fact that TenantFileSystemStorageTestCase.test_format_string
broke without changing anything else supports the above speculation.
The reason is that the new
test_files_are_saved_under_subdirectories_per_tenant() test method
is executed *before* test_format_string()!
  • Loading branch information
atodorov committed Mar 29, 2019
1 parent faddf9d commit c14a480
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions django_tenants/tests/test_filesystem_storage.py
@@ -1,7 +1,10 @@
import warnings

from django.conf import settings
from django.db import connection
from django.core.files.base import ContentFile

from django_tenants import utils
from django_tenants.files.storage import TenantFileSystemStorage
from django_tenants.files.storages import TenantFileSystemStorage as OldTenantFileSystemStorage
from django_tenants.test.cases import TenantTestCase
Expand Down Expand Up @@ -68,3 +71,49 @@ def test_deprecated_module_raises_warning(self):

OldTenantFileSystemStorage()
self.assertTrue(any(deprecation_warning in str(w.message) for w in warns))

def test_files_are_saved_under_subdirectories_per_tenant(self):
storage = TenantFileSystemStorage()

connection.set_schema_to_public()
tenant1 = utils.get_tenant_model()(schema_name='tenant1')
tenant1.save()

domain1 = utils.get_tenant_domain_model()(tenant=tenant1, domain='something.test.com')
domain1.save()

connection.set_schema_to_public()
tenant2 = utils.get_tenant_model()(schema_name='tenant2')
tenant2.save()

domain2 = utils.get_tenant_domain_model()(tenant=tenant2, domain='example.com')
domain2.save()

# this file should be saved on the public schema
public_file_name = storage.save('hello_world.txt', ContentFile('Hello World'))
public_os_path = storage.path(public_file_name)

# switch to tenant1
with utils.tenant_context(tenant1):
t1_file_name = storage.save('hello_from_1.txt', ContentFile('Hello T1'))
t1_os_path = storage.path(t1_file_name)

# switch to tenant2
with utils.tenant_context(tenant2):
t2_file_name = storage.save('hello_from_2.txt', ContentFile('Hello T2'))
t2_os_path = storage.path(t2_file_name)

# assert the paths are correct
self.assertTrue(public_os_path.endswith('%s/public/%s' % (settings.MEDIA_ROOT, public_file_name)))
self.assertTrue(t1_os_path.endswith('%s/tenant1/%s' % (settings.MEDIA_ROOT, t1_file_name)))
self.assertTrue(t2_os_path.endswith('%s/tenant2/%s' % (settings.MEDIA_ROOT, t2_file_name)))

# assert contents are correct
with open(public_os_path, 'r') as f:
self.assertEqual(f.read(), 'Hello World')

with open(t1_os_path, 'r') as f:
self.assertEqual(f.read(), 'Hello T1')

with open(t2_os_path, 'r') as f:
self.assertEqual(f.read(), 'Hello T2')

0 comments on commit c14a480

Please sign in to comment.