2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "test_helper"
|
2018-05-16 20:13:36 -04:00
|
|
|
|
|
|
|
class UploadServiceTest < ActiveSupport::TestCase
|
2018-10-04 22:52:18 -04:00
|
|
|
setup do
|
2022-11-25 15:06:54 -05:00
|
|
|
@user = create(:user, created_at: 2.weeks.ago)
|
2019-02-18 19:29:13 -05:00
|
|
|
CurrentUser.user = @user
|
|
|
|
UploadWhitelist.create!(pattern: '*', reason: 'test')
|
2018-10-04 22:52:18 -04:00
|
|
|
end
|
|
|
|
|
2018-05-16 20:13:36 -04:00
|
|
|
context "::Utils" do
|
|
|
|
subject { UploadService::Utils }
|
|
|
|
|
2018-11-11 15:06:35 -05:00
|
|
|
context "#get_file_for_upload" do
|
2018-08-06 20:39:25 -04:00
|
|
|
context "for a non-source site" do
|
|
|
|
setup do
|
2022-04-05 12:50:34 -04:00
|
|
|
@source = "https://upload.wikimedia.org/wikipedia/commons/c/c5/Moraine_Lake_17092005.jpg"
|
2018-08-06 20:39:25 -04:00
|
|
|
@upload = Upload.new
|
2022-09-17 13:32:21 -04:00
|
|
|
@upload.direct_url = @source
|
2022-11-25 12:44:36 -05:00
|
|
|
Downloads::File.any_instance.stubs(:download!).returns(fixture_file_upload("test.jpg"))
|
2018-08-06 20:39:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
should "work on a jpeg" do
|
2018-11-11 15:06:35 -05:00
|
|
|
file = subject.get_file_for_upload(@upload)
|
2018-08-06 20:39:25 -04:00
|
|
|
|
|
|
|
assert_operator(File.size(file.path), :>, 0)
|
|
|
|
|
|
|
|
file.close
|
|
|
|
end
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context ".calculate_dimensions" do
|
|
|
|
context "for a video" do
|
|
|
|
setup do
|
2022-11-26 08:02:06 -05:00
|
|
|
@path = file_fixture("test-512x512.webm").to_s
|
2022-04-05 12:50:34 -04:00
|
|
|
@upload = Upload.new(file_ext: "webm")
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
should "return the dimensions" do
|
2022-11-26 08:02:06 -05:00
|
|
|
w, h = @upload.calculate_dimensions(@path)
|
2022-05-08 13:05:12 -04:00
|
|
|
assert_operator(w, :>, 0)
|
|
|
|
assert_operator(h, :>, 0)
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-05 12:50:34 -04:00
|
|
|
context "for an image" do
|
2018-05-16 20:13:36 -04:00
|
|
|
setup do
|
2022-11-26 08:02:06 -05:00
|
|
|
@path = file_fixture("test.jpg").to_s
|
2018-09-22 18:13:30 -04:00
|
|
|
@upload = Upload.new(file_ext: "jpg")
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
should "find the dimensions" do
|
2022-11-26 08:02:06 -05:00
|
|
|
w, h = @upload.calculate_dimensions(@path)
|
2022-05-08 13:05:12 -04:00
|
|
|
assert_operator(w, :>, 0)
|
|
|
|
assert_operator(h, :>, 0)
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context ".generate_resizes" do
|
|
|
|
context "for a video" do
|
|
|
|
context "for a webm" do
|
|
|
|
setup do
|
2022-11-25 12:44:36 -05:00
|
|
|
@file = file_fixture("test-512x512.webm").open
|
2022-11-26 10:20:16 -05:00
|
|
|
@upload = UploadService.new(attributes_for(:upload).merge(file: @file, uploader: @user)).start!
|
2022-05-26 16:49:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
@file.close
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
should "generate a video" do
|
2018-07-03 19:37:53 -04:00
|
|
|
preview, crop, sample = subject.generate_resizes(@file, @upload)
|
2018-05-16 20:13:36 -04:00
|
|
|
assert_operator(File.size(preview.path), :>, 0)
|
2018-07-03 19:37:53 -04:00
|
|
|
assert_operator(File.size(crop.path), :>, 0)
|
2022-05-26 12:10:20 -04:00
|
|
|
preview_image = Vips::Image.new_from_file(preview.path)
|
|
|
|
crop_image = Vips::Image.new_from_file(crop.path)
|
|
|
|
assert_equal(150, preview_image.width)
|
|
|
|
assert_equal(150, preview_image.height)
|
|
|
|
assert_equal(150, crop_image.width)
|
|
|
|
assert_equal(150, crop_image.height)
|
2018-05-16 20:13:36 -04:00
|
|
|
preview.close
|
|
|
|
preview.unlink
|
2018-07-03 19:37:53 -04:00
|
|
|
crop.close
|
|
|
|
crop.unlink
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for an image" do
|
|
|
|
teardown do
|
|
|
|
@file.close
|
|
|
|
end
|
|
|
|
|
|
|
|
setup do
|
2022-09-17 13:32:21 -04:00
|
|
|
@upload = mock
|
2018-05-16 20:13:36 -04:00
|
|
|
@upload.stubs(:is_video?).returns(false)
|
|
|
|
@upload.stubs(:is_image?).returns(true)
|
|
|
|
@upload.stubs(:image_width).returns(1200)
|
|
|
|
@upload.stubs(:image_height).returns(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for a jpeg" do
|
|
|
|
setup do
|
2022-11-25 12:44:36 -05:00
|
|
|
@file = file_fixture("test.jpg").open
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
should "generate a preview" do
|
2018-07-03 19:37:53 -04:00
|
|
|
preview, crop, sample = subject.generate_resizes(@file, @upload)
|
2018-05-16 20:13:36 -04:00
|
|
|
assert_operator(File.size(preview.path), :>, 0)
|
2018-07-03 19:37:53 -04:00
|
|
|
assert_operator(File.size(crop.path), :>, 0)
|
2018-05-16 20:13:36 -04:00
|
|
|
assert_operator(File.size(sample.path), :>, 0)
|
|
|
|
preview.close
|
|
|
|
preview.unlink
|
|
|
|
sample.close
|
|
|
|
sample.unlink
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for a png" do
|
|
|
|
setup do
|
2022-11-25 12:44:36 -05:00
|
|
|
@file = file_fixture("test.png").open
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
should "generate a preview" do
|
2018-07-03 19:37:53 -04:00
|
|
|
preview, crop, sample = subject.generate_resizes(@file, @upload)
|
2018-05-16 20:13:36 -04:00
|
|
|
assert_operator(File.size(preview.path), :>, 0)
|
2018-07-03 19:37:53 -04:00
|
|
|
assert_operator(File.size(crop.path), :>, 0)
|
2018-05-16 20:13:36 -04:00
|
|
|
assert_operator(File.size(sample.path), :>, 0)
|
|
|
|
preview.close
|
|
|
|
preview.unlink
|
|
|
|
sample.close
|
|
|
|
sample.unlink
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for a gif" do
|
|
|
|
setup do
|
2022-11-25 12:44:36 -05:00
|
|
|
@file = file_fixture("test.gif").open
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
should "generate a preview" do
|
2018-07-03 19:37:53 -04:00
|
|
|
preview, crop, sample = subject.generate_resizes(@file, @upload)
|
2018-05-16 20:13:36 -04:00
|
|
|
assert_operator(File.size(preview.path), :>, 0)
|
2018-07-03 19:37:53 -04:00
|
|
|
assert_operator(File.size(crop.path), :>, 0)
|
2018-05-16 20:13:36 -04:00
|
|
|
assert_operator(File.size(sample.path), :>, 0)
|
|
|
|
preview.close
|
|
|
|
preview.unlink
|
|
|
|
sample.close
|
|
|
|
sample.unlink
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context ".generate_video_preview_for" do
|
|
|
|
context "for an mp4" do
|
|
|
|
setup do
|
2022-11-25 12:44:36 -05:00
|
|
|
@path = file_fixture("test-300x300.mp4").to_s
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
should "generate a video" do
|
2022-04-13 02:54:50 -04:00
|
|
|
sample = PostThumbnailer.generate_video_preview_for(@path, 100)
|
2018-05-16 20:13:36 -04:00
|
|
|
assert_operator(File.size(sample.path), :>, 0)
|
|
|
|
sample.close
|
|
|
|
sample.unlink
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for a webm" do
|
|
|
|
setup do
|
2022-11-25 12:44:36 -05:00
|
|
|
@path = file_fixture("test-512x512.webm").to_s
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
should "generate a video" do
|
2022-04-13 02:54:50 -04:00
|
|
|
sample = PostThumbnailer.generate_video_preview_for(@path, 100)
|
2018-05-16 20:13:36 -04:00
|
|
|
assert_operator(File.size(sample.path), :>, 0)
|
|
|
|
sample.close
|
|
|
|
sample.unlink
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-06-18 13:10:30 -04:00
|
|
|
end
|
|
|
|
|
2018-05-16 20:13:36 -04:00
|
|
|
context "#start!" do
|
|
|
|
subject { UploadService }
|
|
|
|
|
|
|
|
setup do
|
2018-07-22 02:25:06 -04:00
|
|
|
@source = "https://raikou1.donmai.us/d3/4e/d34e4cf0a437a5d65f8e82b7bcd02606.jpg"
|
2022-11-25 15:06:54 -05:00
|
|
|
CurrentUser.user = create(:user, created_at: 1.month.ago)
|
2022-04-05 12:50:34 -04:00
|
|
|
@build_service = ->(**params) { subject.new({ rating: "s", uploader: CurrentUser.user, uploader_ip_addr: CurrentUser.ip_addr }.merge(params)) }
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "automatic tagging" do
|
|
|
|
should "tag animated png files" do
|
2022-11-25 12:44:36 -05:00
|
|
|
service = @build_service.call(file: fixture_file_upload("apng/normal_apng.png"))
|
2018-05-16 20:13:36 -04:00
|
|
|
upload = service.start!
|
|
|
|
assert_match(/animated_png/, upload.tag_string)
|
|
|
|
end
|
|
|
|
|
|
|
|
should "tag animated gif files" do
|
2022-11-25 12:44:36 -05:00
|
|
|
service = @build_service.call(file: fixture_file_upload("test-animated-86x52.gif"))
|
2018-05-16 20:13:36 -04:00
|
|
|
upload = service.start!
|
|
|
|
assert_match(/animated_gif/, upload.tag_string)
|
|
|
|
end
|
|
|
|
|
|
|
|
should "not tag static gif files" do
|
2022-11-25 12:44:36 -05:00
|
|
|
service = @build_service.call(file: fixture_file_upload("test-static-32x32.gif"))
|
2018-05-16 20:13:36 -04:00
|
|
|
upload = service.start!
|
|
|
|
assert_no_match(/animated_gif/, upload.tag_string)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "that is too large" do
|
|
|
|
setup do
|
2022-09-17 13:32:21 -04:00
|
|
|
Danbooru.config.stubs(:max_image_resolution).returns(31 * 31)
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
should "should fail validation" do
|
2022-11-25 12:44:36 -05:00
|
|
|
service = @build_service.call(file: fixture_file_upload("test-large.jpg"))
|
2018-05-16 20:13:36 -04:00
|
|
|
upload = service.start!
|
|
|
|
assert_match(/image resolution is too large/, upload.status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-19 11:30:49 -05:00
|
|
|
should "create an upload" do
|
2022-04-05 12:50:34 -04:00
|
|
|
service = @build_service.call(source: @source)
|
2018-07-22 02:25:06 -04:00
|
|
|
|
2022-02-19 11:30:49 -05:00
|
|
|
assert_difference(-> { Upload.count }) do
|
|
|
|
service.start!
|
2018-07-22 02:25:06 -04:00
|
|
|
end
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
|
2022-02-19 11:30:49 -05:00
|
|
|
should "assign the rating from tags" do
|
2022-04-13 02:54:50 -04:00
|
|
|
service = @build_service.call(source: @source, rating: "s", tag_string: "blah")
|
|
|
|
post = service.start!
|
2018-05-16 20:13:36 -04:00
|
|
|
|
2022-04-13 02:54:50 -04:00
|
|
|
assert_equal(true, post.valid?)
|
|
|
|
assert_equal("s", post.rating)
|
|
|
|
assert_equal("blah", post.tag_string)
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
2018-11-11 13:17:58 -05:00
|
|
|
|
|
|
|
context "with a source containing unicode characters" do
|
|
|
|
should "normalize unicode characters in the source field" do
|
|
|
|
source1 = "poke\u0301mon" # pokémon (nfd form)
|
|
|
|
source2 = "pok\u00e9mon" # pokémon (nfc form)
|
2022-11-25 12:44:36 -05:00
|
|
|
service = @build_service.call(source: source1, rating: "s", file: fixture_file_upload("test.jpg"))
|
2018-11-11 13:17:58 -05:00
|
|
|
|
|
|
|
assert_nothing_raised { @upload = service.start! }
|
2022-09-17 13:32:21 -04:00
|
|
|
assert_equal(source2, @upload.post.source)
|
2018-11-11 13:17:58 -05:00
|
|
|
end
|
|
|
|
end
|
2018-11-11 15:06:35 -05:00
|
|
|
|
|
|
|
context "without a file or a source url" do
|
|
|
|
should "fail gracefully" do
|
2022-04-05 12:50:34 -04:00
|
|
|
service = @build_service.call(source: "blah", rating: "s")
|
2018-11-11 15:06:35 -05:00
|
|
|
|
|
|
|
assert_nothing_raised { @upload = service.start! }
|
|
|
|
assert_equal(true, @upload.is_errored?)
|
|
|
|
assert_match(/No file or source URL provided/, @upload.status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with both a file and a source url" do
|
|
|
|
should "upload the file and set the source field to the given source" do
|
2022-11-25 12:44:36 -05:00
|
|
|
service = @build_service.call(file: fixture_file_upload("test.jpg"), source: "http://www.example.com", rating: "s")
|
2018-11-11 15:06:35 -05:00
|
|
|
|
|
|
|
assert_nothing_raised { @upload = service.start! }
|
|
|
|
assert_equal(true, @upload.is_completed?)
|
|
|
|
assert_equal("ecef68c44edb8a0d6a3070b5f8e8ee76", @upload.md5)
|
|
|
|
assert_equal("http://www.example.com", @upload.source)
|
|
|
|
end
|
|
|
|
end
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "#create_post_from_upload" do
|
|
|
|
subject { UploadService }
|
|
|
|
|
|
|
|
setup do
|
2022-04-05 12:50:34 -04:00
|
|
|
CurrentUser.user = create(:user, created_at: 1.month.ago)
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "for an image" do
|
|
|
|
setup do
|
2022-11-25 15:06:54 -05:00
|
|
|
@upload = create(:source_upload, file_size: 1000, md5: "12345", file_ext: "jpg", image_width: 100, image_height: 100, file: Tempfile.new)
|
2018-05-16 20:13:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
should "create a post" do
|
|
|
|
post = subject.new({}).create_post_from_upload(@upload)
|
|
|
|
assert_equal([], post.errors.full_messages)
|
|
|
|
assert_not_nil(post.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|