55from unittest import mock
66
77from django .conf import settings
8- from django .core .files .storage import FileSystemStorage
98from django .core .management import call_command
109from django .test import TestCase
1110from kolibri_content .apps import KolibriContentConfig
1817)
1918
2019from contentcuration .models import Country
21-
22-
23- class FileSystemStorageWithoutPath :
24- """
25- A wrapper around FileSystemStorage that does not expose the `path` method,
26- as this will not be available in production where S3Storage is used.
27- This cannot be solved by just mocking the `path` method, because
28- it is used by the `FileSystemStorage` class internally.
29- """
30-
31- def __init__ (self , * args , ** kwargs ):
32- self ._inner = FileSystemStorage (* args , ** kwargs )
33-
34- def __getattr__ (self , name ):
35- if name == "path" :
36- raise NotImplementedError (
37- "The 'path' method is intentionally not available."
38- )
39- return getattr (self ._inner , name )
40-
41- def __dir__ (self ):
42- return [x for x in dir (self ._inner ) if x != "path" ]
43-
44-
45- class FileSystemStorageWithoutPathTestCase (TestCase ):
46- # Sanity-checks that the wrapper above works as expected,
47- # not actually testing application code
48-
49- def setUp (self ):
50- super ().setUp ()
51-
52- self ._temp_directory_ctx = tempfile .TemporaryDirectory ()
53- self .temp_dir = self ._temp_directory_ctx .__enter__ ()
54-
55- self .storage = FileSystemStorageWithoutPath (location = self .temp_dir )
56-
57- def tearDown (self ):
58- self ._temp_directory_ctx .__exit__ (None , None , None )
59- super ().tearDown ()
60-
61- def test_open_works (self ):
62- test_content = "test content"
63-
64- with self .storage .open ("filename" , "w" ) as f :
65- f .write (test_content )
66-
67- with open (os .path .join (self .temp_dir , "filename" ), "r" ) as f :
68- content = f .read ()
69- self .assertEqual (content , test_content )
70-
71- def test_path_does_not_work (self ):
72- with self .assertRaises (NotImplementedError ):
73- self .storage .path ("filename" )
20+ from contentcuration .tests .utils .restricted_filesystemstorage import (
21+ RestrictedFileSystemStorage ,
22+ )
7423
7524
7625class ExportTestCase (TestCase ):
@@ -80,7 +29,7 @@ def setUp(self):
8029 self ._temp_directory_ctx = tempfile .TemporaryDirectory ()
8130 test_db_root_dir = self ._temp_directory_ctx .__enter__ ()
8231
83- self .storage = FileSystemStorageWithoutPath (location = test_db_root_dir )
32+ self .storage = RestrictedFileSystemStorage (location = test_db_root_dir )
8433
8534 self ._storage_patch_ctx = mock .patch (
8635 "kolibri_public.utils.export_channel_to_kolibri_public.storage" ,
0 commit comments