root/vendor/file-column-0.3.1/test/file_column_test.rb @ 2

Revision 2, 14.9 kB (checked in by falcon, 17 years ago)

added plugins

  • Property svn:executable set to *
Line 
1require 'abstract_unit'
2
3class Entry < ActiveRecord::Base
4    attr_accessor :validation_should_fail
5   
6    def validate
7        errors.add("image","some stupid error") if @validation_should_fail
8    end
9
10    def after_assign
11      @after_assign_called = true
12    end
13
14    def after_assign_called?
15      @after_assign_called
16    end
17end
18
19class Movie < ActiveRecord::Base
20end
21
22
23class FileColumnTest < Test::Unit::TestCase
24 
25  def setup
26    # we define the file_columns here so that we can change
27    # settings easily in a single test
28
29    Entry.file_column :image
30    Entry.file_column :file
31    Movie.file_column :movie
32   
33  end
34 
35  def teardown
36    FileUtils.rm_rf File.dirname(__FILE__)+"/public/entry/"
37    FileUtils.rm_rf File.dirname(__FILE__)+"/public/movie/"
38  end
39 
40  def test_column_write_method
41    assert Entry.new.respond_to?("image=")
42  end
43 
44  def test_column_read_method
45    assert Entry.new.respond_to?("image")
46  end
47 
48  def test_sanitize_filename
49    assert_equal "test.jpg", FileColumn::sanitize_filename("test.jpg")
50    assert FileColumn::sanitize_filename("../../very_tricky/foo.bar") !~ /[\\\/]/, "slashes not removed"
51    assert_equal "__foo", FileColumn::sanitize_filename('`*foo')
52    assert_equal "foo.txt", FileColumn::sanitize_filename('c:\temp\foo.txt')
53    assert_equal "_.", FileColumn::sanitize_filename(".")
54  end
55 
56  def test_default_options
57    e = Entry.new
58    assert_match %r{/public/entry/image}, e.image_options[:store_dir]
59    assert_match %r{/public/entry/image/tmp}, e.image_options[:tmp_base_dir]
60  end
61 
62  def test_assign_without_save_with_tempfile
63    do_test_assign_without_save(:tempfile)
64  end
65 
66  def test_assign_without_save_with_stringio
67    do_test_assign_without_save(:stringio)
68  end
69 
70  def do_test_assign_without_save(upload_type)
71    e = Entry.new
72    e.image = uploaded_file(file_path("skanthak.png"), "image/png", "skanthak.png", upload_type)
73    assert e.image.is_a?(String), "#{e.image.inspect} is not a String"
74    assert File.exists?(e.image)
75    assert FileUtils.identical?(e.image, file_path("skanthak.png"))
76  end
77 
78  def test_filename_preserved
79    e = Entry.new
80    e.image = uploaded_file(file_path("kerb.jpg"), "image/jpeg", "local_filename.jpg")
81    assert_equal "local_filename.jpg", File.basename(e.image)
82  end
83 
84  def test_filename_stored_in_attribute
85    e = Entry.new("image" => uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg"))
86    assert_equal "kerb.jpg", e["image"]
87  end
88 
89  def test_extension_added
90    e = Entry.new
91    e.image = uploaded_file(file_path("kerb.jpg"), "image/jpeg", "local_filename")
92    assert_equal "local_filename.jpg", File.basename(e.image)
93    assert_equal "local_filename.jpg", e["image"]
94  end
95 
96  def test_extension_unknown_type
97    e = Entry.new
98    e.image = uploaded_file(file_path("kerb.jpg"), "not/known", "local_filename")
99    assert_equal "local_filename", File.basename(e.image)
100    assert_equal "local_filename", e["image"]
101  end
102
103  def test_extension_unknown_type_with_extension
104    e = Entry.new
105    e.image = uploaded_file(file_path("kerb.jpg"), "not/known", "local_filename.abc")
106    assert_equal "local_filename.abc", File.basename(e.image)
107    assert_equal "local_filename.abc", e["image"]
108  end
109
110  def test_extension_corrected
111    e = Entry.new
112    e.image = uploaded_file(file_path("kerb.jpg"), "image/jpeg", "local_filename.jpeg")
113    assert_equal "local_filename.jpg", File.basename(e.image)
114    assert_equal "local_filename.jpg", e["image"]
115  end
116
117  def test_double_extension
118    e = Entry.new
119    e.image = uploaded_file(file_path("kerb.jpg"), "application/x-tgz", "local_filename.tar.gz")
120    assert_equal "local_filename.tar.gz", File.basename(e.image)
121    assert_equal "local_filename.tar.gz", e["image"]
122  end
123
124  def test_get_content_type_with_file
125    options = FileColumn::ClassMethods::DEFAULT_OPTIONS.dup
126    options[:file_exec] = "/usr/bin/file"
127
128    # run this test only if the machine we are running on
129    # has the file utility installed
130    if File.executable?(options[:file_exec])
131      file = FileColumn::TempUploadedFile.new(options, nil, nil)
132      file.instance_variable_set :@local_file_path, file_path("kerb.jpg")
133     
134      assert_equal "image/jpeg", file.get_content_type
135    else
136      puts "Warning: Skipping test_get_content_type_with_file test as '#{options[:file_exec]}' does not exist"
137    end
138  end
139
140  def test_do_not_fix_file_extensions
141    options = FileColumn::ClassMethods::DEFAULT_OPTIONS.dup
142    options[:base_path] = File.join(options[:root_path], "entry")
143    options = FileColumn.init_options(options, "entry", "image")
144    options[:fix_file_extensions] = nil
145
146    entry = Entry.new
147    file = FileColumn::NoUploadedFile.new(options, entry, "image")
148    file = file.assign uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb")
149
150    assert_equal "kerb", File.basename(file.absolute_path)
151  end
152
153  def test_correct_extension
154    file = FileColumn::TempUploadedFile.new(FileColumn::ClassMethods::DEFAULT_OPTIONS, nil, nil)
155   
156    assert_equal "filename.jpg", file.correct_extension("filename.jpeg","jpg")
157    assert_equal "filename.tar.gz", file.correct_extension("filename.jpg","tar.gz")
158    assert_equal "filename.jpg", file.correct_extension("filename.tar.gz","jpg")
159    assert_equal "Protokoll_01.09.2005.doc", file.correct_extension("Protokoll_01.09.2005","doc")
160    assert_equal "strange.filenames.exist.jpg", file.correct_extension("strange.filenames.exist","jpg")
161    assert_equal "another.strange.one.jpg", file.correct_extension("another.strange.one.png","jpg")
162  end
163
164  def test_assign_with_save
165    e = Entry.new
166    e.image = uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg")
167    tmp_file_path = e.image
168    assert e.save
169    assert File.exists?(e.image)
170    assert FileUtils.identical?(e.image, file_path("kerb.jpg"))
171    assert_equal "#{e.id}/kerb.jpg", e.image_relative_path
172    assert !File.exists?(tmp_file_path), "temporary file '#{tmp_file_path}' not removed"
173    assert !File.exists?(File.dirname(tmp_file_path)), "temporary directory '#{File.dirname(tmp_file_path)}' not removed"
174   
175    local_path = e.image
176    e = Entry.find(e.id)
177    assert_equal local_path, e.image
178  end
179 
180  def test_dir_methods
181    e = Entry.new
182    e.image = uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg")
183    e.save
184   
185    assert_equal File.expand_path(File.join(RAILS_ROOT, "public", "entry", "image", e.id.to_s)),
186    e.image_dir
187    assert_equal File.join(e.id.to_s), 
188    e.image_relative_dir
189  end
190
191  def test_suffix_parameter
192    e = Entry.new
193    assert_nil e.image("thumb")
194    assert_nil e.image("thumb")
195
196    e.image = uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg")
197   
198    assert_equal "kerb-thumb.jpg", File.basename(e.image("thumb"))
199    assert_equal "kerb-thumb.jpg", File.basename(e.image_relative_path("thumb"))  end
200
201  def test_absolute_path_is_simple
202    # we make :root_path more complicated to test that it is normalized in absolute paths
203    Entry.file_column :image, {:root_path => File.join(RAILS_ROOT, "public") + "/../public" }
204   
205    e = Entry.new
206    e.image = uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg")
207    assert File.exists?(e.image)
208    assert e.image !~ /\.\./, "#{e.image} is not a simple path"
209  end
210
211
212  def test_cleanup_after_destroy
213    e = Entry.new("image" => uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg"))
214    assert e.save
215    local_path = e.image
216    assert File.exists?(local_path)
217    assert e.destroy
218    assert !File.exists?(local_path), "'#{local_path}' still exists although entry was destroyed"
219    assert !File.exists?(File.dirname(local_path))
220  end
221 
222  def test_keep_tmp_image
223    e = Entry.new("image" => uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg"))
224    e.validation_should_fail = true
225    assert !e.save, "e should not save due to validation errors"
226    assert File.exists?(local_path = e.image)
227    image_temp = e.image_temp
228    e = Entry.new("image_temp" => image_temp)
229    assert_equal local_path, e.image
230    assert e.save
231    assert FileUtils.identical?(e.image, file_path("kerb.jpg"))
232  end
233 
234  def test_keep_tmp_image_with_existing_image
235    e = Entry.new("image" =>uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg"))
236    assert e.save
237    assert File.exists?(local_path = e.image)
238    e = Entry.find(e.id)
239    e.image = uploaded_file(file_path("skanthak.png"), "image/png", "skanthak.png")
240    e.validation_should_fail = true
241    assert !e.save
242    temp_path = e.image_temp
243    e = Entry.find(e.id)
244    e.image_temp = temp_path
245    assert e.save
246   
247    assert FileUtils.identical?(e.image, file_path("skanthak.png"))
248    assert !File.exists?(local_path), "old image has not been deleted"
249  end
250 
251  def test_replace_tmp_image_temp_first
252    do_test_replace_tmp_image([:image_temp, :image])
253  end
254 
255  def test_replace_tmp_image_temp_last
256    do_test_replace_tmp_image([:image, :image_temp])
257  end
258 
259  def do_test_replace_tmp_image(order)
260    e = Entry.new("image" => uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg"))
261    e.validation_should_fail = true
262    assert !e.save
263    image_temp = e.image_temp
264    temp_path = e.image
265    new_img = uploaded_file(file_path("skanthak.png"), "image/png", "skanthak.png")
266    e = Entry.new
267    for method in order
268      case method
269      when :image_temp then e.image_temp = image_temp
270      when :image then e.image = new_img
271      end
272    end
273    assert e.save
274    assert FileUtils.identical?(e.image, file_path("skanthak.png")), "'#{e.image}' is not the expected 'skanthak.png'"
275    assert !File.exists?(temp_path), "temporary file '#{temp_path}' is not cleaned up"
276    assert !File.exists?(File.dirname(temp_path)), "temporary directory not cleaned up"
277    assert e.image_just_uploaded?
278  end
279 
280  def test_replace_image_on_saved_object
281    e = Entry.new("image" => uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg"))
282    assert e.save
283    old_file = e.image
284    e = Entry.find(e.id)
285    e.image = uploaded_file(file_path("skanthak.png"), "image/png", "skanthak.png")
286    assert e.save
287    assert FileUtils.identical?(file_path("skanthak.png"), e.image)
288    assert old_file != e.image
289    assert !File.exists?(old_file), "'#{old_file}' has not been cleaned up"
290  end
291 
292  def test_edit_without_touching_image
293    e = Entry.new("image" => uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg"))
294    assert e.save
295    e = Entry.find(e.id)
296    assert e.save
297    assert FileUtils.identical?(file_path("kerb.jpg"), e.image)
298  end
299 
300  def test_save_without_image
301    e = Entry.new
302    assert e.save
303    e.reload
304    assert_nil e.image
305  end
306 
307  def test_delete_saved_image
308    e = Entry.new("image" => uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg"))
309    assert e.save
310    local_path = e.image
311    e.image = nil
312    assert_nil e.image
313    assert File.exists?(local_path), "file '#{local_path}' should not be deleted until transaction is saved"
314    assert e.save
315    assert_nil e.image
316    assert !File.exists?(local_path)
317    e.reload
318    assert_nil e["image"]
319    e = Entry.find(e.id)
320    assert_nil e.image
321  end
322 
323  def test_delete_tmp_image
324    e = Entry.new("image" => uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg"))
325    local_path = e.image
326    e.image = nil
327    assert_nil e.image
328    assert !File.exists?(local_path)
329  end
330 
331  def test_delete_nonexistant_image
332    e = Entry.new
333    e.image = nil
334    assert e.save
335    assert_nil e.image
336  end
337 
338  def test_ie_filename
339    e = Entry.new("image" => uploaded_file(file_path("kerb.jpg"), "image/jpeg", 'c:\images\kerb.jpg'))
340    assert e.image_relative_path =~ /^tmp\/[\d\.]+\/kerb\.jpg$/, "relative path '#{e.image_relative_path}' was not as expected"
341    assert File.exists?(e.image)
342  end
343 
344  def test_just_uploaded?
345    e = Entry.new("image" => uploaded_file(file_path("kerb.jpg"), "image/jpeg", 'c:\images\kerb.jpg'))
346    assert e.image_just_uploaded?
347    assert e.save
348    assert e.image_just_uploaded?
349   
350    e = Entry.new("image" => uploaded_file(file_path("kerb.jpg"), "image/jpeg", 'kerb.jpg'))
351    temp_path = e.image_temp
352    e = Entry.new("image_temp" => temp_path)
353    assert !e.image_just_uploaded?
354    assert e.save
355    assert !e.image_just_uploaded?
356  end
357 
358  def test_empty_tmp
359    e = Entry.new
360    e.image_temp = ""
361    assert_nil e.image
362  end
363 
364  def test_empty_tmp_with_image
365    e = Entry.new
366    e.image_temp = ""
367    e.image = uploaded_file(file_path("kerb.jpg"), "image/jpeg", 'c:\images\kerb.jpg')
368    local_path = e.image
369    assert File.exists?(local_path)
370    e.image_temp = ""
371    assert local_path, e.image
372  end
373 
374  def test_empty_filename
375    e = Entry.new
376    assert_equal "", e["file"]
377    assert_nil e.file
378    assert_nil e["image"]
379    assert_nil e.image
380  end
381 
382  def test_with_two_file_columns
383    e = Entry.new
384    e.image = uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg")
385    e.file = uploaded_file(file_path("skanthak.png"), "image/png", "skanthak.png")
386    assert e.save
387    assert_match %{/entry/image/}, e.image
388    assert_match %{/entry/file/}, e.file
389    assert FileUtils.identical?(e.image, file_path("kerb.jpg"))
390    assert FileUtils.identical?(e.file, file_path("skanthak.png"))
391  end
392 
393  def test_with_two_models
394    e = Entry.new(:image => uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg"))
395    m = Movie.new(:movie => uploaded_file(file_path("skanthak.png"), "image/png", "skanthak.png"))
396    assert e.save
397    assert m.save
398    assert_match %{/entry/image/}, e.image
399    assert_match %{/movie/movie/}, m.movie
400    assert FileUtils.identical?(e.image, file_path("kerb.jpg"))
401    assert FileUtils.identical?(m.movie, file_path("skanthak.png"))
402  end
403
404  def test_no_file_uploaded
405    e = Entry.new
406    assert_nothing_raised { e.image =
407        uploaded_file(nil, "application/octet-stream", "", :stringio) }
408    assert_equal nil, e.image
409  end
410
411  # when safari submits a form where no file has been
412  # selected, it does not transmit a content-type and
413  # the result is an empty string ""
414  def test_no_file_uploaded_with_safari
415    e = Entry.new
416    assert_nothing_raised { e.image = "" }
417    assert_equal nil, e.image
418  end
419
420  def test_detect_wrong_encoding
421    e = Entry.new
422    assert_raise(TypeError) { e.image ="img42.jpg" }
423  end
424
425  def test_move_additional_files_from_tmp
426    e = Entry.new
427    e.image = uploaded_file(file_path("skanthak.png"), "image/png", "skanthak.png")
428    FileUtils.cp file_path("kerb.jpg"), File.dirname(e.image)
429    assert e.save
430    dir = File.dirname(e.image)
431    assert File.exists?(File.join(dir, "skanthak.png"))
432    assert File.exists?(File.join(dir, "kerb.jpg"))
433  end
434
435  def test_serializable_before_save
436    e = Entry.new
437    e.image = uploaded_file(file_path("skanthak.png"), "image/png", "skanthak.png")
438    assert_nothing_raised { 
439      flash = Marshal.dump(e) 
440      e = Marshal.load(flash)
441    }
442    assert File.exists?(e.image)
443  end
444
445  def test_after_assign
446    Entry.file_column :image, :after_assign => [:after_assign]
447    e = Entry.new
448    e.image = upload("skanthak.png")
449    assert e.after_assign_called?
450  end
451end
Note: See TracBrowser for help on using the browser.