1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
require 'spec_helper'
describe CarrierWave::Uploader do
before do
@uploader_class = Class.new(CarrierWave::Uploader::Base)
@uploader = @uploader_class.new
end
after do
FileUtils.rm_rf(public_path)
end
describe '#store_dir' do
it "should default to the config option" do
expect(@uploader.store_dir).to eq('uploads')
end
end
describe '#filename' do
it "should default to nil" do
expect(@uploader.filename).to be_nil
end
end
describe '#store!' do
before do
@file = File.open(file_path('test.jpg'))
allow(CarrierWave).to receive(:generate_cache_id).and_return('1390890634-26112-1234-2122')
@cached_file = double('a cached file')
allow(@cached_file).to receive(:delete)
@stored_file = double('a stored file')
allow(@stored_file).to receive(:path).and_return('/path/to/somewhere')
allow(@stored_file).to receive(:url).and_return('http://www.example.com')
@storage = double('a storage engine')
allow(@storage).to receive(:cache!).and_return(@cached_file)
allow(@storage).to receive(:retrieve_from_cache!).and_return(@cached_file)
allow(@storage).to receive(:store!).and_return(@stored_file)
allow(@storage).to receive(:identifier).and_return('this-is-me')
allow(@storage).to receive(:delete_dir!).with("uploads/tmp/#{CarrierWave.generate_cache_id}")
allow(@uploader_class.storage).to receive(:new).with(@uploader).and_return(@storage)
end
it "should set the current path" do
@uploader.store!(@file)
expect(@uploader.current_path).to eq('/path/to/somewhere')
end
it "should not be cached" do
@uploader.store!(@file)
expect(@uploader).not_to be_cached
end
it "should set the url" do
@uploader.store!(@file)
expect(@uploader.url).to eq('http://www.example.com')
end
it "should set the identifier" do
@uploader.store!(@file)
expect(@uploader.identifier).to eq('this-is-me')
end
it "should, if a file is given as argument, cache that file" do
expect(@uploader).to receive(:cache!).with(@file)
@uploader.store!(@file)
end
it "should use a previously cached file if no argument is given" do
@uploader.cache!(File.open(file_path('test.jpg')))
expect(@uploader).not_to receive(:cache!)
@uploader.store!
end
it "should instruct the storage engine to store the file" do
@uploader.cache!(@file)
expect(@storage).to receive(:store!).with(@uploader.file).and_return(:monkey)
@uploader.store!
end
it "should reset the cache_name" do
@uploader.cache!(@file)
@uploader.store!
expect(@uploader.cache_name).to be_nil
end
it "should cache the result given by the storage engine" do
@uploader.store!(@file)
expect(@uploader.file).to eq(@stored_file)
end
it "should delete the old file" do
@uploader.cache!(@file)
expect(@uploader.file).to receive(:delete).and_return(true)
@uploader.store!
end
context "with the cache_only option set to true" do
before do
@uploader_class.cache_only = true
end
it "should not instruct the storage engine to store the file" do
@uploader.cache!(@file)
expect(@storage).not_to receive(:store!)
@uploader.store!
end
it "should still be cached" do
@uploader.store!(@file)
expect(@uploader).to be_cached
end
it "should not reset the cache_name" do
@uploader.cache!(@file)
@uploader.store!
expect(@uploader.cache_name).not_to be_nil
end
it "should not delete the old file" do
@uploader.cache!(@file)
expect(@uploader.file).not_to receive(:delete)
@uploader.store!
end
end
context "with the delete_tmp_file_after_storage option set to false" do
before do
@uploader_class.delete_tmp_file_after_storage = false
end
it "should not delete the old file" do
@uploader.cache!(@file)
expect(@uploader.file).not_to receive(:delete)
@uploader.store!
end
it "should not delete the old cache_id" do
@uploader.cache!(@file)
expect(@storage).not_to receive(:delete_dir!)
@uploader.store!
end
end
it "should delete the old cache_id" do
@uploader.cache!(@file)
expect(@storage).to receive(:delete_dir!)
@uploader.store!
end
it "should do nothing when trying to store an empty file" do
@uploader.store!(nil)
end
it "should not re-store a retrieved file" do
@stored_file = double('a stored file')
allow(@storage).to receive(:retrieve!).and_return(@stored_file)
expect(@uploader_class.storage).not_to receive(:store!)
@uploader.retrieve_from_store!('monkey.txt')
@uploader.store!
end
end
describe '#retrieve_from_store!' do
before do
@cached_file = double('a cached file')
allow(@cached_file).to receive(:delete)
@stored_file = double('a stored file')
allow(@stored_file).to receive(:path).and_return('/path/to/somewhere')
allow(@stored_file).to receive(:url).and_return('http://www.example.com')
@storage = double('a storage engine')
allow(@storage).to receive(:retrieve_from_cache!).and_return(@cached_file)
allow(@storage).to receive(:retrieve!).and_return(@stored_file)
allow(@storage).to receive(:identifier).and_return('this-is-me')
allow(@uploader_class.storage).to receive(:new).with(@uploader).and_return(@storage)
end
it "should set the current path" do
@uploader.retrieve_from_store!('monkey.txt')
expect(@uploader.current_path).to eq('/path/to/somewhere')
end
it "should not be cached" do
@uploader.retrieve_from_store!('monkey.txt')
expect(@uploader).not_to be_cached
end
it "should set the url" do
@uploader.retrieve_from_store!('monkey.txt')
expect(@uploader.url).to eq('http://www.example.com')
end
it "should set the identifier" do
@uploader.retrieve_from_store!('monkey.txt')
expect(@uploader.identifier).to eq('this-is-me')
end
it "should instruct the storage engine to retrieve the file and store the result" do
expect(@storage).to receive(:retrieve!).with('monkey.txt').and_return(@stored_file)
@uploader.retrieve_from_store!('monkey.txt')
expect(@uploader.file).to eq(@stored_file)
end
it "should overwrite a file that has already been cached" do
@uploader.retrieve_from_cache!('1369894322-345-1234-2255/test.jpeg')
@uploader.retrieve_from_store!('bork.txt')
expect(@uploader.file).to eq(@stored_file)
end
end
describe 'with an overridden filename' do
before do
@uploader_class.class_eval do
def filename; "foo.jpg"; end
end
end
it "should create new files if there is a file" do
@file = File.open(file_path('test.jpg'))
@uploader.store!(@file)
@path = ::File.expand_path(@uploader.store_path, @uploader.root)
expect(File.exist?(@path)).to be_truthy
end
it "should not create new files if there is no file" do
@uploader.store!(nil)
@path = ::File.expand_path(@uploader.store_path, @uploader.root)
expect(File.exist?(@path)).to be_falsey
end
end
describe 'without a store dir' do
before do
@uploader_class.class_eval do
def store_dir; nil; end
end
end
it "should create a new file with a valid path and url" do
@file = File.open(file_path('test.jpg'))
@uploader.store!(@file)
@path = ::File.expand_path(@uploader.store_path, @uploader.root)
expect(File.exist?(@path)).to be_truthy
expect(@uploader.url).to eq('/test.jpg')
end
end
describe 'with an overridden, reversing, filename' do
before do
@uploader_class.class_eval do
def filename
super.reverse unless super.blank?
end
end
end
describe '#store!' do
before do
@file = File.open(file_path('test.jpg'))
allow(CarrierWave).to receive(:generate_cache_id).and_return('1390890634-26112-1234-2122')
@cached_file = double('a cached file')
allow(@cached_file).to receive(:delete)
@stored_file = double('a stored file')
allow(@stored_file).to receive(:path).and_return('/path/to/somewhere')
allow(@stored_file).to receive(:url).and_return('http://www.example.com')
@storage = double('a storage engine')
allow(@storage).to receive(:cache!).and_return(@cached_file)
allow(@storage).to receive(:store!).and_return(@stored_file)
allow(@storage).to receive(:delete_dir!).with("uploads/tmp/#{CarrierWave.generate_cache_id}")
allow(@uploader_class.storage).to receive(:new).with(@uploader).and_return(@storage)
end
it "should set the current path" do
@uploader.store!(@file)
expect(@uploader.current_path).to eq('/path/to/somewhere')
end
it "should set the url" do
@uploader.store!(@file)
expect(@uploader.url).to eq('http://www.example.com')
end
it "should, if a file is given as argument, reverse the filename" do
@uploader.store!(@file)
expect(@uploader.filename).to eq('gpj.tset')
end
end
describe '#retrieve_from_store!' do
before do
@stored_file = double('a stored file')
allow(@stored_file).to receive(:path).and_return('/path/to/somewhere')
allow(@stored_file).to receive(:url).and_return('http://www.example.com')
@storage = double('a storage engine')
allow(@storage).to receive(:retrieve!).and_return(@stored_file)
allow(@uploader_class.storage).to receive(:new).with(@uploader).and_return(@storage)
end
it "should set the current path" do
@uploader.retrieve_from_store!('monkey.txt')
expect(@uploader.current_path).to eq('/path/to/somewhere')
end
it "should set the url" do
@uploader.retrieve_from_store!('monkey.txt')
expect(@uploader.url).to eq('http://www.example.com')
end
it "should pass the identifier to the storage engine" do
expect(@storage).to receive(:retrieve!).with('monkey.txt').and_return(@stored_file)
@uploader.retrieve_from_store!('monkey.txt')
expect(@uploader.file).to eq(@stored_file)
end
it "should not set the filename" do
@uploader.retrieve_from_store!('monkey.txt')
expect(@uploader.filename).to be_nil
end
end
end
describe "#store! with the move_to_store option" do
before do
@file = File.open(file_path('test.jpg'))
@uploader_class.permissions = 0777
@uploader_class.directory_permissions = 0777
allow(CarrierWave).to receive(:generate_cache_id).and_return('1369894322-345-1234-2255')
end
context "set to true" do
before do
@uploader_class.move_to_store = true
end
it "should move it from the tmp dir to the store dir" do
@uploader.cache!(@file)
@cached_path = @uploader.file.path
@stored_path = ::File.expand_path(@uploader.store_path, @uploader.root)
expect(@cached_path).to eq(public_path('uploads/tmp/1369894322-345-1234-2255/test.jpg'))
expect(File.exist?(@cached_path)).to be_truthy
expect(File.exist?(@stored_path)).to be_falsey
@uploader.store!
expect(File.exist?(@cached_path)).to be_falsey
expect(File.exist?(@stored_path)).to be_truthy
end
it "should use move_to() during store!()" do
@uploader.cache!(@file)
@stored_path = ::File.expand_path(@uploader.store_path, @uploader.root)
expect(@uploader.file).to receive(:move_to).with(@stored_path, 0777, 0777)
expect(@uploader.file).not_to receive(:copy_to)
@uploader.store!
end
end
context "set to false" do
before do
@uploader_class.move_to_store = false
end
it "should use copy_to() during store!()" do
@uploader.cache!(@file)
@stored_path = ::File.expand_path(@uploader.store_path, @uploader.root)
expect(@uploader.file).to receive(:copy_to).with(@stored_path, 0777, 0777)
expect(@uploader.file).not_to receive(:move_to)
@uploader.store!
end
end
end
end
|