carrierwave 0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of carrierwave might be problematic. Click here for more details.

@@ -0,0 +1,120 @@
1
+ $TESTING=true
2
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
3
+
4
+ require 'rubygems'
5
+
6
+ if ENV["AS"]
7
+ puts "--> using ActiveSupport"
8
+ require 'activesupport'
9
+ else
10
+ puts "--> using Extlib"
11
+ require 'extlib'
12
+ end
13
+
14
+ require 'tempfile'
15
+ #require 'ruby-debug'
16
+ require 'spec'
17
+
18
+ require 'carrierwave'
19
+
20
+ alias :running :lambda
21
+
22
+ def file_path( *paths )
23
+ File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', *paths))
24
+ end
25
+
26
+ def public_path( *paths )
27
+ File.expand_path(File.join(File.dirname(__FILE__), 'public', *paths))
28
+ end
29
+
30
+ CarrierWave.config[:public] = public_path
31
+ CarrierWave.config[:root] = File.expand_path(File.dirname(__FILE__))
32
+
33
+ module SanitizedFileSpecHelper
34
+ def stub_merb_tempfile(filename)
35
+ raise "#{path} file does not exist" unless File.exist?(file_path(filename))
36
+
37
+ t = Tempfile.new(filename)
38
+ FileUtils.copy_file(file_path(filename), t.path)
39
+
40
+ return t
41
+ end
42
+
43
+ def stub_tempfile(filename, mime_type=nil, fake_name=nil)
44
+ raise "#{path} file does not exist" unless File.exist?(file_path(filename))
45
+
46
+ t = Tempfile.new(filename)
47
+ FileUtils.copy_file(file_path(filename), t.path)
48
+
49
+ # This is stupid, but for some reason rspec won't play nice...
50
+ eval <<-EOF
51
+ def t.original_filename; '#{fake_name || filename}'; end
52
+ def t.content_type; '#{mime_type}'; end
53
+ def t.local_path; path; end
54
+ EOF
55
+
56
+ return t
57
+ end
58
+
59
+ def stub_stringio(filename, mime_type=nil, fake_name=nil)
60
+ if filename
61
+ t = StringIO.new( IO.read( file_path( filename ) ) )
62
+ else
63
+ t = StringIO.new
64
+ end
65
+ t.stub!(:local_path).and_return("")
66
+ t.stub!(:original_filename).and_return(filename || fake_name)
67
+ t.stub!(:content_type).and_return(mime_type)
68
+ return t
69
+ end
70
+
71
+ def stub_file(filename, mime_type=nil, fake_name=nil)
72
+ f = File.open(file_path(filename))
73
+ return f
74
+ end
75
+
76
+ class BeIdenticalTo
77
+ def initialize(expected)
78
+ @expected = expected
79
+ end
80
+ def matches?(actual)
81
+ @actual = actual
82
+ FileUtils.identical?(@actual, @expected)
83
+ end
84
+ def failure_message
85
+ "expected #{@actual.inspect} to be identical to #{@expected.inspect}"
86
+ end
87
+ def negative_failure_message
88
+ "expected #{@actual.inspect} to not be identical to #{@expected.inspect}"
89
+ end
90
+ end
91
+
92
+ def be_identical_to(expected)
93
+ BeIdenticalTo.new(expected)
94
+ end
95
+
96
+ class HavePermissions
97
+ def initialize(expected)
98
+ @expected = expected
99
+ end
100
+
101
+ def matches?(actual)
102
+ @actual = actual
103
+ # Satisfy expectation here. Return false or raise an error if it's not met.
104
+ (File.stat(@actual.path).mode & 0777) == @expected
105
+ end
106
+
107
+ def failure_message
108
+ "expected #{@actual.inspect} to have permissions #{@expected.to_s(8)}, but they were #{(File.stat(@actual.path).mode & 0777).to_s(8)}"
109
+ end
110
+
111
+ def negative_failure_message
112
+ "expected #{@actual.inspect} not to have permissions #{@expected.to_s(8)}, but it did"
113
+ end
114
+ end
115
+
116
+ def have_permissions(expected)
117
+ HavePermissions.new(expected)
118
+ end
119
+
120
+ end
@@ -0,0 +1,739 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe CarrierWave::Uploader do
4
+
5
+ before do
6
+ @uploader_class = Class.new(CarrierWave::Uploader)
7
+ @uploader = @uploader_class.new
8
+ end
9
+
10
+ after do
11
+ FileUtils.rm_rf(public_path)
12
+ end
13
+
14
+ describe '.version' do
15
+ it "should add it to .versions" do
16
+ @uploader_class.version :thumb
17
+ @uploader_class.versions[:thumb].should be_a(Class)
18
+ @uploader_class.versions[:thumb].ancestors.should include(@uploader_class)
19
+ end
20
+
21
+ it "should add an accessor which returns the version" do
22
+ @uploader_class.version :thumb
23
+ @uploader.thumb.should be_a(@uploader_class)
24
+ end
25
+
26
+ it "should add it to #versions which returns the version" do
27
+ @uploader_class.version :thumb
28
+ @uploader.versions[:thumb].should be_a(@uploader_class)
29
+ end
30
+
31
+ it "should set the version name" do
32
+ @uploader_class.version :thumb
33
+ @uploader.version_name.should == nil
34
+ @uploader.thumb.version_name.should == :thumb
35
+ end
36
+
37
+ it "should set the version name on the class" do
38
+ @uploader_class.version :thumb
39
+ @uploader.class.version_name.should == nil
40
+ @uploader.thumb.class.version_name.should == :thumb
41
+ end
42
+
43
+ it "should remember mount options" do
44
+ model = mock('a model')
45
+ @uploader_class.version :thumb
46
+ @uploader = @uploader_class.new(model, :gazelle)
47
+
48
+ @uploader.thumb.model.should == model
49
+ @uploader.thumb.mounted_as.should == :gazelle
50
+ end
51
+
52
+ it "should apply any overrides given in a block" do
53
+ @uploader_class.version :thumb do
54
+ def store_dir
55
+ public_path('monkey/apache')
56
+ end
57
+ end
58
+ @uploader.store_dir.should == 'uploads'
59
+ @uploader.thumb.store_dir.should == public_path('monkey/apache')
60
+ end
61
+
62
+ end
63
+
64
+ describe '.process' do
65
+ it "should add a single processor when a symbol is given" do
66
+ @uploader_class.process :sepiatone
67
+ @uploader.should_receive(:sepiatone)
68
+ @uploader.process!
69
+ end
70
+
71
+ it "should add multiple processors when an array of symbols is given" do
72
+ @uploader_class.process :sepiatone, :desaturate, :invert
73
+ @uploader.should_receive(:sepiatone)
74
+ @uploader.should_receive(:desaturate)
75
+ @uploader.should_receive(:invert)
76
+ @uploader.process!
77
+ end
78
+
79
+ it "should add a single processor with an argument when a hash is given" do
80
+ @uploader_class.process :format => 'png'
81
+ @uploader.should_receive(:format).with('png')
82
+ @uploader.process!
83
+ end
84
+
85
+ it "should add a single processor with several argument when a hash is given" do
86
+ @uploader_class.process :resize => [200, 300]
87
+ @uploader.should_receive(:resize).with(200, 300)
88
+ @uploader.process!
89
+ end
90
+
91
+ it "should add multiple processors when an hash with multiple keys is given" do
92
+ @uploader_class.process :resize => [200, 300], :format => 'png'
93
+ @uploader.should_receive(:resize).with(200, 300)
94
+ @uploader.should_receive(:format).with('png')
95
+ @uploader.process!
96
+ end
97
+ end
98
+
99
+ describe ".storage" do
100
+ before do
101
+ CarrierWave::Storage::File.stub!(:setup!)
102
+ CarrierWave::Storage::S3.stub!(:setup!)
103
+ end
104
+
105
+ it "should set the storage if an argument is given" do
106
+ storage = mock('some kind of storage')
107
+ storage.should_receive(:setup!)
108
+ @uploader_class.storage storage
109
+ @uploader_class.storage.should == storage
110
+ end
111
+
112
+ it "should default to file" do
113
+ @uploader_class.storage.should == CarrierWave::Storage::File
114
+ end
115
+
116
+ it "should set the storage from the configured shortcuts if a symbol is given" do
117
+ @uploader_class.storage :file
118
+ @uploader_class.storage.should == CarrierWave::Storage::File
119
+ end
120
+
121
+ it "should remember the storage when inherited" do
122
+ @uploader_class.storage :s3
123
+ subclass = Class.new(@uploader_class)
124
+ subclass.storage.should == CarrierWave::Storage::S3
125
+ end
126
+
127
+ it "should be changeable when inherited" do
128
+ @uploader_class.storage :s3
129
+ subclass = Class.new(@uploader_class)
130
+ subclass.storage.should == CarrierWave::Storage::S3
131
+ subclass.storage :file
132
+ subclass.storage.should == CarrierWave::Storage::File
133
+ end
134
+ end
135
+
136
+ describe '#blank' do
137
+ it "should be true when nothing has been done" do
138
+ @uploader.should be_blank
139
+ end
140
+
141
+ it "should not be true when the file is empty" do
142
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
143
+ @uploader.should be_blank
144
+ end
145
+
146
+ it "should not be true when a file has been cached" do
147
+ @uploader.cache!(File.open(file_path('test.jpg')))
148
+ @uploader.should_not be_blank
149
+ end
150
+ end
151
+
152
+ describe '#store_dir' do
153
+ it "should default to the config option" do
154
+ @uploader.store_dir.should == 'uploads'
155
+ end
156
+ end
157
+
158
+ describe '#cache_dir' do
159
+ it "should default to the config option" do
160
+ @uploader.cache_dir.should == 'uploads/tmp'
161
+ end
162
+ end
163
+
164
+ describe '#root' do
165
+ it "should default to the config option" do
166
+ @uploader.root.should == public_path('..')
167
+ end
168
+ end
169
+
170
+ describe '#filename' do
171
+ it "should default to nil" do
172
+ @uploader.filename.should be_nil
173
+ end
174
+ end
175
+
176
+ describe '#model' do
177
+ it "should be remembered from initialization" do
178
+ model = mock('a model object')
179
+ @uploader = @uploader_class.new(model)
180
+ @uploader.model.should == model
181
+ end
182
+ end
183
+
184
+ describe '#mounted_as' do
185
+ it "should be remembered from initialization" do
186
+ model = mock('a model object')
187
+ @uploader = @uploader_class.new(model, :llama)
188
+ @uploader.model.should == model
189
+ @uploader.mounted_as.should == :llama
190
+ end
191
+ end
192
+
193
+ describe '#url' do
194
+ before do
195
+ CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
196
+ end
197
+
198
+ it "should default to nil" do
199
+ @uploader.url.should be_nil
200
+ end
201
+
202
+ it "should get the directory relative to public, prepending a slash" do
203
+ @uploader.cache!(File.open(file_path('test.jpg')))
204
+ @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
205
+ end
206
+
207
+ it "should return file#url if available" do
208
+ @uploader.cache!(File.open(file_path('test.jpg')))
209
+ @uploader.file.stub!(:url).and_return('http://www.example.com/someurl.jpg')
210
+ @uploader.url.should == 'http://www.example.com/someurl.jpg'
211
+ end
212
+
213
+ it "should get the directory relative to public, if file#url is blank" do
214
+ @uploader.cache!(File.open(file_path('test.jpg')))
215
+ @uploader.file.stub!(:url).and_return('')
216
+ @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
217
+ end
218
+ end
219
+
220
+ describe '#to_s' do
221
+ before do
222
+ CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
223
+ end
224
+
225
+ it "should default to nil" do
226
+ @uploader.to_s.should be_nil
227
+ end
228
+
229
+ it "should get the directory relative to public, prepending a slash" do
230
+ @uploader.cache!(File.open(file_path('test.jpg')))
231
+ @uploader.to_s.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
232
+ end
233
+
234
+ it "should return file#url if available" do
235
+ @uploader.cache!(File.open(file_path('test.jpg')))
236
+ @uploader.file.stub!(:url).and_return('http://www.example.com/someurl.jpg')
237
+ @uploader.to_s.should == 'http://www.example.com/someurl.jpg'
238
+ end
239
+ end
240
+
241
+ describe '#cache!' do
242
+
243
+ before do
244
+ CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
245
+ end
246
+
247
+ it "should cache a file" do
248
+ @uploader.cache!(File.open(file_path('test.jpg')))
249
+ @uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
250
+ end
251
+
252
+ it "should store the cache name" do
253
+ @uploader.cache!(File.open(file_path('test.jpg')))
254
+ @uploader.cache_name.should == '20071201-1234-345-2255/test.jpg'
255
+ end
256
+
257
+ it "should set the filename to the file's sanitized filename" do
258
+ @uploader.cache!(File.open(file_path('test.jpg')))
259
+ @uploader.filename.should == 'test.jpg'
260
+ end
261
+
262
+ it "should move it to the tmp dir" do
263
+ @uploader.cache!(File.open(file_path('test.jpg')))
264
+ @uploader.file.path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
265
+ @uploader.file.exists?.should be_true
266
+ end
267
+
268
+ it "should set the url" do
269
+ @uploader.cache!(File.open(file_path('test.jpg')))
270
+ @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
271
+ end
272
+
273
+ it "should trigger a process!" do
274
+ @uploader.should_receive(:process!)
275
+ @uploader.cache!(File.open(file_path('test.jpg')))
276
+ end
277
+
278
+ it "should raise an error when trying to cache a string" do
279
+ running {
280
+ @uploader.cache!(file_path('test.jpg'))
281
+ }.should raise_error(CarrierWave::FormNotMultipart)
282
+ end
283
+
284
+ it "should raise an error when trying to cache a pathname" do
285
+ running {
286
+ @uploader.cache!(Pathname.new(file_path('test.jpg')))
287
+ }.should raise_error(CarrierWave::FormNotMultipart)
288
+ end
289
+
290
+ it "should do nothing when trying to cache an empty file" do
291
+ @uploader.cache!(nil)
292
+ end
293
+ end
294
+
295
+ describe '#retrieve_from_cache!' do
296
+ it "should cache a file" do
297
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
298
+ @uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
299
+ end
300
+
301
+ it "should set the path to the tmp dir" do
302
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
303
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpeg')
304
+ end
305
+
306
+ it "should overwrite a file that has already been cached" do
307
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
308
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/bork.txt')
309
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/bork.txt')
310
+ end
311
+
312
+ it "should store the cache_name" do
313
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
314
+ @uploader.cache_name.should == '20071201-1234-345-2255/test.jpeg'
315
+ end
316
+
317
+ it "should store the filename" do
318
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
319
+ @uploader.filename.should == 'test.jpeg'
320
+ end
321
+
322
+ it "should set the url" do
323
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
324
+ @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpeg'
325
+ end
326
+
327
+ it "should raise an error when the cache_id has an invalid format" do
328
+ running {
329
+ @uploader.retrieve_from_cache!('12345/test.jpeg')
330
+ }.should raise_error(CarrierWave::InvalidParameter)
331
+
332
+ @uploader.file.should be_nil
333
+ @uploader.filename.should be_nil
334
+ @uploader.cache_name.should be_nil
335
+ end
336
+
337
+ it "should raise an error when the original_filename contains invalid characters" do
338
+ running {
339
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/te/st.jpeg')
340
+ }.should raise_error(CarrierWave::InvalidParameter)
341
+ running {
342
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/te??%st.jpeg')
343
+ }.should raise_error(CarrierWave::InvalidParameter)
344
+
345
+ @uploader.file.should be_nil
346
+ @uploader.filename.should be_nil
347
+ @uploader.cache_name.should be_nil
348
+ end
349
+ end
350
+
351
+ describe '#retrieve_from_cache' do
352
+ it "should cache a file" do
353
+ @uploader.retrieve_from_cache('20071201-1234-345-2255/test.jpeg')
354
+ @uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
355
+ end
356
+
357
+ it "should not overwrite a file that has already been cached" do
358
+ @uploader.retrieve_from_cache('20071201-1234-345-2255/test.jpeg')
359
+ @uploader.retrieve_from_cache('20071201-1234-345-2255/bork.txt')
360
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpeg')
361
+ end
362
+
363
+ it "should do nothing when the cache_id has an invalid format" do
364
+ @uploader.retrieve_from_cache('12345/test.jpeg')
365
+ @uploader.file.should be_nil
366
+ @uploader.filename.should be_nil
367
+ @uploader.cache_name.should be_nil
368
+ end
369
+
370
+ it "should do nothing when the filename contains invalid characters" do
371
+ @uploader.retrieve_from_cache('20071201-1234-345-2255/te??%st.jpeg')
372
+ @uploader.file.should be_nil
373
+ @uploader.filename.should be_nil
374
+ @uploader.cache_name.should be_nil
375
+ end
376
+ end
377
+
378
+ describe '#store!' do
379
+ before do
380
+ @file = File.open(file_path('test.jpg'))
381
+
382
+ @stored_file = mock('a stored file')
383
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
384
+ @stored_file.stub!(:url).and_return('http://www.example.com')
385
+ @stored_file.stub!(:identifier).and_return('this-is-me')
386
+
387
+ @uploader_class.storage.stub!(:store!).and_return(@stored_file)
388
+ end
389
+
390
+ it "should set the current path" do
391
+ @uploader.store!(@file)
392
+ @uploader.current_path.should == '/path/to/somewhere'
393
+ end
394
+
395
+ it "should set the url" do
396
+ @uploader.store!(@file)
397
+ @uploader.url.should == 'http://www.example.com'
398
+ end
399
+
400
+ it "should set the identifier" do
401
+ @uploader.store!(@file)
402
+ @uploader.identifier.should == 'this-is-me'
403
+ end
404
+
405
+ it "should, if a file is given as argument, cache that file" do
406
+ @uploader.should_receive(:cache!).with(@file)
407
+ @uploader.store!(@file)
408
+ end
409
+
410
+ it "should use a previously cached file if no argument is given" do
411
+ @uploader.cache!(File.open(file_path('test.jpg')))
412
+ @uploader.should_not_receive(:cache!)
413
+ @uploader.store!
414
+ end
415
+
416
+ it "should instruct the storage engine to store the file" do
417
+ @uploader.cache!(@file)
418
+ @uploader_class.storage.should_receive(:store!).with(@uploader, @uploader.file).and_return(:monkey)
419
+ @uploader.store!
420
+ end
421
+
422
+ it "should reset the cache_name" do
423
+ @uploader.cache!(@file)
424
+ @uploader.store!
425
+ @uploader.cache_name.should be_nil
426
+ end
427
+
428
+ it "should cache the result given by the storage engine" do
429
+ @uploader.store!(@file)
430
+ @uploader.file.should == @stored_file
431
+ end
432
+
433
+ it "should do nothing when trying to store an empty file" do
434
+ @uploader.store!(nil)
435
+ end
436
+ end
437
+
438
+ describe '#retrieve_from_store!' do
439
+ before do
440
+ @stored_file = mock('a stored file')
441
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
442
+ @stored_file.stub!(:url).and_return('http://www.example.com')
443
+ @stored_file.stub!(:identifier).and_return('this-is-me')
444
+
445
+ @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
446
+ end
447
+
448
+ it "should set the current path" do
449
+ @uploader.retrieve_from_store!('monkey.txt')
450
+ @uploader.current_path.should == '/path/to/somewhere'
451
+ end
452
+
453
+ it "should set the url" do
454
+ @uploader.retrieve_from_store!('monkey.txt')
455
+ @uploader.url.should == 'http://www.example.com'
456
+ end
457
+
458
+ it "should set the identifier" do
459
+ @uploader.retrieve_from_store!('monkey.txt')
460
+ @uploader.identifier.should == 'this-is-me'
461
+ end
462
+
463
+ it "should instruct the storage engine to retrieve the file and store the result" do
464
+ @uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
465
+ @uploader.retrieve_from_store!('monkey.txt')
466
+ @uploader.file.should == @stored_file
467
+ end
468
+
469
+ it "should overwrite a file that has already been cached" do
470
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
471
+ @uploader.retrieve_from_store!('bork.txt')
472
+ @uploader.file.should == @stored_file
473
+ end
474
+ end
475
+
476
+ describe '#retrieve_from_store' do
477
+ before do
478
+ @stored_file = mock('a stored file')
479
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
480
+ @stored_file.stub!(:url).and_return('http://www.example.com')
481
+ @stored_file.stub!(:identifier).and_return('this-is-me')
482
+
483
+ @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
484
+ end
485
+
486
+ it "should set the current path" do
487
+ @uploader.retrieve_from_store('monkey.txt')
488
+ @uploader.current_path.should == '/path/to/somewhere'
489
+ end
490
+
491
+ it "should set the url" do
492
+ @uploader.retrieve_from_store('monkey.txt')
493
+ @uploader.url.should == 'http://www.example.com'
494
+ end
495
+
496
+ it "should set the identifier" do
497
+ @uploader.retrieve_from_store('monkey.txt')
498
+ @uploader.identifier.should == 'this-is-me'
499
+ end
500
+
501
+ it "should instruct the storage engine to retrieve the file and store the result" do
502
+ @uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
503
+ @uploader.retrieve_from_store('monkey.txt')
504
+ @uploader.file.should == @stored_file
505
+ end
506
+
507
+ it "should not overwrite a file that has already been cached" do
508
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
509
+ @uploader.retrieve_from_store('bork.txt')
510
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpeg')
511
+ end
512
+ end
513
+
514
+ describe 'with a version' do
515
+ before do
516
+ @uploader_class.version(:thumb)
517
+ end
518
+
519
+ describe '#cache!' do
520
+
521
+ before do
522
+ CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
523
+ end
524
+
525
+ it "should suffix the version's store_dir" do
526
+ @uploader.cache!(File.open(file_path('test.jpg')))
527
+ @uploader.store_dir.should == 'uploads'
528
+ @uploader.thumb.store_dir.should == 'uploads/thumb'
529
+ end
530
+
531
+ it "should move it to the tmp dir with the filename prefixed" do
532
+ @uploader.cache!(File.open(file_path('test.jpg')))
533
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
534
+ @uploader.thumb.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/thumb_test.jpg')
535
+ @uploader.file.exists?.should be_true
536
+ @uploader.thumb.file.exists?.should be_true
537
+ end
538
+ end
539
+
540
+ describe '#retrieve_from_cache!' do
541
+ it "should set the path to the tmp dir" do
542
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
543
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
544
+ @uploader.thumb.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/thumb_test.jpg')
545
+ end
546
+
547
+ it "should suffix the version's store_dir" do
548
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
549
+ @uploader.store_dir.should == 'uploads'
550
+ @uploader.thumb.store_dir.should == 'uploads/thumb'
551
+ end
552
+ end
553
+
554
+ describe '#store!' do
555
+ before do
556
+ @file = File.open(file_path('test.jpg'))
557
+
558
+ @stored_file = mock('a stored file')
559
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
560
+ @stored_file.stub!(:url).and_return('http://www.example.com')
561
+
562
+ @uploader_class.storage.stub!(:store!).and_return(@stored_file)
563
+ end
564
+
565
+ after do
566
+ CarrierWave.config[:use_cache] = true
567
+ end
568
+
569
+ it "should set the current path for the version" do
570
+ pending "find a decent way to spec this"
571
+ @uploader.store!(@file)
572
+ @uploader.current_path.should == '/path/to/somewhere'
573
+ @uploader.thumb.current_path.should == '/path/to/somewhere'
574
+ end
575
+
576
+ it "should set the url" do
577
+ pending "find a decent way to spec this"
578
+ @uploader.store!(@file)
579
+ @uploader.url.should == 'http://www.example.com'
580
+ end
581
+
582
+ it "should, if a file is given as argument, suffix the version's store_dir" do
583
+ @uploader.store!(@file)
584
+ @uploader.store_dir.should == 'uploads'
585
+ @uploader.thumb.store_dir.should == 'uploads/thumb'
586
+ end
587
+
588
+ it "should, if a files is given as an argument and use_cache is false, suffix the version's store_dir" do
589
+ CarrierWave.config[:use_cache] = false
590
+ @uploader.store!(@file)
591
+ @uploader.store_dir.should == 'uploads'
592
+ @uploader.thumb.store_dir.should == 'uploads/thumb'
593
+ end
594
+
595
+ end
596
+
597
+ describe '#retrieve_from_store!' do
598
+ before do
599
+ @stored_file = mock('a stored file')
600
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
601
+ @stored_file.stub!(:url).and_return('http://www.example.com')
602
+
603
+ @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
604
+ end
605
+
606
+ it "should set the current path" do
607
+ @uploader.retrieve_from_store!('monkey.txt')
608
+ @uploader.current_path.should == '/path/to/somewhere'
609
+ end
610
+
611
+ it "should set the url" do
612
+ @uploader.retrieve_from_store!('monkey.txt')
613
+ @uploader.url.should == 'http://www.example.com'
614
+ end
615
+
616
+ it "should pass the identifier to the storage engine" do
617
+ @uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
618
+ @uploader.retrieve_from_store!('monkey.txt')
619
+ @uploader.file.should == @stored_file
620
+ end
621
+
622
+ it "should not set the filename" do
623
+ @uploader.retrieve_from_store!('monkey.txt')
624
+ @uploader.filename.should be_nil
625
+ end
626
+ end
627
+ end
628
+
629
+ describe 'with an overridden, reversing, filename' do
630
+ before do
631
+ @uploader_class.class_eval do
632
+ def filename
633
+ super.reverse unless super.blank?
634
+ end
635
+ end
636
+ end
637
+
638
+ describe '#cache!' do
639
+
640
+ before do
641
+ CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
642
+ end
643
+
644
+ it "should set the filename to the file's reversed filename" do
645
+ @uploader.cache!(File.open(file_path('test.jpg')))
646
+ @uploader.filename.should == "gpj.tset"
647
+ end
648
+
649
+ it "should move it to the tmp dir with the filename unreversed" do
650
+ @uploader.cache!(File.open(file_path('test.jpg')))
651
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
652
+ @uploader.file.exists?.should be_true
653
+ end
654
+ end
655
+
656
+ describe '#retrieve_from_cache!' do
657
+ it "should set the path to the tmp dir" do
658
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
659
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
660
+ end
661
+
662
+ it "should set the filename to the reversed name of the file" do
663
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
664
+ @uploader.filename.should == "gpj.tset"
665
+ end
666
+ end
667
+
668
+ describe '#store!' do
669
+ before do
670
+ @file = File.open(file_path('test.jpg'))
671
+
672
+ @stored_file = mock('a stored file')
673
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
674
+ @stored_file.stub!(:url).and_return('http://www.example.com')
675
+
676
+ @uploader_class.storage.stub!(:store!).and_return(@stored_file)
677
+ end
678
+
679
+ after do
680
+ CarrierWave.config[:use_cache] = true
681
+ end
682
+
683
+ it "should set the current path" do
684
+ @uploader.store!(@file)
685
+ @uploader.current_path.should == '/path/to/somewhere'
686
+ end
687
+
688
+ it "should set the url" do
689
+ @uploader.store!(@file)
690
+ @uploader.url.should == 'http://www.example.com'
691
+ end
692
+
693
+ it "should, if a file is given as argument, reverse the filename" do
694
+ @uploader.store!(@file)
695
+ @uploader.filename.should == 'gpj.tset'
696
+ end
697
+
698
+ it "should, if a files is given as an argument and use_cache is false, reverse the filename" do
699
+ CarrierWave.config[:use_cache] = false
700
+ @uploader.store!(@file)
701
+ @uploader.filename.should == 'gpj.tset'
702
+ end
703
+
704
+ end
705
+
706
+ describe '#retrieve_from_store!' do
707
+ before do
708
+ @stored_file = mock('a stored file')
709
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
710
+ @stored_file.stub!(:url).and_return('http://www.example.com')
711
+
712
+ @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
713
+ end
714
+
715
+ it "should set the current path" do
716
+ @uploader.retrieve_from_store!('monkey.txt')
717
+ @uploader.current_path.should == '/path/to/somewhere'
718
+ end
719
+
720
+ it "should set the url" do
721
+ @uploader.retrieve_from_store!('monkey.txt')
722
+ @uploader.url.should == 'http://www.example.com'
723
+ end
724
+
725
+ it "should pass the identifier to the storage engine" do
726
+ @uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
727
+ @uploader.retrieve_from_store!('monkey.txt')
728
+ @uploader.file.should == @stored_file
729
+ end
730
+
731
+ it "should not set the filename" do
732
+ @uploader.retrieve_from_store!('monkey.txt')
733
+ @uploader.filename.should be_nil
734
+ end
735
+ end
736
+
737
+ end
738
+
739
+ end