2017-07-06 10:22:44 +00:00
|
|
|
require "service/shared_service_tests"
|
2017-07-09 16:03:13 +00:00
|
|
|
require "httparty"
|
2017-07-06 10:22:44 +00:00
|
|
|
|
|
|
|
if SERVICE_CONFIGURATIONS[:s3]
|
|
|
|
class ActiveStorage::Service::S3ServiceTest < ActiveSupport::TestCase
|
2017-07-08 23:55:50 +00:00
|
|
|
SERVICE = ActiveStorage::Service.configure(:s3, SERVICE_CONFIGURATIONS)
|
2017-07-06 10:22:44 +00:00
|
|
|
|
|
|
|
include ActiveStorage::Service::SharedServiceTests
|
2017-07-09 15:04:37 +00:00
|
|
|
|
2017-07-09 16:03:13 +00:00
|
|
|
test "direct upload" do
|
|
|
|
begin
|
2017-07-24 16:41:34 +00:00
|
|
|
key = SecureRandom.base58(24)
|
|
|
|
data = "Something else entirely!"
|
|
|
|
checksum = Digest::MD5.base64digest(data)
|
|
|
|
url = @service.url_for_direct_upload(key, expires_in: 5.minutes, content_type: "text/plain", content_length: data.size, checksum: checksum)
|
2017-07-09 16:03:13 +00:00
|
|
|
|
2017-07-16 23:17:18 +00:00
|
|
|
HTTParty.put(
|
2017-07-09 16:03:13 +00:00
|
|
|
url,
|
|
|
|
body: data,
|
2017-07-24 16:41:34 +00:00
|
|
|
headers: { "Content-Type" => "text/plain", "Content-MD5" => checksum },
|
2017-07-09 16:03:13 +00:00
|
|
|
debug_output: STDOUT
|
|
|
|
)
|
2017-07-13 19:54:06 +00:00
|
|
|
|
2017-07-09 16:03:13 +00:00
|
|
|
assert_equal data, @service.download(key)
|
|
|
|
ensure
|
|
|
|
@service.delete key
|
|
|
|
end
|
|
|
|
end
|
2017-07-13 19:54:06 +00:00
|
|
|
|
2017-07-09 15:04:37 +00:00
|
|
|
test "signed URL generation" do
|
2017-07-23 22:50:31 +00:00
|
|
|
assert_match /#{SERVICE_CONFIGURATIONS[:s3][:bucket]}\.s3.(\S+)?amazonaws.com.*response-content-disposition=inline.*avatar\.png.*response-content-type=image%2Fpng/,
|
|
|
|
@service.url(FIXTURE_KEY, expires_in: 5.minutes, disposition: :inline, filename: "avatar.png", content_type: "image/png")
|
2017-07-09 15:04:37 +00:00
|
|
|
end
|
2017-07-20 16:29:37 +00:00
|
|
|
|
|
|
|
test "uploading with server-side encryption" do
|
2017-07-25 03:50:20 +00:00
|
|
|
config = SERVICE_CONFIGURATIONS.deep_merge(s3: { upload: { server_side_encryption: "AES256" }})
|
|
|
|
service = ActiveStorage::Service.configure(:s3, config)
|
2017-07-20 16:29:37 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
key = SecureRandom.base58(24)
|
|
|
|
data = "Something else entirely!"
|
2017-07-25 03:50:20 +00:00
|
|
|
service.upload key, StringIO.new(data), checksum: Digest::MD5.base64digest(data)
|
2017-07-20 16:29:37 +00:00
|
|
|
|
2017-07-25 03:50:20 +00:00
|
|
|
assert_equal "AES256", service.bucket.object(key).server_side_encryption
|
2017-07-20 16:29:37 +00:00
|
|
|
ensure
|
2017-07-25 03:50:20 +00:00
|
|
|
service.delete key
|
2017-07-20 16:29:37 +00:00
|
|
|
end
|
|
|
|
end
|
2017-07-06 10:22:44 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
puts "Skipping S3 Service tests because no S3 configuration was supplied"
|
|
|
|
end
|