Raise error on unpermitted params.
Fail loudly if we forget to whitelist a param instead of silently ignoring it. misc models: convert to strong params. artist commentaries: convert to strong params. * Disallow changing or setting post_id to a nonexistent post. artists: convert to strong params. * Disallow setting `is_banned` in create/update actions. Changing it this way instead of with the ban/unban actions would leave the artist in a partially banned state. bans: convert to strong params. * Disallow changing the user_id after the ban has been created. comments: convert to strong params. favorite groups: convert to strong params. news updates: convert to strong params. post appeals: convert to strong params. post flags: convert to strong params. * Disallow users from setting the `is_deleted` / `is_resolved` flags. ip bans: convert to strong params. user feedbacks: convert to strong params. * Disallow users from setting `disable_dmail_notification` when creating feedbacks. * Disallow changing the user_id after the feedback has been created. notes: convert to strong params. wiki pages: convert to strong params. * Also fix non-Builders being able to delete wiki pages. saved searches: convert to strong params. pools: convert to strong params. * Disallow setting `post_count` or `is_deleted` in create/update actions. janitor trials: convert to strong params. post disapprovals: convert to strong params. * Factor out quick-mod bar to shared partial. * Fix quick-mod bar to use `Post#is_approvable?` to determine visibility of Approve button. dmail filters: convert to strong params. password resets: convert to strong params. user name change requests: convert to strong params. posts: convert to strong params. users: convert to strong params. * Disallow setting password_hash, last_logged_in_at, last_forum_read_at, has_mail, and dmail_filter_attributes[user_id]. * Remove initialize_default_image_size (dead code). uploads: convert to strong params. * Remove `initialize_status` because status already defaults to pending in the database. tag aliases/implications: convert to strong params. tags: convert to strong params. forum posts: convert to strong params. * Disallow changing the topic_id after creating the post. * Disallow setting is_deleted (destroy/undelete actions should be used instead). * Remove is_sticky / is_locked (nonexistent attributes). forum topics: convert to strong params. * merges https://github.com/evazion/danbooru/tree/wip-rails-5.1 * lock pg gem to 0.21 (1.0.0 is incompatible with rails 5.1.4) * switch to factorybot and change all references Co-authored-by: r888888888 <r888888888@gmail.com> Co-authored-by: evazion <noizave@gmail.com> add diffs
This commit is contained in:
@@ -1,15 +1,10 @@
|
||||
require 'test_helper'
|
||||
|
||||
class UploadTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
super
|
||||
|
||||
mock_iqdb_service!
|
||||
end
|
||||
|
||||
context "In all cases" do
|
||||
setup do
|
||||
user = FactoryGirl.create(:contributor_user)
|
||||
mock_iqdb_service!
|
||||
user = FactoryBot.create(:contributor_user)
|
||||
CurrentUser.user = user
|
||||
CurrentUser.ip_addr = "127.0.0.1"
|
||||
end
|
||||
@@ -22,12 +17,12 @@ class UploadTest < ActiveSupport::TestCase
|
||||
context "An upload" do
|
||||
context "from a user that is limited" do
|
||||
setup do
|
||||
CurrentUser.user = FactoryGirl.create(:user, :created_at => 1.year.ago)
|
||||
CurrentUser.user = FactoryBot.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 = FactoryBot.build(:jpg_upload, :tag_string => "")
|
||||
@upload.save
|
||||
assert_equal(["You have reached your upload limit for the day"], @upload.errors.full_messages)
|
||||
end
|
||||
@@ -35,51 +30,64 @@ class UploadTest < ActiveSupport::TestCase
|
||||
|
||||
context "image size calculator" do
|
||||
should "discover the dimensions for a compressed SWF" do
|
||||
@upload = FactoryGirl.create(:upload, file: upload_file("test/files/compressed.swf"))
|
||||
@upload = FactoryBot.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)
|
||||
@upload = FactoryBot.create(:jpg_upload)
|
||||
assert_equal([500, 335], @upload.calculate_dimensions)
|
||||
end
|
||||
|
||||
should "discover the dimensions for a JPG with EXIF data" do
|
||||
@upload = FactoryGirl.create(:upload, file: upload_file("test/files/test-exif-small.jpg"))
|
||||
@upload = FactoryBot.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(:upload, file: upload_file("test/files/test-blank.jpg"))
|
||||
@upload = FactoryBot.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(:upload, file: upload_file("test/files/test.png"))
|
||||
@upload = FactoryBot.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(:upload, file: upload_file("test/files/test.gif"))
|
||||
@upload = FactoryBot.create(:upload, file: upload_file("test/files/test.gif"))
|
||||
assert_equal([400, 400], @upload.calculate_dimensions)
|
||||
@upload = FactoryBot.create(:upload, :file_path => "#{Rails.root}/test/files/compressed.swf")
|
||||
@upload.calculate_dimensions
|
||||
assert_equal(607, @upload.image_width)
|
||||
assert_equal(756, @upload.image_height)
|
||||
end
|
||||
end
|
||||
|
||||
context "content type calculator" do
|
||||
should "know how to parse jpeg, png, gif, and swf file headers" do
|
||||
@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")))
|
||||
@upload = FactoryBot.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 = FactoryBot.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
|
||||
|
||||
context "downloader" do
|
||||
context "for a zip that is not an ugoira" do
|
||||
should "not validate" do
|
||||
@upload = FactoryGirl.create(:upload, file: upload_file("test/files/invalid_ugoira.zip"))
|
||||
@upload = FactoryBot.create(:upload, file: upload_file("test/files/invalid_ugoira.zip"))
|
||||
@upload.process!
|
||||
assert_equal("error: RuntimeError - missing frame data for ugoira", @upload.status)
|
||||
end
|
||||
@@ -88,7 +96,8 @@ 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(:upload, :source => @url, :tag_string => "ugoira")
|
||||
@upload = FactoryBot.create(:source_upload, :source => @url, :tag_string => "ugoira")
|
||||
@output_file = Tempfile.new("download")
|
||||
end
|
||||
|
||||
should "process successfully" do
|
||||
@@ -97,34 +106,43 @@ class UploadTest < ActiveSupport::TestCase
|
||||
assert_equal("zip", @upload.file_header_to_file_ext(output_file))
|
||||
end
|
||||
end
|
||||
|
||||
should "initialize the final path after downloading a file" do
|
||||
@upload = FactoryBot.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
|
||||
should "classify HTTP sources as downloadable" do
|
||||
@upload = FactoryGirl.create(:source_upload, :source => "http://www.example.com/1.jpg")
|
||||
@upload = FactoryBot.create(:source_upload, :source => "http://www.example.com/1.jpg")
|
||||
assert_not_nil(@upload.is_downloadable?)
|
||||
end
|
||||
|
||||
should "classify HTTPS sources as downloadable" do
|
||||
@upload = FactoryGirl.create(:source_upload, :source => "https://www.example.com/1.jpg")
|
||||
@upload = FactoryBot.create(:source_upload, :source => "https://www.example.com/1.jpg")
|
||||
assert_not_nil(@upload.is_downloadable?)
|
||||
end
|
||||
|
||||
should "classify non-HTTP/HTTPS sources as not downloadable" do
|
||||
@upload = FactoryGirl.create(:source_upload, :source => "ftp://www.example.com/1.jpg")
|
||||
@upload = FactoryBot.create(:source_upload, :source => "ftp://www.example.com/1.jpg")
|
||||
assert_nil(@upload.is_downloadable?)
|
||||
end
|
||||
end
|
||||
|
||||
context "file processor" do
|
||||
should "parse and process a cgi file representation" do
|
||||
@upload = FactoryGirl.create(:upload, file: upload_file("test/files/test.jpg"))
|
||||
@upload = FactoryBot.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
|
||||
@upload = FactoryGirl.create(:upload, file: upload_file("test/files/alpha.png"))
|
||||
@upload = FactoryBot.create(:upload, file: upload_file("test/files/alpha.png"))
|
||||
assert_nothing_raised {@upload.process_upload}
|
||||
assert_equal(1136, @upload.file_size)
|
||||
end
|
||||
@@ -132,23 +150,27 @@ class UploadTest < ActiveSupport::TestCase
|
||||
|
||||
context "hash calculator" do
|
||||
should "caculate the hash" do
|
||||
@upload = FactoryGirl.create(:jpg_upload)
|
||||
@upload.process_upload
|
||||
@upload = FactoryBot.create(:jpg_upload)
|
||||
@upload.calculate_hash(@upload.file_path)
|
||||
assert_equal("ecef68c44edb8a0d6a3070b5f8e8ee76", @upload.md5)
|
||||
end
|
||||
end
|
||||
|
||||
context "resizer" do
|
||||
should "generate several resized versions of the image" do
|
||||
@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)
|
||||
@upload = FactoryBot.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)
|
||||
end
|
||||
end
|
||||
|
||||
should "increment the uploaders post_upload_count" do
|
||||
@upload = FactoryGirl.create(:source_upload)
|
||||
@upload = FactoryBot.create(:source_upload)
|
||||
assert_difference("CurrentUser.user.post_upload_count", 1) do
|
||||
@upload.process!
|
||||
CurrentUser.user.reload
|
||||
@@ -157,11 +179,14 @@ class UploadTest < ActiveSupport::TestCase
|
||||
|
||||
context "with an artist commentary" do
|
||||
setup do
|
||||
@upload = FactoryGirl.create(:source_upload,
|
||||
include_artist_commentary: "1",
|
||||
artist_commentary_title: "",
|
||||
artist_commentary_desc: "blah",
|
||||
)
|
||||
@upload = FactoryBot.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"
|
||||
end
|
||||
|
||||
should "create an artist commentary when processed" do
|
||||
@@ -172,7 +197,7 @@ class UploadTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
should "process completely for a downloaded image" do
|
||||
@upload = FactoryGirl.create(:source_upload,
|
||||
@upload = FactoryBot.create(:source_upload,
|
||||
:rating => "s",
|
||||
:uploader_ip_addr => "127.0.0.1",
|
||||
:tag_string => "hoge foo"
|
||||
@@ -198,17 +223,17 @@ class UploadTest < ActiveSupport::TestCase
|
||||
|
||||
context "automatic tagging" do
|
||||
should "tag animated png files" do
|
||||
@upload = FactoryGirl.build(:upload, file_ext: "png", file: upload_file("test/files/apng/normal_apng.png"))
|
||||
@upload = FactoryBot.build(:upload, file_ext: "png", file: upload_file("test/files/apng/normal_apng.png"))
|
||||
assert_equal("animated_png", @upload.automatic_tags)
|
||||
end
|
||||
|
||||
should "tag animated gif files" do
|
||||
@upload = FactoryGirl.build(:upload, file_ext: "gif", file: upload_file("test/files/test-animated-86x52.gif"))
|
||||
@upload = FactoryBot.build(:upload, file_ext: "gif", file: upload_file("test/files/test-animated-86x52.gif"))
|
||||
assert_equal("animated_gif", @upload.automatic_tags)
|
||||
end
|
||||
|
||||
should "not tag static gif files" do
|
||||
@upload = FactoryGirl.build(:upload, file_ext: "gif", file: upload_file("test/files/test-static-32x32.gif"))
|
||||
@upload = FactoryBot.build(:upload, file_ext: "gif", file: upload_file("test/files/test-static-32x32.gif"))
|
||||
assert_equal("", @upload.automatic_tags)
|
||||
end
|
||||
end
|
||||
@@ -216,7 +241,7 @@ class UploadTest < ActiveSupport::TestCase
|
||||
context "that is too large" do
|
||||
should "should fail validation" do
|
||||
Danbooru.config.stubs(:max_image_resolution).returns(31*31)
|
||||
@upload = FactoryGirl.create(:upload, file: upload_file("test/files/test-static-32x32.gif"))
|
||||
@upload = FactoryBot.create(:upload, file: upload_file("test/files/test-static-32x32.gif"))
|
||||
@upload.process!
|
||||
assert_match(/image resolution is too large/, @upload.status)
|
||||
end
|
||||
@@ -224,7 +249,14 @@ 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")
|
||||
skip "ffmpeg is not installed" unless check_ffmpeg
|
||||
|
||||
@upload = FactoryBot.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"
|
||||
)
|
||||
assert_difference(["PixivUgoiraFrameData.count", "Post.count"]) do
|
||||
@upload.process!
|
||||
assert_equal([], @upload.errors.full_messages)
|
||||
@@ -241,7 +273,7 @@ class UploadTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
should "process completely for an uploaded image" do
|
||||
@upload = FactoryGirl.create(:jpg_upload,
|
||||
@upload = FactoryBot.create(:jpg_upload,
|
||||
:rating => "s",
|
||||
:uploader_ip_addr => "127.0.0.1",
|
||||
:tag_string => "hoge foo",
|
||||
@@ -265,7 +297,7 @@ class UploadTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
should "process completely for a .webm" do
|
||||
upload = FactoryGirl.create(:upload, rating: "s", file: upload_file("test/files/test-512x512.webm"))
|
||||
upload = FactoryBot.create(:upload, rating: "s", file: upload_file("test/files/test-512x512.webm"))
|
||||
|
||||
assert_difference("Post.count") do
|
||||
assert_nothing_raised { upload.process! }
|
||||
@@ -284,7 +316,7 @@ class UploadTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
should "process completely for a .mp4" do
|
||||
upload = FactoryGirl.create(:upload, rating: "s", file: upload_file("test/files/test-300x300.mp4"))
|
||||
upload = FactoryBot.create(:upload, rating: "s", file: upload_file("test/files/test-300x300.mp4"))
|
||||
|
||||
assert_difference("Post.count") do
|
||||
assert_nothing_raised { upload.process! }
|
||||
@@ -303,11 +335,22 @@ class UploadTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
should "process completely for a null source" do
|
||||
@upload = FactoryGirl.create(:jpg_upload, :source => nil)
|
||||
@upload = FactoryBot.create(:jpg_upload, :source => nil)
|
||||
|
||||
assert_difference("Post.count") do
|
||||
assert_nothing_raised {@upload.process!}
|
||||
end
|
||||
end
|
||||
|
||||
should "delete the temporary file upon completion" do
|
||||
@upload = FactoryBot.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
|
||||
|
||||
Reference in New Issue
Block a user