-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test case for default storage class
to confirm that settings have been applied correctly
- Loading branch information
Showing
1 changed file
with
28 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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() | ||
|
@@ -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( | ||
|
@@ -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 |