aboutsummaryrefslogtreecommitdiffstats
path: root/test/service
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2017-07-06 12:22:44 +0200
committerDavid Heinemeier Hansson <david@loudthinking.com>2017-07-06 12:22:44 +0200
commit35d5bddabcd8f0eccc7de3ddf60431ea196508a1 (patch)
treeae57c818c471e2d1c0fe322f5d8448df039bfb63 /test/service
parentb3a9f3556dedb80cfa6336e6241d933baeb4f906 (diff)
downloadrails-35d5bddabcd8f0eccc7de3ddf60431ea196508a1.tar.gz
rails-35d5bddabcd8f0eccc7de3ddf60431ea196508a1.tar.bz2
rails-35d5bddabcd8f0eccc7de3ddf60431ea196508a1.zip
Rename from Site to Service now that we're called Active Storage
Diffstat (limited to 'test/service')
-rw-r--r--test/service/.gitignore1
-rw-r--r--test/service/configurations-example.yml11
-rw-r--r--test/service/disk_service_test.rb8
-rw-r--r--test/service/gcs_service_test.rb20
-rw-r--r--test/service/mirror_service_test.rb30
-rw-r--r--test/service/s3_service_test.rb11
-rw-r--r--test/service/shared_service_tests.rb63
7 files changed, 144 insertions, 0 deletions
diff --git a/test/service/.gitignore b/test/service/.gitignore
new file mode 100644
index 0000000000..c102131f3d
--- /dev/null
+++ b/test/service/.gitignore
@@ -0,0 +1 @@
+configurations.yml
diff --git a/test/service/configurations-example.yml b/test/service/configurations-example.yml
new file mode 100644
index 0000000000..031197342a
--- /dev/null
+++ b/test/service/configurations-example.yml
@@ -0,0 +1,11 @@
+# Copy this file to configurations.yml and edit the credentials to match your IAM test account and bucket
+s3:
+ access_key_id:
+ secret_access_key:
+ region:
+ bucket:
+
+gcs:
+ project:
+ keyfile:
+ bucket:
diff --git a/test/service/disk_service_test.rb b/test/service/disk_service_test.rb
new file mode 100644
index 0000000000..5dd7cff303
--- /dev/null
+++ b/test/service/disk_service_test.rb
@@ -0,0 +1,8 @@
+require "tmpdir"
+require "service/shared_service_tests"
+
+class ActiveStorage::Service::DiskServiceTest < ActiveSupport::TestCase
+ SERVICE = ActiveStorage::Service.configure(:Disk, root: File.join(Dir.tmpdir, "active_storage"))
+
+ include ActiveStorage::Service::SharedServiceTests
+end
diff --git a/test/service/gcs_service_test.rb b/test/service/gcs_service_test.rb
new file mode 100644
index 0000000000..42f9cd3061
--- /dev/null
+++ b/test/service/gcs_service_test.rb
@@ -0,0 +1,20 @@
+require "service/shared_service_tests"
+
+if SERVICE_CONFIGURATIONS[:gcs]
+ class ActiveStorage::Service::GCSServiceTest < ActiveSupport::TestCase
+ SERVICE = ActiveStorage::Service.configure(:GCS, SERVICE_CONFIGURATIONS[:gcs])
+
+ include ActiveStorage::Service::SharedServiceTests
+
+ test "signed URL generation" do
+ travel_to Time.now do
+ url = SERVICE.bucket.signed_url(FIXTURE_KEY, expires: 120) +
+ "&response-content-disposition=inline%3B+filename%3D%22test.txt%22"
+
+ assert_equal url, @service.url(FIXTURE_KEY, expires_in: 2.minutes, disposition: :inline, filename: "test.txt")
+ end
+ end
+ end
+else
+ puts "Skipping GCS Service tests because no GCS configuration was supplied"
+end
diff --git a/test/service/mirror_service_test.rb b/test/service/mirror_service_test.rb
new file mode 100644
index 0000000000..3b22c4f049
--- /dev/null
+++ b/test/service/mirror_service_test.rb
@@ -0,0 +1,30 @@
+require "tmpdir"
+require "service/shared_service_tests"
+
+class ActiveStorage::Service::MirrorServiceTest < ActiveSupport::TestCase
+ PRIMARY_DISK_SERVICE = ActiveStorage::Service.configure(:Disk, root: File.join(Dir.tmpdir, "active_storage"))
+ SECONDARY_DISK_SERVICE = ActiveStorage::Service.configure(:Disk, root: File.join(Dir.tmpdir, "active_storage_mirror"))
+
+ SERVICE = ActiveStorage::Service.configure :Mirror, services: [ PRIMARY_DISK_SERVICE, SECONDARY_DISK_SERVICE ]
+
+ include ActiveStorage::Service::SharedServiceTests
+
+ test "uploading was done to all services" do
+ begin
+ key = SecureRandom.base58(24)
+ data = "Something else entirely!"
+ io = StringIO.new(data)
+ @service.upload(key, io)
+
+ assert_equal data, PRIMARY_DISK_SERVICE.download(key)
+ assert_equal data, SECONDARY_DISK_SERVICE.download(key)
+ ensure
+ @service.delete key
+ end
+ end
+
+ test "existing in all services" do
+ assert PRIMARY_DISK_SERVICE.exist?(FIXTURE_KEY)
+ assert SECONDARY_DISK_SERVICE.exist?(FIXTURE_KEY)
+ end
+end
diff --git a/test/service/s3_service_test.rb b/test/service/s3_service_test.rb
new file mode 100644
index 0000000000..604dfd6c60
--- /dev/null
+++ b/test/service/s3_service_test.rb
@@ -0,0 +1,11 @@
+require "service/shared_service_tests"
+
+if SERVICE_CONFIGURATIONS[:s3]
+ class ActiveStorage::Service::S3ServiceTest < ActiveSupport::TestCase
+ SERVICE = ActiveStorage::Service.configure(:S3, SERVICE_CONFIGURATIONS[:s3])
+
+ include ActiveStorage::Service::SharedServiceTests
+ end
+else
+ puts "Skipping S3 Service tests because no S3 configuration was supplied"
+end
diff --git a/test/service/shared_service_tests.rb b/test/service/shared_service_tests.rb
new file mode 100644
index 0000000000..16672ab49b
--- /dev/null
+++ b/test/service/shared_service_tests.rb
@@ -0,0 +1,63 @@
+require "test_helper"
+require "active_support/core_ext/securerandom"
+require "yaml"
+
+SERVICE_CONFIGURATIONS = begin
+ YAML.load_file(File.expand_path("../configurations.yml", __FILE__)).deep_symbolize_keys
+rescue Errno::ENOENT
+ puts "Missing service configuration file in test/services/configurations.yml"
+end
+
+module ActiveStorage::Service::SharedServiceTests
+ extend ActiveSupport::Concern
+
+ FIXTURE_KEY = SecureRandom.base58(24)
+ FIXTURE_FILE = StringIO.new("Hello world!")
+
+ included do
+ setup do
+ @service = self.class.const_get(:SERVICE)
+ @service.upload FIXTURE_KEY, FIXTURE_FILE
+ FIXTURE_FILE.rewind
+ end
+
+ teardown do
+ @service.delete FIXTURE_KEY
+ FIXTURE_FILE.rewind
+ end
+
+ test "uploading" do
+ begin
+ key = SecureRandom.base58(24)
+ data = "Something else entirely!"
+ @service.upload(key, StringIO.new(data))
+
+ assert_equal data, @service.download(key)
+ ensure
+ @service.delete key
+ end
+ end
+
+ test "downloading" do
+ assert_equal FIXTURE_FILE.read, @service.download(FIXTURE_KEY)
+ end
+
+ test "existing" do
+ assert @service.exist?(FIXTURE_KEY)
+ assert_not @service.exist?(FIXTURE_KEY + "nonsense")
+ end
+
+ test "deleting" do
+ @service.delete FIXTURE_KEY
+ assert_not @service.exist?(FIXTURE_KEY)
+ end
+
+ test "sizing" do
+ assert_equal FIXTURE_FILE.size, @service.byte_size(FIXTURE_KEY)
+ end
+
+ test "checksumming" do
+ assert_equal Digest::MD5.hexdigest(FIXTURE_FILE.read), @service.checksum(FIXTURE_KEY)
+ end
+ end
+end