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,624 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe CarrierWave::SanitizedFile do
4
+
5
+ include SanitizedFileSpecHelper
6
+
7
+ before do
8
+ unless File.exists?(file_path('llama.jpg'))
9
+ FileUtils.cp(file_path('test.jpg'), file_path('llama.jpg'))
10
+ end
11
+ end
12
+
13
+ after(:all) do
14
+ if File.exists?(file_path('llama.jpg'))
15
+ FileUtils.rm(file_path('llama.jpg'))
16
+ end
17
+ FileUtils.rm_rf(public_path)
18
+ end
19
+
20
+ describe '#empty?' do
21
+
22
+ it "should be empty for nil" do
23
+ @sanitized_file = CarrierWave::SanitizedFile.new(nil)
24
+ @sanitized_file.should be_empty
25
+ end
26
+
27
+ it "should be empty for an empty string" do
28
+ @sanitized_file = CarrierWave::SanitizedFile.new("")
29
+ @sanitized_file.should be_empty
30
+ end
31
+
32
+ it "should be empty for an empty StringIO" do
33
+ @sanitized_file = CarrierWave::SanitizedFile.new(StringIO.new(""))
34
+ @sanitized_file.should be_empty
35
+ end
36
+
37
+ it "should be empty for a file with a zero size" do
38
+ empty_file = mock('emptyfile')
39
+ empty_file.should_receive(:size).at_least(:once).and_return(0)
40
+ @sanitized_file = CarrierWave::SanitizedFile.new(empty_file)
41
+ @sanitized_file.should be_empty
42
+ end
43
+
44
+ end
45
+
46
+ describe '#original_filename' do
47
+ it "should default to the original_filename" do
48
+ file = mock('file', :original_filename => 'llama.jpg')
49
+ sanitized_file = CarrierWave::SanitizedFile.new(file)
50
+ sanitized_file.original_filename.should == "llama.jpg"
51
+ end
52
+
53
+ it "should defer to the base name of the path if original_filename is unavailable" do
54
+ file = mock('file', :path => '/path/to/test.jpg')
55
+ sanitized_file = CarrierWave::SanitizedFile.new(file)
56
+ sanitized_file.original_filename.should == "test.jpg"
57
+ end
58
+
59
+ it "should be nil otherwise" do
60
+ file = mock('file')
61
+ sanitized_file = CarrierWave::SanitizedFile.new(file)
62
+ sanitized_file.original_filename.should be_nil
63
+ end
64
+ end
65
+
66
+ describe '#basename' do
67
+ it "should return the basename for complicated extensions" do
68
+ @sanitized_file = CarrierWave::SanitizedFile.new(file_path('complex.filename.tar.gz'))
69
+ @sanitized_file.basename.should == "complex.filename"
70
+ end
71
+
72
+ it "should be the filename if the file has no extension" do
73
+ @sanitized_file = CarrierWave::SanitizedFile.new(file_path('complex'))
74
+ @sanitized_file.basename.should == "complex"
75
+ end
76
+ end
77
+
78
+ describe '#extension' do
79
+ it "should return the extension for complicated extensions" do
80
+ @sanitized_file = CarrierWave::SanitizedFile.new(file_path('complex.filename.tar.gz'))
81
+ @sanitized_file.extension.should == "tar.gz"
82
+ end
83
+
84
+ it "should be an empty string if the file has no extension" do
85
+ @sanitized_file = CarrierWave::SanitizedFile.new(file_path('complex'))
86
+ @sanitized_file.extension.should == ""
87
+ end
88
+ end
89
+
90
+ describe '#filename' do
91
+
92
+ before do
93
+ @sanitized_file = CarrierWave::SanitizedFile.new(nil)
94
+ end
95
+
96
+ it "should default to the original filename if it is valid" do
97
+ @sanitized_file.should_receive(:original_filename).at_least(:once).and_return("llama.jpg")
98
+ @sanitized_file.filename.should == "llama.jpg"
99
+ end
100
+
101
+ it "should remove illegal characters from a filename" do
102
+ @sanitized_file.should_receive(:original_filename).at_least(:once).and_return("test-s,%&m#st?.jpg")
103
+ @sanitized_file.filename.should == "test-s___m_st_.jpg"
104
+ end
105
+
106
+ it "should remove slashes from the filename" do
107
+ @sanitized_file.should_receive(:original_filename).at_least(:once).and_return("../../very_tricky/foo.bar")
108
+ @sanitized_file.filename.should_not =~ /[\\\/]/
109
+ end
110
+
111
+ it "should remove illegal characters if there is no extension" do
112
+ @sanitized_file.should_receive(:original_filename).at_least(:once).and_return('`*foo')
113
+ @sanitized_file.filename.should == "__foo"
114
+ end
115
+
116
+ it "should remove the path prefix on Windows" do
117
+ @sanitized_file.should_receive(:original_filename).at_least(:once).and_return('c:\temp\foo.txt')
118
+ @sanitized_file.filename.should == "foo.txt"
119
+ end
120
+
121
+ it "should make sure the *nix directory thingies can't be used as filenames" do
122
+ @sanitized_file.should_receive(:original_filename).at_least(:once).and_return(".")
123
+ @sanitized_file.filename.should == "_."
124
+ end
125
+
126
+ it "should downcase uppercase filenames" do
127
+ @sanitized_file.should_receive(:original_filename).at_least(:once).and_return("DSC4056.JPG")
128
+ @sanitized_file.filename.should == "dsc4056.jpg"
129
+ end
130
+
131
+ end
132
+
133
+ shared_examples_for "all valid sanitized files" do
134
+
135
+ describe '#empty?' do
136
+ it "should not be empty" do
137
+ @sanitized_file.should_not be_empty
138
+ end
139
+ end
140
+
141
+ describe '#original_filename' do
142
+ it "should return the original filename" do
143
+ @sanitized_file.original_filename.should == "llama.jpg"
144
+ end
145
+ end
146
+
147
+ describe '#filename' do
148
+ it "should return the filename" do
149
+ @sanitized_file.filename.should == "llama.jpg"
150
+ end
151
+ end
152
+
153
+ describe '#basename' do
154
+ it "should return the basename" do
155
+ @sanitized_file.basename.should == "llama"
156
+ end
157
+ end
158
+
159
+ describe '#extension' do
160
+ it "should return the extension" do
161
+ @sanitized_file.extension.should == "jpg"
162
+ end
163
+ end
164
+
165
+ describe "#read" do
166
+ it "should return the contents of the file" do
167
+ @sanitized_file.read.should == "this is stuff"
168
+ end
169
+ end
170
+
171
+ describe "#size" do
172
+ it "should return the size of the file" do
173
+ @sanitized_file.size.should == 13
174
+ end
175
+ end
176
+
177
+ describe '#move_to' do
178
+
179
+ after do
180
+ FileUtils.rm(file_path('gurr.png'))
181
+ end
182
+
183
+ it "should be moved to the correct location" do
184
+ @sanitized_file.move_to(file_path('gurr.png'))
185
+
186
+ File.exists?( file_path('gurr.png') ).should be_true
187
+ end
188
+
189
+ it "should have changed its path when moved" do
190
+ @sanitized_file.move_to(file_path('gurr.png'))
191
+ @sanitized_file.path.should == file_path('gurr.png')
192
+ end
193
+
194
+ it "should have changed its filename when moved" do
195
+ @sanitized_file.move_to(file_path('gurr.png'))
196
+ @sanitized_file.filename.should == 'gurr.png'
197
+ end
198
+
199
+ it "should have changed its basename when moved" do
200
+ @sanitized_file.move_to(file_path('gurr.png'))
201
+ @sanitized_file.basename.should == 'gurr'
202
+ end
203
+
204
+ it "should have changed its extension when moved" do
205
+ @sanitized_file.move_to(file_path('gurr.png'))
206
+ @sanitized_file.extension.should == 'png'
207
+ end
208
+
209
+ describe "with permissions set" do
210
+ before do
211
+ @sanitized_file.options[:permissions] = 0755
212
+ end
213
+
214
+ it "should set the right permissions" do
215
+ @sanitized_file.move_to(file_path('gurr.png'))
216
+ @sanitized_file.should have_permissions(0755)
217
+ end
218
+ end
219
+
220
+ end
221
+
222
+ describe '#copy_to' do
223
+
224
+ after do
225
+ FileUtils.rm(file_path('gurr.png'))
226
+ end
227
+
228
+ it "should be copied to the correct location" do
229
+ @sanitized_file.copy_to(file_path('gurr.png'))
230
+
231
+ File.exists?( file_path('gurr.png') ).should be_true
232
+
233
+ file_path('gurr.png').should be_identical_to(file_path('llama.jpg'))
234
+ end
235
+
236
+ it "should not have changed its path when copied" do
237
+ running { @sanitized_file.copy_to(file_path('gurr.png')) }.should_not change(@sanitized_file, :path)
238
+ end
239
+
240
+ it "should not have changed its filename when copied" do
241
+ running { @sanitized_file.copy_to(file_path('gurr.png')) }.should_not change(@sanitized_file, :filename)
242
+ end
243
+
244
+ it "should return an object of the same class when copied" do
245
+ new_file = @sanitized_file.copy_to(file_path('gurr.png'))
246
+ new_file.should be_an_instance_of(@sanitized_file.class)
247
+ end
248
+
249
+ it "should adjust the path of the object that is returned when copied" do
250
+ new_file = @sanitized_file.copy_to(file_path('gurr.png'))
251
+ new_file.path.should == file_path('gurr.png')
252
+ end
253
+
254
+ it "should adjust the filename of the object that is returned when copied" do
255
+ new_file = @sanitized_file.copy_to(file_path('gurr.png'))
256
+ new_file.filename.should == 'gurr.png'
257
+ end
258
+
259
+ it "should adjust the basename of the object that is returned when copied" do
260
+ new_file = @sanitized_file.copy_to(file_path('gurr.png'))
261
+ new_file.basename.should == 'gurr'
262
+ end
263
+
264
+ it "should adjust the extension of the object that is returned when copied" do
265
+ new_file = @sanitized_file.copy_to(file_path('gurr.png'))
266
+ new_file.extension.should == 'png'
267
+ end
268
+
269
+ describe "with permissions set" do
270
+ before do
271
+ @sanitized_file.options[:permissions] = 0755
272
+ end
273
+
274
+ it "should set the right permissions" do
275
+ new_file = @sanitized_file.copy_to(file_path('gurr.png'))
276
+ new_file.should have_permissions(0755)
277
+ end
278
+ end
279
+
280
+ end
281
+
282
+ end
283
+
284
+ shared_examples_for "all valid sanitized files that are stored on disk" do
285
+ describe '#move_to' do
286
+ it "should not raise an error when moved to its own location" do
287
+ running { @sanitized_file.move_to(@sanitized_file.path) }.should_not raise_error
288
+ end
289
+
290
+ it "should remove the original file" do
291
+ original_path = @sanitized_file.path
292
+ @sanitized_file.move_to(public_path('blah.txt'))
293
+ File.exist?(original_path).should be_false
294
+ end
295
+ end
296
+
297
+ describe '#copy_to' do
298
+ it "should return a new instance when copied to its own location" do
299
+ running {
300
+ new_file = @sanitized_file.copy_to(@sanitized_file.path)
301
+ new_file.should be_an_instance_of(@sanitized_file.class)
302
+ }.should_not raise_error
303
+ end
304
+
305
+ it "should not remove the original file" do
306
+ new_file = @sanitized_file.copy_to(public_path('blah.txt'))
307
+ File.exist?(@sanitized_file.path).should be_true
308
+ File.exist?(new_file.path).should be_true
309
+ end
310
+ end
311
+
312
+ describe '#exists?' do
313
+ it "should be true" do
314
+ @sanitized_file.exists?.should be_true
315
+ end
316
+ end
317
+
318
+ describe '#delete' do
319
+ it "should remove it from the filesystem" do
320
+ File.exists?(@sanitized_file.path).should be_true
321
+ @sanitized_file.delete
322
+ File.exists?(@sanitized_file.path).should be_false
323
+ end
324
+ end
325
+ end
326
+
327
+ describe "with a valid Hash" do
328
+ before do
329
+ @hash = {
330
+ "tempfile" => stub_merb_tempfile('llama.jpg'),
331
+ "filename" => "llama.jpg",
332
+ "content_type" => 'image/jpeg'
333
+ }
334
+ @sanitized_file = CarrierWave::SanitizedFile.new(@hash)
335
+ end
336
+
337
+ it_should_behave_like "all valid sanitized files"
338
+
339
+ it_should_behave_like "all valid sanitized files that are stored on disk"
340
+
341
+ describe '#path' do
342
+ it "should return the path of the tempfile" do
343
+ @sanitized_file.path.should_not be_nil
344
+ @sanitized_file.path.should == @hash["tempfile"].path
345
+ end
346
+ end
347
+
348
+ describe '#string?' do
349
+ it "should be false" do
350
+ @sanitized_file.string?.should be_false
351
+ end
352
+ end
353
+
354
+ end
355
+
356
+ describe "with a valid Tempfile" do
357
+ before do
358
+ @tempfile = stub_tempfile('llama.jpg', 'image/jpeg')
359
+ @sanitized_file = CarrierWave::SanitizedFile.new(@tempfile)
360
+ end
361
+
362
+ it_should_behave_like "all valid sanitized files"
363
+
364
+ it_should_behave_like "all valid sanitized files that are stored on disk"
365
+
366
+ describe '#string?' do
367
+ it "should be false" do
368
+ @sanitized_file.string?.should be_false
369
+ end
370
+ end
371
+
372
+ describe '#path' do
373
+ it "should return the path of the tempfile" do
374
+ @sanitized_file.path.should_not be_nil
375
+ @sanitized_file.path.should == @tempfile.path
376
+ end
377
+ end
378
+
379
+ end
380
+
381
+ describe "with a valid StringIO" do
382
+ before do
383
+ @sanitized_file = CarrierWave::SanitizedFile.new(stub_stringio('llama.jpg', 'image/jpeg'))
384
+ end
385
+
386
+ it_should_behave_like "all valid sanitized files"
387
+
388
+ describe '#exists?' do
389
+ it "should be false" do
390
+ @sanitized_file.exists?.should be_false
391
+ end
392
+ end
393
+
394
+ describe '#string?' do
395
+ it "should be false" do
396
+ @sanitized_file.string?.should be_false
397
+ end
398
+ end
399
+
400
+ describe '#path' do
401
+ it "should be nil" do
402
+ @sanitized_file.path.should be_nil
403
+ end
404
+ end
405
+
406
+ describe '#delete' do
407
+ it "should not raise an error" do
408
+ running { @sanitized_file.delete }.should_not raise_error
409
+ end
410
+ end
411
+
412
+ end
413
+
414
+ describe "with a valid File object" do
415
+ before do
416
+ FileUtils.cp(file_path('test.jpg'), file_path('llama.jpg'))
417
+ @sanitized_file = CarrierWave::SanitizedFile.new(stub_file('llama.jpg', 'image/jpeg'))
418
+ @sanitized_file.should_not be_empty
419
+ end
420
+
421
+ it_should_behave_like "all valid sanitized files"
422
+
423
+ it_should_behave_like "all valid sanitized files that are stored on disk"
424
+
425
+ describe '#string?' do
426
+ it "should be false" do
427
+ @sanitized_file.string?.should be_false
428
+ end
429
+ end
430
+
431
+ describe '#path' do
432
+ it "should return the path of the file" do
433
+ @sanitized_file.path.should_not be_nil
434
+ @sanitized_file.path.should == file_path('llama.jpg')
435
+ end
436
+ end
437
+
438
+ end
439
+
440
+ describe "with a valid path" do
441
+ before do
442
+ FileUtils.cp(file_path('test.jpg'), file_path('llama.jpg'))
443
+ @sanitized_file = CarrierWave::SanitizedFile.new(file_path('llama.jpg'))
444
+ @sanitized_file.should_not be_empty
445
+ end
446
+
447
+ it_should_behave_like "all valid sanitized files"
448
+
449
+ it_should_behave_like "all valid sanitized files that are stored on disk"
450
+
451
+ describe '#string?' do
452
+ it "should be true" do
453
+ @sanitized_file.string?.should be_true
454
+ end
455
+ end
456
+
457
+ describe '#path' do
458
+ it "should return the path of the file" do
459
+ @sanitized_file.path.should_not be_nil
460
+ @sanitized_file.path.should == file_path('llama.jpg')
461
+ end
462
+ end
463
+
464
+ end
465
+
466
+ describe "with a valid Pathname" do
467
+ before do
468
+ FileUtils.copy_file(file_path('test.jpg'), file_path('llama.jpg'))
469
+ @sanitized_file = CarrierWave::SanitizedFile.new(Pathname.new(file_path('llama.jpg')))
470
+ @sanitized_file.should_not be_empty
471
+ end
472
+
473
+ it_should_behave_like "all valid sanitized files"
474
+
475
+ it_should_behave_like "all valid sanitized files that are stored on disk"
476
+
477
+ describe '#string?' do
478
+ it "should be true" do
479
+ @sanitized_file.string?.should be_true
480
+ end
481
+ end
482
+
483
+ describe '#path' do
484
+ it "should return the path of the file" do
485
+ @sanitized_file.path.should_not be_nil
486
+ @sanitized_file.path.should == file_path('llama.jpg')
487
+ end
488
+ end
489
+
490
+ end
491
+
492
+ describe "that is empty" do
493
+ before do
494
+ @empty = CarrierWave::SanitizedFile.new(nil)
495
+ end
496
+
497
+ describe '#empty?' do
498
+ it "should be true" do
499
+ @empty.should be_empty
500
+ end
501
+ end
502
+
503
+ describe '#exists?' do
504
+ it "should be false" do
505
+ @empty.exists?.should be_false
506
+ end
507
+ end
508
+
509
+ describe '#string?' do
510
+ it "should be false" do
511
+ @empty.string?.should be_false
512
+ end
513
+ end
514
+
515
+ describe '#size' do
516
+ it "should be zero" do
517
+ @empty.size.should be_zero
518
+ end
519
+ end
520
+
521
+ describe '#path' do
522
+ it "should be nil" do
523
+ @empty.path.should be_nil
524
+ end
525
+ end
526
+
527
+ describe '#original_filename' do
528
+ it "should be nil" do
529
+ @empty.original_filename.should be_nil
530
+ end
531
+ end
532
+
533
+ describe '#filename' do
534
+ it "should be nil" do
535
+ @empty.filename.should be_nil
536
+ end
537
+ end
538
+
539
+ describe '#basename' do
540
+ it "should be nil" do
541
+ @empty.basename.should be_nil
542
+ end
543
+ end
544
+
545
+ describe '#extension' do
546
+ it "should be nil" do
547
+ @empty.extension.should be_nil
548
+ end
549
+ end
550
+
551
+ describe '#delete' do
552
+ it "should not raise an error" do
553
+ running { @empty.delete }.should_not raise_error
554
+ end
555
+ end
556
+ end
557
+
558
+ describe "that is an empty string" do
559
+ before do
560
+ @empty = CarrierWave::SanitizedFile.new("")
561
+ end
562
+
563
+ describe '#empty?' do
564
+ it "should be true" do
565
+ @empty.should be_empty
566
+ end
567
+ end
568
+
569
+ describe '#exists?' do
570
+ it "should be false" do
571
+ @empty.exists?.should be_false
572
+ end
573
+ end
574
+
575
+ describe '#string?' do
576
+ it "should be false" do
577
+ @empty.string?.should be_false
578
+ end
579
+ end
580
+
581
+ describe '#size' do
582
+ it "should be zero" do
583
+ @empty.size.should be_zero
584
+ end
585
+ end
586
+
587
+ describe '#path' do
588
+ it "should be nil" do
589
+ @empty.path.should be_nil
590
+ end
591
+ end
592
+
593
+ describe '#original_filename' do
594
+ it "should be nil" do
595
+ @empty.original_filename.should be_nil
596
+ end
597
+ end
598
+
599
+ describe '#filename' do
600
+ it "should be nil" do
601
+ @empty.filename.should be_nil
602
+ end
603
+ end
604
+
605
+ describe '#basename' do
606
+ it "should be nil" do
607
+ @empty.basename.should be_nil
608
+ end
609
+ end
610
+
611
+ describe '#extension' do
612
+ it "should be nil" do
613
+ @empty.extension.should be_nil
614
+ end
615
+ end
616
+
617
+ describe '#delete' do
618
+ it "should not raise an error" do
619
+ running { @empty.delete }.should_not raise_error
620
+ end
621
+ end
622
+ end
623
+
624
+ end