Skip to content

Commit

Permalink
Add test case for default storage class
Browse files Browse the repository at this point in the history
to confirm that settings have been applied correctly
  • Loading branch information
atodorov committed Jan 3, 2025
1 parent 2253922 commit a66a7cc
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions tcms_tenants/tests/test_storage.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Copyright (c) 2019-2021 Alexander Todorov <[email protected]>
# Copyright (c) 2019-2025 Alexander Todorov <[email protected]>
#
# Licensed under GNU Affero General Public License v3 or later (AGPLv3+)
# https://www.gnu.org/licenses/agpl-3.0.html

# pylint: disable=too-many-ancestors
from django.db import connection
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from django.test import override_settings

from django_tenants import utils
Expand All @@ -15,14 +16,14 @@


class TenantFileSystemStorageTestCase(LoggedInTestCase):
storage = TenantFileSystemStorage()

@override_settings(
MEDIA_ROOT="apps_dir/media",
MEDIA_URL="/media/",
MULTITENANT_RELATIVE_MEDIA_ROOT="%s",
)
def test_files_are_saved_under_subdirectories_per_tenant(self):
storage = TenantFileSystemStorage()

connection.set_schema_to_public()
tenant2 = utils.get_tenant_model()(schema_name="tenant2", owner=UserFactory())
tenant2.save()
Expand All @@ -31,21 +32,27 @@ def test_files_are_saved_under_subdirectories_per_tenant(self):
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)
public_url = storage.url(public_file_name)
public_file_name = self.storage.save(
"hello_world.txt", ContentFile("Hello World")
)
public_os_path = self.storage.path(public_file_name)
public_url = self.storage.url(public_file_name)

# switch to tenant1
with utils.tenant_context(self.tenant):
t1_file_name = storage.save("hello_from_1.txt", ContentFile("Hello T1"))
t1_os_path = storage.path(t1_file_name)
t1_url = storage.url(t1_file_name)
t1_file_name = self.storage.save(
"hello_from_1.txt", ContentFile("Hello T1")
)
t1_os_path = self.storage.path(t1_file_name)
t1_url = self.storage.url(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)
t2_url = storage.url(t2_file_name)
t2_file_name = self.storage.save(
"hello_from_2.txt", ContentFile("Hello T2")
)
t2_os_path = self.storage.path(t2_file_name)
t2_url = self.storage.url(t2_file_name)

# assert the paths are correct
self.assertTrue(
Expand All @@ -68,3 +75,12 @@ def test_files_are_saved_under_subdirectories_per_tenant(self):

with open(t2_os_path, "r", encoding="utf-8") as fobj:
self.assertEqual(fobj.read(), "Hello T2")


class DefaultStorageTestCase(TenantFileSystemStorageTestCase):
"""
Should result in the same behavior confirming that default
settings have been applied correctly!
"""

storage = default_storage

0 comments on commit a66a7cc

Please sign in to comment.