1-
21import os
2+ from os .path import join , dirname
33import pytest
44from tests .config import LocalConfig as config
5-
65from flask_cloudy import (InvalidExtensionError , Object , get_driver_class ,
76 get_file_extension , get_file_extension_type ,
87 get_file_name , get_provider_name )
98from libcloud .storage .base import Container , StorageDriver
109
11- CWD = os . path . dirname (__file__ )
10+ CWD = dirname (__file__ )
1211
13- CONTAINER = "%s/%s" % (CWD , config .STORAGE_CONTAINER ) if config .STORAGE_PROVIDER == "LOCAL" else config .STORAGE_CONTAINER
12+ CONTAINER = "%s/%s" % (
13+ CWD , config .STORAGE_CONTAINER
14+ ) if config .STORAGE_PROVIDER == "LOCAL" else config .STORAGE_CONTAINER
1415
1516
1617class TestUtilities :
@@ -31,6 +32,7 @@ def test_get_file_name(self):
3132 def test_get_provider_name (self ):
3233 class GoogleStorageDriver (object ):
3334 pass
35+
3436 driver = GoogleStorageDriver ()
3537 assert get_provider_name (driver ) == "google_storage"
3638
@@ -64,6 +66,22 @@ def test_storage_object(self, storage):
6466 o = storage .create (object_name )
6567 assert isinstance (o , Object )
6668
69+ def test_empty_storage_object_evaluates_false (self , storage ):
70+ """Check that storage object eval false if they are empty."""
71+ object_name = "hello.txt"
72+ o = storage .create (object_name )
73+ if o :
74+ raise ValueError ("Object evaluated to false" )
75+
76+ def test_storage_object_evaluates_true (self , storage , temp_txt_file ):
77+ """Check that storage object eval true if they are not empty."""
78+ o = storage .upload (temp_txt_file .name )
79+ o1 = storage .get (o .name )
80+ if not o :
81+ raise ValueError ("Storage upload object evaluated to false" )
82+ if not o1 :
83+ raise ValueError ("Storage get object evaluated to false" )
84+
6785 def test_object_type_extension (self , storage ):
6886 object_name = "hello.jpg"
6987 o = storage .create (object_name )
@@ -81,79 +99,112 @@ def test_object_object_path(self, storage):
8199 p = "%s/%s" % (o .container .name , o .name )
82100 assert o .path .endswith (p )
83101
84- def test_storage_upload_invalid (self , storage ):
102+ def test_storage_upload_invalid (self , storage , temp_js_file ):
103+ """Check .js extensions are not allowed by default."""
85104 object_name = "my-js/hello.js"
86105 with pytest .raises (InvalidExtensionError ):
87- storage .upload (CWD + "/data/hello.js" , name = object_name )
106+ storage .upload (temp_js_file . name , name = object_name )
88107
89- def test_storage_upload_ovewrite (self , storage ):
90- object_name = "my-txt-hello.txt"
91- o = storage .upload (CWD + "/data/hello.txt" , name = object_name , overwrite = True )
92- assert isinstance (o , Object )
93- assert o .name == object_name
94-
95- def test_storage_get (self , storage ):
96- object_name = "my-txt-helloIII.txt"
97- o = storage .upload (CWD + "/data/hello.txt" , name = object_name , overwrite = True )
108+ def test_storage_upload_overwrite (self , storage , temp_txt_file ,
109+ temp_js_file ):
110+ object_name = "hello.txt"
111+ o = storage .upload (temp_js_file .name ,
112+ name = object_name ,
113+ overwrite = True ,
114+ allowed_extensions = ["js" ])
115+ o_ow = storage .upload (temp_txt_file .name ,
116+ name = object_name ,
117+ overwrite = True )
118+ assert isinstance (o_ow , Object )
119+ assert o_ow .name == o .name
120+ assert o_ow .name == object_name
121+
122+ def test_storage_upload_no_overwrite (self , storage , temp_txt_file ,
123+ temp_js_file ):
124+ object_name = "hello.txt"
125+ o = storage .upload (temp_js_file .name ,
126+ overwrite = True ,
127+ allowed_extensions = ["js" ])
128+ o_no_ow = storage .upload (temp_txt_file .name ,
129+ name = object_name ,
130+ overwrite = False )
131+ assert isinstance (o_no_ow , Object )
132+ assert o .name != o_no_ow .name
133+
134+ def test_storage_get (self , storage , temp_txt_file ):
135+ object_name = "test_storage_get.txt"
136+ o = storage .upload (temp_txt_file .name ,
137+ name = object_name ,
138+ overwrite = True )
98139 o2 = storage .get (o .name )
99140 assert isinstance (o2 , Object )
100141
101142 def test_storage_get_none (self , storage ):
102143 o2 = storage .get ("idonexist" )
103144 assert o2 is None
104145
105- def test_storage_upload (self , storage ):
106- object_name = "my-txt-hello2 .txt"
107- storage .upload (CWD + "/data/hello.txt" , name = object_name )
108- o = storage .upload (CWD + "/data/hello.txt" , name = object_name )
146+ def test_storage_upload (self , storage , temp_txt_file ):
147+ object_name = "test_storage_upload .txt"
148+ storage .upload (temp_txt_file . name , name = object_name )
149+ o = storage .upload (temp_txt_file . name , name = object_name )
109150 assert isinstance (o , Object )
110151 assert o .name != object_name
111152
112- def test_storage_upload_use_filename_name (self , storage ):
113- object_name = "hello.js"
114- o = storage .upload (CWD + "/data/hello.js" , overwrite = True , allowed_extensions = ["js" ])
153+ def test_storage_upload_use_filename_name (self , storage , temp_js_file ):
154+ """Check that uploaded files retain thier name."""
155+ object_name = os .path .basename (temp_js_file .name )
156+ o = storage .upload (temp_js_file .name ,
157+ overwrite = True ,
158+ allowed_extensions = ["js" ])
115159 assert o .name == object_name
116160
117- def test_storage_upload_append_extension (self , storage ):
118- object_name = "my-txt-hello-hello"
119- o = storage .upload (CWD + "/data/hello.txt" , object_name , overwrite = True )
161+ def test_storage_upload_append_extension (self , storage , temp_txt_file ):
162+ """Check that uploaded names get an appended extension."""
163+ object_name = "test_storage_upload_append_extension"
164+ o = storage .upload (temp_txt_file .name , object_name , overwrite = True )
120165 assert get_file_extension (o .name ) == "txt"
121166
122- def test_storage_upload_with_prefix (self , storage ):
123- object_name = "my-txt-hello-hello"
167+ def test_storage_upload_with_prefix (self , storage , temp_txt_file ):
168+ object_name = os . path . splitext ( os . path . basename ( temp_txt_file . name ))[ 0 ]
124169 prefix = "dir1/dir2/dir3/"
125170 full_name = "%s%s.%s" % (prefix , object_name , "txt" )
126- o = storage .upload (CWD + "/data/hello.txt" , name = object_name , prefix = prefix , overwrite = True )
171+ o = storage .upload (temp_txt_file .name ,
172+ name = object_name ,
173+ prefix = prefix ,
174+ overwrite = True )
127175 assert full_name in storage
128176 assert o .name == full_name
129177
130- def test_save_to (self , storage ):
131- object_name = "my-txt-hello-to-save.txt"
132- o = storage .upload (CWD + "/data/hello.txt" , name = object_name )
133- file = o .save_to (CWD + "/data" , overwrite = True )
134- file2 = o .save_to (CWD + "/data" , name = "my_new_file" , overwrite = True )
178+ def test_save_to (self , storage , temp_dir , temp_txt_file ):
179+ object_name = "test_save_to.txt"
180+ o = storage .upload (temp_txt_file .name , name = object_name )
181+ file = o .save_to (temp_dir , overwrite = True )
182+ file2 = o .save_to (
183+ temp_dir ,
184+ name = "my_new_file" ,
185+ overwrite = True )
186+ print (o , o .name , file , temp_dir , file2 )
135187 assert os .path .isfile (file )
136- assert file2 == CWD + "/data/ my_new_file.txt"
188+ assert file2 == join ( temp_dir , " my_new_file.txt")
137189
138- def test_werkzeug_upload (self , storage ):
139- try :
140- import werkzeug
141- except ImportError :
142- return
143- object_name = "my-txt-hello.txt"
144- filepath = CWD + "/data/hello.txt"
145- file = None
146- with open (filepath , 'rb' ) as fp :
147- file = werkzeug .datastructures .FileStorage (fp )
148- file .filename = object_name
149- o = storage .upload (file , overwrite = True )
150- assert isinstance (o , Object )
151- assert o .name == object_name
152-
153- def test_local_server (self , storage ):
190+ def test_local_server (self , storage , temp_txt_file ):
154191 """Test the local server function."""
155192 object_name = "test_local_server.txt"
156- storage .upload (CWD + "/data/hello.txt" , name = object_name , overwrite = True )
193+ storage .upload (temp_txt_file .name , name = object_name , overwrite = True )
194+ print (storage .app .view_functions )
157195 file_server = storage .app .view_functions ['FLASK_CLOUDY_SERVER' ]
158196 response = file_server (object_name )
159197 assert response .status_code == 200
198+
199+ @pytest .mark .timeout (30 )
200+ def test_werkzeug_upload (self , storage , temp_png_file ):
201+ try :
202+ import werkzeug
203+ except ImportError :
204+ return
205+ object_name = "test-werkzeug-upload.txt"
206+ file = werkzeug .datastructures .FileStorage (temp_png_file )
207+ file .filename = object_name
208+ o = storage .upload (file , overwrite = True )
209+ assert isinstance (o , Object )
210+ assert o .name == object_name
0 commit comments