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"
|
2018-02-01 10:49:25 -05:00
|
|
|
require "active_support/testing/method_call_assertions"
|
2017-07-01 06:47:13 -04:00
|
|
|
|
2017-07-06 05:33:29 -04:00
|
|
|
class ActiveStorage::BlobTest < ActiveSupport::TestCase
|
2018-02-01 10:49:25 -05:00
|
|
|
include ActiveSupport::Testing::MethodCallAssertions
|
|
|
|
|
2018-02-12 15:09:52 -05:00
|
|
|
test ".unattached scope returns not attached blobs" do
|
|
|
|
class UserWithHasOneAttachedDependentFalse < User
|
|
|
|
has_one_attached :avatar, dependent: false
|
|
|
|
end
|
|
|
|
|
|
|
|
ActiveStorage::Blob.delete_all
|
|
|
|
blob_1 = create_blob filename: "funky.jpg"
|
|
|
|
blob_2 = create_blob filename: "town.jpg"
|
|
|
|
|
|
|
|
user = UserWithHasOneAttachedDependentFalse.create!
|
|
|
|
user.avatar.attach blob_1
|
|
|
|
|
|
|
|
assert_equal [blob_2], ActiveStorage::Blob.unattached
|
|
|
|
user.destroy
|
|
|
|
assert_equal [blob_1, blob_2].map(&:id).sort, ActiveStorage::Blob.unattached.pluck(:id).sort
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2018-01-15 13:06:17 -05:00
|
|
|
test "create after upload extracts content type from data" do
|
|
|
|
blob = create_file_blob content_type: "application/octet-stream"
|
|
|
|
assert_equal "image/jpeg", blob.content_type
|
|
|
|
end
|
|
|
|
|
|
|
|
test "create after upload extracts content type from filename" do
|
|
|
|
blob = create_blob content_type: "application/octet-stream"
|
|
|
|
assert_equal "text/plain", blob.content_type
|
|
|
|
end
|
|
|
|
|
2018-05-06 11:25:05 -04:00
|
|
|
test "create after upload extracts content_type from io when no content_type given and identify: false" do
|
|
|
|
blob = create_blob content_type: nil, identify: false
|
|
|
|
assert_equal "text/plain", blob.content_type
|
|
|
|
end
|
|
|
|
|
|
|
|
test "create after upload uses content_type when identify: false" do
|
|
|
|
blob = create_blob data: "Article,dates,analysis\n1, 2, 3", filename: "table.csv", content_type: "text/csv", identify: false
|
|
|
|
assert_equal "text/csv", blob.content_type
|
|
|
|
end
|
|
|
|
|
2018-01-28 02:17:37 -05:00
|
|
|
test "image?" do
|
|
|
|
blob = create_file_blob filename: "racecar.jpg"
|
|
|
|
assert_predicate blob, :image?
|
|
|
|
assert_not_predicate blob, :audio?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "video?" do
|
|
|
|
blob = create_file_blob(filename: "video.mp4", content_type: "video/mp4")
|
|
|
|
assert_predicate blob, :video?
|
|
|
|
assert_not_predicate blob, :audio?
|
|
|
|
end
|
|
|
|
|
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!"
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate blob, :text?
|
|
|
|
assert_not_predicate blob, :audio?
|
2017-07-28 16:27:43 -04:00
|
|
|
end
|
|
|
|
|
2017-07-10 16:17:48 -04:00
|
|
|
test "download yields chunks" do
|
2018-04-29 07:01:44 -04:00
|
|
|
blob = create_blob data: "a" * 5.0625.megabytes
|
2017-07-10 16:17:48 -04:00
|
|
|
chunks = []
|
|
|
|
|
|
|
|
blob.download do |chunk|
|
|
|
|
chunks << chunk
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 2, chunks.size
|
2018-04-29 07:01:44 -04:00
|
|
|
assert_equal "a" * 5.megabytes, chunks.first
|
|
|
|
assert_equal "a" * 64.kilobytes, chunks.second
|
2017-07-10 16:17:48 -04:00
|
|
|
end
|
|
|
|
|
2018-05-16 22:12:31 -04:00
|
|
|
test "open" do
|
|
|
|
create_file_blob(filename: "racecar.jpg").open do |file|
|
|
|
|
assert file.binmode?
|
|
|
|
assert_equal 0, file.pos
|
|
|
|
assert_match(/\.jpg\z/, file.path)
|
|
|
|
assert_equal file_fixture("racecar.jpg").binread, file.read, "Expected downloaded file to match fixture file"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-17 19:14:11 -04:00
|
|
|
test "open in a custom tempdir" do
|
|
|
|
tempdir = Dir.mktmpdir
|
|
|
|
|
|
|
|
create_file_blob(filename: "racecar.jpg").open(tempdir: tempdir) do |file|
|
|
|
|
assert file.binmode?
|
|
|
|
assert_equal 0, file.pos
|
|
|
|
assert_match(/\.jpg\z/, file.path)
|
|
|
|
assert file.path.starts_with?(tempdir)
|
|
|
|
assert_equal file_fixture("racecar.jpg").binread, file.read, "Expected downloaded file to match fixture file"
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
2018-01-04 13:35:54 -05:00
|
|
|
test "urls force attachment as content disposition for content types served as binary" do
|
|
|
|
blob = create_blob(content_type: "text/html")
|
|
|
|
|
|
|
|
freeze_time do
|
|
|
|
assert_equal expected_url_for(blob, disposition: :attachment), blob.service_url
|
|
|
|
assert_equal expected_url_for(blob, disposition: :attachment), blob.service_url(disposition: :inline)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-08 09:44:14 -05:00
|
|
|
test "urls allow for custom filename" do
|
|
|
|
blob = create_blob(filename: "original.txt")
|
|
|
|
new_filename = ActiveStorage::Filename.new("new.txt")
|
|
|
|
|
|
|
|
freeze_time do
|
|
|
|
assert_equal expected_url_for(blob), blob.service_url
|
|
|
|
assert_equal expected_url_for(blob, filename: new_filename), blob.service_url(filename: new_filename)
|
2018-02-07 21:15:55 -05:00
|
|
|
assert_equal expected_url_for(blob, filename: new_filename), blob.service_url(filename: "new.txt")
|
|
|
|
assert_equal expected_url_for(blob, filename: blob.filename), blob.service_url(filename: nil)
|
2018-01-08 09:44:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-01 10:49:25 -05:00
|
|
|
test "urls allow for custom options" do
|
|
|
|
blob = create_blob(filename: "original.txt")
|
|
|
|
|
|
|
|
options = [
|
|
|
|
blob.key,
|
|
|
|
expires_in: blob.service.url_expires_in,
|
|
|
|
disposition: :inline,
|
|
|
|
content_type: blob.content_type,
|
|
|
|
filename: blob.filename,
|
|
|
|
thumb_size: "300x300",
|
|
|
|
thumb_mode: "crop"
|
|
|
|
]
|
|
|
|
assert_called_with(blob.service, :url, options) do
|
|
|
|
blob.service_url(thumb_size: "300x300", thumb_mode: "crop")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-02 22:43:28 -05:00
|
|
|
test "purge deletes file from external service" do
|
2017-08-17 09:35:24 -04:00
|
|
|
blob = create_blob
|
|
|
|
|
|
|
|
blob.purge
|
|
|
|
assert_not ActiveStorage::Blob.service.exist?(blob.key)
|
|
|
|
end
|
|
|
|
|
2017-12-02 22:43:28 -05:00
|
|
|
test "purge deletes variants from external service" do
|
|
|
|
blob = create_file_blob
|
|
|
|
variant = blob.variant(resize: "100>").processed
|
|
|
|
|
|
|
|
blob.purge
|
|
|
|
assert_not ActiveStorage::Blob.service.exist?(variant.key)
|
|
|
|
end
|
|
|
|
|
2017-07-03 14:14:28 -04:00
|
|
|
private
|
2018-01-08 09:44:14 -05:00
|
|
|
def expected_url_for(blob, disposition: :inline, filename: nil)
|
|
|
|
filename ||= blob.filename
|
|
|
|
query_string = { content_type: blob.content_type, disposition: "#{disposition}; #{filename.parameters}" }.to_param
|
2018-03-26 09:36:39 -04:00
|
|
|
"https://example.com/rails/active_storage/disk/#{ActiveStorage.verifier.generate(blob.key, expires_in: 5.minutes, purpose: :blob_key)}/#{filename}?#{query_string}"
|
2017-07-04 11:34:37 -04:00
|
|
|
end
|
2017-06-30 13:12:58 -04:00
|
|
|
end
|