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
|
|
|
require "active_storage/blob"
|
2017-07-01 06:47:13 -04:00
|
|
|
|
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-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
|
|
|
|
|
|
|
travel_to Time.now do
|
2017-07-04 11:34:37 -04:00
|
|
|
assert_equal expected_url_for(blob), blob.url
|
|
|
|
assert_equal expected_url_for(blob, disposition: :attachment), blob.url(disposition: :attachment)
|
2017-07-03 15:06:09 -04:00
|
|
|
end
|
2017-07-03 14:14:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2017-07-04 11:34:37 -04:00
|
|
|
def expected_url_for(blob, disposition: :inline)
|
2017-07-06 05:33:29 -04:00
|
|
|
"/rails/blobs/#{ActiveStorage::VerifiedKeyWithExpiration.encode(blob.key, expires_in: 5.minutes)}?disposition=#{disposition}"
|
2017-07-04 11:34:37 -04:00
|
|
|
end
|
2017-06-30 13:12:58 -04:00
|
|
|
end
|