2017-08-12 08:32:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-09 12:03:13 -04:00
|
|
|
require "test_helper"
|
|
|
|
require "database/setup"
|
|
|
|
|
2017-08-01 18:22:20 -04:00
|
|
|
if SERVICE_CONFIGURATIONS[:s3] && SERVICE_CONFIGURATIONS[:s3][:access_key_id].present?
|
2017-07-27 16:15:38 -04:00
|
|
|
class ActiveStorage::S3DirectUploadsControllerTest < ActionDispatch::IntegrationTest
|
2017-07-09 12:03:13 -04:00
|
|
|
setup do
|
|
|
|
@old_service = ActiveStorage::Blob.service
|
|
|
|
ActiveStorage::Blob.service = ActiveStorage::Service.configure(:s3, SERVICE_CONFIGURATIONS)
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
ActiveStorage::Blob.service = @old_service
|
|
|
|
end
|
|
|
|
|
|
|
|
test "creating new direct upload" do
|
2017-07-30 11:00:55 -04:00
|
|
|
checksum = Digest::MD5.base64digest("Hello")
|
|
|
|
|
2017-07-27 16:15:38 -04:00
|
|
|
post rails_direct_uploads_url, params: { blob: {
|
2017-07-30 11:00:55 -04:00
|
|
|
filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain" } }
|
2017-07-09 12:03:13 -04:00
|
|
|
|
2017-07-27 16:15:38 -04:00
|
|
|
response.parsed_body.tap do |details|
|
2017-07-30 11:00:55 -04:00
|
|
|
assert_equal ActiveStorage::Blob.find(details["id"]), ActiveStorage::Blob.find_signed(details["signed_id"])
|
|
|
|
assert_equal "hello.txt", details["filename"]
|
|
|
|
assert_equal 6, details["byte_size"]
|
|
|
|
assert_equal checksum, details["checksum"]
|
|
|
|
assert_equal "text/plain", details["content_type"]
|
2017-08-04 18:56:14 -04:00
|
|
|
assert_match SERVICE_CONFIGURATIONS[:s3][:bucket], details["direct_upload"]["url"]
|
2018-02-21 07:06:25 -05:00
|
|
|
assert_match(/s3(-[-a-z0-9]+)?\.(\S+)?amazonaws\.com/, details["direct_upload"]["url"])
|
2017-07-30 11:00:55 -04:00
|
|
|
assert_equal({ "Content-Type" => "text/plain", "Content-MD5" => checksum }, details["direct_upload"]["headers"])
|
2017-07-23 15:15:30 -04:00
|
|
|
end
|
2017-07-09 12:03:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2017-07-17 09:17:09 -04:00
|
|
|
puts "Skipping S3 Direct Upload tests because no S3 configuration was supplied"
|
|
|
|
end
|
|
|
|
|
|
|
|
if SERVICE_CONFIGURATIONS[:gcs]
|
2017-07-27 16:15:38 -04:00
|
|
|
class ActiveStorage::GCSDirectUploadsControllerTest < ActionDispatch::IntegrationTest
|
2017-07-17 09:17:09 -04:00
|
|
|
setup do
|
|
|
|
@config = SERVICE_CONFIGURATIONS[:gcs]
|
|
|
|
|
|
|
|
@old_service = ActiveStorage::Blob.service
|
|
|
|
ActiveStorage::Blob.service = ActiveStorage::Service.configure(:gcs, SERVICE_CONFIGURATIONS)
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
ActiveStorage::Blob.service = @old_service
|
|
|
|
end
|
|
|
|
|
|
|
|
test "creating new direct upload" do
|
2017-07-30 11:00:55 -04:00
|
|
|
checksum = Digest::MD5.base64digest("Hello")
|
|
|
|
|
2017-07-27 16:15:38 -04:00
|
|
|
post rails_direct_uploads_url, params: { blob: {
|
2017-07-30 11:00:55 -04:00
|
|
|
filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain" } }
|
2017-07-17 09:17:09 -04:00
|
|
|
|
2017-07-27 16:15:38 -04:00
|
|
|
@response.parsed_body.tap do |details|
|
2017-07-30 11:00:55 -04:00
|
|
|
assert_equal ActiveStorage::Blob.find(details["id"]), ActiveStorage::Blob.find_signed(details["signed_id"])
|
|
|
|
assert_equal "hello.txt", details["filename"]
|
|
|
|
assert_equal 6, details["byte_size"]
|
|
|
|
assert_equal checksum, details["checksum"]
|
|
|
|
assert_equal "text/plain", details["content_type"]
|
|
|
|
assert_match %r{storage\.googleapis\.com/#{@config[:bucket]}}, details["direct_upload"]["url"]
|
2018-03-06 13:09:22 -05:00
|
|
|
assert_equal({ "Content-MD5" => checksum }, details["direct_upload"]["headers"])
|
2017-07-23 15:15:30 -04:00
|
|
|
end
|
2017-07-17 09:17:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
puts "Skipping GCS Direct Upload tests because no GCS configuration was supplied"
|
2017-07-09 12:03:13 -04:00
|
|
|
end
|
2017-07-25 21:03:48 -04:00
|
|
|
|
2017-07-31 11:09:12 -04:00
|
|
|
if SERVICE_CONFIGURATIONS[:azure]
|
2017-08-04 18:57:02 -04:00
|
|
|
class ActiveStorage::AzureStorageDirectUploadsControllerTest < ActionDispatch::IntegrationTest
|
2017-07-31 11:09:12 -04:00
|
|
|
setup do
|
|
|
|
@config = SERVICE_CONFIGURATIONS[:azure]
|
|
|
|
|
|
|
|
@old_service = ActiveStorage::Blob.service
|
|
|
|
ActiveStorage::Blob.service = ActiveStorage::Service.configure(:azure, SERVICE_CONFIGURATIONS)
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
ActiveStorage::Blob.service = @old_service
|
|
|
|
end
|
|
|
|
|
|
|
|
test "creating new direct upload" do
|
|
|
|
checksum = Digest::MD5.base64digest("Hello")
|
|
|
|
|
|
|
|
post rails_direct_uploads_url, params: { blob: {
|
|
|
|
filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain" } }
|
|
|
|
|
|
|
|
@response.parsed_body.tap do |details|
|
|
|
|
assert_equal ActiveStorage::Blob.find(details["id"]), ActiveStorage::Blob.find_signed(details["signed_id"])
|
|
|
|
assert_equal "hello.txt", details["filename"]
|
|
|
|
assert_equal 6, details["byte_size"]
|
|
|
|
assert_equal checksum, details["checksum"]
|
|
|
|
assert_equal "text/plain", details["content_type"]
|
|
|
|
assert_match %r{#{@config[:storage_account_name]}\.blob\.core\.windows\.net/#{@config[:container]}}, details["direct_upload"]["url"]
|
|
|
|
assert_equal({ "Content-Type" => "text/plain", "Content-MD5" => checksum, "x-ms-blob-type" => "BlockBlob" }, details["direct_upload"]["headers"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2017-08-04 18:57:02 -04:00
|
|
|
puts "Skipping Azure Storage Direct Upload tests because no Azure Storage configuration was supplied"
|
2017-07-31 11:09:12 -04:00
|
|
|
end
|
|
|
|
|
2017-07-27 16:15:38 -04:00
|
|
|
class ActiveStorage::DiskDirectUploadsControllerTest < ActionDispatch::IntegrationTest
|
2017-07-25 21:03:48 -04:00
|
|
|
test "creating new direct upload" do
|
2017-07-30 11:00:55 -04:00
|
|
|
checksum = Digest::MD5.base64digest("Hello")
|
|
|
|
|
2017-07-27 16:15:38 -04:00
|
|
|
post rails_direct_uploads_url, params: { blob: {
|
2017-07-30 11:00:55 -04:00
|
|
|
filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain" } }
|
2017-07-25 21:03:48 -04:00
|
|
|
|
2017-07-27 16:15:38 -04:00
|
|
|
@response.parsed_body.tap do |details|
|
2017-07-30 11:00:55 -04:00
|
|
|
assert_equal ActiveStorage::Blob.find(details["id"]), ActiveStorage::Blob.find_signed(details["signed_id"])
|
|
|
|
assert_equal "hello.txt", details["filename"]
|
|
|
|
assert_equal 6, details["byte_size"]
|
|
|
|
assert_equal checksum, details["checksum"]
|
|
|
|
assert_equal "text/plain", details["content_type"]
|
2017-08-04 23:21:21 -04:00
|
|
|
assert_match(/rails\/active_storage\/disk/, details["direct_upload"]["url"])
|
2017-07-30 11:00:55 -04:00
|
|
|
assert_equal({ "Content-Type" => "text/plain" }, details["direct_upload"]["headers"])
|
2017-07-25 21:03:48 -04:00
|
|
|
end
|
|
|
|
end
|
2018-04-10 06:36:47 -04:00
|
|
|
|
|
|
|
test "creating new direct upload does not include root in json" do
|
|
|
|
checksum = Digest::MD5.base64digest("Hello")
|
|
|
|
|
|
|
|
set_include_root_in_json(true) do
|
|
|
|
post rails_direct_uploads_url, params: { blob: {
|
|
|
|
filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain" } }
|
|
|
|
end
|
|
|
|
|
|
|
|
@response.parsed_body.tap do |details|
|
|
|
|
assert_nil details["blob"]
|
|
|
|
assert_not_nil details["id"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def set_include_root_in_json(value)
|
|
|
|
original = ActiveRecord::Base.include_root_in_json
|
|
|
|
ActiveRecord::Base.include_root_in_json = value
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.include_root_in_json = original
|
|
|
|
end
|
2017-07-25 21:03:48 -04:00
|
|
|
end
|