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"
|
2017-08-01 13:56:39 -04:00
|
|
|
require "net/http"
|
2017-07-06 06:22:44 -04:00
|
|
|
|
2018-03-19 11:25:40 -04:00
|
|
|
if SERVICE_CONFIGURATIONS[:s3]
|
2017-07-06 06:22:44 -04:00
|
|
|
class ActiveStorage::Service::S3ServiceTest < ActiveSupport::TestCase
|
2017-07-08 19:55:50 -04:00
|
|
|
SERVICE = ActiveStorage::Service.configure(:s3, SERVICE_CONFIGURATIONS)
|
2017-07-06 06:22:44 -04:00
|
|
|
|
|
|
|
include ActiveStorage::Service::SharedServiceTests
|
2017-07-09 11:04:37 -04:00
|
|
|
|
2017-07-09 12:03:13 -04:00
|
|
|
test "direct upload" do
|
|
|
|
begin
|
2017-07-24 12:41:34 -04: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 12:03:13 -04:00
|
|
|
|
2017-08-01 13:56:39 -04:00
|
|
|
uri = URI.parse url
|
|
|
|
request = Net::HTTP::Put.new uri.request_uri
|
|
|
|
request.body = data
|
2017-08-02 14:13:11 -04:00
|
|
|
request.add_field "Content-Type", "text/plain"
|
|
|
|
request.add_field "Content-MD5", checksum
|
2017-08-01 13:56:39 -04:00
|
|
|
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
|
|
|
|
http.request request
|
|
|
|
end
|
2017-07-13 15:54:06 -04:00
|
|
|
|
2017-07-09 12:03:13 -04:00
|
|
|
assert_equal data, @service.download(key)
|
|
|
|
ensure
|
|
|
|
@service.delete key
|
|
|
|
end
|
|
|
|
end
|
2017-07-13 15:54:06 -04:00
|
|
|
|
2018-09-04 23:21:44 -04:00
|
|
|
test "upload a zero byte file" do
|
|
|
|
blob = directly_upload_file_blob filename: "empty_file.txt", content_type: nil
|
|
|
|
user = User.create! name: "DHH", avatar: blob
|
|
|
|
|
|
|
|
assert_equal user.avatar.blob, blob
|
|
|
|
end
|
|
|
|
|
2017-07-09 11:04:37 -04:00
|
|
|
test "signed URL generation" do
|
2018-06-25 18:49:26 -04:00
|
|
|
url = @service.url(@key, expires_in: 5.minutes,
|
2017-09-28 16:43:37 -04:00
|
|
|
disposition: :inline, filename: ActiveStorage::Filename.new("avatar.png"), content_type: "image/png")
|
2017-08-04 18:56:14 -04:00
|
|
|
|
2018-02-21 07:06:25 -05:00
|
|
|
assert_match(/s3(-[-a-z0-9]+)?\.(\S+)?amazonaws.com.*response-content-disposition=inline.*avatar\.png.*response-content-type=image%2Fpng/, url)
|
2017-08-04 18:56:14 -04:00
|
|
|
assert_match SERVICE_CONFIGURATIONS[:s3][:bucket], url
|
2017-07-09 11:04:37 -04:00
|
|
|
end
|
2017-07-20 12:29:37 -04:00
|
|
|
|
|
|
|
test "uploading with server-side encryption" do
|
2017-08-03 14:41:23 -04:00
|
|
|
config = SERVICE_CONFIGURATIONS.deep_merge(s3: { upload: { server_side_encryption: "AES256" } })
|
2017-07-24 23:50:20 -04:00
|
|
|
service = ActiveStorage::Service.configure(:s3, config)
|
2017-07-20 12:29:37 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
key = SecureRandom.base58(24)
|
|
|
|
data = "Something else entirely!"
|
2017-07-24 23:50:20 -04:00
|
|
|
service.upload key, StringIO.new(data), checksum: Digest::MD5.base64digest(data)
|
2017-07-20 12:29:37 -04:00
|
|
|
|
2017-07-24 23:50:20 -04:00
|
|
|
assert_equal "AES256", service.bucket.object(key).server_side_encryption
|
2017-07-20 12:29:37 -04:00
|
|
|
ensure
|
2017-07-24 23:50:20 -04:00
|
|
|
service.delete key
|
2017-07-20 12:29:37 -04:00
|
|
|
end
|
|
|
|
end
|
2017-07-06 06:22:44 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
puts "Skipping S3 Service tests because no S3 configuration was supplied"
|
|
|
|
end
|