2017-08-12 12:32:15 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-06 10:22:44 +00:00
|
|
|
require "service/shared_service_tests"
|
2017-08-01 17:56:39 +00:00
|
|
|
require "net/http"
|
2017-07-06 10:22:44 +00:00
|
|
|
|
2017-08-01 22:22:20 +00:00
|
|
|
if SERVICE_CONFIGURATIONS[:s3] && SERVICE_CONFIGURATIONS[:s3][:access_key_id].present?
|
2017-07-06 10:22:44 +00:00
|
|
|
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-08-01 17:56:39 +00:00
|
|
|
uri = URI.parse url
|
|
|
|
request = Net::HTTP::Put.new uri.request_uri
|
|
|
|
request.body = data
|
2017-08-02 18:13:11 +00:00
|
|
|
request.add_field "Content-Type", "text/plain"
|
|
|
|
request.add_field "Content-MD5", checksum
|
2017-08-01 17:56:39 +00:00
|
|
|
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
|
|
|
|
http.request request
|
|
|
|
end
|
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-08-04 22:56:14 +00:00
|
|
|
url = @service.url(FIXTURE_KEY, expires_in: 5.minutes,
|
2017-08-20 22:45:39 +00:00
|
|
|
disposition: "inline; filename=\"avatar.png\"", filename: "avatar.png", content_type: "image/png")
|
2017-08-04 22:56:14 +00:00
|
|
|
|
2017-08-05 03:21:21 +00:00
|
|
|
assert_match(/s3\.(\S+)?amazonaws.com.*response-content-disposition=inline.*avatar\.png.*response-content-type=image%2Fpng/, url)
|
2017-08-04 22:56:14 +00:00
|
|
|
assert_match SERVICE_CONFIGURATIONS[:s3][:bucket], url
|
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-08-03 18:41:23 +00:00
|
|
|
config = SERVICE_CONFIGURATIONS.deep_merge(s3: { upload: { server_side_encryption: "AES256" } })
|
2017-07-25 03:50:20 +00:00
|
|
|
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
|