diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/attachments_test.rb | 4 | ||||
-rw-r--r-- | test/blob_test.rb | 15 | ||||
-rw-r--r-- | test/database/setup.rb | 2 | ||||
-rw-r--r-- | test/direct_uploads_controller_test.rb | 36 | ||||
-rw-r--r-- | test/filename_test.rb | 10 | ||||
-rw-r--r-- | test/service/configurator_test.rb | 1 | ||||
-rw-r--r-- | test/service/disk_service_test.rb | 2 | ||||
-rw-r--r-- | test/service/gcs_service_test.rb | 22 | ||||
-rw-r--r-- | test/service/mirror_service_test.rb | 4 | ||||
-rw-r--r-- | test/service/s3_service_test.rb | 43 |
10 files changed, 104 insertions, 35 deletions
diff --git a/test/attachments_test.rb b/test/attachments_test.rb index ec7e9fcd5b..9b88b18247 100644 --- a/test/attachments_test.rb +++ b/test/attachments_test.rb @@ -66,7 +66,7 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase test "attach new blobs" do @user.highlights.attach( - { io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" }, + { io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" }, { io: StringIO.new("IT"), filename: "country.jpg", content_type: "image/jpg" }) assert_equal "town.jpg", @user.highlights.first.filename.to_s @@ -76,7 +76,7 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase test "purge attached blobs" do @user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "wonky.jpg") highlight_keys = @user.highlights.collect(&:key) - + @user.highlights.purge assert_not @user.highlights.attached? assert_not ActiveStorage::Blob.service.exist?(highlight_keys.first) diff --git a/test/blob_test.rb b/test/blob_test.rb index 60cf5426a8..ddc000ed51 100644 --- a/test/blob_test.rb +++ b/test/blob_test.rb @@ -12,10 +12,23 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase assert_equal Digest::MD5.base64digest(data), blob.checksum end + test "download yields chunks" do + blob = create_blob data: "a" * 75.kilobytes + chunks = [] + + blob.download do |chunk| + chunks << chunk + end + + assert_equal 2, chunks.size + assert_equal "a" * 64.kilobytes, chunks.first + assert_equal "a" * 11.kilobytes, chunks.second + end + test "urls expiring in 5 minutes" do blob = create_blob - travel_to Time.now do + freeze_time do assert_equal expected_url_for(blob), blob.url assert_equal expected_url_for(blob, disposition: :attachment), blob.url(disposition: :attachment) end diff --git a/test/database/setup.rb b/test/database/setup.rb index 5921412b0c..b12038ee68 100644 --- a/test/database/setup.rb +++ b/test/database/setup.rb @@ -1,6 +1,6 @@ require "active_storage/migration" require_relative "create_users_migration" -ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') +ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") ActiveStorageCreateTables.migrate(:up) ActiveStorageCreateUsers.migrate(:up) diff --git a/test/direct_uploads_controller_test.rb b/test/direct_uploads_controller_test.rb index bed985148e..8aa61f53cb 100644 --- a/test/direct_uploads_controller_test.rb +++ b/test/direct_uploads_controller_test.rb @@ -7,7 +7,7 @@ require "action_controller/test_case" require "active_storage/direct_uploads_controller" if SERVICE_CONFIGURATIONS[:s3] - class ActiveStorage::DirectUploadsControllerTest < ActionController::TestCase + class ActiveStorage::S3DirectUploadsControllerTest < ActionController::TestCase setup do @blob = create_blob @routes = Routes @@ -27,10 +27,40 @@ if SERVICE_CONFIGURATIONS[:s3] details = JSON.parse(@response.body) - assert_match /rails-activestorage\.s3.amazonaws\.com/, details["url"] + assert_match /#{SERVICE_CONFIGURATIONS[:s3][:bucket]}\.s3.(\S+)?amazonaws\.com/, details["url"] assert_equal "hello.txt", GlobalID::Locator.locate_signed(details["sgid"]).filename.to_s end end else - puts "Skipping Direct Upload tests because no S3 configuration was supplied" + puts "Skipping S3 Direct Upload tests because no S3 configuration was supplied" +end + +if SERVICE_CONFIGURATIONS[:gcs] + class ActiveStorage::GCSDirectUploadsControllerTest < ActionController::TestCase + setup do + @blob = create_blob + @routes = Routes + @controller = ActiveStorage::DirectUploadsController.new + @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 + post :create, params: { blob: { + filename: "hello.txt", byte_size: 6, checksum: Digest::MD5.base64digest("Hello"), content_type: "text/plain" } } + + details = JSON.parse(@response.body) + + assert_match %r{storage\.googleapis\.com/#{@config[:bucket]}}, details["url"] + assert_equal "hello.txt", GlobalID::Locator.locate_signed(details["sgid"]).filename.to_s + end + end +else + puts "Skipping GCS Direct Upload tests because no GCS configuration was supplied" end diff --git a/test/filename_test.rb b/test/filename_test.rb index 448ba7f766..e448238675 100644 --- a/test/filename_test.rb +++ b/test/filename_test.rb @@ -4,8 +4,8 @@ class ActiveStorage::FilenameTest < ActiveSupport::TestCase test "sanitize" do "%$|:;/\t\r\n\\".each_char do |character| filename = ActiveStorage::Filename.new("foo#{character}bar.pdf") - assert_equal 'foo-bar.pdf', filename.sanitized - assert_equal 'foo-bar.pdf', filename.to_s + assert_equal "foo-bar.pdf", filename.sanitized + assert_equal "foo-bar.pdf", filename.to_s end end @@ -23,14 +23,14 @@ class ActiveStorage::FilenameTest < ActiveSupport::TestCase test "strips RTL override chars used to spoof unsafe executables as docs" do # Would be displayed in Windows as "evilexe.pdf" due to the right-to-left # (RTL) override char! - assert_equal 'evil-fdp.exe', ActiveStorage::Filename.new("evil\u{202E}fdp.exe").sanitized + assert_equal "evil-fdp.exe", ActiveStorage::Filename.new("evil\u{202E}fdp.exe").sanitized end test "compare case-insensitively" do - assert_operator ActiveStorage::Filename.new('foobar.pdf'), :==, ActiveStorage::Filename.new('FooBar.PDF') + assert_operator ActiveStorage::Filename.new("foobar.pdf"), :==, ActiveStorage::Filename.new("FooBar.PDF") end test "compare sanitized" do - assert_operator ActiveStorage::Filename.new('foo-bar.pdf'), :==, ActiveStorage::Filename.new("foo\tbar.pdf") + assert_operator ActiveStorage::Filename.new("foo-bar.pdf"), :==, ActiveStorage::Filename.new("foo\tbar.pdf") end end diff --git a/test/service/configurator_test.rb b/test/service/configurator_test.rb index f8e4dccc9c..c69b8d5087 100644 --- a/test/service/configurator_test.rb +++ b/test/service/configurator_test.rb @@ -12,4 +12,3 @@ class ActiveStorage::Service::ConfiguratorTest < ActiveSupport::TestCase end end end - diff --git a/test/service/disk_service_test.rb b/test/service/disk_service_test.rb index f7752b25ef..e9a96003f1 100644 --- a/test/service/disk_service_test.rb +++ b/test/service/disk_service_test.rb @@ -7,6 +7,6 @@ class ActiveStorage::Service::DiskServiceTest < ActiveSupport::TestCase test "url generation" do assert_match /rails\/active_storage\/disk\/.*\/avatar\.png\?disposition=inline/, - @service.url(FIXTURE_KEY, expires_in: 5.minutes, disposition: :inline, filename: "avatar.png") + @service.url(FIXTURE_KEY, expires_in: 5.minutes, disposition: :inline, filename: "avatar.png") end end diff --git a/test/service/gcs_service_test.rb b/test/service/gcs_service_test.rb index 7d4700498b..4cde4b9289 100644 --- a/test/service/gcs_service_test.rb +++ b/test/service/gcs_service_test.rb @@ -1,4 +1,5 @@ require "service/shared_service_tests" +require "httparty" if SERVICE_CONFIGURATIONS[:gcs] class ActiveStorage::Service::GCSServiceTest < ActiveSupport::TestCase @@ -6,8 +7,27 @@ if SERVICE_CONFIGURATIONS[:gcs] include ActiveStorage::Service::SharedServiceTests + test "direct upload" do + begin + key = SecureRandom.base58(24) + data = "Something else entirely!" + direct_upload_url = @service.url_for_direct_upload(key, expires_in: 5.minutes, content_type: "text/plain", content_length: data.size) + + HTTParty.put( + direct_upload_url, + body: data, + headers: { "Content-Type" => "text/plain" }, + debug_output: STDOUT + ) + + assert_equal data, @service.download(key) + ensure + @service.delete key + end + end + test "signed URL generation" do - travel_to Time.now do + freeze_time do url = SERVICE.bucket.signed_url(FIXTURE_KEY, expires: 120) + "&response-content-disposition=inline%3B+filename%3D%22test.txt%22" diff --git a/test/service/mirror_service_test.rb b/test/service/mirror_service_test.rb index 8bda01f169..fd3d8125d6 100644 --- a/test/service/mirror_service_test.rb +++ b/test/service/mirror_service_test.rb @@ -8,7 +8,7 @@ class ActiveStorage::Service::MirrorServiceTest < ActiveSupport::TestCase end.to_h config = mirror_config.merge \ - mirror: { service: "Mirror", primary: 'primary', mirrors: mirror_config.keys }, + mirror: { service: "Mirror", primary: "primary", mirrors: mirror_config.keys }, primary: { service: "Disk", root: Dir.mktmpdir("active_storage_tests_primary") } SERVICE = ActiveStorage::Service.configure :mirror, config @@ -45,7 +45,7 @@ class ActiveStorage::Service::MirrorServiceTest < ActiveSupport::TestCase end test "URL generation in primary service" do - travel_to Time.now do + freeze_time do assert_equal SERVICE.primary.url(FIXTURE_KEY, expires_in: 2.minutes, disposition: :inline, filename: "test.txt"), @service.url(FIXTURE_KEY, expires_in: 2.minutes, disposition: :inline, filename: "test.txt") end diff --git a/test/service/s3_service_test.rb b/test/service/s3_service_test.rb index 167aa78a17..049511497b 100644 --- a/test/service/s3_service_test.rb +++ b/test/service/s3_service_test.rb @@ -1,6 +1,5 @@ require "service/shared_service_tests" require "httparty" -require "uri" if SERVICE_CONFIGURATIONS[:s3] class ActiveStorage::Service::S3ServiceTest < ActiveSupport::TestCase @@ -9,37 +8,45 @@ if SERVICE_CONFIGURATIONS[:s3] include ActiveStorage::Service::SharedServiceTests test "direct upload" do - # FIXME: This test is failing because of a mismatched request signature, but it works in the browser. - skip - begin key = SecureRandom.base58(24) data = "Something else entirely!" - direct_upload_url = @service.url_for_direct_upload(key, expires_in: 5.minutes, content_type: "text/plain", content_length: data.size) - - url = URI.parse(direct_upload_url).to_s.split("?").first - query = CGI::parse(URI.parse(direct_upload_url).query).collect { |(k, v)| [ k, v.first ] }.to_h + url = @service.url_for_direct_upload(key, expires_in: 5.minutes, content_type: "text/plain", content_length: data.size) - HTTParty.post( + HTTParty.put( url, - query: query, body: data, - headers: { - "Content-Type": "text/plain", - "Origin": "http://localhost:3000" - }, + headers: { "Content-Type" => "text/plain" }, debug_output: STDOUT ) - + assert_equal data, @service.download(key) ensure @service.delete key end end - + test "signed URL generation" do - assert_match /rails-activestorage\.s3\.amazonaws\.com.*response-content-disposition=inline.*avatar\.png/, - @service.url(FIXTURE_KEY, expires_in: 5.minutes, disposition: :inline, filename: "avatar.png") + assert_match /#{SERVICE_CONFIGURATIONS[:s3][:bucket]}\.s3.(\S+)?amazonaws.com.*response-content-disposition=inline.*avatar\.png/, + @service.url(FIXTURE_KEY, expires_in: 5.minutes, disposition: :inline, filename: "avatar.png") + end + + test "uploading with server-side encryption" do + config = {} + config[:s3] = SERVICE_CONFIGURATIONS[:s3].merge \ + upload: { server_side_encryption: "AES256" } + + sse_service = ActiveStorage::Service.configure(:s3, config) + + begin + key = SecureRandom.base58(24) + data = "Something else entirely!" + sse_service.upload(key, StringIO.new(data), checksum: Digest::MD5.base64digest(data)) + + assert_equal "AES256", sse_service.bucket.object(key).server_side_encryption + ensure + sse_service.delete key + end end end else |