test/integration_test.rb in paperclip-3.0.2 vs test/integration_test.rb in paperclip-3.0.3

- old
+ new

@@ -1,17 +1,22 @@ +# encoding: utf-8 + require './test/helper' +require 'open-uri' class IntegrationTest < Test::Unit::TestCase context "Many models at once" do setup do rebuild_model - @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb') + @file = File.new(fixture_file("5k.png"), 'rb') 300.times do |i| Dummy.create! :avatar => @file end end + teardown { @file.close } + should "not exceed the open file limit" do assert_nothing_raised do dummies = Dummy.find(:all) dummies.each { |dummy| dummy.avatar } end @@ -20,13 +25,11 @@ context "An attachment" do setup do rebuild_model :styles => { :thumb => "50x50#" } @dummy = Dummy.new - @file = File.new(File.join(File.dirname(__FILE__), - "fixtures", - "5k.png"), 'rb') + @file = File.new(fixture_file("5k.png"), 'rb') @dummy.avatar = @file assert @dummy.save end teardown { @file.close } @@ -77,17 +80,15 @@ end end context "Attachment" do setup do - @thumb_path = "./test/../public/system/dummies/avatars/000/000/001/thumb/5k.png" + @thumb_path = "tmp/public/system/dummies/avatars/000/000/001/thumb/5k.png" File.delete(@thumb_path) if File.exists?(@thumb_path) rebuild_model :styles => { :thumb => "50x50#" } @dummy = Dummy.new - @file = File.new(File.join(File.dirname(__FILE__), - "fixtures", - "5k.png"), 'rb') + @file = File.new(fixture_file("5k.png"), 'rb') end teardown { @file.close } @@ -106,19 +107,17 @@ end end context "Attachment with no generated thumbnails" do setup do - @thumb_small_path = "./test/../public/system/dummies/avatars/000/000/001/thumb_small/5k.png" - @thumb_large_path = "./test/../public/system/dummies/avatars/000/000/001/thumb_large/5k.png" + @thumb_small_path = "tmp/public/system/dummies/avatars/000/000/001/thumb_small/5k.png" + @thumb_large_path = "tmp/public/system/dummies/avatars/000/000/001/thumb_large/5k.png" File.delete(@thumb_small_path) if File.exists?(@thumb_small_path) File.delete(@thumb_large_path) if File.exists?(@thumb_large_path) rebuild_model :styles => { :thumb_small => "50x50#", :thumb_large => "60x60#" } @dummy = Dummy.new - @file = File.new(File.join(File.dirname(__FILE__), - "fixtures", - "5k.png"), 'rb') + @file = File.new(fixture_file("5k.png"), 'rb') @dummy.avatar.post_processing = false @dummy.avatar = @file assert @dummy.save @dummy.avatar.post_processing = true @@ -151,13 +150,11 @@ context "A model that modifies its original" do setup do rebuild_model :styles => { :original => "2x2#" } @dummy = Dummy.new - @file = File.new(File.join(File.dirname(__FILE__), - "fixtures", - "5k.png"), 'rb') + @file = File.new(fixture_file("5k.png"), 'rb') @dummy.avatar = @file end should "report the file size of the processed file and not the original" do assert_not_equal File.size(@file.path), @dummy.avatar.size @@ -170,13 +167,11 @@ setup do rebuild_model :styles => { :large => "100x100", :medium => "50x50" }, :path => ":rails_root/tmp/:id/:attachments/:style.:extension" @dummy = Dummy.new - @file = File.new(File.join(File.dirname(__FILE__), - "fixtures", - "5k.png"), 'rb') + @file = File.new(fixture_file("5k.png"), 'rb') @dummy.avatar = @file end teardown { @file.close } @@ -279,16 +274,17 @@ [000,002,022].each do |umask| context "when the umask is #{umask}" do setup do rebuild_model @dummy = Dummy.new - @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb') + @file = File.new(fixture_file("5k.png"), 'rb') @umask = File.umask(umask) end teardown do File.umask @umask + @file.close end should "respect the current umask" do @dummy.avatar = @file @dummy.save @@ -304,18 +300,20 @@ :thumb => ["32x32#", :gif] }, :default_style => :medium, :url => "/:attachment/:class/:style/:id/:basename.:extension", :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension" @dummy = Dummy.new - @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb') - @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"), 'rb') + @file = File.new(fixture_file("5k.png"), 'rb') + @bad_file = File.new(fixture_file("bad.png"), 'rb') assert @dummy.avatar = @file assert @dummy.valid?, @dummy.errors.full_messages.join(", ") assert @dummy.save end + teardown { [@file, @bad_file].each(&:close) } + should "write and delete its files" do [["434x66", :original], ["300x46", :large], ["100x15", :medium], ["32x32", :thumb]].each do |geo, style| @@ -400,15 +398,17 @@ end context "that is assigned its file from another Paperclip attachment" do setup do @dummy2 = Dummy.new - @file2 = File.new(File.join(FIXTURES_DIR, "12k.png"), 'rb') + @file2 = File.new(fixture_file("12k.png"), 'rb') assert @dummy2.avatar = @file2 @dummy2.save end + teardown { @file2.close } + should "work when assigned a file" do assert_not_equal `identify -format "%wx%h" "#{@dummy.avatar.path(:original)}"`, `identify -format "%wx%h" "#{@dummy2.avatar.path(:original)}"` assert @dummy.avatar = @dummy2.avatar @@ -425,24 +425,60 @@ setup do Dummy.class_eval do has_many :attachments, :class_name => 'Dummy' end + @file = File.new(fixture_file("5k.png"), 'rb') @dummy = Dummy.new - @dummy.avatar = File.new(File.join(File.dirname(__FILE__), - "fixtures", - "5k.png"), 'rb') + @dummy.avatar = @file end + teardown { @file.close } + should "should not error when saving" do - assert_nothing_raised do - @dummy.save! + @dummy.save! + end + end + + context "A model with an attachment with hash in file name" do + setup do + @settings = { :styles => { :thumb => "50x50#" }, + :path => ":rails_root/public/system/:attachment/:id_partition/:style/:hash.:extension", + :url => "/system/:attachment/:id_partition/:style/:hash.:extension", + :hash_secret => "somesecret" } + + rebuild_model @settings + + @file = File.new(fixture_file("5k.png"), 'rb') + @dummy = Dummy.create! :avatar => @file + end + + teardown do + @file.close + end + + should "be accessible" do + assert File.exists?(@dummy.avatar.path(:original)) + assert File.exists?(@dummy.avatar.path(:thumb)) + end + + context "when new style is added" do + setup do + @dummy.avatar.options[:styles][:mini] = "25x25#" + @dummy.avatar.instance_variable_set :@normalized_styles, nil + @dummy.avatar.reprocess! 'mini' end + + should "make all the styles accessible" do + assert File.exists?(@dummy.avatar.path(:original)) + assert File.exists?(@dummy.avatar.path(:thumb)) + assert File.exists?(@dummy.avatar.path(:mini)) + end end end - if ENV['S3_TEST_BUCKET'] + if ENV['S3_BUCKET'] def s3_files_for attachment [:thumb, :medium, :large, :original].inject({}) do |files, style| data = `curl "#{attachment.url(style)}" 2>/dev/null`.chomp t = Tempfile.new("paperclip-test") t.binmode @@ -465,25 +501,33 @@ setup do rebuild_model :styles => { :large => "300x300>", :medium => "100x100", :thumb => ["32x32#", :gif] }, :storage => :s3, - :s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml")), + :s3_credentials => File.new(fixture_file('s3.yml')), + :s3_options => { :logger => Paperclip.logger }, :default_style => :medium, - :bucket => ENV['S3_TEST_BUCKET'], + :bucket => ENV['S3_BUCKET'], :path => ":class/:attachment/:id/:style/:basename.:extension" + @dummy = Dummy.new - @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb') - @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"), 'rb') + @file = File.new(fixture_file('5k.png'), 'rb') + @bad_file = File.new(fixture_file('bad.png'), 'rb') - assert @dummy.avatar = @file - assert @dummy.valid? - assert @dummy.save + @dummy.avatar = @file + @dummy.valid? + @dummy.save! - @files_on_s3 = s3_files_for @dummy.avatar + @files_on_s3 = s3_files_for(@dummy.avatar) end + teardown do + @file.close + @bad_file.close + @files_on_s3.values.each(&:close) if @files_on_s3 + end + context 'assigning itself to a new model' do setup do @d2 = Dummy.new @d2.avatar = @dummy.avatar @d2.save @@ -515,19 +559,10 @@ ["32x32", :thumb]].each do |geo, style| cmd = %Q[identify -format "%wx%h" "#{@d2_files[style].path}"] assert_equal geo, `#{cmd}`.chomp, cmd end - @dummy.avatar = "not a valid file but not nil" - assert_equal File.basename(@file.path), @dummy.avatar_file_name - assert @dummy.valid? - assert @dummy.save - - [:thumb, :medium, :large, :original].each do |style| - assert @dummy.avatar.exists?(style) - end - @dummy.avatar.clear assert_nil @dummy.avatar_file_name assert @dummy.valid? assert @dummy.save @@ -541,30 +576,31 @@ should "work exactly the same when new as when reloaded" do @d2 = Dummy.find(@dummy.id) assert_equal @dummy.avatar_file_name, @d2.avatar_file_name + [:thumb, :medium, :large, :original].each do |style| - assert_equal @dummy.avatar.to_file(style).read, @d2.avatar.to_file(style).read + begin + first_file = open(@dummy.avatar.url(style)) + second_file = open(@dummy.avatar.url(style)) + assert_equal first_file.read, second_file.read + ensure + first_file.close if first_file + second_file.close if second_file + end end - saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) } - @d2.avatar.clear assert @d2.save [:thumb, :medium, :large, :original].each do |style| assert ! @dummy.avatar.exists?(style) end end - should "know the difference between good files, bad files, not files, and nil" do - expected = @dummy.avatar.to_file - @dummy.avatar = "not a file" - assert @dummy.valid? - assert_equal expected.read, @dummy.avatar.to_file.read - + should "know the difference between good files, bad files, and nil" do @dummy.avatar = @bad_file assert ! @dummy.valid? @dummy.avatar = nil assert @dummy.valid? @@ -589,9 +625,20 @@ should "have the right content type" do headers = s3_headers_for(@dummy.avatar, :original) assert_equal 'image/png', headers['content-type'] end + + context "with non-english character in the file name" do + setup do + + @file.stubs(:original_filename).returns("クリップ.png") + @dummy.avatar = @file + end + + should "not raise any error" do + @dummy.save! + end + end end end end -