2017-08-12 08:32:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-06 06:22:44 -04:00
|
|
|
require "service/shared_service_tests"
|
|
|
|
|
|
|
|
class ActiveStorage::Service::DiskServiceTest < ActiveSupport::TestCase
|
2019-10-01 16:54:37 -04:00
|
|
|
tmp_config = { tmp: { service: "Disk", root: File.join(Dir.tmpdir, "active_storage") } }
|
|
|
|
SERVICE = ActiveStorage::Service.configure(:tmp, tmp_config)
|
2017-07-06 06:22:44 -04:00
|
|
|
|
|
|
|
include ActiveStorage::Service::SharedServiceTests
|
2017-07-09 11:04:37 -04:00
|
|
|
|
2019-10-01 16:54:37 -04:00
|
|
|
test "name" do
|
|
|
|
assert_equal :tmp, @service.name
|
|
|
|
end
|
|
|
|
|
2021-02-10 08:31:22 -05:00
|
|
|
test "url_for_direct_upload" do
|
|
|
|
original_url_options = Rails.application.routes.default_url_options.dup
|
|
|
|
Rails.application.routes.default_url_options.merge!(protocol: "http", host: "test.example.com", port: 3001)
|
|
|
|
|
|
|
|
key = SecureRandom.base58(24)
|
|
|
|
data = "Something else entirely!"
|
|
|
|
checksum = Digest::MD5.base64digest(data)
|
|
|
|
|
|
|
|
begin
|
|
|
|
assert_match(/^https:\/\/example.com\/rails\/active_storage\/disk\/.*$/,
|
|
|
|
@service.url_for_direct_upload(key, expires_in: 5.minutes, content_type: "text/plain", content_length: data.size, checksum: checksum))
|
|
|
|
ensure
|
|
|
|
Rails.application.routes.default_url_options = original_url_options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-01 08:29:37 -04:00
|
|
|
test "URL generation" do
|
2019-07-09 15:54:35 -04:00
|
|
|
original_url_options = Rails.application.routes.default_url_options.dup
|
|
|
|
Rails.application.routes.default_url_options.merge!(protocol: "http", host: "test.example.com", port: 3001)
|
|
|
|
begin
|
2019-12-06 14:26:00 -05:00
|
|
|
assert_match(/^https:\/\/example.com\/rails\/active_storage\/disk\/.*\/avatar\.png$/,
|
2019-07-09 15:54:35 -04:00
|
|
|
@service.url(@key, expires_in: 5.minutes, disposition: :inline, filename: ActiveStorage::Filename.new("avatar.png"), content_type: "image/png"))
|
|
|
|
ensure
|
|
|
|
Rails.application.routes.default_url_options = original_url_options
|
|
|
|
end
|
2017-07-09 11:04:37 -04:00
|
|
|
end
|
2018-02-28 18:59:00 -05:00
|
|
|
|
2021-02-10 08:31:22 -05:00
|
|
|
test "URL generation without ActiveStorage::Current.url_options set" do
|
|
|
|
ActiveStorage::Current.url_options = nil
|
2021-04-16 08:35:28 -04:00
|
|
|
|
|
|
|
error = assert_raises ArgumentError do
|
|
|
|
@service.url(@key, expires_in: 5.minutes, disposition: :inline, filename: ActiveStorage::Filename.new("avatar.png"), content_type: "image/png")
|
|
|
|
end
|
|
|
|
|
2021-02-10 08:31:22 -05:00
|
|
|
assert_equal("Cannot generate URL for avatar.png using Disk service, please set ActiveStorage::Current.url_options.", error.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "URL generation keeps working with ActiveStorage::Current.host set" do
|
|
|
|
ActiveStorage::Current.url_options = nil
|
2021-08-10 12:28:01 -04:00
|
|
|
assert_deprecated { ActiveStorage::Current.host = "https://example.com" }
|
2021-02-10 08:31:22 -05:00
|
|
|
|
|
|
|
original_url_options = Rails.application.routes.default_url_options.dup
|
|
|
|
Rails.application.routes.default_url_options.merge!(protocol: "http", host: "test.example.com", port: 3001)
|
|
|
|
begin
|
|
|
|
assert_match(/^http:\/\/example.com:3001\/rails\/active_storage\/disk\/.*\/avatar\.png$/,
|
|
|
|
@service.url(@key, expires_in: 5.minutes, disposition: :inline, filename: ActiveStorage::Filename.new("avatar.png"), content_type: "image/png"))
|
|
|
|
ensure
|
|
|
|
Rails.application.routes.default_url_options = original_url_options
|
|
|
|
end
|
2021-04-16 08:35:28 -04:00
|
|
|
end
|
|
|
|
|
2018-02-28 18:59:00 -05:00
|
|
|
test "headers_for_direct_upload generation" do
|
2018-06-25 18:49:26 -04:00
|
|
|
assert_equal({ "Content-Type" => "application/json" }, @service.headers_for_direct_upload(@key, content_type: "application/json"))
|
2018-02-28 18:59:00 -05:00
|
|
|
end
|
2021-05-06 15:51:06 -04:00
|
|
|
|
|
|
|
test "root" do
|
|
|
|
assert_equal tmp_config.dig(:tmp, :root), @service.root
|
|
|
|
end
|
|
|
|
|
|
|
|
test "can change root" do
|
|
|
|
tmp_path_2 = File.join(Dir.tmpdir, "active_storage_2")
|
|
|
|
@service.root = tmp_path_2
|
|
|
|
|
|
|
|
assert_equal tmp_path_2, @service.root
|
|
|
|
ensure
|
|
|
|
@service.root = tmp_config.dig(:tmp, :root)
|
|
|
|
end
|
2017-07-06 06:22:44 -04:00
|
|
|
end
|