From 3dcb852b66a5819d1a62f9ede6713c9063ccc74a Mon Sep 17 00:00:00 2001 From: Vasily Fedoseyev Date: Wed, 10 Apr 2024 14:14:10 +0300 Subject: [PATCH] Tests for optimizer --- test/optimizer_test.rb | 92 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 test/optimizer_test.rb diff --git a/test/optimizer_test.rb b/test/optimizer_test.rb new file mode 100644 index 0000000..abc490f --- /dev/null +++ b/test/optimizer_test.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true + +require 'test_helper' + +class OptimizerTest < Test::Unit::TestCase + setup do + @pixel_jpg = Base64.decode64( + "/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAP#{'/'*86}wgALCAABAAEBAREA/8QAFBAB#{'A'*21}P/aAAgBAQABPxA" + ) + @pixel_png = Base64.decode64( + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg" + ) + @pixel_gif = Base64.decode64("R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7") + end + + def with_tempfile(name, content) + Tempfile.create([File.basename(name), File.extname(name)]) do |tempfile| + tempfile.binmode + tempfile.write(content) + tempfile.flush + tempfile.rewind + yield tempfile + end + end + + context "Paperclip::Optimizer" do + context "#real_content_type" do + should "detect jpeg" do + with_tempfile('pixel.jpg', @pixel_jpg) do |tempfile| + assert_equal("image/jpeg", Paperclip::Optimizer.new(tempfile, {}).real_content_type) + end + end + + should("detect png") do + with_tempfile('image.svg.png', @pixel_png) do |tempfile| + assert_equal("image/png", Paperclip::Optimizer.new(tempfile, {}).real_content_type) + end + end + + should("detect gif") do + with_tempfile('Spiked.gif', @pixel_gif) do |tempfile| + assert_equal("image/gif", Paperclip::Optimizer.new(tempfile, {}).real_content_type) + end + end + end + + context "#make" do + should "process jpeg" do + with_tempfile('pixel.jpg', @pixel_jpg) do |tempfile| + res = Paperclip::Optimizer.new(tempfile, {}).make + assert_equal("image/jpeg", Paperclip::Upfile.content_type_from_file(res.path)) + end + end + + should("process png") do + with_tempfile('image.svg.png', @pixel_png) do |tempfile| + res = Paperclip::Optimizer.new(tempfile, {}).make + assert_equal("image/png", Paperclip::Upfile.content_type_from_file(res.path)) + end + end + + should("process gif") do + with_tempfile('Spiked.gif', @pixel_gif) do |tempfile| + res = Paperclip::Optimizer.new(tempfile, {}).make + assert_equal("image/gif", Paperclip::Upfile.content_type_from_file(res.path)) + end + end + + should("pass others") do + invalid_content = "invalid gif" + with_tempfile('Spiked.gif', invalid_content) do |tempfile| + res = Paperclip::Optimizer.new(tempfile, {}).make + assert_equal(tempfile, res) + assert_equal(invalid_content, res.read) + end + end + + should("handle errors") do + invalid_content = "invalid gif" + with_tempfile('Spiked.gif', invalid_content) do |tempfile| + processor = Paperclip::Optimizer.new(tempfile, {}) + processor.stubs(real_content_type: 'image/gif') + Open3.stubs(:capture3).raises("lala") + Paperclip.expects(:log).with { _1.include?("lala") } + res = processor.make + assert_equal(tempfile, res) + assert_equal(invalid_content, res.read) + end + end + end + end +end