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"
|
2017-07-01 06:06:08 -04:00
|
|
|
require "active_support/testing/autorun"
|
2017-09-28 16:43:37 -04:00
|
|
|
require "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-08-07 05:40:20 -04:00
|
|
|
# Filter out Minitest backtrace while allowing backtrace from other libraries
|
|
|
|
# to be shown.
|
|
|
|
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
|
|
|
|
def create_blob(data: "Hello world!", filename: "hello.txt", content_type: "text/plain")
|
2017-07-06 05:33:29 -04:00
|
|
|
ActiveStorage::Blob.create_after_upload! io: StringIO.new(data), filename: filename, content_type: content_type
|
2017-07-04 12:10:53 -04:00
|
|
|
end
|
2017-07-21 17:34:18 -04:00
|
|
|
|
2017-11-13 16:36:39 -05:00
|
|
|
def create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg", metadata: nil)
|
|
|
|
ActiveStorage::Blob.create_after_upload! io: file_fixture(filename).open, filename: filename, content_type: content_type, metadata: metadata
|
2017-07-21 17:34:18 -04:00
|
|
|
end
|
2017-07-26 14:58:59 -04:00
|
|
|
|
2017-07-25 21:03:48 -04:00
|
|
|
def create_blob_before_direct_upload(filename: "hello.txt", byte_size:, checksum:, content_type: "text/plain")
|
|
|
|
ActiveStorage::Blob.create_before_direct_upload! filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type
|
|
|
|
end
|
|
|
|
|
2018-03-21 23:16:36 -04:00
|
|
|
def directly_upload_file_blob(filename: "racecar.jpg", content_type: "image/jpeg")
|
|
|
|
file = file_fixture(filename)
|
|
|
|
byte_size = file.size
|
|
|
|
checksum = Digest::MD5.file(file).base64digest
|
|
|
|
|
|
|
|
create_blob_before_direct_upload(filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type).tap do |blob|
|
|
|
|
ActiveStorage::Blob.service.upload(blob.key, file.open)
|
|
|
|
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
|
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
|
|
|
|
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
|