tests: fix upload tests.

This commit is contained in:
evazion
2018-03-19 23:17:24 -05:00
parent 41a4ff15cd
commit d089be9f8a
13 changed files with 96 additions and 273 deletions

View File

@@ -1,5 +1,3 @@
require 'fileutils'
FactoryGirl.define do FactoryGirl.define do
factory(:upload) do factory(:upload) do
rating "s" rating "s"
@@ -15,51 +13,10 @@ FactoryGirl.define do
end end
factory(:jpg_upload) do factory(:jpg_upload) do
content_type "image/jpeg"
file do file do
f = Tempfile.new f = Tempfile.new
f.write(File.read("#{Rails.root}/test/files/test.jpg")) IO.copy_stream("#{Rails.root}/test/files/test.jpg", f.path)
f.seek(0) ActionDispatch::Http::UploadedFile.new(tempfile: f, filename: "test.jpg")
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"
end end
end end
end end

View File

@@ -39,7 +39,7 @@ class PostReplacementsControllerTest < ActionController::TestCase
assert_response :success assert_response :success
assert_equal("https://www.google.com/intl/en_ALL/images/logo.gif", @post.source) assert_equal("https://www.google.com/intl/en_ALL/images/logo.gif", @post.source)
assert_equal("e80d1c59a673f560785784fb1ac10959", @post.md5) 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
end end

View File

@@ -45,6 +45,7 @@ class ActiveSupport::TestCase
end end
teardown do teardown do
FileUtils.rm_rf(Danbooru.config.storage_manager.base_dir)
Cache.clear Cache.clear
end end
end end

View File

@@ -1,18 +1,14 @@
module DownloadTestHelper module DownloadTestHelper
def assert_downloaded(expected_filesize, source) 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 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 end
assert_equal(expected_filesize, tempfile.size, "Tested source URL: #{source}")
end end
def assert_rewritten(expected_source, test_source) def assert_rewritten(expected_source, test_source)
tempfile = Tempfile.new("danbooru-test") download = Downloads::File.new(test_source)
download = Downloads::File.new(test_source, tempfile.path)
rewritten_source, _, _ = download.before_download(test_source, {}) rewritten_source, _, _ = download.before_download(test_source, {})
assert_match(expected_source, rewritten_source, "Tested source URL: #{test_source}") assert_match(expected_source, rewritten_source, "Tested source URL: #{test_source}")

View File

@@ -1,23 +1,10 @@
module UploadTestHelper module UploadTestHelper
def upload_file(path, content_type, filename) def upload_file(path)
tempfile = Tempfile.new(filename) file = Tempfile.new(binmode: true)
FileUtils.copy_file(path, tempfile.path) 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 yield uploaded_file if block_given?
alias local_path path uploaded_file
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))
end end
end end

View File

@@ -5,9 +5,7 @@ module Downloads
context "a download for a (small) artstation image" do context "a download for a (small) artstation image" do
setup do setup do
@source = "https://cdnb3.artstation.com/p/assets/images/images/003/716/071/large/aoi-ogata-hate-city.jpg?1476754974" @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)
@download = Downloads::File.new(@source, @tempfile.path)
@download.download!
end end
should "download the large image instead" do should "download the large image instead" do
@@ -18,8 +16,7 @@ module Downloads
context "for an image where an original does not exist" do context "for an image where an original does not exist" do
setup do setup do
@source = "https://cdna.artstation.com/p/assets/images/images/004/730/278/large/mendel-oh-dragonll.jpg" @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)
@download = Downloads::File.new(@source, @tempfile.path)
@download.download! @download.download!
end end
@@ -38,8 +35,7 @@ module Downloads
context "a download for a https://$artist.artstation.com/projects/$id page" do context "a download for a https://$artist.artstation.com/projects/$id page" do
setup do setup do
@source = "https://dantewontdie.artstation.com/projects/YZK5q" @source = "https://dantewontdie.artstation.com/projects/YZK5q"
@tempfile = Tempfile.new("danbooru-test") @download = Downloads::File.new(@source)
@download = Downloads::File.new(@source, @tempfile.path)
@download.download! @download.download!
end end

View File

@@ -5,9 +5,8 @@ module Downloads
context "a download for a deviant art html page" do context "a download for a deviant art html page" do
setup do setup do
@source = "http://starbitt.deviantart.com/art/09271X-636962118" @source = "http://starbitt.deviantart.com/art/09271X-636962118"
@tempfile = Tempfile.new("danbooru-test") @download = Downloads::File.new(@source)
@download = Downloads::File.new(@source, @tempfile.path) @tempfile = @download.download!
@download.download!
end end
should "set the html page as the source" do should "set the html page as the source" do

View File

@@ -5,12 +5,7 @@ module Downloads
context "A twitter video download" do context "A twitter video download" do
setup do setup do
@source = "https://twitter.com/CincinnatiZoo/status/859073537713328129" @source = "https://twitter.com/CincinnatiZoo/status/859073537713328129"
@tempfile = Tempfile.new("danbooru-test") @download = Downloads::File.new(@source)
@download = Downloads::File.new(@source, @tempfile.path)
end
teardown do
@tempfile.close
end end
should "preserve the twitter source" do should "preserve the twitter source" do
@@ -22,12 +17,8 @@ module Downloads
context "A post download" do context "A post download" do
setup do setup do
@source = "http://www.google.com/intl/en_ALL/images/logo.gif" @source = "http://www.google.com/intl/en_ALL/images/logo.gif"
@download = Downloads::File.new(@source)
@tempfile = Tempfile.new("danbooru-test") @tempfile = Tempfile.new("danbooru-test")
@download = Downloads::File.new(@source, @tempfile.path)
end
teardown do
@tempfile.close
end end
context "that fails" do context "that fails" do
@@ -49,10 +40,9 @@ module Downloads
end end
should "store the file in the tempfile path" do should "store the file in the tempfile path" do
@download.download! tempfile = @download.download!
assert_equal(@source, @download.source) assert_equal(@source, @download.source)
assert(::File.exists?(@tempfile.path), "temp file should exist") assert_operator(tempfile.size, :>, 0, "should have data")
assert(::File.size(@tempfile.path) > 0, "should have data")
end end
end end
end end

View File

@@ -4,13 +4,9 @@ module Downloads
class PixivTest < ActiveSupport::TestCase class PixivTest < ActiveSupport::TestCase
context "An ugoira site for pixiv" do context "An ugoira site for pixiv" do
setup do setup do
@tempfile = Tempfile.new("danbooru-test") @download = Downloads::File.new("http://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247364")
@download = Downloads::File.new("http://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247364", @tempfile.path) @tempfile = @download.download!
@download.download! @tempfile.close!
end
teardown do
@tempfile.unlink
end end
should "capture the data" do should "capture the data" do

View File

@@ -3,9 +3,7 @@ require "test_helper"
class PixivUgoiraConverterTest < ActiveSupport::TestCase class PixivUgoiraConverterTest < ActiveSupport::TestCase
context "An ugoira converter" do context "An ugoira converter" do
setup do setup do
@zipped_body = "#{Rails.root}/test/fixtures/ugoira.zip" @zipfile = upload_file("test/fixtures/ugoira.zip").tempfile
@write_file = Tempfile.new("converted")
@preview_write_file = Tempfile.new("preview")
@frame_data = [ @frame_data = [
{"file" => "000000.jpg", "delay" => 200}, {"file" => "000000.jpg", "delay" => 200},
{"file" => "000001.jpg", "delay" => 200}, {"file" => "000001.jpg", "delay" => 200},
@@ -15,16 +13,11 @@ class PixivUgoiraConverterTest < ActiveSupport::TestCase
] ]
end end
teardown do
@write_file.unlink
@preview_write_file.unlink
end
should "output to webm" do should "output to webm" do
@converter = PixivUgoiraConverter sample_file = PixivUgoiraConverter.generate_webm(@zipfile, @frame_data)
@converter.convert(@zipped_body, @write_file.path, @preview_write_file.path, @frame_data) preview_file = PixivUgoiraConverter.generate_preview(@zipfile)
assert_operator(File.size(@write_file.path), :>, 1_000) assert_operator(sample_file.size, :>, 1_000)
assert_operator(File.size(@preview_write_file.path), :>, 0) assert_operator(preview_file.size, :>, 0)
end end
end end
end end

View File

@@ -1,15 +1,6 @@
require 'test_helper' require 'test_helper'
class PostReplacementTest < ActiveSupport::TestCase 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 def setup
super super
@@ -68,7 +59,7 @@ class PostReplacementTest < ActiveSupport::TestCase
assert_equal(5969, @post.file_size) assert_equal(5969, @post.file_size)
assert_equal("png", @post.file_ext) assert_equal("png", @post.file_ext)
assert_equal("8f9327db2597fa57d2f42b4a6c5a9855", @post.md5) 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) assert_equal("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png", @post.source)
end end
end end
@@ -102,7 +93,7 @@ class PostReplacementTest < ActiveSupport::TestCase
assert_equal(8558, @post.file_size) assert_equal(8558, @post.file_size)
assert_equal("gif", @post.file_ext) assert_equal("gif", @post.file_ext)
assert_equal("e80d1c59a673f560785784fb1ac10959", @post.md5) 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) assert_equal("https://www.google.com/intl/en_ALL/images/logo.gif", @post.source)
end end
@@ -155,18 +146,17 @@ class PostReplacementTest < ActiveSupport::TestCase
assert_equal(16275, @post.file_size) assert_equal(16275, @post.file_size)
assert_equal("png", @post.file_ext) assert_equal("png", @post.file_ext)
assert_equal("4ceadc314938bc27f3574053a3e1459a", @post.md5) 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.source)
assert_equal("https://i.pximg.net/img-original/img/2017/04/04/08/54/15/62247350_p0.png", @post.replacements.last.replacement_url) assert_equal("https://i.pximg.net/img-original/img/2017/04/04/08/54/15/62247350_p0.png", @post.replacements.last.replacement_url)
end end
should "delete the old files after three days" do should "delete the old files after thirty days" do
old_file_path, old_preview_file_path, old_large_file_path = @post.file_path, @post.preview_file_path, @post.large_file_path 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") @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_file_path))
assert(File.exists?(old_preview_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 Timecop.travel(Time.now + PostReplacement::DELETION_GRACE_PERIOD + 1.day) do
Delayed::Worker.new.work_off 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_file_path))
assert_not(File.exists?(old_preview_file_path)) assert_not(File.exists?(old_preview_file_path))
assert_not(File.exists?(old_large_file_path))
end end
end end
@@ -188,7 +177,7 @@ class PostReplacementTest < ActiveSupport::TestCase
assert_equal(2804, @post.file_size) assert_equal(2804, @post.file_size)
assert_equal("zip", @post.file_ext) assert_equal("zip", @post.file_ext)
assert_equal("cad1da177ef309bf40a117c17b8eecf5", @post.md5) 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("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) 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=62247364")
@post.replace!(replacement_url: "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247350") @post.replace!(replacement_url: "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247350")
assert(File.exists?(@post.file_path)) assert_nothing_raised { @post.file(:original) }
assert(File.exists?(@post.preview_file_path)) assert_nothing_raised { @post.file(:preview) }
assert(File.exists?(@post.large_file_path))
Timecop.travel(Time.now + PostReplacement::DELETION_GRACE_PERIOD + 1.day) do Timecop.travel(Time.now + PostReplacement::DELETION_GRACE_PERIOD + 1.day) do
Delayed::Worker.new.work_off Delayed::Worker.new.work_off
end end
assert(File.exists?(@post.file_path)) assert_nothing_raised { @post.file(:original) }
assert(File.exists?(@post.preview_file_path)) assert_nothing_raised { @post.file(:preview) }
assert(File.exists?(@post.large_file_path))
end end
end end
@@ -231,14 +218,14 @@ class PostReplacementTest < ActiveSupport::TestCase
Delayed::Worker.new.work_off Delayed::Worker.new.work_off
end end
assert(File.exists?(@post1.file_path)) assert_nothing_raised { @post1.file(:original) }
assert(File.exists?(@post2.file_path)) assert_nothing_raised { @post2.file(:original) }
end end
end end
context "a post with an uploaded file" do context "a post with an uploaded file" do
should "work" 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: "") @post.replace!(replacement_file: file, replacement_url: "")
assert_equal(@post.md5, Digest::MD5.file(file.tempfile).hexdigest) assert_equal(@post.md5, Digest::MD5.file(file.tempfile).hexdigest)
assert_equal("file://test.png", @post.replacements.last.replacement_url) 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 context "a post with the same file" do
should "not raise a duplicate error" 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 assert_nothing_raised do
@post.replace!(replacement_file: file, replacement_url: "") @post.replace!(replacement_file: file, replacement_url: "")
end end
@@ -276,7 +263,7 @@ class PostReplacementTest < ActiveSupport::TestCase
end end
should "not queue a deletion or log a comment" do 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 assert_no_difference(["@post.comments.count"]) do
@post.replace!(replacement_file: file, replacement_url: "") @post.replace!(replacement_file: file, replacement_url: "")
end end

View File

@@ -35,17 +35,15 @@ class PostTest < ActiveSupport::TestCase
end end
should "delete the files" do should "delete the files" do
assert_equal(true, File.exists?(@post.preview_file_path)) assert_nothing_raised { @post.file(:preview) }
assert_equal(true, File.exists?(@post.large_file_path)) assert_nothing_raised { @post.file(:original) }
assert_equal(true, File.exists?(@post.file_path))
TestAfterCommit.with_commits(true) do TestAfterCommit.with_commits(true) do
@post.expunge! @post.expunge!
end end
assert_equal(false, File.exists?(@post.preview_file_path)) assert_raise(StandardError) { @post.file(:preview) }
assert_equal(false, File.exists?(@post.large_file_path)) assert_raise(StandardError) { @post.file(:original) }
assert_equal(false, File.exists?(@post.file_path))
end end
should "remove all favorites" do should "remove all favorites" do

View File

@@ -17,21 +17,15 @@ class UploadTest < ActiveSupport::TestCase
teardown do teardown do
CurrentUser.user = nil CurrentUser.user = nil
CurrentUser.ip_addr = nil CurrentUser.ip_addr = nil
@upload.delete_temp_file if @upload
end end
context "An upload" do context "An upload" do
teardown do
FileUtils.rm_f(Dir.glob("#{Rails.root}/tmp/test.*"))
end
context "from a user that is limited" do context "from a user that is limited" do
setup do setup do
CurrentUser.user = FactoryGirl.create(:user, :created_at => 1.year.ago) CurrentUser.user = FactoryGirl.create(:user, :created_at => 1.year.ago)
User.any_instance.stubs(:upload_limit).returns(0) User.any_instance.stubs(:upload_limit).returns(0)
end end
should "fail creation" do should "fail creation" do
@upload = FactoryGirl.build(:jpg_upload, :tag_string => "") @upload = FactoryGirl.build(:jpg_upload, :tag_string => "")
@upload.save @upload.save
@@ -41,73 +35,51 @@ class UploadTest < ActiveSupport::TestCase
context "image size calculator" do context "image size calculator" do
should "discover the dimensions for a compressed SWF" do should "discover the dimensions for a compressed SWF" do
@upload = FactoryGirl.create(:upload, :file_path => "#{Rails.root}/test/files/compressed.swf") @upload = FactoryGirl.create(:upload, file: upload_file("test/files/compressed.swf"))
@upload.calculate_dimensions(@upload.file_path) assert_equal([607, 756], @upload.calculate_dimensions)
assert_equal(607, @upload.image_width)
assert_equal(756, @upload.image_height)
end end
should "discover the dimensions for a JPG with JFIF data" do should "discover the dimensions for a JPG with JFIF data" do
@upload = FactoryGirl.create(:jpg_upload) @upload = FactoryGirl.create(:jpg_upload)
assert_nothing_raised {@upload.calculate_dimensions(@upload.file_path)} assert_equal([500, 335], @upload.calculate_dimensions)
assert_equal(500, @upload.image_width)
assert_equal(335, @upload.image_height)
end end
should "discover the dimensions for a JPG with EXIF data" do should "discover the dimensions for a JPG with EXIF data" do
@upload = FactoryGirl.create(:exif_jpg_upload) @upload = FactoryGirl.create(:upload, file: upload_file("test/files/test-exif-small.jpg"))
assert_nothing_raised {@upload.calculate_dimensions(@upload.file_path)} assert_equal([529, 600], @upload.calculate_dimensions)
assert_equal(529, @upload.image_width)
assert_equal(600, @upload.image_height)
end end
should "discover the dimensions for a JPG with no header data" do should "discover the dimensions for a JPG with no header data" do
@upload = FactoryGirl.create(:blank_jpg_upload) @upload = FactoryGirl.create(:upload, file: upload_file("test/files/test-blank.jpg"))
assert_nothing_raised {@upload.calculate_dimensions(@upload.file_path)} assert_equal([668, 996], @upload.calculate_dimensions)
assert_equal(668, @upload.image_width)
assert_equal(996, @upload.image_height)
end end
should "discover the dimensions for a PNG" do should "discover the dimensions for a PNG" do
@upload = FactoryGirl.create(:png_upload) @upload = FactoryGirl.create(:upload, file: upload_file("test/files/test.png"))
assert_nothing_raised {@upload.calculate_dimensions(@upload.file_path)} assert_equal([768, 1024], @upload.calculate_dimensions)
assert_equal(768, @upload.image_width)
assert_equal(1024, @upload.image_height)
end end
should "discover the dimensions for a GIF" do should "discover the dimensions for a GIF" do
@upload = FactoryGirl.create(:gif_upload) @upload = FactoryGirl.create(:upload, file: upload_file("test/files/test.gif"))
assert_nothing_raised {@upload.calculate_dimensions(@upload.file_path)} assert_equal([400, 400], @upload.calculate_dimensions)
assert_equal(400, @upload.image_width)
assert_equal(400, @upload.image_height)
end end
end end
context "content type calculator" do context "content type calculator" do
should "know how to parse jpeg, png, gif, and swf file headers" do should "know how to parse jpeg, png, gif, and swf file headers" do
@upload = FactoryGirl.create(:jpg_upload) @upload = FactoryGirl.build(:jpg_upload)
assert_equal("image/jpeg", @upload.file_header_to_content_type("#{Rails.root}/test/files/test.jpg")) assert_equal("jpg", @upload.file_header_to_file_ext(File.open("#{Rails.root}/test/files/test.jpg")))
assert_equal("image/gif", @upload.file_header_to_content_type("#{Rails.root}/test/files/test.gif")) assert_equal("gif", @upload.file_header_to_file_ext(File.open("#{Rails.root}/test/files/test.gif")))
assert_equal("image/png", @upload.file_header_to_content_type("#{Rails.root}/test/files/test.png")) assert_equal("png", @upload.file_header_to_file_ext(File.open("#{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("swf", @upload.file_header_to_file_ext(File.open("#{Rails.root}/test/files/compressed.swf")))
assert_equal("application/octet-stream", @upload.file_header_to_content_type("#{Rails.root}/README.md")) assert_equal("bin", @upload.file_header_to_file_ext(File.open("#{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(""))
end end
end end
context "downloader" do context "downloader" do
context "for a zip that is not an ugoira" do context "for a zip that is not an ugoira" do
should "not validate" do should "not validate" do
FileUtils.cp("#{Rails.root}/test/files/invalid_ugoira.zip", "#{Rails.root}/tmp") @upload = FactoryGirl.create(:upload, file: upload_file("test/files/invalid_ugoira.zip"))
@upload = Upload.create(:file => upload_zip("#{Rails.root}/tmp/invalid_ugoira.zip"), :rating => "q", :tag_string => "xxx")
@upload.process! @upload.process!
assert_equal("error: RuntimeError - missing frame data for ugoira", @upload.status) assert_equal("error: RuntimeError - missing frame data for ugoira", @upload.status)
end end
@@ -116,30 +88,15 @@ class UploadTest < ActiveSupport::TestCase
context "that is a pixiv ugoira" do context "that is a pixiv ugoira" do
setup do setup do
@url = "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=46378654" @url = "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=46378654"
@upload = FactoryGirl.create(:source_upload, :source => @url, :tag_string => "ugoira") @upload = FactoryGirl.create(:upload, :source => @url, :tag_string => "ugoira")
@output_file = Tempfile.new("download")
end end
teardown do
@output_file.unlink
end
should "process successfully" do should "process successfully" do
@upload.download_from_source(@output_file.path) _, _, output_file = @upload.download_from_source(@url, "")
assert_operator(File.size(@output_file.path), :>, 1_000) assert_operator(output_file.size, :>, 1_000)
assert_equal("application/zip", @upload.file_header_to_content_type(@output_file.path)) assert_equal("zip", @upload.file_header_to_file_ext(output_file))
assert_equal("zip", @upload.content_type_to_file_ext(@upload.file_header_to_content_type(@output_file.path)))
end end
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 end
context "determining if a file is downloadable" do context "determining if a file is downloadable" do
@@ -161,46 +118,32 @@ class UploadTest < ActiveSupport::TestCase
context "file processor" do context "file processor" do
should "parse and process a cgi file representation" do should "parse and process a cgi file representation" do
FileUtils.cp("#{Rails.root}/test/files/test.jpg", "#{Rails.root}/tmp") @upload = FactoryGirl.create(:upload, file: upload_file("test/files/test.jpg"))
@upload = Upload.new(:file => upload_jpeg("#{Rails.root}/tmp/test.jpg")) assert_nothing_raised {@upload.process_upload}
assert_nothing_raised {@upload.convert_cgi_file} assert_equal(28086, @upload.file_size)
assert(File.exists?(@upload.file_path))
assert_equal(28086, File.size(@upload.file_path))
end end
should "process a transparent png" do should "process a transparent png" do
FileUtils.cp("#{Rails.root}/test/files/alpha.png", "#{Rails.root}/tmp") @upload = FactoryGirl.create(:upload, file: upload_file("test/files/alpha.png"))
@upload = Upload.new(:file => upload_file("#{Rails.root}/tmp/alpha.png", "image/png", "alpha.png")) assert_nothing_raised {@upload.process_upload}
assert_nothing_raised {@upload.convert_cgi_file} assert_equal(1136, @upload.file_size)
assert(File.exists?(@upload.file_path))
assert_equal(1136, File.size(@upload.file_path))
end end
end end
context "hash calculator" do context "hash calculator" do
should "caculate the hash" do should "caculate the hash" do
@upload = FactoryGirl.create(:jpg_upload) @upload = FactoryGirl.create(:jpg_upload)
@upload.calculate_hash(@upload.file_path) @upload.process_upload
assert_equal("ecef68c44edb8a0d6a3070b5f8e8ee76", @upload.md5) assert_equal("ecef68c44edb8a0d6a3070b5f8e8ee76", @upload.md5)
end end
end end
context "resizer" do 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 should "generate several resized versions of the image" do
@upload = FactoryGirl.create(:large_jpg_upload) @upload = FactoryGirl.create(:upload, file_ext: "jpg", image_width: 1356, image_height: 911, file: upload_file("test/files/test-large.jpg"))
@upload.calculate_hash(@upload.file_path) preview_file, sample_file = @upload.generate_resizes
@upload.calculate_dimensions(@upload.file_path) assert_operator(preview_file.size, :>, 1_000)
assert_nothing_raised {@upload.generate_resizes(@upload.file_path)} assert_operator(sample_file.size, :>, 1_000)
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)
end end
end end
@@ -215,13 +158,10 @@ class UploadTest < ActiveSupport::TestCase
context "with an artist commentary" do context "with an artist commentary" do
setup do setup do
@upload = FactoryGirl.create(:source_upload, @upload = FactoryGirl.create(:source_upload,
:rating => "s", include_artist_commentary: "1",
:uploader_ip_addr => "127.0.0.1", artist_commentary_title: "",
:tag_string => "hoge foo" artist_commentary_desc: "blah",
) )
@upload.include_artist_commentary = "1"
@upload.artist_commentary_title = ""
@upload.artist_commentary_desc = "blah"
end end
should "create an artist commentary when processed" do should "create an artist commentary when processed" do
@@ -258,12 +198,7 @@ class UploadTest < ActiveSupport::TestCase
end end
should "process completely for a pixiv ugoira" do should "process completely for a pixiv ugoira" do
@upload = FactoryGirl.create(:source_upload, @upload = FactoryGirl.create(:source_upload, source: "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=46378654")
: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"
)
assert_difference(["PixivUgoiraFrameData.count", "Post.count"]) do assert_difference(["PixivUgoiraFrameData.count", "Post.count"]) do
@upload.process! @upload.process!
assert_equal([], @upload.errors.full_messages) 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_width)
assert_equal(60, post.image_height) 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_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_nothing_raised { post.file(:original) }
assert_operator(File.size(post.preview_file_path), :>, 0) assert_nothing_raised { post.file(:preview) }
end end
should "process completely for an uploaded image" do should "process completely for an uploaded image" do
@upload = FactoryGirl.create(:jpg_upload, @upload = FactoryGirl.create(:jpg_upload,
:rating => "s", :rating => "s",
:uploader_ip_addr => "127.0.0.1", :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_difference("Post.count") do
assert_nothing_raised {@upload.process!} 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("127.0.0.1", post.uploader_ip_addr.to_s)
assert_equal(@upload.md5, post.md5) assert_equal(@upload.md5, post.md5)
assert_equal("jpg", post.file_ext) assert_equal("jpg", post.file_ext)
assert(File.exists?(post.file_path)) assert_nothing_raised { post.file(:original) }
assert_equal(28086, File.size(post.file_path)) assert_equal(28086, post.file(:original).size)
assert_equal(post.id, @upload.post_id) assert_equal(post.id, @upload.post_id)
assert_equal("completed", @upload.status) assert_equal("completed", @upload.status)
end end
@@ -310,16 +244,5 @@ class UploadTest < ActiveSupport::TestCase
assert_nothing_raised {@upload.process!} assert_nothing_raised {@upload.process!}
end end
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
end end