2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-11-25 13:56:14 -05:00
|
|
|
ENV["RAILS_ENV"] ||= "test"
|
|
|
|
ENV["MT_NO_EXPECTATIONS"] = "true"
|
|
|
|
require_relative "../config/environment"
|
|
|
|
require "rails/test_help"
|
2010-08-18 18:42:33 -04:00
|
|
|
|
2022-11-25 13:56:14 -05:00
|
|
|
require "factory_bot_rails"
|
|
|
|
require "mocha/minitest"
|
|
|
|
require "shoulda-context"
|
|
|
|
require "shoulda-matchers"
|
|
|
|
require "webmock/minitest"
|
2010-02-04 15:08:49 -05:00
|
|
|
|
2022-11-25 13:56:14 -05:00
|
|
|
require "sidekiq/testing"
|
|
|
|
Sidekiq::Testing.fake!
|
2023-06-08 14:39:14 -04:00
|
|
|
# https://github.com/sidekiq/sidekiq/issues/5907#issuecomment-1536457365
|
|
|
|
Sidekiq.configure_client do |cfg|
|
|
|
|
cfg.logger.level = Logger::WARN
|
|
|
|
end
|
2010-02-04 15:08:49 -05:00
|
|
|
|
2017-02-04 22:27:59 -05:00
|
|
|
Shoulda::Matchers.configure do |config|
|
|
|
|
config.integrate do |with|
|
2018-04-02 13:51:26 -04:00
|
|
|
with.test_framework :minitest
|
2017-02-04 22:27:59 -05:00
|
|
|
with.library :rails
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-25 11:27:38 -05:00
|
|
|
WebMock.disable_net_connect!(allow: [
|
2023-10-02 12:57:07 -04:00
|
|
|
Danbooru.config.opensearch_host,
|
2022-11-25 11:27:38 -05:00
|
|
|
])
|
|
|
|
|
2022-11-25 12:44:36 -05:00
|
|
|
FactoryBot::SyntaxRunner.class_eval do
|
|
|
|
include ActiveSupport::Testing::FileFixtures
|
|
|
|
include ActionDispatch::TestProcess::FixtureFile
|
|
|
|
self.file_fixture_path = ActiveSupport::TestCase.file_fixture_path
|
|
|
|
end
|
|
|
|
|
2023-06-06 13:52:02 -04:00
|
|
|
# Make tests not take ages. Remove the const first to avoid a const redefinition warning.
|
|
|
|
BCrypt::Engine.send(:remove_const, :DEFAULT_COST)
|
|
|
|
BCrypt::Engine::DEFAULT_COST = BCrypt::Engine::MIN_COST
|
|
|
|
|
2023-10-02 12:57:07 -04:00
|
|
|
# Clear the opensearch indicies completly
|
2023-09-16 11:09:33 -04:00
|
|
|
Post.document_store.create_index!(delete_existing: true)
|
|
|
|
PostVersion.document_store.create_index!(delete_existing: true)
|
2023-09-08 13:17:47 -04:00
|
|
|
|
2017-01-13 20:21:05 -05:00
|
|
|
class ActiveSupport::TestCase
|
2022-11-25 12:44:36 -05:00
|
|
|
include ActionDispatch::TestProcess::FixtureFile
|
2022-11-25 15:06:54 -05:00
|
|
|
include FactoryBot::Syntax::Methods
|
2017-06-21 12:02:33 -04:00
|
|
|
|
|
|
|
setup do
|
2018-05-01 18:24:24 -04:00
|
|
|
Socket.stubs(:gethostname).returns("www.example.com")
|
2018-01-02 17:32:38 -05:00
|
|
|
Danbooru.config.stubs(:enable_sock_puppet_validation?).returns(false)
|
2021-11-14 16:16:36 -05:00
|
|
|
Danbooru.config.stubs(:disable_throttles?).returns(true)
|
2018-03-14 17:57:29 -04:00
|
|
|
|
2021-01-23 13:19:27 -05:00
|
|
|
FileUtils.mkdir_p("#{Rails.root}/tmp/test-storage2")
|
2019-09-09 11:17:05 -04:00
|
|
|
storage_manager = StorageManager::Local.new(base_dir: "#{Rails.root}/tmp/test-storage2")
|
2018-03-14 17:57:29 -04:00
|
|
|
Danbooru.config.stubs(:storage_manager).returns(storage_manager)
|
2018-03-18 17:33:26 -04:00
|
|
|
Danbooru.config.stubs(:backup_storage_manager).returns(StorageManager::Null.new)
|
2019-09-09 11:35:50 -04:00
|
|
|
Danbooru.config.stubs(:enable_email_verification?).returns(false)
|
2022-11-26 10:20:16 -05:00
|
|
|
CurrentUser.ip_addr = "127.0.0.1"
|
2017-06-21 12:02:33 -04:00
|
|
|
end
|
2017-04-15 22:51:56 -04:00
|
|
|
|
|
|
|
teardown do
|
2019-09-09 11:17:05 -04:00
|
|
|
# The below line is only mildly insane and may have resulted in the destruction of my data several times.
|
|
|
|
FileUtils.rm_rf("#{Rails.root}/tmp/test-storage2")
|
2017-04-15 22:51:56 -04:00
|
|
|
Cache.clear
|
2022-11-26 08:02:06 -05:00
|
|
|
RequestStore.clear!
|
2017-04-15 22:51:56 -04:00
|
|
|
end
|
2022-11-25 14:15:48 -05:00
|
|
|
|
2022-11-26 09:20:15 -05:00
|
|
|
def as(user, ip_addr = "127.0.0.1", &)
|
|
|
|
CurrentUser.scoped(user, ip_addr, &)
|
2022-11-25 14:15:48 -05:00
|
|
|
end
|
2023-02-24 08:14:44 -05:00
|
|
|
|
|
|
|
def with_inline_jobs(&)
|
|
|
|
Sidekiq::Testing.inline!(&)
|
|
|
|
end
|
2023-09-07 11:04:34 -04:00
|
|
|
|
|
|
|
# TODO: Remove with upgrade to Rails 7.1
|
|
|
|
def stub_const(mod, constant, new_value)
|
|
|
|
old_value = mod.const_get(constant, false)
|
|
|
|
mod.send(:remove_const, constant)
|
|
|
|
mod.const_set(constant, new_value)
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
mod.send(:remove_const, constant)
|
|
|
|
mod.const_set(constant, old_value)
|
|
|
|
end
|
2023-09-08 13:51:35 -04:00
|
|
|
|
|
|
|
def reset_post_index
|
|
|
|
# This seems slightly faster than deleting and recreating the index
|
2023-09-16 11:09:33 -04:00
|
|
|
Post.document_store.delete_by_query(query: "*", body: {})
|
|
|
|
Post.document_store.refresh_index!
|
2023-09-08 13:51:35 -04:00
|
|
|
end
|
2017-01-13 20:21:05 -05:00
|
|
|
end
|
|
|
|
|
2018-04-02 13:51:26 -04:00
|
|
|
class ActionDispatch::IntegrationTest
|
|
|
|
def method_authenticated(method_name, url, user, options)
|
2024-12-18 09:02:02 -05:00
|
|
|
post session_path, params: { session: { name: user.name, password: user.password } }
|
2020-11-12 00:06:12 -05:00
|
|
|
self.send(method_name, url, **options)
|
2018-04-02 13:51:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_auth(url, user, options = {})
|
|
|
|
method_authenticated(:get, url, user, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_auth(url, user, options = {})
|
|
|
|
method_authenticated(:post, url, user, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def put_auth(url, user, options = {})
|
|
|
|
method_authenticated(:put, url, user, options)
|
2017-01-13 20:21:05 -05:00
|
|
|
end
|
|
|
|
|
2018-04-02 13:51:26 -04:00
|
|
|
def delete_auth(url, user, options = {})
|
|
|
|
method_authenticated(:delete, url, user, options)
|
2017-01-13 20:21:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-27 09:48:52 -05:00
|
|
|
module ActionView
|
|
|
|
class TestCase
|
|
|
|
# Stub webpacker method so these tests don't compile assets
|
|
|
|
def asset_pack_path(name, **_options)
|
|
|
|
name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-28 16:28:13 -05:00
|
|
|
Rails.application.load_seed
|