Files
danbooru/test/functional/post_regenerations_controller_test.rb
evazion b378785582 Fix #3692: Rotate pictures based on metadata
Rotate the image based on the EXIF orientation flag when generating
thumbnails and samples.

Also fix the width and height to be calculated correctly for rotated
images. Vips gives us the unrotated width and height of the image; we
have to detect whether the image is rotated and swap the width and
height manually to correct them. For example, if an image with the
"Rotate 90 CW" flag is 100x500 before rotation, then after rotation it's
500x100. This should fix #4883 (Exif rotation breaks Javascript fit-to-window)

We also have to fix it so that regenerating a post updates the width and
height of the post, in the event that it's a rotated image.

Finally we set `image-orientation: from-image;` even though it's
probably not necessary.
2021-09-22 11:12:50 -05:00

79 lines
2.9 KiB
Ruby

require 'test_helper'
class PostRegenerationsControllerTest < ActionDispatch::IntegrationTest
context "The post regenerations controller" do
setup do
@mod = create(:moderator_user, name: "yukari", created_at: 1.month.ago)
@upload = assert_successful_upload("test/files/test.jpg", user: @mod)
@post = @upload.post
perform_enqueued_jobs # add post to iqdb
end
context "create action" do
should "render" do
post_auth post_regenerations_path, @mod, params: { post_id: @post.id, category: "iqdb" }
assert_redirected_to @post
assert_enqueued_jobs(1, only: RegeneratePostJob)
end
should "not allow non-mods to regenerate posts" do
post_auth post_regenerations_path, create(:user), params: { post_id: @post.id, category: "iqdb" }
assert_response 403
end
context "for an IQDB regeneration" do
should "regenerate IQDB" do
post_auth post_regenerations_path, @mod, params: { post_id: @post.id, category: "iqdb" }
perform_enqueued_jobs
end
should "log a mod action" do
post_auth post_regenerations_path, @mod, params: { post_id: @post.id, category: "iqdb" }
perform_enqueued_jobs
assert_equal(@mod, ModAction.last.creator)
assert_equal("post_regenerate_iqdb", ModAction.last.category)
assert_equal("<@#{@mod.name}> regenerated IQDB for post ##{@post.id}", ModAction.last.description)
end
end
context "for an image sample regeneration" do
should "regenerate missing thumbnails" do
@preview_file_size = @post.file(:preview).size
@post.storage_manager.delete_file(@post.id, @post.md5, @post.file_ext, :preview)
assert_raise(Errno::ENOENT) { @post.file(:preview) }
post_auth post_regenerations_path, @mod, params: { post_id: @post.id }
perform_enqueued_jobs
assert_equal(@preview_file_size, @post.file(:preview).size)
end
should "log a mod action" do
post_auth post_regenerations_path, @mod, params: { post_id: @post.id }
perform_enqueued_jobs
assert_equal(@mod, ModAction.last.creator)
assert_equal("post_regenerate", ModAction.last.category)
assert_equal("<@#{@mod.name}> regenerated image samples for post ##{@post.id}", ModAction.last.description)
end
should "fix the width and height of exif-rotated images" do
@upload = assert_successful_upload("test/files/test-rotation-90cw.jpg", user: @mod)
@post = @upload.post
post_auth post_regenerations_path, @mod, params: { post_id: @post.id }
perform_enqueued_jobs
@post.reload
assert_equal(96, @post.image_width)
assert_equal(128, @post.image_height)
assert_equal(96, @post.media_asset.image_width)
assert_equal(128, @post.media_asset.image_height)
end
end
end
end
end