2017-08-12 08:32:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-01 06:06:08 -04:00
|
|
|
require "test_helper"
|
2017-07-01 06:47:13 -04:00
|
|
|
require "database/setup"
|
|
|
|
|
2017-07-06 05:33:29 -04:00
|
|
|
class ActiveStorage::BlobTest < ActiveSupport::TestCase
|
2017-07-01 08:25:02 -04:00
|
|
|
test "create after upload sets byte size and checksum" do
|
|
|
|
data = "Hello world!"
|
2017-07-03 14:14:28 -04:00
|
|
|
blob = create_blob data: data
|
2017-07-01 08:25:02 -04:00
|
|
|
|
|
|
|
assert_equal data, blob.download
|
|
|
|
assert_equal data.length, blob.byte_size
|
2017-07-06 09:41:42 -04:00
|
|
|
assert_equal Digest::MD5.base64digest(data), blob.checksum
|
2017-06-30 13:12:58 -04:00
|
|
|
end
|
2017-07-03 14:14:28 -04:00
|
|
|
|
2017-07-28 16:49:43 -04:00
|
|
|
test "text?" do
|
2017-07-28 16:27:43 -04:00
|
|
|
blob = create_blob data: "Hello world!"
|
2017-07-28 16:49:43 -04:00
|
|
|
assert blob.text?
|
|
|
|
assert_not blob.audio?
|
2017-07-28 16:27:43 -04:00
|
|
|
end
|
|
|
|
|
2017-07-10 16:17:48 -04:00
|
|
|
test "download yields chunks" do
|
2017-07-13 18:09:56 -04:00
|
|
|
blob = create_blob data: "a" * 75.kilobytes
|
2017-07-10 16:17:48 -04:00
|
|
|
chunks = []
|
|
|
|
|
|
|
|
blob.download do |chunk|
|
|
|
|
chunks << chunk
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 2, chunks.size
|
2017-07-13 18:09:56 -04:00
|
|
|
assert_equal "a" * 64.kilobytes, chunks.first
|
|
|
|
assert_equal "a" * 11.kilobytes, chunks.second
|
2017-07-10 16:17:48 -04:00
|
|
|
end
|
|
|
|
|
2017-07-04 11:34:37 -04:00
|
|
|
test "urls expiring in 5 minutes" do
|
2017-07-03 14:14:28 -04:00
|
|
|
blob = create_blob
|
2017-07-03 15:06:09 -04:00
|
|
|
|
2017-07-19 13:58:23 -04:00
|
|
|
freeze_time do
|
2017-07-24 12:14:29 -04:00
|
|
|
assert_equal expected_url_for(blob), blob.service_url
|
|
|
|
assert_equal expected_url_for(blob, disposition: :attachment), blob.service_url(disposition: :attachment)
|
2017-07-03 15:06:09 -04:00
|
|
|
end
|
2017-07-03 14:14:28 -04:00
|
|
|
end
|
|
|
|
|
2017-08-17 09:35:24 -04:00
|
|
|
test "purge removes from external service" do
|
|
|
|
blob = create_blob
|
|
|
|
|
|
|
|
blob.purge
|
|
|
|
assert_not ActiveStorage::Blob.service.exist?(blob.key)
|
|
|
|
end
|
|
|
|
|
2017-07-03 14:14:28 -04:00
|
|
|
private
|
2017-07-04 11:34:37 -04:00
|
|
|
def expected_url_for(blob, disposition: :inline)
|
2017-08-21 16:34:18 -04:00
|
|
|
query_string = { content_type: blob.content_type, disposition: "#{disposition}; #{blob.filename.parameters}" }.to_param
|
2017-07-27 16:15:38 -04:00
|
|
|
"/rails/active_storage/disk/#{ActiveStorage.verifier.generate(blob.key, expires_in: 5.minutes, purpose: :blob_key)}/#{blob.filename}?#{query_string}"
|
2017-07-04 11:34:37 -04:00
|
|
|
end
|
2017-06-30 13:12:58 -04:00
|
|
|
end
|