Merge pull request #47725 from pawlik/possible-bug-in-activestorage-with-rack-test-uploaded-file

Using Rack::Test::UploadedFile.new with  `StringIO` causes an exception
This commit is contained in:
Jean Boussier 2023-04-26 12:07:26 +02:00 committed by GitHub
commit 22698947db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

@ -22,8 +22,12 @@ def blob
def upload
case attachable
when ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile
when ActionDispatch::Http::UploadedFile
blob.upload_without_unfurling(attachable.open)
when Rack::Test::UploadedFile
blob.upload_without_unfurling(
attachable.respond_to?(:open) ? attachable.open : attachable
)
when Hash
blob.upload_without_unfurling(attachable.fetch(:io))
end
@ -53,7 +57,7 @@ def find_or_build_blob
case attachable
when ActiveStorage::Blob
attachable
when ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile
when ActionDispatch::Http::UploadedFile
ActiveStorage::Blob.build_after_unfurling(
io: attachable.open,
filename: attachable.original_filename,
@ -61,6 +65,14 @@ def find_or_build_blob
record: record,
service_name: attachment_service_name
)
when Rack::Test::UploadedFile
ActiveStorage::Blob.build_after_unfurling(
io: attachable.respond_to?(:open) ? attachable.open : attachable,
filename: attachable.original_filename,
content_type: attachable.content_type,
record: record,
service_name: attachment_service_name
)
when Hash
ActiveStorage::Blob.build_after_unfurling(
**attachable.reverse_merge(

@ -56,6 +56,15 @@ class ActiveStorage::OneAttachedTest < ActiveSupport::TestCase
assert_equal "racecar.jpg", @user.avatar.filename.to_s
end
test "attaching StringIO attachable to an existing record" do
upload = Rack::Test::UploadedFile.new StringIO.new(""), original_filename: "test.txt"
@user.avatar.attach upload
assert_not_nil @user.avatar_attachment
assert_not_nil @user.avatar_blob
end
test "attaching a new blob from an uploaded file to an existing record passes record" do
upload = fixture_file_upload("racecar.jpg")
def upload.open