jnicklas-carrierwave 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,618 @@
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 '#move_to' do
172
+
173
+ after do
174
+ FileUtils.rm(file_path('gurr.png'))
175
+ end
176
+
177
+ it "should be moved to the correct location" do
178
+ @sanitized_file.move_to(file_path('gurr.png'))
179
+
180
+ File.exists?( file_path('gurr.png') ).should be_true
181
+ end
182
+
183
+ it "should have changed its path when moved" do
184
+ @sanitized_file.move_to(file_path('gurr.png'))
185
+ @sanitized_file.path.should == file_path('gurr.png')
186
+ end
187
+
188
+ it "should have changed its filename when moved" do
189
+ @sanitized_file.move_to(file_path('gurr.png'))
190
+ @sanitized_file.filename.should == 'gurr.png'
191
+ end
192
+
193
+ it "should have changed its basename when moved" do
194
+ @sanitized_file.move_to(file_path('gurr.png'))
195
+ @sanitized_file.basename.should == 'gurr'
196
+ end
197
+
198
+ it "should have changed its extension when moved" do
199
+ @sanitized_file.move_to(file_path('gurr.png'))
200
+ @sanitized_file.extension.should == 'png'
201
+ end
202
+
203
+ describe "with permissions set" do
204
+ before do
205
+ @sanitized_file.options[:permissions] = 0755
206
+ end
207
+
208
+ it "should set the right permissions" do
209
+ @sanitized_file.move_to(file_path('gurr.png'))
210
+ @sanitized_file.should have_permissions(0755)
211
+ end
212
+ end
213
+
214
+ end
215
+
216
+ describe '#copy_to' do
217
+
218
+ after do
219
+ FileUtils.rm(file_path('gurr.png'))
220
+ end
221
+
222
+ it "should be copied to the correct location" do
223
+ @sanitized_file.copy_to(file_path('gurr.png'))
224
+
225
+ File.exists?( file_path('gurr.png') ).should be_true
226
+
227
+ file_path('gurr.png').should be_identical_to(file_path('llama.jpg'))
228
+ end
229
+
230
+ it "should not have changed its path when copied" do
231
+ running { @sanitized_file.copy_to(file_path('gurr.png')) }.should_not change(@sanitized_file, :path)
232
+ end
233
+
234
+ it "should not have changed its filename when copied" do
235
+ running { @sanitized_file.copy_to(file_path('gurr.png')) }.should_not change(@sanitized_file, :filename)
236
+ end
237
+
238
+ it "should return an object of the same class when copied" do
239
+ new_file = @sanitized_file.copy_to(file_path('gurr.png'))
240
+ new_file.should be_an_instance_of(@sanitized_file.class)
241
+ end
242
+
243
+ it "should adjust the path of the object that is returned when copied" do
244
+ new_file = @sanitized_file.copy_to(file_path('gurr.png'))
245
+ new_file.path.should == file_path('gurr.png')
246
+ end
247
+
248
+ it "should adjust the filename of the object that is returned when copied" do
249
+ new_file = @sanitized_file.copy_to(file_path('gurr.png'))
250
+ new_file.filename.should == 'gurr.png'
251
+ end
252
+
253
+ it "should adjust the basename of the object that is returned when copied" do
254
+ new_file = @sanitized_file.copy_to(file_path('gurr.png'))
255
+ new_file.basename.should == 'gurr'
256
+ end
257
+
258
+ it "should adjust the extension of the object that is returned when copied" do
259
+ new_file = @sanitized_file.copy_to(file_path('gurr.png'))
260
+ new_file.extension.should == 'png'
261
+ end
262
+
263
+ describe "with permissions set" do
264
+ before do
265
+ @sanitized_file.options[:permissions] = 0755
266
+ end
267
+
268
+ it "should set the right permissions" do
269
+ new_file = @sanitized_file.copy_to(file_path('gurr.png'))
270
+ new_file.should have_permissions(0755)
271
+ end
272
+ end
273
+
274
+ end
275
+
276
+ end
277
+
278
+ shared_examples_for "all valid sanitized files that are stored on disk" do
279
+ describe '#move_to' do
280
+ it "should not raise an error when moved to its own location" do
281
+ running { @sanitized_file.move_to(@sanitized_file.path) }.should_not raise_error
282
+ end
283
+
284
+ it "should remove the original file" do
285
+ original_path = @sanitized_file.path
286
+ @sanitized_file.move_to(public_path('blah.txt'))
287
+ File.exist?(original_path).should be_false
288
+ end
289
+ end
290
+
291
+ describe '#copy_to' do
292
+ it "should return a new instance when copied to its own location" do
293
+ running {
294
+ new_file = @sanitized_file.copy_to(@sanitized_file.path)
295
+ new_file.should be_an_instance_of(@sanitized_file.class)
296
+ }.should_not raise_error
297
+ end
298
+
299
+ it "should not remove the original file" do
300
+ new_file = @sanitized_file.copy_to(public_path('blah.txt'))
301
+ File.exist?(@sanitized_file.path).should be_true
302
+ File.exist?(new_file.path).should be_true
303
+ end
304
+ end
305
+
306
+ describe '#exists?' do
307
+ it "should be true" do
308
+ @sanitized_file.exists?.should be_true
309
+ end
310
+ end
311
+
312
+ describe '#delete' do
313
+ it "should remove it from the filesystem" do
314
+ File.exists?(@sanitized_file.path).should be_true
315
+ @sanitized_file.delete
316
+ File.exists?(@sanitized_file.path).should be_false
317
+ end
318
+ end
319
+ end
320
+
321
+ describe "with a valid Hash" do
322
+ before do
323
+ @hash = {
324
+ "tempfile" => stub_merb_tempfile('llama.jpg'),
325
+ "filename" => "llama.jpg",
326
+ "content_type" => 'image/jpeg'
327
+ }
328
+ @sanitized_file = CarrierWave::SanitizedFile.new(@hash)
329
+ end
330
+
331
+ it_should_behave_like "all valid sanitized files"
332
+
333
+ it_should_behave_like "all valid sanitized files that are stored on disk"
334
+
335
+ describe '#path' do
336
+ it "should return the path of the tempfile" do
337
+ @sanitized_file.path.should_not be_nil
338
+ @sanitized_file.path.should == @hash["tempfile"].path
339
+ end
340
+ end
341
+
342
+ describe '#string?' do
343
+ it "should be false" do
344
+ @sanitized_file.string?.should be_false
345
+ end
346
+ end
347
+
348
+ end
349
+
350
+ describe "with a valid Tempfile" do
351
+ before do
352
+ @tempfile = stub_tempfile('llama.jpg', 'image/jpeg')
353
+ @sanitized_file = CarrierWave::SanitizedFile.new(@tempfile)
354
+ end
355
+
356
+ it_should_behave_like "all valid sanitized files"
357
+
358
+ it_should_behave_like "all valid sanitized files that are stored on disk"
359
+
360
+ describe '#string?' do
361
+ it "should be false" do
362
+ @sanitized_file.string?.should be_false
363
+ end
364
+ end
365
+
366
+ describe '#path' do
367
+ it "should return the path of the tempfile" do
368
+ @sanitized_file.path.should_not be_nil
369
+ @sanitized_file.path.should == @tempfile.path
370
+ end
371
+ end
372
+
373
+ end
374
+
375
+ describe "with a valid StringIO" do
376
+ before do
377
+ @sanitized_file = CarrierWave::SanitizedFile.new(stub_stringio('llama.jpg', 'image/jpeg'))
378
+ end
379
+
380
+ it_should_behave_like "all valid sanitized files"
381
+
382
+ describe '#exists?' do
383
+ it "should be false" do
384
+ @sanitized_file.exists?.should be_false
385
+ end
386
+ end
387
+
388
+ describe '#string?' do
389
+ it "should be false" do
390
+ @sanitized_file.string?.should be_false
391
+ end
392
+ end
393
+
394
+ describe '#path' do
395
+ it "should be nil" do
396
+ @sanitized_file.path.should be_nil
397
+ end
398
+ end
399
+
400
+ describe '#delete' do
401
+ it "should not raise an error" do
402
+ running { @sanitized_file.delete }.should_not raise_error
403
+ end
404
+ end
405
+
406
+ end
407
+
408
+ describe "with a valid File object" do
409
+ before do
410
+ FileUtils.cp(file_path('test.jpg'), file_path('llama.jpg'))
411
+ @sanitized_file = CarrierWave::SanitizedFile.new(stub_file('llama.jpg', 'image/jpeg'))
412
+ @sanitized_file.should_not be_empty
413
+ end
414
+
415
+ it_should_behave_like "all valid sanitized files"
416
+
417
+ it_should_behave_like "all valid sanitized files that are stored on disk"
418
+
419
+ describe '#string?' do
420
+ it "should be false" do
421
+ @sanitized_file.string?.should be_false
422
+ end
423
+ end
424
+
425
+ describe '#path' do
426
+ it "should return the path of the file" do
427
+ @sanitized_file.path.should_not be_nil
428
+ @sanitized_file.path.should == file_path('llama.jpg')
429
+ end
430
+ end
431
+
432
+ end
433
+
434
+ describe "with a valid path" do
435
+ before do
436
+ FileUtils.cp(file_path('test.jpg'), file_path('llama.jpg'))
437
+ @sanitized_file = CarrierWave::SanitizedFile.new(file_path('llama.jpg'))
438
+ @sanitized_file.should_not be_empty
439
+ end
440
+
441
+ it_should_behave_like "all valid sanitized files"
442
+
443
+ it_should_behave_like "all valid sanitized files that are stored on disk"
444
+
445
+ describe '#string?' do
446
+ it "should be true" do
447
+ @sanitized_file.string?.should be_true
448
+ end
449
+ end
450
+
451
+ describe '#path' do
452
+ it "should return the path of the file" do
453
+ @sanitized_file.path.should_not be_nil
454
+ @sanitized_file.path.should == file_path('llama.jpg')
455
+ end
456
+ end
457
+
458
+ end
459
+
460
+ describe "with a valid Pathname" do
461
+ before do
462
+ FileUtils.copy_file(file_path('test.jpg'), file_path('llama.jpg'))
463
+ @sanitized_file = CarrierWave::SanitizedFile.new(Pathname.new(file_path('llama.jpg')))
464
+ @sanitized_file.should_not be_empty
465
+ end
466
+
467
+ it_should_behave_like "all valid sanitized files"
468
+
469
+ it_should_behave_like "all valid sanitized files that are stored on disk"
470
+
471
+ describe '#string?' do
472
+ it "should be true" do
473
+ @sanitized_file.string?.should be_true
474
+ end
475
+ end
476
+
477
+ describe '#path' do
478
+ it "should return the path of the file" do
479
+ @sanitized_file.path.should_not be_nil
480
+ @sanitized_file.path.should == file_path('llama.jpg')
481
+ end
482
+ end
483
+
484
+ end
485
+
486
+ describe "that is empty" do
487
+ before do
488
+ @empty = CarrierWave::SanitizedFile.new(nil)
489
+ end
490
+
491
+ describe '#empty?' do
492
+ it "should be true" do
493
+ @empty.should be_empty
494
+ end
495
+ end
496
+
497
+ describe '#exists?' do
498
+ it "should be false" do
499
+ @empty.exists?.should be_false
500
+ end
501
+ end
502
+
503
+ describe '#string?' do
504
+ it "should be false" do
505
+ @empty.string?.should be_false
506
+ end
507
+ end
508
+
509
+ describe '#size' do
510
+ it "should be zero" do
511
+ @empty.size.should be_zero
512
+ end
513
+ end
514
+
515
+ describe '#path' do
516
+ it "should be nil" do
517
+ @empty.path.should be_nil
518
+ end
519
+ end
520
+
521
+ describe '#original_filename' do
522
+ it "should be nil" do
523
+ @empty.original_filename.should be_nil
524
+ end
525
+ end
526
+
527
+ describe '#filename' do
528
+ it "should be nil" do
529
+ @empty.filename.should be_nil
530
+ end
531
+ end
532
+
533
+ describe '#basename' do
534
+ it "should be nil" do
535
+ @empty.basename.should be_nil
536
+ end
537
+ end
538
+
539
+ describe '#extension' do
540
+ it "should be nil" do
541
+ @empty.extension.should be_nil
542
+ end
543
+ end
544
+
545
+ describe '#delete' do
546
+ it "should not raise an error" do
547
+ running { @empty.delete }.should_not raise_error
548
+ end
549
+ end
550
+ end
551
+
552
+ describe "that is an empty string" do
553
+ before do
554
+ @empty = CarrierWave::SanitizedFile.new("")
555
+ end
556
+
557
+ describe '#empty?' do
558
+ it "should be true" do
559
+ @empty.should be_empty
560
+ end
561
+ end
562
+
563
+ describe '#exists?' do
564
+ it "should be false" do
565
+ @empty.exists?.should be_false
566
+ end
567
+ end
568
+
569
+ describe '#string?' do
570
+ it "should be false" do
571
+ @empty.string?.should be_false
572
+ end
573
+ end
574
+
575
+ describe '#size' do
576
+ it "should be zero" do
577
+ @empty.size.should be_zero
578
+ end
579
+ end
580
+
581
+ describe '#path' do
582
+ it "should be nil" do
583
+ @empty.path.should be_nil
584
+ end
585
+ end
586
+
587
+ describe '#original_filename' do
588
+ it "should be nil" do
589
+ @empty.original_filename.should be_nil
590
+ end
591
+ end
592
+
593
+ describe '#filename' do
594
+ it "should be nil" do
595
+ @empty.filename.should be_nil
596
+ end
597
+ end
598
+
599
+ describe '#basename' do
600
+ it "should be nil" do
601
+ @empty.basename.should be_nil
602
+ end
603
+ end
604
+
605
+ describe '#extension' do
606
+ it "should be nil" do
607
+ @empty.extension.should be_nil
608
+ end
609
+ end
610
+
611
+ describe '#delete' do
612
+ it "should not raise an error" do
613
+ running { @empty.delete }.should_not raise_error
614
+ end
615
+ end
616
+ end
617
+
618
+ end