diff --git a/test/factories/upload.rb b/test/factories/upload.rb index c4ac00104..cd6a89d0d 100644 --- a/test/factories/upload.rb +++ b/test/factories/upload.rb @@ -1,5 +1,3 @@ -require 'fileutils' - FactoryGirl.define do factory(:upload) do rating "s" @@ -15,51 +13,10 @@ FactoryGirl.define do end factory(:jpg_upload) do - content_type "image/jpeg" file do f = Tempfile.new - f.write(File.read("#{Rails.root}/test/files/test.jpg")) - f.seek(0) - f - end - end - - factory(:exif_jpg_upload) do - content_type "image/jpeg" - file_path do - FileUtils.cp("#{Rails.root}/test/files/test-exif-small.jpg", "#{Rails.root}/tmp") - "#{Rails.root}/tmp/test-exif-small.jpg" - end - end - - factory(:blank_jpg_upload) do - content_type "image/jpeg" - file_path do - FileUtils.cp("#{Rails.root}/test/files/test-blank.jpg", "#{Rails.root}/tmp") - "#{Rails.root}/tmp/test-blank.jpg" - end - end - - factory(:large_jpg_upload) do - file_ext "jpg" - content_type "image/jpeg" - file_path do - FileUtils.cp("#{Rails.root}/test/files/test-large.jpg", "#{Rails.root}/tmp") - "#{Rails.root}/tmp/test-large.jpg" - end - end - - factory(:png_upload) do - file_path do - FileUtils.cp("#{Rails.root}/test/files/test.png", "#{Rails.root}/tmp") - "#{Rails.root}/tmp/test.png" - end - end - - factory(:gif_upload) do - file_path do - FileUtils.cp("#{Rails.root}/test/files/test.gif", "#{Rails.root}/tmp") - "#{Rails.root}/tmp/test.gif" + IO.copy_stream("#{Rails.root}/test/files/test.jpg", f.path) + ActionDispatch::Http::UploadedFile.new(tempfile: f, filename: "test.jpg") end end end diff --git a/test/functional/post_replacements_controller_test.rb b/test/functional/post_replacements_controller_test.rb index c4d05597e..5944215de 100644 --- a/test/functional/post_replacements_controller_test.rb +++ b/test/functional/post_replacements_controller_test.rb @@ -39,7 +39,7 @@ class PostReplacementsControllerTest < ActionController::TestCase assert_response :success assert_equal("https://www.google.com/intl/en_ALL/images/logo.gif", @post.source) assert_equal("e80d1c59a673f560785784fb1ac10959", @post.md5) - assert_equal("e80d1c59a673f560785784fb1ac10959", Digest::MD5.file(@post.file_path).hexdigest) + assert_equal("e80d1c59a673f560785784fb1ac10959", Digest::MD5.file(@post.file(:original)).hexdigest) end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 477e9eac2..9a7dd29da 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -45,6 +45,7 @@ class ActiveSupport::TestCase end teardown do + FileUtils.rm_rf(Danbooru.config.storage_manager.base_dir) Cache.clear end end diff --git a/test/test_helpers/download_helper.rb b/test/test_helpers/download_helper.rb index 9097e7cea..8381f4629 100644 --- a/test/test_helpers/download_helper.rb +++ b/test/test_helpers/download_helper.rb @@ -1,18 +1,14 @@ module DownloadTestHelper def assert_downloaded(expected_filesize, source) - tempfile = Tempfile.new("danbooru-test") - download = Downloads::File.new(source, tempfile.path) - assert_nothing_raised(Downloads::File::Error) do - download.download! + tempfile = Downloads::File.new(source).download! + assert_equal(expected_filesize, tempfile.size, "Tested source URL: #{source}") + tempfile.close! end - - assert_equal(expected_filesize, tempfile.size, "Tested source URL: #{source}") end def assert_rewritten(expected_source, test_source) - tempfile = Tempfile.new("danbooru-test") - download = Downloads::File.new(test_source, tempfile.path) + download = Downloads::File.new(test_source) rewritten_source, _, _ = download.before_download(test_source, {}) assert_match(expected_source, rewritten_source, "Tested source URL: #{test_source}") diff --git a/test/test_helpers/upload_test_helper.rb b/test/test_helpers/upload_test_helper.rb index 3b29574e6..66b404278 100644 --- a/test/test_helpers/upload_test_helper.rb +++ b/test/test_helpers/upload_test_helper.rb @@ -1,23 +1,10 @@ module UploadTestHelper - def upload_file(path, content_type, filename) - tempfile = Tempfile.new(filename) - FileUtils.copy_file(path, tempfile.path) + def upload_file(path) + file = Tempfile.new(binmode: true) + IO.copy_stream("#{Rails.root}/#{path}", file.path) + uploaded_file = ActionDispatch::Http::UploadedFile.new(tempfile: file, filename: File.basename(path)) - (class << tempfile; self; end).class_eval do - alias local_path path - define_method(:tempfile) {self} - define_method(:original_filename) {filename} - define_method(:content_type) {content_type} - end - - tempfile - end - - def upload_jpeg(path) - upload_file(path, "image/jpeg", File.basename(path)) - end - - def upload_zip(path) - upload_file(path, "application/zip", File.basename(path)) + yield uploaded_file if block_given? + uploaded_file end end diff --git a/test/unit/downloads/art_station_test.rb b/test/unit/downloads/art_station_test.rb index 077007b8d..528210bdb 100644 --- a/test/unit/downloads/art_station_test.rb +++ b/test/unit/downloads/art_station_test.rb @@ -5,9 +5,7 @@ module Downloads context "a download for a (small) artstation image" do setup do @source = "https://cdnb3.artstation.com/p/assets/images/images/003/716/071/large/aoi-ogata-hate-city.jpg?1476754974" - @tempfile = Tempfile.new("danbooru-test") - @download = Downloads::File.new(@source, @tempfile.path) - @download.download! + @download = Downloads::File.new(@source) end should "download the large image instead" do @@ -18,8 +16,7 @@ module Downloads context "for an image where an original does not exist" do setup do @source = "https://cdna.artstation.com/p/assets/images/images/004/730/278/large/mendel-oh-dragonll.jpg" - @tempfile = Tempfile.new("danbooru-test") - @download = Downloads::File.new(@source, @tempfile.path) + @download = Downloads::File.new(@source) @download.download! end @@ -38,8 +35,7 @@ module Downloads context "a download for a https://$artist.artstation.com/projects/$id page" do setup do @source = "https://dantewontdie.artstation.com/projects/YZK5q" - @tempfile = Tempfile.new("danbooru-test") - @download = Downloads::File.new(@source, @tempfile.path) + @download = Downloads::File.new(@source) @download.download! end diff --git a/test/unit/downloads/deviant_art_test.rb b/test/unit/downloads/deviant_art_test.rb index 42013174d..7cb58c5bd 100644 --- a/test/unit/downloads/deviant_art_test.rb +++ b/test/unit/downloads/deviant_art_test.rb @@ -5,9 +5,8 @@ module Downloads context "a download for a deviant art html page" do setup do @source = "http://starbitt.deviantart.com/art/09271X-636962118" - @tempfile = Tempfile.new("danbooru-test") - @download = Downloads::File.new(@source, @tempfile.path) - @download.download! + @download = Downloads::File.new(@source) + @tempfile = @download.download! end should "set the html page as the source" do diff --git a/test/unit/downloads/file_test.rb b/test/unit/downloads/file_test.rb index 126b4f174..7698941d8 100644 --- a/test/unit/downloads/file_test.rb +++ b/test/unit/downloads/file_test.rb @@ -5,12 +5,7 @@ module Downloads context "A twitter video download" do setup do @source = "https://twitter.com/CincinnatiZoo/status/859073537713328129" - @tempfile = Tempfile.new("danbooru-test") - @download = Downloads::File.new(@source, @tempfile.path) - end - - teardown do - @tempfile.close + @download = Downloads::File.new(@source) end should "preserve the twitter source" do @@ -22,12 +17,8 @@ module Downloads context "A post download" do setup do @source = "http://www.google.com/intl/en_ALL/images/logo.gif" + @download = Downloads::File.new(@source) @tempfile = Tempfile.new("danbooru-test") - @download = Downloads::File.new(@source, @tempfile.path) - end - - teardown do - @tempfile.close end context "that fails" do @@ -49,10 +40,9 @@ module Downloads end should "store the file in the tempfile path" do - @download.download! + tempfile = @download.download! assert_equal(@source, @download.source) - assert(::File.exists?(@tempfile.path), "temp file should exist") - assert(::File.size(@tempfile.path) > 0, "should have data") + assert_operator(tempfile.size, :>, 0, "should have data") end end end diff --git a/test/unit/downloads/pixiv_test.rb b/test/unit/downloads/pixiv_test.rb index 66cabf113..3441ee81b 100644 --- a/test/unit/downloads/pixiv_test.rb +++ b/test/unit/downloads/pixiv_test.rb @@ -4,13 +4,9 @@ module Downloads class PixivTest < ActiveSupport::TestCase context "An ugoira site for pixiv" do setup do - @tempfile = Tempfile.new("danbooru-test") - @download = Downloads::File.new("http://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247364", @tempfile.path) - @download.download! - end - - teardown do - @tempfile.unlink + @download = Downloads::File.new("http://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247364") + @tempfile = @download.download! + @tempfile.close! end should "capture the data" do diff --git a/test/unit/pixiv_ugoira_converter_test.rb b/test/unit/pixiv_ugoira_converter_test.rb index ed7b9a146..bba9e4c58 100644 --- a/test/unit/pixiv_ugoira_converter_test.rb +++ b/test/unit/pixiv_ugoira_converter_test.rb @@ -3,9 +3,7 @@ require "test_helper" class PixivUgoiraConverterTest < ActiveSupport::TestCase context "An ugoira converter" do setup do - @zipped_body = "#{Rails.root}/test/fixtures/ugoira.zip" - @write_file = Tempfile.new("converted") - @preview_write_file = Tempfile.new("preview") + @zipfile = upload_file("test/fixtures/ugoira.zip").tempfile @frame_data = [ {"file" => "000000.jpg", "delay" => 200}, {"file" => "000001.jpg", "delay" => 200}, @@ -15,16 +13,11 @@ class PixivUgoiraConverterTest < ActiveSupport::TestCase ] end - teardown do - @write_file.unlink - @preview_write_file.unlink - end - should "output to webm" do - @converter = PixivUgoiraConverter - @converter.convert(@zipped_body, @write_file.path, @preview_write_file.path, @frame_data) - assert_operator(File.size(@write_file.path), :>, 1_000) - assert_operator(File.size(@preview_write_file.path), :>, 0) + sample_file = PixivUgoiraConverter.generate_webm(@zipfile, @frame_data) + preview_file = PixivUgoiraConverter.generate_preview(@zipfile) + assert_operator(sample_file.size, :>, 1_000) + assert_operator(preview_file.size, :>, 0) end end -end \ No newline at end of file +end diff --git a/test/unit/post_replacement_test.rb b/test/unit/post_replacement_test.rb index 08ecea2e5..8d8df7653 100644 --- a/test/unit/post_replacement_test.rb +++ b/test/unit/post_replacement_test.rb @@ -1,15 +1,6 @@ require 'test_helper' class PostReplacementTest < ActiveSupport::TestCase - def upload_file(path, filename, &block) - Tempfile.open do |file| - file.write(File.read(path)) - file.seek(0) - uploaded_file = ActionDispatch::Http::UploadedFile.new(tempfile: file, filename: filename) - yield uploaded_file - end - end - def setup super @@ -68,7 +59,7 @@ class PostReplacementTest < ActiveSupport::TestCase assert_equal(5969, @post.file_size) assert_equal("png", @post.file_ext) assert_equal("8f9327db2597fa57d2f42b4a6c5a9855", @post.md5) - assert_equal("8f9327db2597fa57d2f42b4a6c5a9855", Digest::MD5.file(@post.file_path).hexdigest) + assert_equal("8f9327db2597fa57d2f42b4a6c5a9855", Digest::MD5.file(@post.file).hexdigest) assert_equal("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png", @post.source) end end @@ -102,7 +93,7 @@ class PostReplacementTest < ActiveSupport::TestCase assert_equal(8558, @post.file_size) assert_equal("gif", @post.file_ext) assert_equal("e80d1c59a673f560785784fb1ac10959", @post.md5) - assert_equal("e80d1c59a673f560785784fb1ac10959", Digest::MD5.file(@post.file_path).hexdigest) + assert_equal("e80d1c59a673f560785784fb1ac10959", Digest::MD5.file(@post.file).hexdigest) assert_equal("https://www.google.com/intl/en_ALL/images/logo.gif", @post.source) end @@ -155,18 +146,17 @@ class PostReplacementTest < ActiveSupport::TestCase assert_equal(16275, @post.file_size) assert_equal("png", @post.file_ext) assert_equal("4ceadc314938bc27f3574053a3e1459a", @post.md5) - assert_equal("4ceadc314938bc27f3574053a3e1459a", Digest::MD5.file(@post.file_path).hexdigest) + assert_equal("4ceadc314938bc27f3574053a3e1459a", Digest::MD5.file(@post.file).hexdigest) assert_equal("https://i.pximg.net/img-original/img/2017/04/04/08/54/15/62247350_p0.png", @post.source) assert_equal("https://i.pximg.net/img-original/img/2017/04/04/08/54/15/62247350_p0.png", @post.replacements.last.replacement_url) end - should "delete the old files after three days" do - old_file_path, old_preview_file_path, old_large_file_path = @post.file_path, @post.preview_file_path, @post.large_file_path + should "delete the old files after thirty days" do + old_file_path, old_preview_file_path = @post.file(:original).path, @post.file(:preview).path @post.replace!(replacement_url: "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247350") assert(File.exists?(old_file_path)) assert(File.exists?(old_preview_file_path)) - assert(File.exists?(old_large_file_path)) Timecop.travel(Time.now + PostReplacement::DELETION_GRACE_PERIOD + 1.day) do Delayed::Worker.new.work_off @@ -174,7 +164,6 @@ class PostReplacementTest < ActiveSupport::TestCase assert_not(File.exists?(old_file_path)) assert_not(File.exists?(old_preview_file_path)) - assert_not(File.exists?(old_large_file_path)) end end @@ -188,7 +177,7 @@ class PostReplacementTest < ActiveSupport::TestCase assert_equal(2804, @post.file_size) assert_equal("zip", @post.file_ext) assert_equal("cad1da177ef309bf40a117c17b8eecf5", @post.md5) - assert_equal("cad1da177ef309bf40a117c17b8eecf5", Digest::MD5.file(@post.file_path).hexdigest) + assert_equal("cad1da177ef309bf40a117c17b8eecf5", Digest::MD5.file(@post.file).hexdigest) assert_equal("https://i.pximg.net/img-zip-ugoira/img/2017/04/04/08/57/38/62247364_ugoira1920x1080.zip", @post.source) assert_equal([{"file"=>"000000.jpg", "delay"=>125}, {"file"=>"000001.jpg", "delay"=>125}], @post.pixiv_ugoira_frame_data.data) @@ -201,17 +190,15 @@ class PostReplacementTest < ActiveSupport::TestCase @post.replace!(replacement_url: "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247364") @post.replace!(replacement_url: "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247350") - assert(File.exists?(@post.file_path)) - assert(File.exists?(@post.preview_file_path)) - assert(File.exists?(@post.large_file_path)) + assert_nothing_raised { @post.file(:original) } + assert_nothing_raised { @post.file(:preview) } Timecop.travel(Time.now + PostReplacement::DELETION_GRACE_PERIOD + 1.day) do Delayed::Worker.new.work_off end - assert(File.exists?(@post.file_path)) - assert(File.exists?(@post.preview_file_path)) - assert(File.exists?(@post.large_file_path)) + assert_nothing_raised { @post.file(:original) } + assert_nothing_raised { @post.file(:preview) } end end @@ -231,14 +218,14 @@ class PostReplacementTest < ActiveSupport::TestCase Delayed::Worker.new.work_off end - assert(File.exists?(@post1.file_path)) - assert(File.exists?(@post2.file_path)) + assert_nothing_raised { @post1.file(:original) } + assert_nothing_raised { @post2.file(:original) } end end context "a post with an uploaded file" do should "work" do - upload_file("#{Rails.root}/test/files/test.png", "test.png") do |file| + upload_file("test/files/test.png") do |file| @post.replace!(replacement_file: file, replacement_url: "") assert_equal(@post.md5, Digest::MD5.file(file.tempfile).hexdigest) assert_equal("file://test.png", @post.replacements.last.replacement_url) @@ -268,7 +255,7 @@ class PostReplacementTest < ActiveSupport::TestCase context "a post with the same file" do should "not raise a duplicate error" do - upload_file("#{Rails.root}/test/files/test.jpg", "test.jpg") do |file| + upload_file("test/files/test.jpg") do |file| assert_nothing_raised do @post.replace!(replacement_file: file, replacement_url: "") end @@ -276,7 +263,7 @@ class PostReplacementTest < ActiveSupport::TestCase end should "not queue a deletion or log a comment" do - upload_file("#{Rails.root}/test/files/test.jpg", "test.jpg") do |file| + upload_file("test/files/test.jpg") do |file| assert_no_difference(["@post.comments.count"]) do @post.replace!(replacement_file: file, replacement_url: "") end diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index d805c11fe..a09ba0134 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -35,17 +35,15 @@ class PostTest < ActiveSupport::TestCase end should "delete the files" do - assert_equal(true, File.exists?(@post.preview_file_path)) - assert_equal(true, File.exists?(@post.large_file_path)) - assert_equal(true, File.exists?(@post.file_path)) + assert_nothing_raised { @post.file(:preview) } + assert_nothing_raised { @post.file(:original) } TestAfterCommit.with_commits(true) do @post.expunge! end - assert_equal(false, File.exists?(@post.preview_file_path)) - assert_equal(false, File.exists?(@post.large_file_path)) - assert_equal(false, File.exists?(@post.file_path)) + assert_raise(StandardError) { @post.file(:preview) } + assert_raise(StandardError) { @post.file(:original) } end should "remove all favorites" do diff --git a/test/unit/upload_test.rb b/test/unit/upload_test.rb index 022d66e6a..2a4dc196d 100644 --- a/test/unit/upload_test.rb +++ b/test/unit/upload_test.rb @@ -17,21 +17,15 @@ class UploadTest < ActiveSupport::TestCase teardown do CurrentUser.user = nil CurrentUser.ip_addr = nil - - @upload.delete_temp_file if @upload end context "An upload" do - teardown do - FileUtils.rm_f(Dir.glob("#{Rails.root}/tmp/test.*")) - end - context "from a user that is limited" do setup do CurrentUser.user = FactoryGirl.create(:user, :created_at => 1.year.ago) User.any_instance.stubs(:upload_limit).returns(0) end - + should "fail creation" do @upload = FactoryGirl.build(:jpg_upload, :tag_string => "") @upload.save @@ -41,73 +35,51 @@ class UploadTest < ActiveSupport::TestCase context "image size calculator" do should "discover the dimensions for a compressed SWF" do - @upload = FactoryGirl.create(:upload, :file_path => "#{Rails.root}/test/files/compressed.swf") - @upload.calculate_dimensions(@upload.file_path) - assert_equal(607, @upload.image_width) - assert_equal(756, @upload.image_height) + @upload = FactoryGirl.create(:upload, file: upload_file("test/files/compressed.swf")) + assert_equal([607, 756], @upload.calculate_dimensions) end should "discover the dimensions for a JPG with JFIF data" do @upload = FactoryGirl.create(:jpg_upload) - assert_nothing_raised {@upload.calculate_dimensions(@upload.file_path)} - assert_equal(500, @upload.image_width) - assert_equal(335, @upload.image_height) + assert_equal([500, 335], @upload.calculate_dimensions) end should "discover the dimensions for a JPG with EXIF data" do - @upload = FactoryGirl.create(:exif_jpg_upload) - assert_nothing_raised {@upload.calculate_dimensions(@upload.file_path)} - assert_equal(529, @upload.image_width) - assert_equal(600, @upload.image_height) + @upload = FactoryGirl.create(:upload, file: upload_file("test/files/test-exif-small.jpg")) + assert_equal([529, 600], @upload.calculate_dimensions) end should "discover the dimensions for a JPG with no header data" do - @upload = FactoryGirl.create(:blank_jpg_upload) - assert_nothing_raised {@upload.calculate_dimensions(@upload.file_path)} - assert_equal(668, @upload.image_width) - assert_equal(996, @upload.image_height) + @upload = FactoryGirl.create(:upload, file: upload_file("test/files/test-blank.jpg")) + assert_equal([668, 996], @upload.calculate_dimensions) end should "discover the dimensions for a PNG" do - @upload = FactoryGirl.create(:png_upload) - assert_nothing_raised {@upload.calculate_dimensions(@upload.file_path)} - assert_equal(768, @upload.image_width) - assert_equal(1024, @upload.image_height) + @upload = FactoryGirl.create(:upload, file: upload_file("test/files/test.png")) + assert_equal([768, 1024], @upload.calculate_dimensions) end should "discover the dimensions for a GIF" do - @upload = FactoryGirl.create(:gif_upload) - assert_nothing_raised {@upload.calculate_dimensions(@upload.file_path)} - assert_equal(400, @upload.image_width) - assert_equal(400, @upload.image_height) + @upload = FactoryGirl.create(:upload, file: upload_file("test/files/test.gif")) + assert_equal([400, 400], @upload.calculate_dimensions) end end context "content type calculator" do should "know how to parse jpeg, png, gif, and swf file headers" do - @upload = FactoryGirl.create(:jpg_upload) - assert_equal("image/jpeg", @upload.file_header_to_content_type("#{Rails.root}/test/files/test.jpg")) - assert_equal("image/gif", @upload.file_header_to_content_type("#{Rails.root}/test/files/test.gif")) - assert_equal("image/png", @upload.file_header_to_content_type("#{Rails.root}/test/files/test.png")) - assert_equal("application/x-shockwave-flash", @upload.file_header_to_content_type("#{Rails.root}/test/files/compressed.swf")) - assert_equal("application/octet-stream", @upload.file_header_to_content_type("#{Rails.root}/README.md")) - end - - should "know how to parse jpeg, png, gif, and swf content types" do - @upload = FactoryGirl.create(:jpg_upload) - assert_equal("jpg", @upload.content_type_to_file_ext("image/jpeg")) - assert_equal("gif", @upload.content_type_to_file_ext("image/gif")) - assert_equal("png", @upload.content_type_to_file_ext("image/png")) - assert_equal("swf", @upload.content_type_to_file_ext("application/x-shockwave-flash")) - assert_equal("bin", @upload.content_type_to_file_ext("")) + @upload = FactoryGirl.build(:jpg_upload) + assert_equal("jpg", @upload.file_header_to_file_ext(File.open("#{Rails.root}/test/files/test.jpg"))) + assert_equal("gif", @upload.file_header_to_file_ext(File.open("#{Rails.root}/test/files/test.gif"))) + assert_equal("png", @upload.file_header_to_file_ext(File.open("#{Rails.root}/test/files/test.png"))) + assert_equal("swf", @upload.file_header_to_file_ext(File.open("#{Rails.root}/test/files/compressed.swf"))) + assert_equal("bin", @upload.file_header_to_file_ext(File.open("#{Rails.root}/README.md"))) end end context "downloader" do context "for a zip that is not an ugoira" do should "not validate" do - FileUtils.cp("#{Rails.root}/test/files/invalid_ugoira.zip", "#{Rails.root}/tmp") - @upload = Upload.create(:file => upload_zip("#{Rails.root}/tmp/invalid_ugoira.zip"), :rating => "q", :tag_string => "xxx") + @upload = FactoryGirl.create(:upload, file: upload_file("test/files/invalid_ugoira.zip")) @upload.process! assert_equal("error: RuntimeError - missing frame data for ugoira", @upload.status) end @@ -116,30 +88,15 @@ class UploadTest < ActiveSupport::TestCase context "that is a pixiv ugoira" do setup do @url = "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=46378654" - @upload = FactoryGirl.create(:source_upload, :source => @url, :tag_string => "ugoira") - @output_file = Tempfile.new("download") + @upload = FactoryGirl.create(:upload, :source => @url, :tag_string => "ugoira") end - teardown do - @output_file.unlink - end - should "process successfully" do - @upload.download_from_source(@output_file.path) - assert_operator(File.size(@output_file.path), :>, 1_000) - assert_equal("application/zip", @upload.file_header_to_content_type(@output_file.path)) - assert_equal("zip", @upload.content_type_to_file_ext(@upload.file_header_to_content_type(@output_file.path))) + _, _, output_file = @upload.download_from_source(@url, "") + assert_operator(output_file.size, :>, 1_000) + assert_equal("zip", @upload.file_header_to_file_ext(output_file)) end end - - should "initialize the final path after downloading a file" do - @upload = FactoryGirl.create(:source_upload) - path = "#{Rails.root}/tmp/test.download.jpg" - assert_nothing_raised {@upload.download_from_source(path)} - assert(File.exists?(path)) - assert_equal(8558, File.size(path)) - assert_equal(path, @upload.file_path) - end end context "determining if a file is downloadable" do @@ -161,46 +118,32 @@ class UploadTest < ActiveSupport::TestCase context "file processor" do should "parse and process a cgi file representation" do - FileUtils.cp("#{Rails.root}/test/files/test.jpg", "#{Rails.root}/tmp") - @upload = Upload.new(:file => upload_jpeg("#{Rails.root}/tmp/test.jpg")) - assert_nothing_raised {@upload.convert_cgi_file} - assert(File.exists?(@upload.file_path)) - assert_equal(28086, File.size(@upload.file_path)) + @upload = FactoryGirl.create(:upload, file: upload_file("test/files/test.jpg")) + assert_nothing_raised {@upload.process_upload} + assert_equal(28086, @upload.file_size) end should "process a transparent png" do - FileUtils.cp("#{Rails.root}/test/files/alpha.png", "#{Rails.root}/tmp") - @upload = Upload.new(:file => upload_file("#{Rails.root}/tmp/alpha.png", "image/png", "alpha.png")) - assert_nothing_raised {@upload.convert_cgi_file} - assert(File.exists?(@upload.file_path)) - assert_equal(1136, File.size(@upload.file_path)) + @upload = FactoryGirl.create(:upload, file: upload_file("test/files/alpha.png")) + assert_nothing_raised {@upload.process_upload} + assert_equal(1136, @upload.file_size) end end context "hash calculator" do should "caculate the hash" do @upload = FactoryGirl.create(:jpg_upload) - @upload.calculate_hash(@upload.file_path) + @upload.process_upload assert_equal("ecef68c44edb8a0d6a3070b5f8e8ee76", @upload.md5) end end context "resizer" do - teardown do - FileUtils.rm_f(Dir.glob("#{Rails.root}/public/data/preview/test.*.jpg")) - FileUtils.rm_f(Dir.glob("#{Rails.root}/public/data/sample/test.*.jpg")) - FileUtils.rm_f(Dir.glob("#{Rails.root}/public/data/test.*.jpg")) - end - should "generate several resized versions of the image" do - @upload = FactoryGirl.create(:large_jpg_upload) - @upload.calculate_hash(@upload.file_path) - @upload.calculate_dimensions(@upload.file_path) - assert_nothing_raised {@upload.generate_resizes(@upload.file_path)} - assert(File.exists?(@upload.resized_file_path_for(Danbooru.config.small_image_width))) - assert(File.size(@upload.resized_file_path_for(Danbooru.config.small_image_width)) > 0) - assert(File.exists?(@upload.resized_file_path_for(Danbooru.config.large_image_width))) - assert(File.size(@upload.resized_file_path_for(Danbooru.config.large_image_width)) > 0) + @upload = FactoryGirl.create(:upload, file_ext: "jpg", image_width: 1356, image_height: 911, file: upload_file("test/files/test-large.jpg")) + preview_file, sample_file = @upload.generate_resizes + assert_operator(preview_file.size, :>, 1_000) + assert_operator(sample_file.size, :>, 1_000) end end @@ -215,13 +158,10 @@ class UploadTest < ActiveSupport::TestCase context "with an artist commentary" do setup do @upload = FactoryGirl.create(:source_upload, - :rating => "s", - :uploader_ip_addr => "127.0.0.1", - :tag_string => "hoge foo" - ) - @upload.include_artist_commentary = "1" - @upload.artist_commentary_title = "" - @upload.artist_commentary_desc = "blah" + include_artist_commentary: "1", + artist_commentary_title: "", + artist_commentary_desc: "blah", + ) end should "create an artist commentary when processed" do @@ -258,12 +198,7 @@ class UploadTest < ActiveSupport::TestCase end should "process completely for a pixiv ugoira" do - @upload = FactoryGirl.create(:source_upload, - :source => "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=46378654", - :rating => "s", - :uploader_ip_addr => "127.0.0.1", - :tag_string => "hoge foo" - ) + @upload = FactoryGirl.create(:source_upload, source: "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=46378654") assert_difference(["PixivUgoiraFrameData.count", "Post.count"]) do @upload.process! assert_equal([], @upload.errors.full_messages) @@ -274,18 +209,17 @@ class UploadTest < ActiveSupport::TestCase assert_equal(60, post.image_width) assert_equal(60, post.image_height) assert_equal("https://i.pximg.net/img-zip-ugoira/img/2014/10/05/23/42/23/46378654_ugoira1920x1080.zip", post.source) - assert_operator(File.size(post.large_file_path), :>, 0) - assert_operator(File.size(post.preview_file_path), :>, 0) + assert_nothing_raised { post.file(:original) } + assert_nothing_raised { post.file(:preview) } end should "process completely for an uploaded image" do @upload = FactoryGirl.create(:jpg_upload, :rating => "s", :uploader_ip_addr => "127.0.0.1", - :tag_string => "hoge foo" + :tag_string => "hoge foo", + :file => upload_file("test/files/test.jpg"), ) - @upload.file = upload_jpeg("#{Rails.root}/test/files/test.jpg") - @upload.convert_cgi_file assert_difference("Post.count") do assert_nothing_raised {@upload.process!} @@ -297,8 +231,8 @@ class UploadTest < ActiveSupport::TestCase assert_equal("127.0.0.1", post.uploader_ip_addr.to_s) assert_equal(@upload.md5, post.md5) assert_equal("jpg", post.file_ext) - assert(File.exists?(post.file_path)) - assert_equal(28086, File.size(post.file_path)) + assert_nothing_raised { post.file(:original) } + assert_equal(28086, post.file(:original).size) assert_equal(post.id, @upload.post_id) assert_equal("completed", @upload.status) end @@ -310,16 +244,5 @@ class UploadTest < ActiveSupport::TestCase assert_nothing_raised {@upload.process!} end end - - should "delete the temporary file upon completion" do - @upload = FactoryGirl.create(:source_upload, - :rating => "s", - :uploader_ip_addr => "127.0.0.1", - :tag_string => "hoge foo" - ) - - @upload.process! - assert(!File.exists?(@upload.temp_file_path)) - end end end