diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/controllers/variants_controller.rb | 24 | ||||
-rw-r--r-- | test/fixtures/files/racecar-100x100-monochrome.jpg | bin | 0 -> 27586 bytes | |||
-rw-r--r-- | test/fixtures/files/racecar-100x100.jpg | bin | 0 -> 29446 bytes | |||
-rw-r--r-- | test/fixtures/files/racecar.jpg | bin | 0 -> 1124062 bytes | |||
-rw-r--r-- | test/test_helper.rb | 22 | ||||
-rw-r--r-- | test/variant_test.rb | 23 |
6 files changed, 66 insertions, 3 deletions
diff --git a/test/controllers/variants_controller.rb b/test/controllers/variants_controller.rb new file mode 100644 index 0000000000..6753584d4d --- /dev/null +++ b/test/controllers/variants_controller.rb @@ -0,0 +1,24 @@ +require "test_helper" +require "database/setup" + +require "active_storage/variants_controller" +require "active_storage/verified_key_with_expiration" + +class ActiveStorage::VariantsControllerTest < ActionController::TestCase + setup do + @routes = Routes + @controller = ActiveStorage::VariantsController.new + + @blob = create_image_blob filename: "racecar.jpg" + end + + test "showing variant inline" do + get :show, params: { + filename: @blob.filename, + encoded_blob_key: ActiveStorage::VerifiedKeyWithExpiration.encode(@blob.key, expires_in: 5.minutes), + variation_key: ActiveStorage::Variation.encode(resize: "100x100") } + + assert_redirected_to /racecar.jpg\?disposition=inline/ + assert_same_image "racecar-100x100.jpg", @blob.variant(resize: "100x100") + end +end diff --git a/test/fixtures/files/racecar-100x100-monochrome.jpg b/test/fixtures/files/racecar-100x100-monochrome.jpg Binary files differnew file mode 100644 index 0000000000..39e683747e --- /dev/null +++ b/test/fixtures/files/racecar-100x100-monochrome.jpg diff --git a/test/fixtures/files/racecar-100x100.jpg b/test/fixtures/files/racecar-100x100.jpg Binary files differnew file mode 100644 index 0000000000..2a515a4912 --- /dev/null +++ b/test/fixtures/files/racecar-100x100.jpg diff --git a/test/fixtures/files/racecar.jpg b/test/fixtures/files/racecar.jpg Binary files differnew file mode 100644 index 0000000000..934b4caa22 --- /dev/null +++ b/test/fixtures/files/racecar.jpg diff --git a/test/test_helper.rb b/test/test_helper.rb index 03593b12c7..20b22049b3 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,5 @@ +$LOAD_PATH << File.expand_path("../../app/controllers", __FILE__) + require "bundler/setup" require "active_support" require "active_support/test_case" @@ -15,7 +17,6 @@ rescue Errno::ENOENT {} end - require "active_storage/service/disk_service" require "tmpdir" ActiveStorage::Blob.service = ActiveStorage::Service::DiskService.new(root: Dir.mktmpdir("active_storage_tests")) @@ -24,20 +25,35 @@ ActiveStorage::Service.logger = ActiveSupport::Logger.new(STDOUT) require "active_storage/verified_key_with_expiration" ActiveStorage::VerifiedKeyWithExpiration.verifier = ActiveSupport::MessageVerifier.new("Testing") +require "active_storage/variation" +ActiveStorage::Variation.verifier = ActiveSupport::MessageVerifier.new("Testing") + class ActiveSupport::TestCase private def create_blob(data: "Hello world!", filename: "hello.txt", content_type: "text/plain") ActiveStorage::Blob.create_after_upload! io: StringIO.new(data), filename: filename, content_type: content_type end + + def create_image_blob(filename: "racecar.jpg", content_type: "image/jpeg") + ActiveStorage::Blob.create_after_upload! \ + io: File.open(File.expand_path("../fixtures/files/#{filename}", __FILE__)), + filename: filename, content_type: content_type + end + + def assert_same_image(fixture_filename, variant) + assert_equal \ + File.binread(File.expand_path("../fixtures/files/#{fixture_filename}", __FILE__)), + File.binread(variant.service.send(:path_for, variant.key)) + end end require "action_controller" require "action_controller/test_case" - class ActionController::TestCase Routes = ActionDispatch::Routing::RouteSet.new.tap do |routes| routes.draw do - eval(File.read(File.expand_path("../../lib/active_storage/routes.rb", __FILE__))) + # FIXME: Hacky way to avoid having to instantiate the real engine + eval(File.readlines(File.expand_path("../../config/routes.rb", __FILE__)).slice(1..-2).join("\n")) end end end diff --git a/test/variant_test.rb b/test/variant_test.rb new file mode 100644 index 0000000000..5294b87135 --- /dev/null +++ b/test/variant_test.rb @@ -0,0 +1,23 @@ +require "test_helper" +require "database/setup" +require "active_storage/variant" + +class ActiveStorage::VariantTest < ActiveSupport::TestCase + setup do + @blob = create_image_blob filename: "racecar.jpg" + end + + test "resized variation" do + variant = @blob.variant(resize: "100x100").processed + + assert_match /racecar.jpg/, variant.url + assert_same_image "racecar-100x100.jpg", variant + end + + test "resized and monochrome variation" do + variant = @blob.variant(resize: "100x100", monochrome: true).processed + + assert_match /racecar.jpg/, variant.url + assert_same_image "racecar-100x100-monochrome.jpg", variant + end +end |