uploadcolumn 0.3.0

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,1053 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ require 'active_record'
4
+
5
+ require File.join(File.dirname(__FILE__), '../lib/upload_column')
6
+
7
+ ActiveRecord::Base.send(:include, UploadColumn)
8
+
9
+ describe "uploading a file" do
10
+ it "should trigger an _after_upload callback" do
11
+ record = mock('a record')
12
+ record.should_receive(:avatar_after_upload)
13
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), record, :avatar)
14
+ end
15
+ end
16
+
17
+ describe "all uploaded files", :shared => true do
18
+ it "should not be empty" do
19
+ @file.should_not be_empty
20
+ end
21
+
22
+ it "should return the correct filesize" do
23
+ @file.size.should == 87582
24
+ end
25
+
26
+ it "should return the original filename" do
27
+ @file.original_filename.should == "kerb.jpg"
28
+ end
29
+
30
+ it "should return the filename" do
31
+ @file.filename.should == "kerb.jpg"
32
+ end
33
+
34
+ it "should return the basename" do
35
+ @file.basename.should == "kerb"
36
+ end
37
+
38
+ it "should return the extension" do
39
+ @file.extension.should == "jpg"
40
+ end
41
+
42
+ after do
43
+ FileUtils.rm_rf(public_path('*'))
44
+ end
45
+ end
46
+
47
+ describe "an uploaded tempfile" do
48
+
49
+ before do
50
+ @file = UploadColumn::UploadedFile.upload(stub_tempfile('kerb.jpg'))
51
+ end
52
+
53
+ it_should_behave_like "all uploaded files"
54
+
55
+ it "should return the correct path" do
56
+ @file.path.should match_path('public', 'tmp', %r{((\d+\.)+\d+)}, 'kerb.jpg')
57
+ end
58
+
59
+ it "should return the correct relative_path" do
60
+ @file.relative_path.should =~ %r{^tmp/((\d+\.)+\d+)/kerb.jpg}
61
+ end
62
+
63
+ it "should return correct dir" do
64
+ @file.dir.should match_path('public', 'tmp', %r{((\d+\.)+\d+)})
65
+ end
66
+ end
67
+
68
+ describe "an uploaded StringIO" do
69
+
70
+ before do
71
+ @file = UploadColumn::UploadedFile.upload(stub_stringio('kerb.jpg'))
72
+ end
73
+
74
+ it_should_behave_like "all uploaded files"
75
+
76
+ it "should return the correct path" do
77
+ @file.path.should match_path('public', 'tmp', %r{((\d+\.)+\d+)}, 'kerb.jpg')
78
+ end
79
+
80
+ it "should return the correct relative_path" do
81
+ @file.relative_path.should =~ %r{^tmp/((\d+\.)+\d+)/kerb.jpg}
82
+ end
83
+
84
+ it "should return correct dir" do
85
+ @file.dir.should match_path('public', 'tmp', %r{((\d+\.)+\d+)})
86
+ end
87
+ end
88
+
89
+ describe "an uploaded File object" do
90
+
91
+ before do
92
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'))
93
+ end
94
+
95
+ it_should_behave_like "all uploaded files"
96
+
97
+ it "should return the correct path" do
98
+ @file.path.should match_path('public', 'tmp', %r{((\d+\.)+\d+)}, 'kerb.jpg')
99
+ end
100
+
101
+ it "should return the correct relative_path" do
102
+ @file.relative_path.should =~ %r{^tmp/((\d+\.)+\d+)/kerb.jpg}
103
+ end
104
+
105
+ it "should return correct dir" do
106
+ @file.dir.should match_path('public', 'tmp', %r{((\d+\.)+\d+)})
107
+ end
108
+ end
109
+
110
+ describe "an uploaded non-empty String" do
111
+ it "should raise an error" do
112
+ lambda do
113
+ UploadColumn::UploadedFile.upload("../README")
114
+ end.should raise_error(UploadColumn::UploadNotMultipartError)
115
+ end
116
+ end
117
+
118
+ describe "an uploded empty file" do
119
+ it "should return nil" do
120
+ file = mock('uploaded empty file')
121
+ file.should_receive(:empty?).and_return(true)
122
+ upload = mock('upload')
123
+ UploadColumn::UploadedFile.should_receive(:new).and_return(file)
124
+
125
+ UploadColumn::UploadedFile.upload(upload).should == nil
126
+ end
127
+ end
128
+
129
+ describe "an UploadedFile" do
130
+ before do
131
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), nil, :donkey)
132
+ end
133
+
134
+ it "should have the correct relative store dir" do
135
+ @file.relative_store_dir.should == 'donkey'
136
+ end
137
+
138
+ it "should have the correct store dir" do
139
+ @file.store_dir.should == File.expand_path('donkey', PUBLIC)
140
+ end
141
+
142
+ it "should have the correct relative tmp dir" do
143
+ @file.relative_tmp_dir.should == 'tmp'
144
+ end
145
+
146
+ it "should have the correct tmp dir" do
147
+ @file.tmp_dir.should == File.expand_path('tmp', PUBLIC)
148
+ end
149
+
150
+ it "should return something sensible on inspect" do
151
+ @file.inspect.should == "<UploadedFile: #{@file.path}>"
152
+ end
153
+ end
154
+
155
+ describe "an UploadedFile where store_dir is a String" do
156
+ before do
157
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), nil, nil, :store_dir => 'monkey')
158
+ end
159
+
160
+ it "should have the correct relative store dir" do
161
+ @file.relative_store_dir.should == 'monkey'
162
+ end
163
+
164
+ it "should have the correct store dir" do
165
+ @file.store_dir.should == File.expand_path('monkey', PUBLIC)
166
+ end
167
+ end
168
+
169
+ describe "an UploadedFile where tmp_dir is a String" do
170
+ before do
171
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), nil, nil, :tmp_dir => 'monkey')
172
+ end
173
+
174
+ it "should have the correct relative tmp dir" do
175
+ @file.relative_tmp_dir.should == 'monkey'
176
+ end
177
+
178
+ it "should have the correct tmp dir" do
179
+ @file.tmp_dir.should == File.expand_path('monkey', PUBLIC)
180
+ end
181
+ end
182
+
183
+ describe "an UploadedFile where filename is a String" do
184
+ before do
185
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), nil, nil, :filename => 'monkey.png', :versions => [:thumb, :large])
186
+ end
187
+
188
+ it "should have the correct filename" do
189
+ @file.filename.should == 'monkey.png'
190
+ end
191
+
192
+ it "should remember the actual filename" do
193
+ @file.actual_filename.should == "kerb.jpg"
194
+ end
195
+
196
+ it "should have versions with the correct filename" do
197
+ @file.thumb.filename.should == 'monkey.png'
198
+ @file.large.filename.should == 'monkey.png'
199
+ end
200
+ end
201
+
202
+ describe "an UploadedFile where filename is a Proc with the record piped in" do
203
+ before do
204
+ record = mock('a record')
205
+ record.stub!(:name).and_return('quack')
206
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), record, nil, :versions => [:thumb, :large], :filename => proc{ |r| r.name })
207
+ end
208
+
209
+ it "should have the correct filename" do
210
+ @file.filename.should == 'quack'
211
+ end
212
+
213
+ it "should remember the actual filename" do
214
+ @file.actual_filename.should == "kerb.jpg"
215
+ end
216
+
217
+ it "should have versions with the correct filename" do
218
+ @file.thumb.filename.should == 'quack'
219
+ @file.large.filename.should == 'quack'
220
+ end
221
+ end
222
+
223
+ describe "an UploadedFile where filename is a Proc with the record and file piped in" do
224
+ before do
225
+ record = mock('a record')
226
+ record.stub!(:name).and_return('quack')
227
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), record, nil, :versions => [:thumb, :large], :filename => proc{ |r, f| "#{r.name}-#{f.basename}-#{f.suffix}quox.#{f.extension}"})
228
+ end
229
+
230
+ it "should have the correct filename" do
231
+ @file.filename.should == 'quack-kerb-quox.jpg'
232
+ end
233
+
234
+ it "should remember the actual filename" do
235
+ @file.actual_filename.should == "kerb.jpg"
236
+ end
237
+
238
+ it "should have versions with the correct filename" do
239
+ @file.thumb.filename.should == 'quack-kerb-thumbquox.jpg'
240
+ @file.large.filename.should == 'quack-kerb-largequox.jpg'
241
+ end
242
+ end
243
+
244
+ describe "an UploadedFile with a filename callback" do
245
+ before do
246
+ @instance = mock('instance with filename callback')
247
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), @instance, :monkey, :versions => [:thumb, :large])
248
+ end
249
+
250
+ it "should have the correct filename" do
251
+ @instance.should_receive(:monkey_filename).with(@file).and_return("llama")
252
+ @file.filename.should == 'llama'
253
+ end
254
+
255
+ it "should remember the actual filename" do
256
+ @file.actual_filename.should == "kerb.jpg"
257
+ end
258
+
259
+ it "should have versions with the correct filename" do
260
+ @instance.should_receive(:monkey_filename).with(@file.thumb).and_return("barr")
261
+ @instance.should_receive(:monkey_filename).with(@file.large).and_return("quox")
262
+ @file.thumb.filename.should == 'barr'
263
+ @file.large.filename.should == 'quox'
264
+ end
265
+ end
266
+
267
+ describe "uploading an UploadedFile where filename is a Proc" do
268
+ before do
269
+ record = mock('a record')
270
+ record.stub!(:name).and_return('quack')
271
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), record, nil, :versions => [:thumb, :large], :filename => proc{ |r, f| "#{r.name}-#{f.basename}-#{f.suffix}quox.#{f.extension}"})
272
+ end
273
+
274
+ it "should have the correct filename" do
275
+ @file.filename.should == 'quack-kerb-quox.jpg'
276
+ end
277
+
278
+ it "should remember the actual filename" do
279
+ @file.actual_filename.should == "kerb.jpg"
280
+ end
281
+
282
+ it "should have versions with the correct filename" do
283
+ @file.thumb.filename.should == 'quack-kerb-thumbquox.jpg'
284
+ @file.large.filename.should == 'quack-kerb-largequox.jpg'
285
+ end
286
+
287
+ it "should have a correct path" do
288
+ @file.path.should match_path(PUBLIC, 'tmp', /(?:\d+\.)+\d+/, 'quack-kerb-quox.jpg' )
289
+ end
290
+
291
+ it "should have versions with correct paths" do
292
+ @file.thumb.path.should match_path(PUBLIC, 'tmp', /(?:\d+\.)+\d+/, 'quack-kerb-thumbquox.jpg' )
293
+ @file.large.path.should match_path(PUBLIC, 'tmp', /(?:\d+\.)+\d+/, 'quack-kerb-largequox.jpg' )
294
+ end
295
+ end
296
+
297
+ describe "an UploadedFile where store_dir is a simple Proc" do
298
+ before do
299
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), nil, nil, :store_dir => proc{'monkey'})
300
+ end
301
+
302
+ it "should have the correct relative store dir" do
303
+ @file.relative_store_dir.should == 'monkey'
304
+ end
305
+
306
+ it "should have the correct store dir" do
307
+ @file.store_dir.should == File.expand_path('monkey', PUBLIC)
308
+ end
309
+ end
310
+
311
+ describe "an UploadedFile where tmp_dir is a simple Proc" do
312
+ before do
313
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), nil, nil, :tmp_dir => proc{'monkey'})
314
+ end
315
+
316
+ it "should have the correct relative tmp dir" do
317
+ @file.relative_tmp_dir.should == 'monkey'
318
+ end
319
+
320
+ it "should have the correct tmp dir" do
321
+ @file.tmp_dir.should == File.expand_path('monkey', PUBLIC)
322
+ end
323
+ end
324
+
325
+ describe "an UploadedFile where store_dir is a Proc and has the record piped in" do
326
+ before do
327
+ record = mock('a record')
328
+ record.stub!(:name).and_return('quack')
329
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), record, nil, :store_dir => proc{ |record| File.join(record.name, 'monkey')})
330
+ end
331
+
332
+ it "should have the correct relative store dir" do
333
+ @file.relative_store_dir.should == 'quack/monkey'
334
+ end
335
+
336
+ it "should have the correct store dir" do
337
+ @file.store_dir.should == File.expand_path('quack/monkey', PUBLIC)
338
+ end
339
+ end
340
+
341
+ describe "an UploadedFile where tmp_dir is a Proc and has the record piped in" do
342
+ before do
343
+ record = mock('a record')
344
+ record.stub!(:name).and_return('quack')
345
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), record, nil, :tmp_dir => proc{ |record| File.join(record.name, 'monkey')})
346
+ end
347
+
348
+ it "should have the correct relative tmp dir" do
349
+ @file.relative_tmp_dir.should == 'quack/monkey'
350
+ end
351
+
352
+ it "should have the correct tmp dir" do
353
+ @file.tmp_dir.should == File.expand_path('quack/monkey', PUBLIC)
354
+ end
355
+ end
356
+
357
+
358
+ describe "an UploadedFile where store_dir is a Proc and has the record and file piped in" do
359
+ before do
360
+ record = mock('a record')
361
+ record.stub!(:name).and_return('quack')
362
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), record, nil, :store_dir => proc{ |r, f| File.join(record.name, f.basename, 'monkey')})
363
+ end
364
+
365
+ it "should have the correct relative store dir" do
366
+ @file.relative_store_dir.should == 'quack/kerb/monkey'
367
+ end
368
+
369
+ it "should have the correct store dir" do
370
+ @file.store_dir.should == File.expand_path('quack/kerb/monkey', PUBLIC)
371
+ end
372
+ end
373
+
374
+ describe "an UploadedFile where tmp_dir is a Proc and has the record and file piped in" do
375
+ before do
376
+ record = mock('a record')
377
+ record.stub!(:name).and_return('quack')
378
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), record, nil, :tmp_dir => proc{ |r, f| File.join(record.name, f.basename, 'monkey')})
379
+ end
380
+
381
+ it "should have the correct relative tmp dir" do
382
+ @file.relative_tmp_dir.should == 'quack/kerb/monkey'
383
+ end
384
+
385
+ it "should have the correct tmp dir" do
386
+ @file.tmp_dir.should == File.expand_path('quack/kerb/monkey', PUBLIC)
387
+ end
388
+ end
389
+
390
+
391
+ describe "an UploadedFile with a store_dir callback" do
392
+ before do
393
+ i = mock('instance with store_dir callback')
394
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), i, :monkey)
395
+ i.should_receive(:monkey_store_dir).with(@file).and_return('llama')
396
+ end
397
+
398
+ it "should have the correct relative store dir" do
399
+ @file.relative_store_dir.should == 'llama'
400
+ end
401
+
402
+ it "should have the correct store dir" do
403
+ @file.store_dir.should == File.expand_path('llama', PUBLIC)
404
+ end
405
+ end
406
+
407
+ describe "an UploadedFile with a tmp_dir callback" do
408
+ before do
409
+ i = mock('instance with a tmp_dir callback')
410
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), i, :monkey)
411
+ i.should_receive(:monkey_tmp_dir).with(@file).and_return('gorilla')
412
+ end
413
+
414
+ it "should have the correct relative tmp dir" do
415
+ @file.relative_tmp_dir.should == 'gorilla'
416
+ end
417
+
418
+ it "should have the correct tmp dir" do
419
+ @file.tmp_dir.should == File.expand_path('gorilla', PUBLIC)
420
+ end
421
+ end
422
+
423
+ describe "an UploadedFile that has just been uploaded" do
424
+
425
+ before do
426
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :monkey)
427
+ end
428
+
429
+ it_should_behave_like "all uploaded files"
430
+
431
+ it "should be new" do
432
+ @file.should be_new_file
433
+ end
434
+
435
+ it "should be a tempfile" do
436
+ @file.should be_a_tempfile
437
+ end
438
+
439
+ it "should exist" do
440
+ @file.should be_in_existence
441
+ end
442
+
443
+ it "should be stored in tmp" do
444
+ @file.path.should match_path('public', 'tmp', %r{((\d+\.)+\d+)}, 'kerb.jpg')
445
+ end
446
+
447
+ end
448
+
449
+ describe "saving an UploadedFile" do
450
+ before do
451
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :monkey)
452
+ end
453
+
454
+ it "should return true" do
455
+ @file.send(:save).should === true
456
+ end
457
+
458
+ it "should copy the file to the correct location" do
459
+ @file.send(:save)
460
+ @file.path.should match_path('public', 'monkey', 'kerb.jpg')
461
+ @file.should be_in_existence
462
+ end
463
+
464
+ after do
465
+ FileUtils.rm_rf(PUBLIC)
466
+ end
467
+
468
+ end
469
+
470
+ describe "a saved UploadedFile" do
471
+ before do
472
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :monkey)
473
+ @file.send(:save)
474
+ end
475
+
476
+ it_should_behave_like "all uploaded files"
477
+
478
+ it "should not be new" do
479
+ @file.should_not be_new_file
480
+ end
481
+
482
+ it "should not be a tempfile" do
483
+ @file.should_not be_a_tempfile
484
+ end
485
+
486
+ it "should return the correct path" do
487
+ @file.path.should match_path('public', 'monkey', 'kerb.jpg')
488
+ end
489
+
490
+ it "should return the correct relative_path" do
491
+ @file.relative_path.should == "monkey/kerb.jpg"
492
+ end
493
+
494
+ it "should return the correct dir" do
495
+ @file.dir.should match_path('public', 'monkey')
496
+ end
497
+
498
+ after do
499
+ FileUtils.rm_rf(PUBLIC)
500
+ end
501
+
502
+ end
503
+
504
+ describe "an UploadedFile with a manipulator" do
505
+ before do
506
+ a_manipulator = Module.new
507
+ a_manipulator.send(:define_method, :monkey! ) { |stuff| stuff }
508
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), nil, :donkey, :manipulator => a_manipulator)
509
+ end
510
+
511
+ it "should extend the object with the manipulator methods." do
512
+ @file.should respond_to(:monkey!)
513
+ end
514
+
515
+ end
516
+
517
+ describe "an UploadedFile with a manipulator and versions" do
518
+ before do
519
+ a_manipulator = Module.new
520
+ a_manipulator.send(:define_method, :monkey! ) { |stuff| stuff }
521
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), nil, :donkey, :versions => [ :thumb, :large ], :manipulator => a_manipulator)
522
+ end
523
+
524
+ it "should extend the object with the manipulator methods." do
525
+ @file.should respond_to(:monkey!)
526
+ end
527
+
528
+ it "should extend the versions with the manipulator methods." do
529
+ @file.thumb.should respond_to(:monkey!)
530
+ @file.large.should respond_to(:monkey!)
531
+ end
532
+
533
+ end
534
+
535
+ describe "an UploadedFile with a manipulator with dependencies" do
536
+
537
+ it "should extend the object with the manipulator methods and load dependencies." do
538
+ process_proxy = mock('nothing in particular')
539
+ a_manipulator = Module.new
540
+ a_manipulator.send(:define_method, :monkey! ) { |stuff| stuff }
541
+ a_manipulator.send(:define_method, :load_manipulator_dependencies) do
542
+ # horrible abuse of Ruby's closures. This allows us to set expectations on the process_proxy
543
+ # and if process! is called, the process_proxy will be adressed instead.
544
+ process_proxy.load_manipulator_dependencies
545
+ end
546
+
547
+ process_proxy.should_receive(:load_manipulator_dependencies)
548
+
549
+ @file = UploadColumn::UploadedFile.new(:open, stub_file('kerb.jpg'), nil, :donkey, :manipulator => a_manipulator)
550
+
551
+ @file.should respond_to(:monkey!)
552
+ end
553
+
554
+ end
555
+
556
+ describe "an UploadedFile with a manipulator and process instruction" do
557
+
558
+ it "should process before iterating versions" do
559
+ process_proxy = mock('nothing in particular')
560
+ a_manipulator = Module.new
561
+ a_manipulator.send(:define_method, :process!) do |*args|
562
+ process_proxy.process!(*args)
563
+ end
564
+ # this will override the base classes initialize_versions option, so we can catch it.
565
+ a_manipulator.send(:define_method, :initialize_versions) do |*args|
566
+ process_proxy.initialize_versions *args
567
+ end
568
+
569
+ process_proxy.should_receive(:process!).with('100x100').ordered
570
+ process_proxy.should_receive(:initialize_versions).ordered
571
+
572
+
573
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :donkey, :process => '100x100', :manipulator => a_manipulator)
574
+ end
575
+
576
+ end
577
+
578
+ describe "an UploadedFile with no versions" do
579
+ it "should not respond to version methods" do
580
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :monkey)
581
+ @file.should_not respond_to(:thumb)
582
+ @file.should_not respond_to(:large)
583
+ end
584
+ end
585
+
586
+ describe "an UploadedFile with versions with illegal names" do
587
+ it "should raise an ArgumentError" do
588
+ lambda do
589
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :monkey, :versions => [ :thumb, :path ])
590
+ end.should raise_error(ArgumentError, 'path is an illegal name for an UploadColumn version.')
591
+ end
592
+ end
593
+
594
+ describe "an UploadedFile with versions" do
595
+ before do
596
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :monkey, :versions => [ :thumb, :large ])
597
+ end
598
+
599
+ it "should respond to version methods" do
600
+ @file.should respond_to(:thumb)
601
+ @file.should respond_to(:large)
602
+ end
603
+
604
+ it "should return an UploadedFile instance when a version method is called" do
605
+ @file.thumb.should be_instance_of(UploadColumn::UploadedFile)
606
+ @file.large.should be_instance_of(UploadColumn::UploadedFile)
607
+ end
608
+ end
609
+
610
+ describe "all versions of uploaded files", :shared => true do
611
+ it "should return the filename including the version" do
612
+ @thumb.filename.should == "kerb-thumb.jpg"
613
+ @large.filename.should == "kerb-large.jpg"
614
+ end
615
+
616
+ it "should return the basename without the version" do
617
+ @thumb.basename.should == "kerb"
618
+ @large.basename.should == "kerb"
619
+ end
620
+
621
+ it "should return the extension" do
622
+ @thumb.extension.should == "jpg"
623
+ @large.extension.should == "jpg"
624
+ end
625
+
626
+ it "should return the correct suffix" do
627
+ @thumb.suffix.should == :thumb
628
+ @large.suffix.should == :large
629
+ end
630
+ end
631
+
632
+ describe "a version of an uploaded UploadedFile" do
633
+ before do
634
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :monkey, :versions => [ :thumb, :large ])
635
+ @thumb = @file.thumb
636
+ @large = @file.large
637
+ end
638
+
639
+ it_should_behave_like "all versions of uploaded files"
640
+
641
+ it "should not be empty" do
642
+ @thumb.should_not be_empty
643
+ @large.should_not be_empty
644
+ end
645
+
646
+ it "should return the correct filesize" do
647
+ @thumb.size.should == 87582
648
+ @large.size.should == 87582
649
+ end
650
+
651
+ it "should return the original filename" do
652
+ @thumb.original_filename.should == "kerb.jpg"
653
+ @large.original_filename.should == "kerb.jpg"
654
+ end
655
+ end
656
+
657
+ describe "uploading a file with versions as a Hash" do
658
+
659
+ it "should process the files with the manipulator" do
660
+
661
+ process_proxy = mock('nothing in particular')
662
+ a_manipulator = Module.new
663
+ a_manipulator.send(:define_method, :process! ) do |stuff|
664
+ # horrible abuse of Ruby's closures. This allows us to set expectations on the process_proxy
665
+ # and if process! is called, the process_proxy will be adressed instead.
666
+ process_proxy.process!(self.filename, stuff)
667
+ end
668
+
669
+ process_proxy.should_receive(:process!).with('kerb-thumb.jpg', '200x200')
670
+ process_proxy.should_receive(:process!).with('kerb-large.jpg', '300x300')
671
+
672
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :donkey, :manipulator => a_manipulator, :versions => { :thumb => '200x200', :large => '300x300' })
673
+ @thumb = @file.thumb
674
+ @large = @file.large
675
+ end
676
+
677
+ end
678
+
679
+
680
+ describe "an version of an UploadedFile with versions as a hash" do
681
+
682
+ before(:each) do
683
+ process_proxy = mock('nothing in particular')
684
+ a_manipulator = Module.new
685
+ a_manipulator.send(:define_method, :process! ) { |stuff| true }
686
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :donkey, :manipulator => a_manipulator, :versions => { :thumb => '200x200', :large => '300x300' })
687
+ @thumb = @file.thumb
688
+ @large = @file.large
689
+ end
690
+
691
+ it_should_behave_like "all versions of uploaded files"
692
+
693
+ it "should not be empty" do
694
+ @thumb.should_not be_empty
695
+ @large.should_not be_empty
696
+ end
697
+
698
+ it "should return the original filename" do
699
+ @thumb.original_filename.should == "kerb.jpg"
700
+ @large.original_filename.should == "kerb.jpg"
701
+ end
702
+
703
+ end
704
+
705
+ describe "a retrieved UploadedFile" do
706
+
707
+ before do
708
+ @file = UploadColumn::UploadedFile.retrieve('kerb.jpg', nil, :monkey)
709
+ @file.stub!(:size).and_return(87582)
710
+ end
711
+
712
+ it_should_behave_like "all uploaded files"
713
+
714
+ it "should not be new" do
715
+ @file.should_not be_new_file
716
+ end
717
+
718
+ it "should not be a tempfile" do
719
+ @file.should_not be_a_tempfile
720
+ end
721
+
722
+ it "should return the correct path" do
723
+ @file.path.should match_path(public_path('monkey/kerb.jpg'))
724
+ end
725
+ end
726
+
727
+ describe "a version of a retrieved UploadedFile" do
728
+
729
+ before do
730
+ @file = UploadColumn::UploadedFile.retrieve('kerb.jpg', nil, :monkey, :versions => [:thumb, :large])
731
+ @thumb = @file.thumb
732
+ @large = @file.large
733
+ end
734
+
735
+ it_should_behave_like "all versions of uploaded files"
736
+
737
+ it "should not be new" do
738
+ @file.should_not be_new_file
739
+ end
740
+
741
+ it "should not be a tempfile" do
742
+ @file.should_not be_a_tempfile
743
+ end
744
+
745
+ it "should return the correct path" do
746
+ @thumb.path.should match_path(public_path('monkey/kerb-thumb.jpg'))
747
+ @large.path.should match_path(public_path('monkey/kerb-large.jpg'))
748
+ end
749
+
750
+ # Since the files don't exist in fixtures/ it wouldn't make sense to test their size
751
+ end
752
+
753
+ describe "a version of a saved UploadedFile" do
754
+ before do
755
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :monkey, :versions => [:thumb, :large])
756
+ @file.send(:save)
757
+ @thumb = @file.thumb
758
+ @large = @file.large
759
+ end
760
+
761
+ it_should_behave_like "all versions of uploaded files"
762
+
763
+ it "should not be new" do
764
+ @file.should_not be_new_file
765
+ end
766
+
767
+ it "should not be a tempfile" do
768
+ @file.should_not be_a_tempfile
769
+ end
770
+
771
+ it "should return the correct path" do
772
+ @thumb.path.should match_path('public', 'monkey', 'kerb-thumb.jpg')
773
+ @large.path.should match_path('public', 'monkey', 'kerb-large.jpg')
774
+ end
775
+ end
776
+
777
+ describe "opening a temporary UploadedFile" do
778
+
779
+ it "should raise an error if the path is incorrectly formed" do
780
+ lambda do
781
+ @file = UploadColumn::UploadedFile.retrieve_temp(file_path('kerb.jpg'))
782
+ end.should raise_error(UploadColumn::TemporaryPathMalformedError, "#{file_path('kerb.jpg')} is not a valid temporary path!")
783
+ end
784
+
785
+ it "should raise an error if its in a subdirectory" do
786
+ lambda do
787
+ @file = UploadColumn::UploadedFile.retrieve_temp('somefolder/1234.56789.1234/donkey.jpg;llama.png')
788
+ end.should raise_error(UploadColumn::TemporaryPathMalformedError, "somefolder/1234.56789.1234/donkey.jpg;llama.png is not a valid temporary path!")
789
+ end
790
+
791
+ it "should raise an error if its relative" do
792
+ lambda do
793
+ @file = UploadColumn::UploadedFile.retrieve_temp('../1234.56789.1234/donkey.jpg;llama.png')
794
+ end.should raise_error(UploadColumn::TemporaryPathMalformedError, "../1234.56789.1234/donkey.jpg;llama.png is not a valid temporary path!")
795
+ end
796
+
797
+ it "should raise an error if the filename is omitted" do
798
+ lambda do
799
+ @file = UploadColumn::UploadedFile.retrieve_temp('1234.56789.1234;llama.png')
800
+ end.should raise_error(UploadColumn::TemporaryPathMalformedError, "1234.56789.1234;llama.png is not a valid temporary path!")
801
+ end
802
+
803
+ it "should not raise an error on nil" do
804
+ lambda do
805
+ @file = UploadColumn::UploadedFile.retrieve_temp(nil)
806
+ end.should_not raise_error
807
+ end
808
+
809
+ it "should not raise an error on empty String" do
810
+ lambda do
811
+ @file = UploadColumn::UploadedFile.retrieve_temp('')
812
+ end.should_not raise_error
813
+ end
814
+ end
815
+
816
+ describe "a retrieved temporary UploadedFile" do
817
+
818
+ before(:all) do
819
+ FileUtils.mkdir_p(public_path('tmp/123455.1233.1233'))
820
+ FileUtils.cp(file_path('kerb.jpg'), public_path('tmp/123455.1233.1233/kerb.jpg'))
821
+ end
822
+
823
+ before do
824
+ @file = UploadColumn::UploadedFile.retrieve_temp('123455.1233.1233/kerb.jpg')
825
+ end
826
+
827
+ it_should_behave_like "all uploaded files"
828
+
829
+ it "should not be new" do
830
+ @file.should_not be_new_file
831
+ end
832
+
833
+ it "should be a tempfile" do
834
+ @file.should be_a_tempfile
835
+ end
836
+
837
+ it "should return the correct path" do
838
+ @file.path.should match_path('public', 'tmp', '123455.1233.1233', 'kerb.jpg')
839
+ end
840
+
841
+ after(:all) do
842
+ FileUtils.rm_rf(PUBLIC)
843
+ end
844
+ end
845
+
846
+ describe "a retrieved temporary UploadedFile with an appended original filename" do
847
+ before(:all) do
848
+ FileUtils.mkdir_p(public_path('tmp/123455.1233.1233'))
849
+ FileUtils.cp(file_path('kerb.jpg'), public_path('tmp/123455.1233.1233/kerb.jpg'))
850
+ end
851
+
852
+ before do
853
+ @file = UploadColumn::UploadedFile.retrieve_temp('123455.1233.1233/kerb.jpg;monkey.png')
854
+ end
855
+
856
+ it "should not be new" do
857
+ @file.should_not be_new_file
858
+ end
859
+
860
+ it "should be a tempfile" do
861
+ @file.should be_a_tempfile
862
+ end
863
+
864
+ it "should return the correct original filename" do
865
+ @file.original_filename.should == "monkey.png"
866
+ end
867
+
868
+ it "should return the correct path" do
869
+ @file.path.should match_path('public', 'tmp', '123455.1233.1233', 'kerb.jpg')
870
+ end
871
+
872
+ after(:all) do
873
+ FileUtils.rm_rf(PUBLIC)
874
+ end
875
+ end
876
+
877
+ describe "a version of a retrieved temporary UploadedFile" do
878
+
879
+ before(:all) do
880
+ FileUtils.mkdir_p(public_path('tmp/123455.1233.1233'))
881
+ FileUtils.cp(file_path('kerb.jpg'), public_path('tmp/123455.1233.1233/kerb.jpg'))
882
+ end
883
+
884
+ before do
885
+ @file = UploadColumn::UploadedFile.retrieve_temp('123455.1233.1233/kerb.jpg', nil, :monkey, :versions => [:thumb, :large])
886
+ @thumb = @file.thumb
887
+ @large = @file.large
888
+ end
889
+
890
+ it_should_behave_like "all versions of uploaded files"
891
+
892
+ it "should not be new" do
893
+ @file.should_not be_new_file
894
+ end
895
+
896
+ it "should be a tempfile" do
897
+ @file.should be_a_tempfile
898
+ end
899
+
900
+ it "should return the correct path" do
901
+ @thumb.path.should match_path(public_path('tmp/123455.1233.1233/kerb-thumb.jpg'))
902
+ @large.path.should match_path(public_path('tmp/123455.1233.1233/kerb-large.jpg'))
903
+ end
904
+
905
+ after(:all) do
906
+ FileUtils.rm_rf(PUBLIC)
907
+ end
908
+ end
909
+
910
+ describe "uploading a file with validate_integrity set to true" do
911
+
912
+ it "should raise an error if no extensions are set" do
913
+ lambda do
914
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, nil, :validate_integrity => true)
915
+ end.should raise_error(UploadColumn::UploadError)
916
+ end
917
+
918
+ it "should not raise an error if the extension is in extensions" do
919
+ lambda do
920
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, nil, :validate_integrity => true, :extensions => %w(jpg gif png))
921
+ end.should_not raise_error
922
+ end
923
+
924
+ it "should raise an error if the extension is not in extensions" do
925
+ lambda do
926
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, nil, :validate_integrity => true, :extensions => %w(doc gif png))
927
+ end.should raise_error(UploadColumn::IntegrityError)
928
+ end
929
+ end
930
+
931
+ describe "An UploadedFile with no web_root set" do
932
+ it "should return the correct URL and to_s" do
933
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :donkey, :versions => [:thumb, :large])
934
+ @file.send(:save)
935
+
936
+ @file.url.should == "/donkey/kerb.jpg"
937
+ @file.to_s.should == "/donkey/kerb.jpg"
938
+ @file.thumb.url.should == "/donkey/kerb-thumb.jpg"
939
+ @file.large.url.should == "/donkey/kerb-large.jpg"
940
+ end
941
+ end
942
+
943
+ describe "An UploadedFile with no web_root set and MS style slashes in its relative path" do
944
+ it "should return the correct URL and to_s" do
945
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :donkey, :versions => [:thumb, :large])
946
+
947
+ @file.should_receive(:relative_path).at_least(:once).and_return('stylesheets\something\monkey\kerb.jpg')
948
+ @file.thumb.should_receive(:relative_path).at_least(:once).and_return('stylesheets\something\monkey\kerb-thumb.jpg')
949
+ @file.large.should_receive(:relative_path).at_least(:once).and_return('stylesheets\something\monkey\kerb-large.jpg')
950
+
951
+ @file.send(:save)
952
+
953
+ @file.url.should == "/stylesheets/something/monkey/kerb.jpg"
954
+ @file.to_s.should == "/stylesheets/something/monkey/kerb.jpg"
955
+ @file.thumb.url.should == "/stylesheets/something/monkey/kerb-thumb.jpg"
956
+ @file.large.url.should == "/stylesheets/something/monkey/kerb-large.jpg"
957
+ end
958
+ end
959
+
960
+ describe "An UploadedFile with an absolute web_root set" do
961
+ it "should return the correct URL and to_s" do
962
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :donkey, :web_root => 'http://ape.com', :versions => [:thumb, :large])
963
+ @file.send(:save)
964
+
965
+ @file.url.should == "http://ape.com/donkey/kerb.jpg"
966
+ @file.to_s.should == "http://ape.com/donkey/kerb.jpg"
967
+ @file.thumb.url.should == "http://ape.com/donkey/kerb-thumb.jpg"
968
+ @file.large.url.should == "http://ape.com/donkey/kerb-large.jpg"
969
+ end
970
+ end
971
+
972
+ describe "An UploadedFile with an absolute web_root set and MS style slashes in its relative path" do
973
+ it "should return the correct URL and to_s" do
974
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :donkey, :web_root => 'http://ape.com', :versions => [:thumb, :large])
975
+ @file.should_receive(:relative_path).at_least(:once).and_return('stylesheets\something\monkey\kerb.jpg')
976
+ @file.thumb.should_receive(:relative_path).at_least(:once).and_return('stylesheets\something\monkey\kerb-thumb.jpg')
977
+ @file.large.should_receive(:relative_path).at_least(:once).and_return('stylesheets\something\monkey\kerb-large.jpg')
978
+
979
+ @file.send(:save)
980
+
981
+ @file.url.should == "http://ape.com/stylesheets/something/monkey/kerb.jpg"
982
+ @file.to_s.should == "http://ape.com/stylesheets/something/monkey/kerb.jpg"
983
+ @file.thumb.url.should == "http://ape.com/stylesheets/something/monkey/kerb-thumb.jpg"
984
+ @file.large.url.should == "http://ape.com/stylesheets/something/monkey/kerb-large.jpg"
985
+ end
986
+ end
987
+
988
+ describe "An UploadedFile with a web_root set" do
989
+ it "should return the correct URL" do
990
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :donkey, :web_root => '/ape', :versions => [:thumb, :large])
991
+ @file.send(:save)
992
+
993
+ @file.url.should == "/ape/donkey/kerb.jpg"
994
+ @file.to_s.should == "/ape/donkey/kerb.jpg"
995
+ @file.thumb.url.should == "/ape/donkey/kerb-thumb.jpg"
996
+ @file.large.url.should == "/ape/donkey/kerb-large.jpg"
997
+ end
998
+ end
999
+
1000
+ describe "the temp_value of an UploadedFile without an original filename" do
1001
+
1002
+ setup do
1003
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :donkey)
1004
+ @file.should_receive(:original_filename).and_return(nil)
1005
+ end
1006
+
1007
+ it "should match the TempValueRegexp" do
1008
+ @file.temp_value.should match(::UploadColumn::TempValueRegexp)
1009
+ end
1010
+
1011
+ it "should end in the filename" do
1012
+ @file.temp_value.should match(/\/kerb\.jpg$/)
1013
+ end
1014
+ end
1015
+
1016
+ describe "the temp_value of an UploadedFile with a different orignal filename" do
1017
+
1018
+ setup do
1019
+ @file = UploadColumn::UploadedFile.upload(stub_file('kerb.jpg'), nil, :donkey)
1020
+ @file.should_receive(:original_filename).at_least(:once).and_return('monkey.png')
1021
+ end
1022
+
1023
+ it "should match the TempValueRegexp" do
1024
+ @file.temp_value.should match(::UploadColumn::TempValueRegexp)
1025
+ end
1026
+
1027
+ it "should append the original_filename" do
1028
+ @file.temp_value.should match(/kerb\.jpg;monkey\.png$/)
1029
+ end
1030
+ end
1031
+
1032
+ describe "the temp_value of a retrieved temporary UploadedFile" do
1033
+
1034
+ setup do
1035
+ @file = UploadColumn::UploadedFile.retrieve_temp('12345.1234.12345/kerb.jpg', nil, :donkey)
1036
+ @file.should_receive(:original_filename).at_least(:once).and_return(nil)
1037
+ end
1038
+
1039
+ it "should be mainatained" do
1040
+ @file.temp_value.should == '12345.1234.12345/kerb.jpg'
1041
+ end
1042
+ end
1043
+
1044
+ describe "the temp_value of an UploadedFile that is not temporary" do
1045
+
1046
+ setup do
1047
+ @file = UploadColumn::UploadedFile.retrieve('kerb.jpg', nil, :donkey)
1048
+ end
1049
+
1050
+ it "should be mainatained" do
1051
+ @file.temp_value.should be_nil
1052
+ end
1053
+ end