1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activestorage/test/service/s3_service_test.rb

67 lines
2.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "service/shared_service_tests"
require "net/http"
if SERVICE_CONFIGURATIONS[:s3]
class ActiveStorage::Service::S3ServiceTest < ActiveSupport::TestCase
SERVICE = ActiveStorage::Service.configure(:s3, SERVICE_CONFIGURATIONS)
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
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
uri = URI.parse url
request = Net::HTTP::Put.new uri.request_uri
request.body = data
request.add_field "Content-Type", "text/plain"
request.add_field "Content-MD5", checksum
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
http.request request
end
2017-07-09 12:03:13 -04:00
assert_equal data, @service.download(key)
ensure
@service.delete key
end
end
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
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")
assert_match(/s3(-[-a-z0-9]+)?\.(\S+)?amazonaws.com.*response-content-disposition=inline.*avatar\.png.*response-content-type=image%2Fpng/, url)
assert_match SERVICE_CONFIGURATIONS[:s3][:bucket], url
2017-07-09 11:04:37 -04:00
end
test "uploading with server-side encryption" do
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)
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-24 23:50:20 -04:00
assert_equal "AES256", service.bucket.object(key).server_side_encryption
ensure
2017-07-24 23:50:20 -04:00
service.delete key
end
end
end
else
puts "Skipping S3 Service tests because no S3 configuration was supplied"
end