diff options
Diffstat (limited to 'activestorage/test/models/blob_test.rb')
-rw-r--r-- | activestorage/test/models/blob_test.rb | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/activestorage/test/models/blob_test.rb b/activestorage/test/models/blob_test.rb new file mode 100644 index 0000000000..8b14bcec87 --- /dev/null +++ b/activestorage/test/models/blob_test.rb @@ -0,0 +1,47 @@ +require "test_helper" +require "database/setup" + +class ActiveStorage::BlobTest < ActiveSupport::TestCase + test "create after upload sets byte size and checksum" do + data = "Hello world!" + blob = create_blob data: data + + assert_equal data, blob.download + assert_equal data.length, blob.byte_size + assert_equal Digest::MD5.base64digest(data), blob.checksum + end + + test "text?" do + blob = create_blob data: "Hello world!" + assert blob.text? + assert_not blob.audio? + end + + test "download yields chunks" do + blob = create_blob data: "a" * 75.kilobytes + chunks = [] + + blob.download do |chunk| + chunks << chunk + end + + assert_equal 2, chunks.size + assert_equal "a" * 64.kilobytes, chunks.first + assert_equal "a" * 11.kilobytes, chunks.second + end + + test "urls expiring in 5 minutes" do + blob = create_blob + + freeze_time do + assert_equal expected_url_for(blob), blob.service_url + assert_equal expected_url_for(blob, disposition: :attachment), blob.service_url(disposition: :attachment) + end + end + + private + def expected_url_for(blob, disposition: :inline) + query_string = { content_type: blob.content_type, disposition: disposition }.to_param + "/rails/active_storage/disk/#{ActiveStorage.verifier.generate(blob.key, expires_in: 5.minutes, purpose: :blob_key)}/#{blob.filename}?#{query_string}" + end +end |