From 0fa491188456332bacddb6310d86014cd8c87688 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Mon, 3 Aug 2026 04:13:34 +0200 Subject: Believe the file over the browser about what was uploaded --- test/models/asset_test.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'test') diff --git a/test/models/asset_test.rb b/test/models/asset_test.rb index ab1cc5df..2677681b 100644 --- a/test/models/asset_test.rb +++ b/test/models/asset_test.rb @@ -1,4 +1,5 @@ require 'test_helper' +require "rack/test/uploaded_file" class AssetTest < ActiveSupport::TestCase @@ -53,4 +54,26 @@ class AssetTest < ActiveSupport::TestCase assert asset.has_credit? assert_not asset.show_credit? end + + test "an upload that claims to be an image but is not is refused" do + Tempfile.create(["fake", ".jpg"]) do |f| + f.write("this is not a jpeg") + f.flush + + asset = Asset.new(:name => "fake") + asset.upload = Rack::Test::UploadedFile.new(f.path, "image/jpeg") + + assert_not asset.valid? + assert_includes asset.errors.full_messages.to_sentence, + I18n.t("activerecord.errors.models.asset.attributes.upload.unreadable_image") + end + end + + test "a real image is accepted and its type comes from the file" do + asset = Asset.new(:name => "real") + asset.upload = Rack::Test::UploadedFile.new(file_fixture("test_image.png"), "image/jpeg") + + assert asset.valid?, asset.errors.full_messages.to_sentence + assert_equal "image/png", asset.upload_content_type + end end -- cgit v1.3