jnicklas-carrierwave 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,709 @@
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 == 'public/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 '#store_dir' do
137
+ it "should default to the config option" do
138
+ @uploader.store_dir.should == 'public/uploads'
139
+ end
140
+ end
141
+
142
+ describe '#cache_dir' do
143
+ it "should default to the config option" do
144
+ @uploader.cache_dir.should == 'public/uploads/tmp'
145
+ end
146
+ end
147
+
148
+ describe '#root' do
149
+ it "should default to the config option" do
150
+ @uploader.root.should == public_path('..')
151
+ end
152
+ end
153
+
154
+ describe '#filename' do
155
+ it "should default to nil" do
156
+ @uploader.filename.should be_nil
157
+ end
158
+ end
159
+
160
+ describe '#model' do
161
+ it "should be remembered from initialization" do
162
+ model = mock('a model object')
163
+ @uploader = @uploader_class.new(model)
164
+ @uploader.model.should == model
165
+ end
166
+ end
167
+
168
+ describe '#mounted_as' do
169
+ it "should be remembered from initialization" do
170
+ model = mock('a model object')
171
+ @uploader = @uploader_class.new(model, :llama)
172
+ @uploader.model.should == model
173
+ @uploader.mounted_as.should == :llama
174
+ end
175
+ end
176
+
177
+ describe '#url' do
178
+ before do
179
+ CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
180
+ end
181
+
182
+ it "should default to nil" do
183
+ @uploader.url.should be_nil
184
+ end
185
+
186
+ it "should get the directory relative to public, prepending a slash" do
187
+ @uploader.cache!(File.open(file_path('test.jpg')))
188
+ @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
189
+ end
190
+
191
+ it "should return file#url if available" do
192
+ @uploader.cache!(File.open(file_path('test.jpg')))
193
+ @uploader.file.stub!(:url).and_return('http://www.example.com/someurl.jpg')
194
+ @uploader.url.should == 'http://www.example.com/someurl.jpg'
195
+ end
196
+
197
+ it "should get the directory relative to public, if file#url is blank" do
198
+ @uploader.cache!(File.open(file_path('test.jpg')))
199
+ @uploader.file.stub!(:url).and_return('')
200
+ @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
201
+ end
202
+ end
203
+
204
+ describe '#to_s' do
205
+ before do
206
+ CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
207
+ end
208
+
209
+ it "should default to nil" do
210
+ @uploader.to_s.should be_nil
211
+ end
212
+
213
+ it "should get the directory relative to public, prepending a slash" do
214
+ @uploader.cache!(File.open(file_path('test.jpg')))
215
+ @uploader.to_s.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
216
+ end
217
+
218
+ it "should return file#url if available" do
219
+ @uploader.cache!(File.open(file_path('test.jpg')))
220
+ @uploader.file.stub!(:url).and_return('http://www.example.com/someurl.jpg')
221
+ @uploader.to_s.should == 'http://www.example.com/someurl.jpg'
222
+ end
223
+ end
224
+
225
+ describe '#cache!' do
226
+
227
+ before do
228
+ CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
229
+ end
230
+
231
+ it "should cache a file" do
232
+ @uploader.cache!(File.open(file_path('test.jpg')))
233
+ @uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
234
+ end
235
+
236
+ it "should store the cache name" do
237
+ @uploader.cache!(File.open(file_path('test.jpg')))
238
+ @uploader.cache_name.should == '20071201-1234-345-2255/test.jpg'
239
+ end
240
+
241
+ it "should set the filename to the file's sanitized filename" do
242
+ @uploader.cache!(File.open(file_path('test.jpg')))
243
+ @uploader.filename.should == 'test.jpg'
244
+ end
245
+
246
+ it "should move it to the tmp dir" do
247
+ @uploader.cache!(File.open(file_path('test.jpg')))
248
+ @uploader.file.path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
249
+ @uploader.file.exists?.should be_true
250
+ end
251
+
252
+ it "should set the url" do
253
+ @uploader.cache!(File.open(file_path('test.jpg')))
254
+ @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
255
+ end
256
+
257
+ it "should trigger a process!" do
258
+ @uploader.should_receive(:process!)
259
+ @uploader.cache!(File.open(file_path('test.jpg')))
260
+ end
261
+ end
262
+
263
+ describe '#retrieve_from_cache!' do
264
+ it "should cache a file" do
265
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
266
+ @uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
267
+ end
268
+
269
+ it "should set the path to the tmp dir" do
270
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
271
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpeg')
272
+ end
273
+
274
+ it "should overwrite a file that has already been cached" do
275
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
276
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/bork.txt')
277
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/bork.txt')
278
+ end
279
+
280
+ it "should store the cache_name" do
281
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
282
+ @uploader.cache_name.should == '20071201-1234-345-2255/test.jpeg'
283
+ end
284
+
285
+ it "should store the filename" do
286
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
287
+ @uploader.filename.should == 'test.jpeg'
288
+ end
289
+
290
+ it "should set the url" do
291
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
292
+ @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpeg'
293
+ end
294
+
295
+ it "should raise an error when the cache_id has an invalid format" do
296
+ running {
297
+ @uploader.retrieve_from_cache!('12345/test.jpeg')
298
+ }.should raise_error(CarrierWave::InvalidParameter)
299
+
300
+ @uploader.file.should be_nil
301
+ @uploader.filename.should be_nil
302
+ @uploader.cache_name.should be_nil
303
+ end
304
+
305
+ it "should raise an error when the original_filename contains invalid characters" do
306
+ running {
307
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/te/st.jpeg')
308
+ }.should raise_error(CarrierWave::InvalidParameter)
309
+ running {
310
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/te??%st.jpeg')
311
+ }.should raise_error(CarrierWave::InvalidParameter)
312
+
313
+ @uploader.file.should be_nil
314
+ @uploader.filename.should be_nil
315
+ @uploader.cache_name.should be_nil
316
+ end
317
+ end
318
+
319
+ describe '#retrieve_from_cache' do
320
+ it "should cache a file" do
321
+ @uploader.retrieve_from_cache('20071201-1234-345-2255/test.jpeg')
322
+ @uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
323
+ end
324
+
325
+ it "should not overwrite a file that has already been cached" do
326
+ @uploader.retrieve_from_cache('20071201-1234-345-2255/test.jpeg')
327
+ @uploader.retrieve_from_cache('20071201-1234-345-2255/bork.txt')
328
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpeg')
329
+ end
330
+
331
+ it "should do nothing when the cache_id has an invalid format" do
332
+ @uploader.retrieve_from_cache('12345/test.jpeg')
333
+ @uploader.file.should be_nil
334
+ @uploader.filename.should be_nil
335
+ @uploader.cache_name.should be_nil
336
+ end
337
+
338
+ it "should do nothing when the filename contains invalid characters" do
339
+ @uploader.retrieve_from_cache('20071201-1234-345-2255/te??%st.jpeg')
340
+ @uploader.file.should be_nil
341
+ @uploader.filename.should be_nil
342
+ @uploader.cache_name.should be_nil
343
+ end
344
+ end
345
+
346
+ describe '#store!' do
347
+ before do
348
+ @file = File.open(file_path('test.jpg'))
349
+
350
+ @stored_file = mock('a stored file')
351
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
352
+ @stored_file.stub!(:url).and_return('http://www.example.com')
353
+ @stored_file.stub!(:identifier).and_return('this-is-me')
354
+
355
+ @uploader_class.storage.stub!(:store!).and_return(@stored_file)
356
+ end
357
+
358
+ it "should set the current path" do
359
+ @uploader.store!(@file)
360
+ @uploader.current_path.should == '/path/to/somewhere'
361
+ end
362
+
363
+ it "should set the url" do
364
+ @uploader.store!(@file)
365
+ @uploader.url.should == 'http://www.example.com'
366
+ end
367
+
368
+ it "should set the identifier" do
369
+ @uploader.store!(@file)
370
+ @uploader.identifier.should == 'this-is-me'
371
+ end
372
+
373
+ it "should, if a file is given as argument, cache that file" do
374
+ @uploader.should_receive(:cache!).with(@file)
375
+ @uploader.store!(@file)
376
+ end
377
+
378
+ it "should, if a files is given as an argument and use_cache is false, not cache that file" do
379
+ CarrierWave.config[:use_cache] = false
380
+ @uploader.should_not_receive(:cache!)
381
+ @uploader.store!(@file)
382
+ CarrierWave.config[:use_cache] = true
383
+ end
384
+
385
+ it "should use a previously cached file if no argument is given" do
386
+ @uploader.should_not_receive(:cache!)
387
+ @uploader.store!
388
+ end
389
+
390
+ it "should instruct the storage engine to store the file" do
391
+ @uploader.cache!(@file)
392
+ @uploader_class.storage.should_receive(:store!).with(@uploader, @uploader.file).and_return(:monkey)
393
+ @uploader.store!
394
+ end
395
+
396
+ it "should reset the cache_name" do
397
+ @uploader.cache!(@file)
398
+ @uploader.store!
399
+ @uploader.cache_name.should be_nil
400
+ end
401
+
402
+ it "should cache the result given by the storage engine" do
403
+ @uploader.store!(@file)
404
+ @uploader.file.should == @stored_file
405
+ end
406
+ end
407
+
408
+ describe '#retrieve_from_store!' do
409
+ before do
410
+ @stored_file = mock('a stored file')
411
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
412
+ @stored_file.stub!(:url).and_return('http://www.example.com')
413
+ @stored_file.stub!(:identifier).and_return('this-is-me')
414
+
415
+ @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
416
+ end
417
+
418
+ it "should set the current path" do
419
+ @uploader.retrieve_from_store!('monkey.txt')
420
+ @uploader.current_path.should == '/path/to/somewhere'
421
+ end
422
+
423
+ it "should set the url" do
424
+ @uploader.retrieve_from_store!('monkey.txt')
425
+ @uploader.url.should == 'http://www.example.com'
426
+ end
427
+
428
+ it "should set the identifier" do
429
+ @uploader.retrieve_from_store!('monkey.txt')
430
+ @uploader.identifier.should == 'this-is-me'
431
+ end
432
+
433
+ it "should instruct the storage engine to retrieve the file and store the result" do
434
+ @uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
435
+ @uploader.retrieve_from_store!('monkey.txt')
436
+ @uploader.file.should == @stored_file
437
+ end
438
+
439
+ it "should overwrite a file that has already been cached" do
440
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
441
+ @uploader.retrieve_from_store!('bork.txt')
442
+ @uploader.file.should == @stored_file
443
+ end
444
+ end
445
+
446
+ describe '#retrieve_from_store' do
447
+ before do
448
+ @stored_file = mock('a stored file')
449
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
450
+ @stored_file.stub!(:url).and_return('http://www.example.com')
451
+ @stored_file.stub!(:identifier).and_return('this-is-me')
452
+
453
+ @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
454
+ end
455
+
456
+ it "should set the current path" do
457
+ @uploader.retrieve_from_store('monkey.txt')
458
+ @uploader.current_path.should == '/path/to/somewhere'
459
+ end
460
+
461
+ it "should set the url" do
462
+ @uploader.retrieve_from_store('monkey.txt')
463
+ @uploader.url.should == 'http://www.example.com'
464
+ end
465
+
466
+ it "should set the identifier" do
467
+ @uploader.retrieve_from_store('monkey.txt')
468
+ @uploader.identifier.should == 'this-is-me'
469
+ end
470
+
471
+ it "should instruct the storage engine to retrieve the file and store the result" do
472
+ @uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
473
+ @uploader.retrieve_from_store('monkey.txt')
474
+ @uploader.file.should == @stored_file
475
+ end
476
+
477
+ it "should not overwrite a file that has already been cached" do
478
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
479
+ @uploader.retrieve_from_store('bork.txt')
480
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpeg')
481
+ end
482
+ end
483
+
484
+ describe 'with a version' do
485
+ before do
486
+ @uploader_class.version(:thumb)
487
+ end
488
+
489
+ describe '#cache!' do
490
+
491
+ before do
492
+ CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
493
+ end
494
+
495
+ it "should suffix the version's store_dir" do
496
+ @uploader.cache!(File.open(file_path('test.jpg')))
497
+ @uploader.store_dir.should == 'public/uploads'
498
+ @uploader.thumb.store_dir.should == 'public/uploads/thumb'
499
+ end
500
+
501
+ it "should move it to the tmp dir with the filename prefixed" do
502
+ @uploader.cache!(File.open(file_path('test.jpg')))
503
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
504
+ @uploader.thumb.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/thumb_test.jpg')
505
+ @uploader.file.exists?.should be_true
506
+ @uploader.thumb.file.exists?.should be_true
507
+ end
508
+ end
509
+
510
+ describe '#retrieve_from_cache!' do
511
+ it "should set the path to the tmp dir" do
512
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
513
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
514
+ @uploader.thumb.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/thumb_test.jpg')
515
+ end
516
+
517
+ it "should suffix the version's store_dir" do
518
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
519
+ @uploader.store_dir.should == 'public/uploads'
520
+ @uploader.thumb.store_dir.should == 'public/uploads/thumb'
521
+ end
522
+ end
523
+
524
+ describe '#store!' do
525
+ before do
526
+ @file = File.open(file_path('test.jpg'))
527
+
528
+ @stored_file = mock('a stored file')
529
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
530
+ @stored_file.stub!(:url).and_return('http://www.example.com')
531
+
532
+ @uploader_class.storage.stub!(:store!).and_return(@stored_file)
533
+ end
534
+
535
+ after do
536
+ CarrierWave.config[:use_cache] = true
537
+ end
538
+
539
+ it "should set the current path for the version" do
540
+ pending "find a decent way to spec this"
541
+ @uploader.store!(@file)
542
+ @uploader.current_path.should == '/path/to/somewhere'
543
+ @uploader.thumb.current_path.should == '/path/to/somewhere'
544
+ end
545
+
546
+ it "should set the url" do
547
+ pending "find a decent way to spec this"
548
+ @uploader.store!(@file)
549
+ @uploader.url.should == 'http://www.example.com'
550
+ end
551
+
552
+ it "should, if a file is given as argument, suffix the version's store_dir" do
553
+ @uploader.store!(@file)
554
+ @uploader.store_dir.should == 'public/uploads'
555
+ @uploader.thumb.store_dir.should == 'public/uploads/thumb'
556
+ end
557
+
558
+ it "should, if a files is given as an argument and use_cache is false, suffix the version's store_dir" do
559
+ CarrierWave.config[:use_cache] = false
560
+ @uploader.store!(@file)
561
+ @uploader.store_dir.should == 'public/uploads'
562
+ @uploader.thumb.store_dir.should == 'public/uploads/thumb'
563
+ end
564
+
565
+ end
566
+
567
+ describe '#retrieve_from_store!' do
568
+ before do
569
+ @stored_file = mock('a stored file')
570
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
571
+ @stored_file.stub!(:url).and_return('http://www.example.com')
572
+
573
+ @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
574
+ end
575
+
576
+ it "should set the current path" do
577
+ @uploader.retrieve_from_store!('monkey.txt')
578
+ @uploader.current_path.should == '/path/to/somewhere'
579
+ end
580
+
581
+ it "should set the url" do
582
+ @uploader.retrieve_from_store!('monkey.txt')
583
+ @uploader.url.should == 'http://www.example.com'
584
+ end
585
+
586
+ it "should pass the identifier to the storage engine" do
587
+ @uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
588
+ @uploader.retrieve_from_store!('monkey.txt')
589
+ @uploader.file.should == @stored_file
590
+ end
591
+
592
+ it "should not set the filename" do
593
+ @uploader.retrieve_from_store!('monkey.txt')
594
+ @uploader.filename.should be_nil
595
+ end
596
+ end
597
+ end
598
+
599
+ describe 'with an overridden, reversing, filename' do
600
+ before do
601
+ @uploader_class.class_eval do
602
+ def filename
603
+ super.reverse unless super.blank?
604
+ end
605
+ end
606
+ end
607
+
608
+ describe '#cache!' do
609
+
610
+ before do
611
+ CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
612
+ end
613
+
614
+ it "should set the filename to the file's reversed filename" do
615
+ @uploader.cache!(File.open(file_path('test.jpg')))
616
+ @uploader.filename.should == "gpj.tset"
617
+ end
618
+
619
+ it "should move it to the tmp dir with the filename unreversed" do
620
+ @uploader.cache!(File.open(file_path('test.jpg')))
621
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
622
+ @uploader.file.exists?.should be_true
623
+ end
624
+ end
625
+
626
+ describe '#retrieve_from_cache!' do
627
+ it "should set the path to the tmp dir" do
628
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
629
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
630
+ end
631
+
632
+ it "should set the filename to the reversed name of the file" do
633
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
634
+ @uploader.filename.should == "gpj.tset"
635
+ end
636
+ end
637
+
638
+ describe '#store!' do
639
+ before do
640
+ @file = File.open(file_path('test.jpg'))
641
+
642
+ @stored_file = mock('a stored file')
643
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
644
+ @stored_file.stub!(:url).and_return('http://www.example.com')
645
+
646
+ @uploader_class.storage.stub!(:store!).and_return(@stored_file)
647
+ end
648
+
649
+ after do
650
+ CarrierWave.config[:use_cache] = true
651
+ end
652
+
653
+ it "should set the current path" do
654
+ @uploader.store!(@file)
655
+ @uploader.current_path.should == '/path/to/somewhere'
656
+ end
657
+
658
+ it "should set the url" do
659
+ @uploader.store!(@file)
660
+ @uploader.url.should == 'http://www.example.com'
661
+ end
662
+
663
+ it "should, if a file is given as argument, reverse the filename" do
664
+ @uploader.store!(@file)
665
+ @uploader.filename.should == 'gpj.tset'
666
+ end
667
+
668
+ it "should, if a files is given as an argument and use_cache is false, reverse the filename" do
669
+ CarrierWave.config[:use_cache] = false
670
+ @uploader.store!(@file)
671
+ @uploader.filename.should == 'gpj.tset'
672
+ end
673
+
674
+ end
675
+
676
+ describe '#retrieve_from_store!' do
677
+ before do
678
+ @stored_file = mock('a stored file')
679
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
680
+ @stored_file.stub!(:url).and_return('http://www.example.com')
681
+
682
+ @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
683
+ end
684
+
685
+ it "should set the current path" do
686
+ @uploader.retrieve_from_store!('monkey.txt')
687
+ @uploader.current_path.should == '/path/to/somewhere'
688
+ end
689
+
690
+ it "should set the url" do
691
+ @uploader.retrieve_from_store!('monkey.txt')
692
+ @uploader.url.should == 'http://www.example.com'
693
+ end
694
+
695
+ it "should pass the identifier to the storage engine" do
696
+ @uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
697
+ @uploader.retrieve_from_store!('monkey.txt')
698
+ @uploader.file.should == @stored_file
699
+ end
700
+
701
+ it "should not set the filename" do
702
+ @uploader.retrieve_from_store!('monkey.txt')
703
+ @uploader.filename.should be_nil
704
+ end
705
+ end
706
+
707
+ end
708
+
709
+ end