2017-08-12 08:32:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-06 06:22:44 -04:00
|
|
|
require "test_helper"
|
|
|
|
require "active_support/core_ext/securerandom"
|
|
|
|
|
|
|
|
module ActiveStorage::Service::SharedServiceTests
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2018-05-17 04:32:27 -04:00
|
|
|
FIXTURE_DATA = (+"\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000\020\000\000\000\020\001\003\000\000\000%=m\"\000\000\000\006PLTE\000\000\000\377\377\377\245\331\237\335\000\000\0003IDATx\234c\370\377\237\341\377_\206\377\237\031\016\2603\334?\314p\1772\303\315\315\f7\215\031\356\024\203\320\275\317\f\367\201R\314\f\017\300\350\377\177\000Q\206\027(\316]\233P\000\000\000\000IEND\256B`\202").force_encoding(Encoding::BINARY)
|
2017-07-06 06:22:44 -04:00
|
|
|
|
|
|
|
included do
|
|
|
|
setup do
|
2018-06-25 18:49:26 -04:00
|
|
|
@key = SecureRandom.base58(24)
|
2017-07-06 06:22:44 -04:00
|
|
|
@service = self.class.const_get(:SERVICE)
|
2018-06-25 18:49:26 -04:00
|
|
|
@service.upload @key, StringIO.new(FIXTURE_DATA)
|
2017-07-06 06:22:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
2018-06-25 18:49:26 -04:00
|
|
|
@service.delete @key
|
2017-07-06 06:22:44 -04:00
|
|
|
end
|
|
|
|
|
2017-07-06 10:01:11 -04:00
|
|
|
test "uploading with integrity" do
|
2017-07-06 06:22:44 -04:00
|
|
|
begin
|
|
|
|
key = SecureRandom.base58(24)
|
|
|
|
data = "Something else entirely!"
|
2017-07-06 10:01:11 -04:00
|
|
|
@service.upload(key, StringIO.new(data), checksum: Digest::MD5.base64digest(data))
|
2017-07-06 06:22:44 -04:00
|
|
|
|
|
|
|
assert_equal data, @service.download(key)
|
|
|
|
ensure
|
|
|
|
@service.delete key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-25 21:03:48 -04:00
|
|
|
test "uploading without integrity" do
|
2017-07-06 10:01:11 -04:00
|
|
|
begin
|
|
|
|
key = SecureRandom.base58(24)
|
|
|
|
data = "Something else entirely!"
|
|
|
|
|
|
|
|
assert_raises(ActiveStorage::IntegrityError) do
|
2017-07-06 15:31:31 -04:00
|
|
|
@service.upload(key, StringIO.new(data), checksum: Digest::MD5.base64digest("bad data"))
|
2017-07-06 10:01:11 -04:00
|
|
|
end
|
2017-07-25 21:03:48 -04:00
|
|
|
|
|
|
|
assert_not @service.exist?(key)
|
2017-07-06 10:01:11 -04:00
|
|
|
ensure
|
|
|
|
@service.delete key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-06 06:22:44 -04:00
|
|
|
test "downloading" do
|
2018-06-25 18:49:26 -04:00
|
|
|
assert_equal FIXTURE_DATA, @service.download(@key)
|
2017-07-06 06:22:44 -04:00
|
|
|
end
|
|
|
|
|
2018-08-18 13:31:33 -04:00
|
|
|
test "downloading a nonexistent file" do
|
|
|
|
assert_raises(ActiveStorage::FileNotFoundError) do
|
|
|
|
@service.download(SecureRandom.base58(24))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-11-06 21:29:37 -05:00
|
|
|
test "downloading in chunks" do
|
2018-05-01 23:20:56 -04:00
|
|
|
key = SecureRandom.base58(24)
|
|
|
|
expected_chunks = [ "a" * 5.megabytes, "b" ]
|
|
|
|
actual_chunks = []
|
2017-11-06 21:29:37 -05:00
|
|
|
|
2018-05-01 23:20:56 -04:00
|
|
|
begin
|
|
|
|
@service.upload key, StringIO.new(expected_chunks.join)
|
|
|
|
|
|
|
|
@service.download key do |chunk|
|
|
|
|
actual_chunks << chunk
|
|
|
|
end
|
2017-11-06 21:29:37 -05:00
|
|
|
|
2018-05-01 23:20:56 -04:00
|
|
|
assert_equal expected_chunks, actual_chunks, "Downloaded chunks did not match uploaded data"
|
|
|
|
ensure
|
|
|
|
@service.delete key
|
|
|
|
end
|
2017-11-06 21:29:37 -05:00
|
|
|
end
|
|
|
|
|
2018-08-18 13:31:33 -04:00
|
|
|
test "downloading a nonexistent file in chunks" do
|
|
|
|
assert_raises(ActiveStorage::FileNotFoundError) do
|
2018-09-25 13:18:20 -04:00
|
|
|
@service.download(SecureRandom.base58(24)) { }
|
2018-08-18 13:31:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2018-03-05 11:53:31 -05:00
|
|
|
test "downloading partially" do
|
2018-06-25 18:49:26 -04:00
|
|
|
assert_equal "\x10\x00\x00", @service.download_chunk(@key, 19..21)
|
|
|
|
assert_equal "\x10\x00\x00", @service.download_chunk(@key, 19...22)
|
2018-03-05 11:53:31 -05:00
|
|
|
end
|
|
|
|
|
2018-08-18 13:31:33 -04:00
|
|
|
test "partially downloading a nonexistent file" do
|
|
|
|
assert_raises(ActiveStorage::FileNotFoundError) do
|
|
|
|
@service.download_chunk(SecureRandom.base58(24), 19..21)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-07-06 06:22:44 -04:00
|
|
|
test "existing" do
|
2018-06-25 18:49:26 -04:00
|
|
|
assert @service.exist?(@key)
|
|
|
|
assert_not @service.exist?(@key + "nonsense")
|
2017-07-06 06:22:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "deleting" do
|
2018-06-25 18:49:26 -04:00
|
|
|
@service.delete @key
|
|
|
|
assert_not @service.exist?(@key)
|
2017-07-06 06:22:44 -04:00
|
|
|
end
|
2017-07-08 18:11:27 -04:00
|
|
|
|
2017-07-06 17:27:27 -04:00
|
|
|
test "deleting nonexistent key" do
|
|
|
|
assert_nothing_raised do
|
|
|
|
@service.delete SecureRandom.base58(24)
|
|
|
|
end
|
|
|
|
end
|
2017-12-02 22:43:28 -05:00
|
|
|
|
|
|
|
test "deleting by prefix" do
|
|
|
|
begin
|
|
|
|
@service.upload("a/a/a", StringIO.new(FIXTURE_DATA))
|
|
|
|
@service.upload("a/a/b", StringIO.new(FIXTURE_DATA))
|
|
|
|
@service.upload("a/b/a", StringIO.new(FIXTURE_DATA))
|
|
|
|
|
|
|
|
@service.delete_prefixed("a/a/")
|
|
|
|
assert_not @service.exist?("a/a/a")
|
|
|
|
assert_not @service.exist?("a/a/b")
|
|
|
|
assert @service.exist?("a/b/a")
|
|
|
|
ensure
|
|
|
|
@service.delete("a/a/a")
|
|
|
|
@service.delete("a/a/b")
|
|
|
|
@service.delete("a/b/a")
|
|
|
|
end
|
|
|
|
end
|
2017-07-06 06:22:44 -04:00
|
|
|
end
|
|
|
|
end
|