|
| 1 | +""" |
| 2 | +Unit tests for storage utilities in edx_django_utils. |
| 3 | +""" |
| 4 | + |
| 5 | +import ddt |
| 6 | +from django.conf import settings |
| 7 | +from django.core.files.storage import default_storage |
| 8 | +from django.test import TestCase |
| 9 | +from django.test.utils import override_settings |
| 10 | + |
| 11 | +from edx_django_utils.storage.utils import get_named_storage |
| 12 | + |
| 13 | + |
| 14 | +@ddt.ddt |
| 15 | +class TestGetNamedStorage(TestCase): |
| 16 | + """ |
| 17 | + Tests for the get_named_storage utility. |
| 18 | + """ |
| 19 | + |
| 20 | + @ddt.data( |
| 21 | + ( |
| 22 | + 'avatars', |
| 23 | + 'AVATAR_BACKEND', |
| 24 | + ( |
| 25 | + {'avatars': { |
| 26 | + 'BACKEND': 'django.core.files.storage.FileSystemStorage', |
| 27 | + 'OPTIONS': {'location': '/tmp/avatars'} |
| 28 | + }}, |
| 29 | + None # No legacy config |
| 30 | + ), |
| 31 | + '/tmp/avatars' |
| 32 | + ), |
| 33 | + ( |
| 34 | + 'avatars', |
| 35 | + 'AVATAR_BACKEND', |
| 36 | + ( |
| 37 | + {}, # Empty STORAGES dict |
| 38 | + { |
| 39 | + 'class': 'django.core.files.storage.FileSystemStorage', |
| 40 | + 'options': {'location': '/tmp/legacy_avatars'} |
| 41 | + } |
| 42 | + ), |
| 43 | + '/tmp/legacy_avatars' |
| 44 | + ), |
| 45 | + ( |
| 46 | + 'certificates', |
| 47 | + 'CERTIFICATE_BACKEND', |
| 48 | + ( |
| 49 | + {'certificates': { |
| 50 | + 'BACKEND': 'django.core.files.storage.FileSystemStorage', |
| 51 | + 'OPTIONS': {'location': '/tmp/certificates'} |
| 52 | + }}, |
| 53 | + None |
| 54 | + ), |
| 55 | + '/tmp/certificates' |
| 56 | + ), |
| 57 | + ( |
| 58 | + 'certificates', |
| 59 | + 'CERTIFICATE_BACKEND', |
| 60 | + ( |
| 61 | + {}, # Empty STORAGES dict |
| 62 | + { |
| 63 | + 'class': 'django.core.files.storage.FileSystemStorage', |
| 64 | + 'options': {'location': '/tmp/legacy_certificates'} |
| 65 | + } |
| 66 | + ), |
| 67 | + '/tmp/legacy_certificates' |
| 68 | + ), |
| 69 | + ) |
| 70 | + @ddt.unpack |
| 71 | + def test_get_named_storage(self, storage_name, legacy_setting_name, config, expected_location): |
| 72 | + """ |
| 73 | + Test get_named_storage with both STORAGES dict and legacy config. |
| 74 | + """ |
| 75 | + storages_config, legacy_config = config |
| 76 | + |
| 77 | + with override_settings(STORAGES=storages_config or {}): |
| 78 | + if legacy_config: |
| 79 | + setattr(settings, legacy_setting_name, legacy_config) |
| 80 | + elif hasattr(settings, legacy_setting_name): |
| 81 | + delattr(settings, legacy_setting_name) |
| 82 | + |
| 83 | + storage = get_named_storage(storage_name, legacy_setting_name=legacy_setting_name) |
| 84 | + self.assertEqual(storage.location, expected_location) |
| 85 | + |
| 86 | + def test_fallback_to_default_storage(self): |
| 87 | + """ |
| 88 | + Test fallback to default_storage when neither STORAGES dict nor legacy config is defined. |
| 89 | + """ |
| 90 | + with override_settings(STORAGES={ |
| 91 | + 'default': { |
| 92 | + 'BACKEND': 'django.core.files.storage.FileSystemStorage', |
| 93 | + 'OPTIONS': {'location': '/tmp/default'} |
| 94 | + } |
| 95 | + }): |
| 96 | + for legacy_setting in ['AVATAR_BACKEND', 'CERTIFICATE_BACKEND', 'PROFILE_IMAGE_BACKEND']: |
| 97 | + if hasattr(settings, legacy_setting): |
| 98 | + delattr(settings, legacy_setting) |
| 99 | + |
| 100 | + for storage_name, legacy_setting_name in [ |
| 101 | + ('avatars', 'AVATAR_BACKEND'), |
| 102 | + ('certificates', 'CERTIFICATE_BACKEND'), |
| 103 | + ('profile_image', 'PROFILE_IMAGE_BACKEND'), |
| 104 | + ]: |
| 105 | + storage = get_named_storage(storage_name, legacy_setting_name=legacy_setting_name) |
| 106 | + self.assertEqual(storage, default_storage) |
0 commit comments