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-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
|
|
|
|
2018-09-11 17:52:27 -04:00
|
|
|
# Filter out the backtrace from minitest while preserving the one from other libraries.
|
2017-08-07 05:40:20 -04:00
|
|
|
Minitest.backtrace_filter = Minitest::BacktraceFilter.new
|
|
|
|
|
2017-07-09 12:03:13 -04:00
|
|
|
require "yaml"
|
|
|
|
SERVICE_CONFIGURATIONS = begin
|
2017-10-13 13:47:56 -04:00
|
|
|
erb = ERB.new(Pathname.new(File.expand_path("service/configurations.yml", __dir__)).read)
|
2017-08-01 12:06:39 -04:00
|
|
|
configuration = YAML.load(erb.result) || {}
|
|
|
|
configuration.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"
|
2018-01-16 20:32:02 -05:00
|
|
|
ActiveStorage::Blob.service = ActiveStorage::Service::DiskService.new(root: Dir.mktmpdir("active_storage_tests"))
|
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")
|
2017-07-11 12:53:17 -04:00
|
|
|
|
2017-07-04 12:10:53 -04:00
|
|
|
class ActiveSupport::TestCase
|
2017-10-13 13:47:56 -04:00
|
|
|
self.file_fixture_path = File.expand_path("fixtures/files", __dir__)
|
2017-07-26 12:51:02 -04:00
|
|
|
|
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-07-19 18:32:20 -04:00
|
|
|
def create_blob(data: "Hello world!", filename: "hello.txt", content_type: "text/plain", identify: true, record: nil)
|
|
|
|
ActiveStorage::Blob.create_after_upload! io: StringIO.new(data), filename: filename, content_type: content_type, identify: identify, record: record
|
2017-07-04 12:10:53 -04:00
|
|
|
end
|
2017-07-21 17:34:18 -04:00
|
|
|
|
2019-07-19 18:32:20 -04:00
|
|
|
def create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg", metadata: nil, record: nil)
|
|
|
|
ActiveStorage::Blob.create_after_upload! io: file_fixture(filename).open, filename: filename, content_type: content_type, metadata: metadata, record: record
|
2017-07-21 17:34:18 -04:00
|
|
|
end
|
2017-07-26 14:58:59 -04:00
|
|
|
|
2019-07-19 18:32:20 -04:00
|
|
|
def create_blob_before_direct_upload(filename: "hello.txt", byte_size:, checksum:, content_type: "text/plain", record: nil)
|
|
|
|
ActiveStorage::Blob.create_before_direct_upload! filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type, record: record
|
2017-07-25 21:03:48 -04:00
|
|
|
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
|
2017-07-05 09:18:50 -04:00
|
|
|
end
|
|
|
|
|
2017-07-23 18:35:25 -04:00
|
|
|
require "global_id"
|
|
|
|
GlobalID.app = "ActiveStorageExampleApp"
|
|
|
|
ActiveRecord::Base.send :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
|
2018-03-05 15:57:52 -05:00
|
|
|
has_one_attached :cover_photo, dependent: false
|
2018-03-05 17:01:31 -05:00
|
|
|
|
2017-08-10 15:58:30 -04:00
|
|
|
has_many_attached :highlights
|
2018-03-05 17:01:31 -05:00
|
|
|
has_many_attached :vlogs, dependent: false
|
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
|
|
|
|
end
|
2019-03-24 13:59:28 -04:00
|
|
|
|
|
|
|
require_relative "../../tools/test_common"
|