uploadcolumn 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/CHANGELOG +123 -0
- data/LICENSE +22 -0
- data/README.rdoc +206 -0
- data/Rakefile +90 -0
- data/VERSION +1 -0
- data/init.rb +15 -0
- data/lib/upload_column/active_record_extension.rb +154 -0
- data/lib/upload_column/configuration.rb +49 -0
- data/lib/upload_column/magic_columns.rb +50 -0
- data/lib/upload_column/manipulators/image_science.rb +86 -0
- data/lib/upload_column/manipulators/rmagick.rb +75 -0
- data/lib/upload_column/rails/action_controller_extension.rb +61 -0
- data/lib/upload_column/rails/asset_tag_extension.rb +17 -0
- data/lib/upload_column/rails/upload_column_helper.rb +45 -0
- data/lib/upload_column/sanitized_file.rb +176 -0
- data/lib/upload_column/uploaded_file.rb +299 -0
- data/lib/upload_column.rb +12 -0
- data/spec/active_record_extension_spec.rb +514 -0
- data/spec/custom_matchers.rb +148 -0
- data/spec/fixtures/animated.gif +0 -0
- data/spec/fixtures/animated_solarized.gif +0 -0
- data/spec/fixtures/invalid-image.jpg +1 -0
- data/spec/fixtures/kerb.jpg +0 -0
- data/spec/fixtures/kerb_solarized.jpg +0 -0
- data/spec/fixtures/netscape.gif +0 -0
- data/spec/fixtures/skanthak.png +0 -0
- data/spec/image_science_manipulator_spec.rb +195 -0
- data/spec/integration_spec.rb +668 -0
- data/spec/magic_columns_spec.rb +120 -0
- data/spec/rmagick_manipulator_spec.rb +186 -0
- data/spec/sanitized_file_spec.rb +496 -0
- data/spec/spec_helper.rb +90 -0
- data/spec/upload_column_spec.rb +65 -0
- data/spec/uploaded_file_spec.rb +1053 -0
- metadata +108 -0
@@ -0,0 +1,496 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
require File.join(File.dirname(__FILE__), '../lib/upload_column/sanitized_file')
|
3
|
+
begin
|
4
|
+
require 'mime/types'
|
5
|
+
rescue LoadError
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "creating a new SanitizedFile" do
|
9
|
+
it "should yield an empty file on empty String, nil, empty StringIO" do
|
10
|
+
UploadColumn::SanitizedFile.new("").should be_empty
|
11
|
+
UploadColumn::SanitizedFile.new(StringIO.new("")).should be_empty
|
12
|
+
UploadColumn::SanitizedFile.new(nil).should be_empty
|
13
|
+
file = mock('emptyfile')
|
14
|
+
file.should_receive(:size).at_least(:once).and_return(0)
|
15
|
+
UploadColumn::SanitizedFile.new(file).should be_empty
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should yield a non empty file" do
|
19
|
+
UploadColumn::SanitizedFile.new(stub_stringio('kerb.jpg', 'image/jpeg')).should_not be_empty
|
20
|
+
UploadColumn::SanitizedFile.new(stub_file('kerb.jpg', 'image/jpeg')).should_not be_empty
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should not change a valid filename" do
|
24
|
+
t = UploadColumn::SanitizedFile.new(stub_file('kerb.jpg', nil, "test.jpg"))
|
25
|
+
t.filename.should == "test.jpg"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should remove illegal characters from a filename" do
|
29
|
+
t = UploadColumn::SanitizedFile.new(stub_file('kerb.jpg', nil, "test-s,%&m#st?.jpg"))
|
30
|
+
t.filename.should == "test-s___m_st_.jpg"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should remove slashes from the filename" do
|
34
|
+
t = UploadColumn::SanitizedFile.new(stub_file('kerb.jpg', nil, "../../very_tricky/foo.bar"))
|
35
|
+
t.filename.should_not =~ /[\\\/]/
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should remove illegal characters if there is no extension" do
|
39
|
+
t = UploadColumn::SanitizedFile.new(stub_file('kerb.jpg', nil, '`*foo'))
|
40
|
+
t.filename.should == "__foo"
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should remove the path prefix on Windows" do
|
44
|
+
t = UploadColumn::SanitizedFile.new(stub_file('kerb.jpg', nil, 'c:\temp\foo.txt'))
|
45
|
+
t.filename.should == "foo.txt"
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should make sure the *nix directory thingies can't be used as filenames" do
|
49
|
+
t = UploadColumn::SanitizedFile.new(stub_file('kerb.jpg', nil, "."))
|
50
|
+
t.filename.should == "_."
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should downcase uppercase filenames" do
|
54
|
+
t = UploadColumn::SanitizedFile.new(stub_file('kerb.jpg', nil, "DSC4056.JPG"))
|
55
|
+
t.filename.should == "dsc4056.jpg"
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
# Note that SanitizedFile#path and #exists? need to be checked seperately as the return values will vary
|
61
|
+
describe "all sanitized files", :shared => true do
|
62
|
+
|
63
|
+
it "should not be empty" do
|
64
|
+
@file.should_not be_empty
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should return the original filename" do
|
68
|
+
@file.original_filename.should == "kerb.jpg"
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should return the filename" do
|
72
|
+
@file.filename.should == "kerb.jpg"
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should return the basename" do
|
76
|
+
@file.basename.should == "kerb"
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should return the extension" do
|
80
|
+
@file.extension.should == "jpg"
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should be moved to the correct location" do
|
84
|
+
@file.move_to(public_path('gurr.jpg'))
|
85
|
+
File.exists?( public_path('gurr.jpg') ).should === true
|
86
|
+
file_path('kerb.jpg').should be_identical_with(public_path('gurr.jpg'))
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should have changed its path when moved" do
|
90
|
+
@file.move_to(public_path('gurr.jpg'))
|
91
|
+
@file.path.should match_path(public_path('gurr.jpg'))
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should have changed its filename when moved" do
|
95
|
+
@file.filename # Make sure the filename has been cached
|
96
|
+
@file.move_to(public_path('gurr.jpg'))
|
97
|
+
@file.filename.should == 'gurr.jpg'
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should have split the filename when moved" do
|
101
|
+
@file.move_to(public_path('gurr.monk'))
|
102
|
+
@file.basename.should == 'gurr'
|
103
|
+
@file.extension.should == 'monk'
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should be copied to the correct location" do
|
107
|
+
@file.copy_to(public_path('gurr.jpg'))
|
108
|
+
File.exists?( public_path('gurr.jpg') ).should === true
|
109
|
+
file_path('kerb.jpg').should be_identical_with(public_path('gurr.jpg'))
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should not have changed its path when copied" do
|
113
|
+
running { @file.copy_to(public_path('gurr.jpg')) }.should_not change(@file, :path)
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should not have changed its filename when copied" do
|
117
|
+
running { @file.copy_to(public_path('gurr.jpg')) }.should_not change(@file, :filename)
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should return an object of the same class when copied" do
|
121
|
+
new_file = @file.copy_to(public_path('gurr.jpg'))
|
122
|
+
new_file.should be_an_instance_of(@file.class)
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should adjust the path of the object that is returned when copied" do
|
126
|
+
new_file = @file.copy_to(public_path('gurr.jpg'))
|
127
|
+
new_file.path.should match_path(public_path('gurr.jpg'))
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should adjust the filename of the object that is returned when copied" do
|
131
|
+
@file.filename # Make sure the filename has been cached
|
132
|
+
@file = @file.copy_to(public_path('gurr.monk'))
|
133
|
+
@file.filename.should == 'gurr.monk'
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should split the filename of the object that is returned when copied" do
|
137
|
+
@file = @file.copy_to(public_path('gurr.monk'))
|
138
|
+
@file.basename.should == 'gurr'
|
139
|
+
@file.extension.should == 'monk'
|
140
|
+
end
|
141
|
+
|
142
|
+
after do
|
143
|
+
FileUtils.rm_rf(PUBLIC)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe "a sanitized Tempfile" do
|
148
|
+
before do
|
149
|
+
@tempfile = stub_tempfile('kerb.jpg', 'image/jpeg')
|
150
|
+
@file = UploadColumn::SanitizedFile.new(@tempfile)
|
151
|
+
end
|
152
|
+
|
153
|
+
it_should_behave_like "all sanitized files"
|
154
|
+
|
155
|
+
it "should not raise an error when moved to its own location" do
|
156
|
+
running { @file.move_to(@file.path) }.should_not raise_error
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should return a new instance when copied to its own location" do
|
160
|
+
running {
|
161
|
+
new_file = @file.copy_to(@file.path)
|
162
|
+
new_file.should be_an_instance_of(@file.class)
|
163
|
+
}.should_not raise_error
|
164
|
+
end
|
165
|
+
|
166
|
+
it "should exist" do
|
167
|
+
@file.should be_in_existence
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should return the correct path" do
|
171
|
+
@file.path.should_not == nil
|
172
|
+
@file.path.should == @tempfile.path
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
describe "a sanitized StringIO" do
|
177
|
+
before do
|
178
|
+
@file = UploadColumn::SanitizedFile.new(stub_stringio('kerb.jpg', 'image/jpeg'))
|
179
|
+
end
|
180
|
+
|
181
|
+
it_should_behave_like "all sanitized files"
|
182
|
+
|
183
|
+
it "should not exist" do
|
184
|
+
@file.should_not be_in_existence
|
185
|
+
end
|
186
|
+
|
187
|
+
it "should return no path" do
|
188
|
+
@file.path.should == nil
|
189
|
+
end
|
190
|
+
|
191
|
+
end
|
192
|
+
|
193
|
+
describe "a sanitized File object" do
|
194
|
+
before do
|
195
|
+
@file = UploadColumn::SanitizedFile.new(stub_file('kerb.jpg', 'image/jpeg'))
|
196
|
+
@file.should_not be_empty
|
197
|
+
end
|
198
|
+
|
199
|
+
it_should_behave_like "all sanitized files"
|
200
|
+
|
201
|
+
it "should not raise an error when moved to its own location" do
|
202
|
+
running { @file.move_to(@file.path) }.should_not raise_error
|
203
|
+
end
|
204
|
+
|
205
|
+
it "should return a new instance when copied to its own location" do
|
206
|
+
running {
|
207
|
+
new_file = @file.copy_to(@file.path)
|
208
|
+
new_file.should be_an_instance_of(@file.class)
|
209
|
+
}.should_not raise_error
|
210
|
+
end
|
211
|
+
|
212
|
+
it "should exits" do
|
213
|
+
@file.should be_in_existence
|
214
|
+
end
|
215
|
+
|
216
|
+
it "should return correct path" do
|
217
|
+
@file.path.should match_path(file_path('kerb.jpg'))
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
describe "a SanitizedFile opened from a path" do
|
222
|
+
before do
|
223
|
+
@file = UploadColumn::SanitizedFile.new(file_path('kerb.jpg'))
|
224
|
+
@file.should_not be_empty
|
225
|
+
end
|
226
|
+
|
227
|
+
it_should_behave_like "all sanitized files"
|
228
|
+
|
229
|
+
it "should not raise an error when moved to its own location" do
|
230
|
+
running { @file.move_to(@file.path) }.should_not raise_error
|
231
|
+
end
|
232
|
+
|
233
|
+
it "should return a new instance when copied to its own location" do
|
234
|
+
running {
|
235
|
+
new_file = @file.copy_to(@file.path)
|
236
|
+
new_file.should be_an_instance_of(@file.class)
|
237
|
+
}.should_not raise_error
|
238
|
+
end
|
239
|
+
|
240
|
+
it "should exits" do
|
241
|
+
@file.should be_in_existence
|
242
|
+
end
|
243
|
+
|
244
|
+
it "should return correct path" do
|
245
|
+
@file.path.should == file_path('kerb.jpg')
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
describe "an empty SanitizedFile" do
|
250
|
+
before do
|
251
|
+
@empty = UploadColumn::SanitizedFile.new(nil)
|
252
|
+
end
|
253
|
+
|
254
|
+
it "should be empty" do
|
255
|
+
@empty.should be_empty
|
256
|
+
end
|
257
|
+
|
258
|
+
it "should not exist" do
|
259
|
+
@empty.should_not be_in_existence
|
260
|
+
end
|
261
|
+
|
262
|
+
it "should have no size" do
|
263
|
+
@empty.size.should == nil
|
264
|
+
end
|
265
|
+
|
266
|
+
it "should have no path" do
|
267
|
+
@empty.path.should == nil
|
268
|
+
end
|
269
|
+
|
270
|
+
it "should have no original filename" do
|
271
|
+
@empty.original_filename.should == nil
|
272
|
+
end
|
273
|
+
|
274
|
+
it "should have no filename" do
|
275
|
+
@empty.filename.should == nil
|
276
|
+
end
|
277
|
+
|
278
|
+
it "should have no basename" do
|
279
|
+
@empty.basename.should == nil
|
280
|
+
end
|
281
|
+
|
282
|
+
it "should have no extension" do
|
283
|
+
@empty.extension.should == nil
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
describe "a SanitizedFile" do
|
288
|
+
|
289
|
+
before do
|
290
|
+
@file = UploadColumn::SanitizedFile.new(stub_tempfile('kerb.jpg', 'image/jpeg'))
|
291
|
+
end
|
292
|
+
|
293
|
+
it "should properly split into basename and extension" do
|
294
|
+
@file.basename.should == "kerb"
|
295
|
+
@file.extension.should == "jpg"
|
296
|
+
end
|
297
|
+
|
298
|
+
it "should do a system call" do
|
299
|
+
@file.send(:system_call, 'echo "monkey"').chomp.should == "monkey"
|
300
|
+
end
|
301
|
+
|
302
|
+
end
|
303
|
+
|
304
|
+
describe "a SanizedFile with a complex filename" do
|
305
|
+
it "properly split into basename and extension" do
|
306
|
+
t = UploadColumn::SanitizedFile.new(stub_tempfile('kerb.jpg', nil, 'complex.filename.tar.gz'))
|
307
|
+
t.basename.should == "complex.filename"
|
308
|
+
t.extension.should == "tar.gz"
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
# FIXME: figure out why this doesn't run
|
313
|
+
#describe "determinating the mime-type with a *nix exec" do
|
314
|
+
#
|
315
|
+
# before do
|
316
|
+
# @file = stub_file('kerb.jpg', nil, 'harg.css')
|
317
|
+
# @sanitized = UploadColumn::SanitizedFile.new(@file)
|
318
|
+
# end
|
319
|
+
#
|
320
|
+
# it "should chomp and return if it has no encoding" do
|
321
|
+
# @sanitized.should_receive(:system_call).with(%(file -bi "#{@file.path}")).and_return("image/jpeg\n")
|
322
|
+
#
|
323
|
+
# @sanitized.send(:get_content_type_from_exec) #.should == "image/jpeg"
|
324
|
+
# end
|
325
|
+
#
|
326
|
+
# it "should chomp and return and chop off the encoding if it has one" do
|
327
|
+
# @sanitized.should_receive(:system_call).with(%(file -bi "#{@file.path}")).and_return("text/plain; charset=utf-8;\n")
|
328
|
+
#
|
329
|
+
# @sanitized.send(:get_content_type_from_exec) #.should == "text/plain"
|
330
|
+
# end
|
331
|
+
#
|
332
|
+
# it "should not crap out when something weird happens" do
|
333
|
+
# @sanitized.should_receive(:system_call).with(%(file -bi "#{@file.path}")).and_return("^blah//(?)wtf???")
|
334
|
+
#
|
335
|
+
# @sanitized.send(:get_content_type_from_exec).should == nil
|
336
|
+
# end
|
337
|
+
#
|
338
|
+
#end
|
339
|
+
|
340
|
+
describe "The mime-type of a Sanitized File" do
|
341
|
+
|
342
|
+
before do
|
343
|
+
@file = stub_file('kerb.jpg', nil, 'harg.css')
|
344
|
+
end
|
345
|
+
|
346
|
+
# TODO: refactor this test so it mocks out system_call
|
347
|
+
it "should be determined via *nix exec" do
|
348
|
+
|
349
|
+
@sanitized = UploadColumn::SanitizedFile.new(@file, :get_content_type_from_file_exec => true)
|
350
|
+
|
351
|
+
@sanitized.stub!(:path).and_return('/path/to/file.jpg')
|
352
|
+
@sanitized.should_receive(:system_call).with(%(file -bi "/path/to/file.jpg")).and_return('text/monkey')
|
353
|
+
|
354
|
+
@sanitized.content_type.should == "text/monkey"
|
355
|
+
end
|
356
|
+
|
357
|
+
it "shouldn't choke up when the *nix exec errors out" do
|
358
|
+
@sanitized = UploadColumn::SanitizedFile.new(@file, :get_content_type_from_file_exec => true)
|
359
|
+
|
360
|
+
lambda {
|
361
|
+
@sanitized.should_receive(:system_call).and_raise('monkey')
|
362
|
+
@sanitized.content_type
|
363
|
+
}.should_not raise_error
|
364
|
+
end
|
365
|
+
|
366
|
+
it "should otherwise be loaded from MIME::Types" do
|
367
|
+
if defined?(MIME::Types)
|
368
|
+
@sanitized = UploadColumn::SanitizedFile.new(@file)
|
369
|
+
|
370
|
+
@sanitized.should_receive(:get_content_type_from_exec).and_return(nil) # Make sure the *nix exec isn't interfering
|
371
|
+
@sanitized.content_type.should == "text/css"
|
372
|
+
else
|
373
|
+
puts "WARNING: Could not run all examples because MIME::Types is not defined, try installing the mime-types gem!"
|
374
|
+
end
|
375
|
+
end
|
376
|
+
|
377
|
+
it "should be taken from the browser if all else fails" do
|
378
|
+
@sanitized = UploadColumn::SanitizedFile.new(@file)
|
379
|
+
|
380
|
+
@file.should_receive(:content_type).at_least(:once).and_return('application/xhtml+xml') # Set up browser behavior
|
381
|
+
# FIXME: this is brittle. There really should be another way of changing this behaviour.
|
382
|
+
@sanitized.should_receive(:get_content_type_from_mime_types).and_return(nil) # Make sure MIME::Types isn't interfering
|
383
|
+
@sanitized.content_type.should == "application/xhtml+xml"
|
384
|
+
end
|
385
|
+
end
|
386
|
+
|
387
|
+
describe "a SanitizedFile with a wrong extension" do
|
388
|
+
|
389
|
+
# This test currently always fails if MIME::Types is unavailable,
|
390
|
+
# TODO: come up with a clever way to stub out the content_type-y behaviour.
|
391
|
+
it "should fix extention if fix_file_extensions is true" do
|
392
|
+
t = UploadColumn::SanitizedFile.new(stub_file('kerb.jpg', 'image/jpeg', 'kerb.css'), :fix_file_extensions => true)
|
393
|
+
|
394
|
+
t.content_type.should == "image/jpeg"
|
395
|
+
t.extension.should == "jpeg"
|
396
|
+
t.filename.should == "kerb.jpeg"
|
397
|
+
end
|
398
|
+
|
399
|
+
it "should not fix extention if fix_file_extensions is false" do
|
400
|
+
t = UploadColumn::SanitizedFile.new(stub_file('kerb.jpg', 'image/jpeg', 'kerb.css'), :fix_file_extensions => false)
|
401
|
+
|
402
|
+
#t.content_type.should == "image/css" FIXME: the result of this is upredictable and
|
403
|
+
# differs, depending on whether or not the user has MIME::Types installed
|
404
|
+
t.extension.should == "css"
|
405
|
+
t.filename.should == "kerb.css"
|
406
|
+
end
|
407
|
+
end
|
408
|
+
|
409
|
+
describe "copying a sanitized Tempfile with permissions set" do
|
410
|
+
before do
|
411
|
+
@file = UploadColumn::SanitizedFile.new(stub_tempfile('kerb.jpg', 'image/jpeg'), :permissions => 0755)
|
412
|
+
@file = @file.copy_to(public_path('gurr.jpg'))
|
413
|
+
end
|
414
|
+
|
415
|
+
it "should set the right permissions" do
|
416
|
+
@file.should have_permissions(0755)
|
417
|
+
end
|
418
|
+
end
|
419
|
+
|
420
|
+
describe "copying a sanitized StringIO with permissions set" do
|
421
|
+
before do
|
422
|
+
@file = UploadColumn::SanitizedFile.new(stub_stringio('kerb.jpg', 'image/jpeg'), :permissions => 0755)
|
423
|
+
@file = @file.copy_to(public_path('gurr.jpg'))
|
424
|
+
end
|
425
|
+
|
426
|
+
it "should set the right permissions" do
|
427
|
+
@file.should have_permissions(0755)
|
428
|
+
end
|
429
|
+
end
|
430
|
+
|
431
|
+
describe "copying a sanitized File object with permissions set" do
|
432
|
+
before do
|
433
|
+
@file = UploadColumn::SanitizedFile.new(stub_file('kerb.jpg', 'image/jpeg'), :permissions => 0755)
|
434
|
+
@file = @file.copy_to(public_path('gurr.jpg'))
|
435
|
+
end
|
436
|
+
|
437
|
+
it "should set the right permissions" do
|
438
|
+
@file.should have_permissions(0755)
|
439
|
+
end
|
440
|
+
end
|
441
|
+
|
442
|
+
describe "copying a sanitized file by path with permissions set" do
|
443
|
+
before do
|
444
|
+
@file = UploadColumn::SanitizedFile.new(file_path('kerb.jpg'), :permissions => 0755)
|
445
|
+
@file = @file.copy_to(public_path('gurr.jpg'))
|
446
|
+
end
|
447
|
+
|
448
|
+
it "should set the right permissions" do
|
449
|
+
@file.should have_permissions(0755)
|
450
|
+
end
|
451
|
+
end
|
452
|
+
|
453
|
+
|
454
|
+
describe "moving a sanitized Tempfile with permissions set" do
|
455
|
+
before do
|
456
|
+
@file = UploadColumn::SanitizedFile.new(stub_tempfile('kerb.jpg', 'image/jpeg'), :permissions => 0755)
|
457
|
+
@file.move_to(public_path('gurr.jpg'))
|
458
|
+
end
|
459
|
+
|
460
|
+
it "should set the right permissions" do
|
461
|
+
@file.should have_permissions(0755)
|
462
|
+
end
|
463
|
+
end
|
464
|
+
|
465
|
+
describe "moving a sanitized StringIO with permissions set" do
|
466
|
+
before do
|
467
|
+
@file = UploadColumn::SanitizedFile.new(stub_stringio('kerb.jpg', 'image/jpeg'), :permissions => 0755)
|
468
|
+
@file.move_to(public_path('gurr.jpg'))
|
469
|
+
end
|
470
|
+
|
471
|
+
it "should set the right permissions" do
|
472
|
+
@file.should have_permissions(0755)
|
473
|
+
end
|
474
|
+
end
|
475
|
+
|
476
|
+
describe "moving a sanitized File object with permissions set" do
|
477
|
+
before do
|
478
|
+
@file = UploadColumn::SanitizedFile.new(stub_file('kerb.jpg', 'image/jpeg'), :permissions => 0755)
|
479
|
+
@file.move_to(public_path('gurr.jpg'))
|
480
|
+
end
|
481
|
+
|
482
|
+
it "should set the right permissions" do
|
483
|
+
@file.should have_permissions(0755)
|
484
|
+
end
|
485
|
+
end
|
486
|
+
|
487
|
+
describe "moving a sanitized file by path with permissions set" do
|
488
|
+
before do
|
489
|
+
@file = UploadColumn::SanitizedFile.new(file_path('kerb.jpg'), :permissions => 0755)
|
490
|
+
@file.move_to(public_path('gurr.jpg'))
|
491
|
+
end
|
492
|
+
|
493
|
+
it "should set the right permissions" do
|
494
|
+
@file.should have_permissions(0755)
|
495
|
+
end
|
496
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'spec'
|
4
|
+
require 'ruby-debug'
|
5
|
+
|
6
|
+
require File.join(File.dirname(__FILE__), 'custom_matchers')
|
7
|
+
|
8
|
+
RAILS_ROOT = File.expand_path(File.dirname(__FILE__)) unless defined?(RAILS_ROOT)
|
9
|
+
PUBLIC = File.expand_path(File.join(RAILS_ROOT, 'public')) unless defined?(PUBLIC)
|
10
|
+
|
11
|
+
def file_path( filename )
|
12
|
+
File.join(File.dirname(__FILE__), 'fixtures', filename)
|
13
|
+
end
|
14
|
+
|
15
|
+
def public_path( filename )
|
16
|
+
File.join(File.dirname(__FILE__), 'public', filename)
|
17
|
+
end
|
18
|
+
|
19
|
+
def stub_tempfile(filename, mime_type=nil, fake_name=nil)
|
20
|
+
raise "#{path} file does not exist" unless File.exist?(file_path(filename))
|
21
|
+
|
22
|
+
t = Tempfile.new(filename)
|
23
|
+
FileUtils.copy_file(file_path(filename), t.path)
|
24
|
+
|
25
|
+
t.stub!(:original_filename).and_return(fake_name || filename)
|
26
|
+
t.stub!(:content_type).and_return(mime_type)
|
27
|
+
t.stub!(:local_path).and_return(t.path)
|
28
|
+
return t
|
29
|
+
end
|
30
|
+
|
31
|
+
def stub_stringio(filename, mime_type=nil, fake_name=nil)
|
32
|
+
if filename
|
33
|
+
t = StringIO.new( IO.read( file_path( filename ) ) )
|
34
|
+
else
|
35
|
+
t = StringIO.new
|
36
|
+
end
|
37
|
+
t.stub!(:local_path).and_return("")
|
38
|
+
t.stub!(:original_filename).and_return(filename || fake_name)
|
39
|
+
t.stub!(:content_type).and_return(mime_type)
|
40
|
+
return t
|
41
|
+
end
|
42
|
+
|
43
|
+
def stub_file(filename, mime_type=nil, fake_name=nil)
|
44
|
+
f = File.open(file_path(filename))
|
45
|
+
f.stub!(:content_type).and_return(mime_type)
|
46
|
+
f.stub!(:original_filename).and_return(fake_name) if fake_name
|
47
|
+
return f
|
48
|
+
end
|
49
|
+
|
50
|
+
module UploadColumnSpecHelper
|
51
|
+
|
52
|
+
def disconnected_model(model_class)
|
53
|
+
model_class.stub!(:columns).and_return([])
|
54
|
+
return model_class.new
|
55
|
+
end
|
56
|
+
|
57
|
+
def setup_standard_mocking
|
58
|
+
@options = mock('options', :null_object => true)
|
59
|
+
Entry.upload_column :avatar, @options
|
60
|
+
@entry = disconnected_model(Entry)
|
61
|
+
mock_file
|
62
|
+
end
|
63
|
+
|
64
|
+
def setup_version_mocking
|
65
|
+
Entry.upload_column :avatar, :versions => [ :thumb, :large ]
|
66
|
+
@entry = disconnected_model(Entry)
|
67
|
+
mock_file
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def mock_file
|
73
|
+
@file = mock('file')
|
74
|
+
|
75
|
+
@uploaded_file = mock('uploaded_file')
|
76
|
+
@uploaded_file.stub!(:actual_filename).and_return('monkey.png')
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
module UniversalSpecHelper
|
81
|
+
|
82
|
+
def running(&block)
|
83
|
+
lambda(&block)
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
Spec::Runner.configure do |config|
|
89
|
+
config.include UniversalSpecHelper
|
90
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
gem 'activerecord'
|
4
|
+
require 'active_record'
|
5
|
+
|
6
|
+
require File.join(File.dirname(__FILE__), '../lib/upload_column')
|
7
|
+
|
8
|
+
describe "UploadColumn" do
|
9
|
+
|
10
|
+
it "should have a default configuration" do
|
11
|
+
UploadColumn.configuration.should be_an_instance_of(Hash)
|
12
|
+
config = UploadColumn.configuration
|
13
|
+
|
14
|
+
config[:tmp_dir].should == 'tmp'
|
15
|
+
config[:store_dir].should be_an_instance_of(Proc)
|
16
|
+
config[:root_dir].should == File.join(RAILS_ROOT, 'public')
|
17
|
+
config[:get_content_type_from_file_exec].should == true
|
18
|
+
config[:fix_file_extensions].should == false
|
19
|
+
config[:process].should == nil
|
20
|
+
config[:permissions].should == 0644
|
21
|
+
config[:extensions].should == UploadColumn.extensions
|
22
|
+
config[:web_root].should == ''
|
23
|
+
config[:manipulator].should == nil
|
24
|
+
config[:versions].should == nil
|
25
|
+
config[:validate_integrity].should == false
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should have a list of allowed extensions" do
|
29
|
+
UploadColumn.extensions.should == %w(asf ai avi doc dvi dwg eps gif gz jpg jpeg mov mp3 mpeg odf pac pdf png ppt psd swf swx tar tar.gz torrent txt wmv wav xls zip)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should have a list of allowed image extensions" do
|
33
|
+
UploadColumn.image_extensions.should == %w(jpg jpeg gif png)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "UploadColumn.configure" do
|
39
|
+
|
40
|
+
after do
|
41
|
+
UploadColumn.reset_configuration
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should yield a configuration proxy" do
|
45
|
+
UploadColumn.configure do |config|
|
46
|
+
config.should be_an_instance_of(UploadColumn::ConfigurationProxy)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should change the configuration of a known option" do
|
51
|
+
UploadColumn.configure do |config|
|
52
|
+
config.web_root = "/monkey"
|
53
|
+
end
|
54
|
+
|
55
|
+
UploadColumn.configuration[:web_root].should == "/monkey"
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should change the configuration of an unknown option" do
|
59
|
+
UploadColumn.configure do |config|
|
60
|
+
config.monkey = ":)"
|
61
|
+
end
|
62
|
+
|
63
|
+
UploadColumn.configuration[:monkey].should == ":)"
|
64
|
+
end
|
65
|
+
end
|