2017-08-12 08:32:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-20 10:52:54 -05:00
|
|
|
ENV["RAILS_ENV"] ||= "test"
|
2017-10-13 13:50:03 -04:00
|
|
|
require_relative "dummy/config/environment.rb"
|
2017-07-21 17:12:29 -04:00
|
|
|
|
2017-07-01 06:06:08 -04:00
|
|
|
require "bundler/setup"
|
|
|
|
require "active_support"
|
2017-07-04 12:10:53 -04:00
|
|
|
require "active_support/test_case"
|
2019-08-01 05:43:25 -04:00
|
|
|
require "active_support/core_ext/object/try"
|
2017-07-01 06:06:08 -04:00
|
|
|
require "active_support/testing/autorun"
|
2019-10-05 20:37:23 -04:00
|
|
|
require "active_support/configuration_file"
|
2019-05-22 15:07:35 -04:00
|
|
|
require "active_storage/service/mirror_service"
|
2018-04-05 19:48:29 -04:00
|
|
|
require "image_processing/mini_magick"
|
2017-08-10 15:58:30 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
require "byebug"
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2017-07-01 06:06:08 -04:00
|
|
|
|
2017-07-22 11:00:16 -04:00
|
|
|
require "active_job"
|
|
|
|
ActiveJob::Base.queue_adapter = :test
|
2017-08-10 15:58:30 -04:00
|
|
|
ActiveJob::Base.logger = ActiveSupport::Logger.new(nil)
|
2017-07-22 11:00:16 -04:00
|
|
|
|
2017-07-09 12:03:13 -04:00
|
|
|
SERVICE_CONFIGURATIONS = begin
|
2019-10-05 20:37:23 -04:00
|
|
|
ActiveSupport::ConfigurationFile.parse(File.expand_path("service/configurations.yml", __dir__)).deep_symbolize_keys
|
2017-07-09 12:03:13 -04:00
|
|
|
rescue Errno::ENOENT
|
|
|
|
puts "Missing service configuration file in test/service/configurations.yml"
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
2017-07-09 16:22:20 -04:00
|
|
|
require "tmpdir"
|
2019-01-14 14:56:35 -05:00
|
|
|
|
2019-10-01 16:54:37 -04:00
|
|
|
Rails.configuration.active_storage.service_configurations = SERVICE_CONFIGURATIONS.merge(
|
2019-01-14 14:56:35 -05:00
|
|
|
"local" => { "service" => "Disk", "root" => Dir.mktmpdir("active_storage_tests") },
|
2019-07-22 11:46:02 -04:00
|
|
|
"local_public" => { "service" => "Disk", "root" => Dir.mktmpdir("active_storage_tests"), "public" => true },
|
2019-01-14 14:56:35 -05:00
|
|
|
"disk_mirror_1" => { "service" => "Disk", "root" => Dir.mktmpdir("active_storage_tests_1") },
|
|
|
|
"disk_mirror_2" => { "service" => "Disk", "root" => Dir.mktmpdir("active_storage_tests_2") },
|
|
|
|
"disk_mirror_3" => { "service" => "Disk", "root" => Dir.mktmpdir("active_storage_tests_3") },
|
|
|
|
"mirror" => { "service" => "Mirror", "primary" => "local", "mirrors" => ["disk_mirror_1", "disk_mirror_2", "disk_mirror_3"] }
|
2019-10-01 16:54:37 -04:00
|
|
|
).deep_stringify_keys
|
2019-01-14 14:56:35 -05:00
|
|
|
|
|
|
|
Rails.configuration.active_storage.service = "local"
|
2017-07-03 15:06:09 -04:00
|
|
|
|
2017-10-22 13:16:59 -04:00
|
|
|
ActiveStorage.logger = ActiveSupport::Logger.new(nil)
|
2017-07-23 12:03:25 -04:00
|
|
|
ActiveStorage.verifier = ActiveSupport::MessageVerifier.new("Testing")
|
2021-01-24 05:10:30 -05:00
|
|
|
ActiveStorage::FixtureSet.file_fixture_path = File.expand_path("fixtures/files", __dir__)
|
2017-07-11 12:53:17 -04:00
|
|
|
|
2017-07-04 12:10:53 -04:00
|
|
|
class ActiveSupport::TestCase
|
2021-01-24 05:10:30 -05:00
|
|
|
self.file_fixture_path = ActiveStorage::FixtureSet.file_fixture_path
|
2017-07-26 12:51:02 -04:00
|
|
|
|
2019-10-11 17:47:42 -04:00
|
|
|
include ActiveRecord::TestFixtures
|
|
|
|
|
2021-01-24 12:29:11 -05:00
|
|
|
self.fixture_path = File.expand_path("fixtures", __dir__)
|
|
|
|
|
2018-03-26 09:36:39 -04:00
|
|
|
setup do
|
|
|
|
ActiveStorage::Current.host = "https://example.com"
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
ActiveStorage::Current.reset
|
|
|
|
end
|
|
|
|
|
2017-07-04 12:10:53 -04:00
|
|
|
private
|
2019-01-14 14:56:35 -05:00
|
|
|
def create_blob(key: nil, data: "Hello world!", filename: "hello.txt", content_type: "text/plain", identify: true, service_name: nil, record: nil)
|
|
|
|
ActiveStorage::Blob.create_and_upload! key: key, io: StringIO.new(data), filename: filename, content_type: content_type, identify: identify, service_name: service_name, record: record
|
2017-07-04 12:10:53 -04:00
|
|
|
end
|
2017-07-21 17:34:18 -04:00
|
|
|
|
2019-12-06 20:28:08 -05:00
|
|
|
def create_file_blob(key: nil, filename: "racecar.jpg", content_type: "image/jpeg", metadata: nil, service_name: nil, record: nil)
|
|
|
|
ActiveStorage::Blob.create_and_upload! io: file_fixture(filename).open, filename: filename, content_type: content_type, metadata: metadata, service_name: service_name, record: record
|
2017-07-21 17:34:18 -04:00
|
|
|
end
|
2017-07-26 14:58:59 -04:00
|
|
|
|
2019-09-29 20:02:57 -04:00
|
|
|
def create_blob_before_direct_upload(key: nil, filename: "hello.txt", byte_size:, checksum:, content_type: "text/plain", record: nil)
|
|
|
|
ActiveStorage::Blob.create_before_direct_upload! key: key, filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type, record: record
|
2017-07-25 21:03:48 -04:00
|
|
|
end
|
|
|
|
|
2019-09-30 19:17:31 -04:00
|
|
|
def build_blob_after_unfurling(key: nil, data: "Hello world!", filename: "hello.txt", content_type: "text/plain", identify: true, record: nil)
|
|
|
|
ActiveStorage::Blob.build_after_unfurling key: key, io: StringIO.new(data), filename: filename, content_type: content_type, identify: identify, record: record
|
|
|
|
end
|
|
|
|
|
2019-07-19 18:32:20 -04:00
|
|
|
def directly_upload_file_blob(filename: "racecar.jpg", content_type: "image/jpeg", record: nil)
|
2018-03-21 23:16:36 -04:00
|
|
|
file = file_fixture(filename)
|
|
|
|
byte_size = file.size
|
|
|
|
checksum = Digest::MD5.file(file).base64digest
|
|
|
|
|
2019-07-19 18:32:20 -04:00
|
|
|
create_blob_before_direct_upload(filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type, record: record).tap do |blob|
|
2019-05-22 15:07:35 -04:00
|
|
|
service = ActiveStorage::Blob.service.try(:primary) || ActiveStorage::Blob.service
|
|
|
|
service.upload(blob.key, file.open)
|
2018-03-21 23:16:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-28 16:43:37 -04:00
|
|
|
def read_image(blob_or_variant)
|
|
|
|
MiniMagick::Image.open blob_or_variant.service.send(:path_for, blob_or_variant.key)
|
2017-07-21 17:45:55 -04:00
|
|
|
end
|
2018-04-25 16:28:30 -04:00
|
|
|
|
|
|
|
def extract_metadata_from(blob)
|
|
|
|
blob.tap(&:analyze).metadata
|
|
|
|
end
|
2018-07-07 23:25:33 -04:00
|
|
|
|
|
|
|
def fixture_file_upload(filename)
|
|
|
|
Rack::Test::UploadedFile.new file_fixture(filename).to_s
|
|
|
|
end
|
2019-01-14 14:56:35 -05:00
|
|
|
|
|
|
|
def with_service(service_name)
|
2019-10-01 16:54:37 -04:00
|
|
|
previous_service = ActiveStorage::Blob.service
|
2021-03-10 19:04:00 -05:00
|
|
|
ActiveStorage::Blob.service = service_name ? ActiveStorage::Blob.services.fetch(service_name) : nil
|
2019-01-14 14:56:35 -05:00
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ActiveStorage::Blob.service = previous_service
|
|
|
|
end
|
2017-07-05 09:18:50 -04:00
|
|
|
end
|
|
|
|
|
2017-07-23 18:35:25 -04:00
|
|
|
require "global_id"
|
|
|
|
GlobalID.app = "ActiveStorageExampleApp"
|
2020-10-06 08:19:24 -04:00
|
|
|
ActiveRecord::Base.include GlobalID::Identification
|
2017-08-10 15:58:30 -04:00
|
|
|
|
|
|
|
class User < ActiveRecord::Base
|
2018-07-07 23:25:33 -04:00
|
|
|
validates :name, presence: true
|
|
|
|
|
2017-08-10 15:58:30 -04:00
|
|
|
has_one_attached :avatar
|
2019-01-14 14:56:35 -05:00
|
|
|
has_one_attached :cover_photo, dependent: false, service: :local
|
2020-05-04 06:01:47 -04:00
|
|
|
has_one_attached :avatar_with_variants do |attachable|
|
|
|
|
attachable.variant :thumb, resize: "100x100"
|
|
|
|
end
|
2018-03-05 17:01:31 -05:00
|
|
|
|
2017-08-10 15:58:30 -04:00
|
|
|
has_many_attached :highlights
|
2019-01-14 14:56:35 -05:00
|
|
|
has_many_attached :vlogs, dependent: false, service: :local
|
2020-05-04 06:01:47 -04:00
|
|
|
has_many_attached :highlights_with_variants do |attachable|
|
|
|
|
attachable.variant :thumb, resize: "100x100"
|
|
|
|
end
|
2017-08-10 15:58:30 -04:00
|
|
|
end
|
2018-07-07 17:04:47 -04:00
|
|
|
|
|
|
|
class Group < ActiveRecord::Base
|
|
|
|
has_one_attached :avatar
|
2020-04-24 13:16:16 -04:00
|
|
|
has_many :users, autosave: true
|
2020-08-04 00:26:27 -04:00
|
|
|
|
|
|
|
accepts_nested_attributes_for :users
|
2018-07-07 17:04:47 -04:00
|
|
|
end
|
2019-03-24 13:59:28 -04:00
|
|
|
|
|
|
|
require_relative "../../tools/test_common"
|